2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2015 Signal Messenger, LLC
|
2021-10-06 16:16:51 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MouseEvent } from 'react';
|
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-10-06 16:16:51 +00:00
|
|
|
import copyText from 'copy-text-to-clipboard';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { Button, ButtonVariant } from './Button';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-10-06 16:16:51 +00:00
|
|
|
import { Spinner } from './Spinner';
|
|
|
|
import { ToastDebugLogError } from './ToastDebugLogError';
|
|
|
|
import { ToastLinkCopied } from './ToastLinkCopied';
|
2022-06-08 22:00:32 +00:00
|
|
|
import { TitleBarContainer } from './TitleBarContainer';
|
|
|
|
import type { ExecuteMenuRoleType } from './TitleBarContainer';
|
2021-10-06 16:16:51 +00:00
|
|
|
import { ToastLoadingFullLogs } from './ToastLoadingFullLogs';
|
|
|
|
import { openLinkInWebBrowser } from '../util/openLinkInWebBrowser';
|
2022-06-02 23:24:35 +00:00
|
|
|
import { createSupportUrl } from '../util/createSupportUrl';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-10-06 16:16:51 +00:00
|
|
|
import { useEscapeHandling } from '../hooks/useEscapeHandling';
|
2022-06-08 22:00:32 +00:00
|
|
|
import { useTheme } from '../hooks/useTheme';
|
2021-10-06 16:16:51 +00:00
|
|
|
|
|
|
|
enum LoadState {
|
|
|
|
NotStarted,
|
|
|
|
Started,
|
|
|
|
Loaded,
|
|
|
|
Submitting,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
closeWindow: () => unknown;
|
|
|
|
downloadLog: (text: string) => unknown;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
fetchLogs: () => Promise<string>;
|
|
|
|
uploadLogs: (logs: string) => Promise<string>;
|
2022-07-05 16:44:53 +00:00
|
|
|
hasCustomTitleBar: boolean;
|
2022-06-08 22:00:32 +00:00
|
|
|
executeMenuRole: ExecuteMenuRoleType;
|
2021-10-06 16:16:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ToastType {
|
|
|
|
Copied,
|
|
|
|
Error,
|
|
|
|
Loading,
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function DebugLogWindow({
|
2021-10-06 16:16:51 +00:00
|
|
|
closeWindow,
|
|
|
|
downloadLog,
|
|
|
|
i18n,
|
|
|
|
fetchLogs,
|
|
|
|
uploadLogs,
|
2022-07-05 16:44:53 +00:00
|
|
|
hasCustomTitleBar,
|
2022-06-08 22:00:32 +00:00
|
|
|
executeMenuRole,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: PropsType): JSX.Element {
|
2021-10-06 16:16:51 +00:00
|
|
|
const [loadState, setLoadState] = useState<LoadState>(LoadState.NotStarted);
|
|
|
|
const [logText, setLogText] = useState<string | undefined>();
|
|
|
|
const [publicLogURL, setPublicLogURL] = useState<string | undefined>();
|
2023-03-30 00:03:25 +00:00
|
|
|
const [textAreaValue, setTextAreaValue] = useState<string>(
|
|
|
|
i18n('icu:loading')
|
|
|
|
);
|
2021-10-06 16:16:51 +00:00
|
|
|
const [toastType, setToastType] = useState<ToastType | undefined>();
|
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
|
2021-10-06 16:16:51 +00:00
|
|
|
useEscapeHandling(closeWindow);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setLoadState(LoadState.Started);
|
|
|
|
|
|
|
|
let shouldCancel = false;
|
|
|
|
|
|
|
|
async function doFetchLogs() {
|
|
|
|
const fetchedLogText = await fetchLogs();
|
|
|
|
|
|
|
|
if (shouldCancel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setToastType(ToastType.Loading);
|
|
|
|
setLogText(fetchedLogText);
|
|
|
|
setLoadState(LoadState.Loaded);
|
|
|
|
|
|
|
|
// This number is somewhat arbitrary; we want to show enough that it's
|
|
|
|
// clear that we need to scroll, but not so many that things get slow.
|
|
|
|
const linesToShow = Math.ceil(Math.min(window.innerHeight, 2000) / 5);
|
|
|
|
const value = fetchedLogText.split(/\n/g, linesToShow).join('\n');
|
|
|
|
|
2023-03-30 00:03:25 +00:00
|
|
|
setTextAreaValue(`${value}\n\n\n${i18n('icu:debugLogLogIsIncomplete')}`);
|
2021-10-06 16:16:51 +00:00
|
|
|
setToastType(undefined);
|
|
|
|
}
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void doFetchLogs();
|
2021-10-06 16:16:51 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
shouldCancel = true;
|
|
|
|
};
|
2022-03-21 18:41:48 +00:00
|
|
|
}, [fetchLogs, i18n]);
|
2021-10-06 16:16:51 +00:00
|
|
|
|
|
|
|
const handleSubmit = async (ev: MouseEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
const text = logText;
|
|
|
|
|
|
|
|
if (!text || text.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoadState(LoadState.Submitting);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const publishedLogURL = await uploadLogs(text);
|
|
|
|
setPublicLogURL(publishedLogURL);
|
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('DebugLogWindow error:', Errors.toLogFormat(error));
|
2021-10-06 16:16:51 +00:00
|
|
|
setLoadState(LoadState.Loaded);
|
|
|
|
setToastType(ToastType.Error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function closeToast() {
|
|
|
|
setToastType(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
let toastElement: JSX.Element | undefined;
|
|
|
|
if (toastType === ToastType.Loading) {
|
|
|
|
toastElement = <ToastLoadingFullLogs i18n={i18n} onClose={closeToast} />;
|
|
|
|
} else if (toastType === ToastType.Copied) {
|
|
|
|
toastElement = <ToastLinkCopied i18n={i18n} onClose={closeToast} />;
|
|
|
|
} else if (toastType === ToastType.Error) {
|
|
|
|
toastElement = <ToastDebugLogError i18n={i18n} onClose={closeToast} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (publicLogURL) {
|
|
|
|
const copyLog = (ev: MouseEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
copyText(publicLogURL);
|
|
|
|
setToastType(ToastType.Copied);
|
|
|
|
};
|
|
|
|
|
2022-06-02 23:24:35 +00:00
|
|
|
const supportURL = createSupportUrl({
|
|
|
|
locale: i18n.getLocale(),
|
|
|
|
query: {
|
|
|
|
debugLog: publicLogURL,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-06 16:16:51 +00:00
|
|
|
return (
|
2022-06-08 22:00:32 +00:00
|
|
|
<TitleBarContainer
|
2022-07-05 16:44:53 +00:00
|
|
|
hasCustomTitleBar={hasCustomTitleBar}
|
2022-06-08 22:00:32 +00:00
|
|
|
theme={theme}
|
|
|
|
executeMenuRole={executeMenuRole}
|
|
|
|
>
|
|
|
|
<div className="DebugLogWindow">
|
|
|
|
<div>
|
|
|
|
<div className="DebugLogWindow__title">
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:debugLogSuccess')}
|
2022-06-08 22:00:32 +00:00
|
|
|
</div>
|
|
|
|
<p className="DebugLogWindow__subtitle">
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:debugLogSuccessNextSteps')}
|
2022-06-08 22:00:32 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="DebugLogWindow__container">
|
|
|
|
<input
|
|
|
|
className="DebugLogWindow__link"
|
|
|
|
readOnly
|
|
|
|
type="text"
|
2023-04-20 17:03:43 +00:00
|
|
|
dir="auto"
|
2022-06-08 22:00:32 +00:00
|
|
|
value={publicLogURL}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="DebugLogWindow__footer">
|
|
|
|
<Button
|
|
|
|
onClick={() => openLinkInWebBrowser(supportURL)}
|
|
|
|
variant={ButtonVariant.Secondary}
|
|
|
|
>
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:reportIssue')}
|
2022-06-08 22:00:32 +00:00
|
|
|
</Button>
|
2023-03-30 00:03:25 +00:00
|
|
|
<Button onClick={copyLog}>{i18n('icu:debugLogCopy')}</Button>
|
2022-06-08 22:00:32 +00:00
|
|
|
</div>
|
|
|
|
{toastElement}
|
|
|
|
</div>
|
|
|
|
</TitleBarContainer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const canSubmit = Boolean(logText) && loadState !== LoadState.Submitting;
|
|
|
|
const canSave = Boolean(logText);
|
|
|
|
const isLoading =
|
|
|
|
loadState === LoadState.Started || loadState === LoadState.Submitting;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TitleBarContainer
|
2022-07-05 16:44:53 +00:00
|
|
|
hasCustomTitleBar={hasCustomTitleBar}
|
2022-06-08 22:00:32 +00:00
|
|
|
theme={theme}
|
|
|
|
executeMenuRole={executeMenuRole}
|
|
|
|
>
|
2021-10-06 16:16:51 +00:00
|
|
|
<div className="DebugLogWindow">
|
|
|
|
<div>
|
2023-03-30 00:03:25 +00:00
|
|
|
<div className="DebugLogWindow__title">
|
|
|
|
{i18n('icu:submitDebugLog')}
|
|
|
|
</div>
|
2021-10-06 16:16:51 +00:00
|
|
|
<p className="DebugLogWindow__subtitle">
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:debugLogExplanation')}
|
2021-10-06 16:16:51 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
2022-08-16 23:19:28 +00:00
|
|
|
{isLoading ? (
|
|
|
|
<div className="DebugLogWindow__container">
|
2022-06-08 22:00:32 +00:00
|
|
|
<Spinner svgSize="normal" />
|
2022-08-16 23:19:28 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="DebugLogWindow__scroll_area">
|
|
|
|
<pre className="DebugLogWindow__scroll_area__text">
|
|
|
|
{textAreaValue}
|
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-10-06 16:16:51 +00:00
|
|
|
<div className="DebugLogWindow__footer">
|
|
|
|
<Button
|
2022-06-08 22:00:32 +00:00
|
|
|
disabled={!canSave}
|
|
|
|
onClick={() => {
|
|
|
|
if (logText) {
|
|
|
|
downloadLog(logText);
|
|
|
|
}
|
|
|
|
}}
|
2021-10-06 16:16:51 +00:00
|
|
|
variant={ButtonVariant.Secondary}
|
|
|
|
>
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:debugLogSave')}
|
2022-06-08 22:00:32 +00:00
|
|
|
</Button>
|
|
|
|
<Button disabled={!canSubmit} onClick={handleSubmit}>
|
2023-03-30 00:03:25 +00:00
|
|
|
{i18n('icu:submit')}
|
2021-10-06 16:16:51 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{toastElement}
|
|
|
|
</div>
|
2022-06-08 22:00:32 +00:00
|
|
|
</TitleBarContainer>
|
2021-10-06 16:16:51 +00:00
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|