mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-05 08:12:22 -08:00
205 lines
7.9 KiB
TypeScript
205 lines
7.9 KiB
TypeScript
import { useState } from 'react';
|
||
import { isTauri } from '@tauri-apps/api/core';
|
||
import { openUrl } from '@tauri-apps/plugin-opener';
|
||
import { Box, Button, Dialog, Text, TextArea, color, config } from 'folds';
|
||
import { Warning, sizedIcon } from '$components/icons/phosphor';
|
||
import * as Sentry from '@sentry/react';
|
||
import { SplashScreen } from '$components/splash-screen';
|
||
import { buildGitHubUrl } from '$features/bug-report';
|
||
import { isMobileTauri } from '$utils/platform';
|
||
|
||
type ErrorPageProps = {
|
||
error: Error;
|
||
/** Sentry event ID Epresent when Sentry.ErrorBoundary captured the crash */
|
||
eventId?: string;
|
||
};
|
||
|
||
function createIssueUrl(error: Error): string {
|
||
const stacktrace = error.stack || 'No stacktrace available';
|
||
|
||
const automatedBugReport = `# Automated Bug Report
|
||
Error occurred in the application.
|
||
|
||
## Error Message
|
||
\`\`\`
|
||
${error.message}
|
||
\`\`\`
|
||
|
||
## Stacktrace
|
||
\`\`\`
|
||
${stacktrace}
|
||
\`\`\``;
|
||
|
||
return buildGitHubUrl('bug', `Error: ${error.message}`, { context: automatedBugReport });
|
||
}
|
||
|
||
async function openIssueUrl(error: Error) {
|
||
const url = createIssueUrl(error);
|
||
if (isTauri()) {
|
||
try {
|
||
await openUrl(url);
|
||
} catch {
|
||
window.open(url, '_blank', 'noopener,noreferrer');
|
||
}
|
||
return;
|
||
}
|
||
window.open(url, '_blank', 'noopener,noreferrer');
|
||
}
|
||
|
||
// This component is used as the fallback for the ErrorBoundary in App.tsx, which means it will be rendered whenever an uncaught error is thrown in any of the child components and not handled locally.
|
||
// It provides a user-friendly error message and options to report the issue or reload the page.
|
||
// Motivation of the design is to encourage users to report issues while also providing them with the necessary information to do so, and to give them an easy way to recover by reloading the page.
|
||
// Note: Since this component is rendered in response to an error, it should be as resilient as possible and avoid any complex logic or dependencies that could potentially throw additional errors.
|
||
export function ErrorPage({ error, eventId }: ErrorPageProps) {
|
||
const sentryEnabled = Sentry.isInitialized();
|
||
const reportedToSentry = sentryEnabled && !!eventId;
|
||
const [feedback, setFeedback] = useState('');
|
||
const [feedbackSubmitted, setFeedbackSubmitted] = useState(false);
|
||
|
||
const submitFeedback = () => {
|
||
if (!eventId || !feedback.trim()) return;
|
||
Sentry.captureFeedback({
|
||
message: feedback.trim(),
|
||
name: 'Crash report follow-up',
|
||
associatedEventId: eventId,
|
||
});
|
||
setFeedbackSubmitted(true);
|
||
};
|
||
|
||
return (
|
||
<SplashScreen>
|
||
<Box grow="Yes" direction="Column" gap="400" alignItems="Center" justifyContent="Center">
|
||
<Dialog
|
||
style={{
|
||
maxWidth: '600px',
|
||
minWidth: '300px',
|
||
padding: config.space.S400,
|
||
}}
|
||
>
|
||
<Box style={{ padding: config.space.S400 }} direction="Column" gap="400">
|
||
<Box direction="Column" gap="100">
|
||
<Box alignItems="Center" gap="200">
|
||
{sizedIcon(Warning, '300', { filled: true, style: { color: color.Critical.Main } })}
|
||
<Text size="H2">Oops! Something went wrong</Text>
|
||
</Box>
|
||
<Text size="T300">
|
||
{reportedToSentry
|
||
? 'An unexpected error occurred. This crash has been automatically reported to our team. You can add more details to help us investigate.'
|
||
: 'An unexpected error occurred. Please try again. If it continues, report the issue on our GitHub using the button below, it will include error details to help us investigate. Thank you for helping improve the app.'}
|
||
</Text>
|
||
{reportedToSentry ? (
|
||
<Box direction="Column" gap="200">
|
||
{isMobileTauri() ? (
|
||
feedbackSubmitted ? (
|
||
<Text size="T300">Thank you for adding details to the report.</Text>
|
||
) : (
|
||
<Box direction="Column" gap="200">
|
||
<TextArea
|
||
size="400"
|
||
variant="SurfaceVariant"
|
||
radii="400"
|
||
rows={3}
|
||
placeholder="What were you doing when the error occurred?"
|
||
value={feedback}
|
||
onChange={(evt) => setFeedback((evt.target as HTMLTextAreaElement).value)}
|
||
/>
|
||
<Button
|
||
variant="Primary"
|
||
onClick={submitFeedback}
|
||
disabled={!feedback.trim()}
|
||
fill="Solid"
|
||
>
|
||
<Text as="span" size="B400">
|
||
Add Details to Report
|
||
</Text>
|
||
</Button>
|
||
</Box>
|
||
)
|
||
) : (
|
||
<Button
|
||
variant="Primary"
|
||
onClick={() => Sentry.showReportDialog({ eventId })}
|
||
fill="Solid"
|
||
title="Opens a short form so you can describe what you were doing before the crash. This helps our team fix the issue faster."
|
||
>
|
||
<Text as="span" size="B400">
|
||
Add Details to Report
|
||
</Text>
|
||
</Button>
|
||
)}
|
||
<Button
|
||
variant="Secondary"
|
||
onClick={() => {
|
||
void openIssueUrl(error);
|
||
}}
|
||
fill="None"
|
||
title="Opens a pre-filled GitHub issue as an alternative reporting method."
|
||
>
|
||
<Text as="span" size="B400">
|
||
Also Report on GitHub
|
||
</Text>
|
||
</Button>
|
||
</Box>
|
||
) : (
|
||
<Button
|
||
variant="Secondary"
|
||
onClick={() => {
|
||
void openIssueUrl(error);
|
||
}}
|
||
fill="Solid"
|
||
title="Clicking this button will open a new issue on our GitHub repository with the error details pre-filled. Please review the information before submitting."
|
||
>
|
||
<Text as="span" size="B400">
|
||
Report Issue
|
||
</Text>
|
||
</Button>
|
||
)}
|
||
<Box
|
||
direction="Column"
|
||
gap="300"
|
||
style={{
|
||
padding: config.space.S300,
|
||
backgroundColor: color.Surface.Container,
|
||
borderRadius: config.radii.R300,
|
||
}}
|
||
>
|
||
<Text size="T300" style={{ color: color.Critical.Main }}>
|
||
{error.message}
|
||
</Text>
|
||
<Box
|
||
style={{
|
||
padding: config.space.S200,
|
||
backgroundColor: color.Surface.Container,
|
||
borderRadius: config.radii.R300,
|
||
overflow: 'auto',
|
||
maxHeight: '200px',
|
||
minHeight: '100px',
|
||
}}
|
||
>
|
||
<Text
|
||
as="pre"
|
||
size="T200"
|
||
style={{ whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}
|
||
>
|
||
{error.stack}
|
||
</Text>
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
<Button
|
||
variant="Primary"
|
||
onClick={() => window.location.reload()}
|
||
fill="Solid"
|
||
title="clicking this will reload the page and hopefully lead to a functioning app again :)"
|
||
>
|
||
<Text as="span" size="B400">
|
||
Reload Page
|
||
</Text>
|
||
</Button>
|
||
</Box>
|
||
</Dialog>
|
||
</Box>
|
||
</SplashScreen>
|
||
);
|
||
}
|