Files
pybricks-code/src/alerts/UnexpectedErrorAlert.tsx
T
David Lechner a3e55c828a move useI18n to i18n.ts
Since we are using the react-i18n babel loader plugin, useI18n can only
be used once per file. Also we have to remember to add the istanbul
ignore next comment each time we use it. By moving this to a common
file, it can be easily copied and pasted when a new submodule is
created and we don't have to remember the rules when calling it.
2022-05-21 13:57:58 -05:00

75 lines
2.6 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import './UnexpectedErrorAlert.scss';
import { AnchorButton, Button, ButtonGroup, Collapse, Intent } from '@blueprintjs/core';
import React, { useState } from 'react';
import { useId } from 'react-aria';
import { CreateToast } from '../i18nToaster';
import { I18nId, useI18n } from './i18n';
type UnexpectedErrorAlertProps = {
error: Error;
};
const UnexpectedErrorAlert: React.VoidFunctionComponent<UnexpectedErrorAlertProps> = ({
error,
}) => {
const i18n = useI18n();
const [isExpanded, setIsExpanded] = useState(false);
const labelId = useId();
return (
<>
<p>{i18n.translate(I18nId.Message, { errorMessage: error.message })}</p>
<span>
<Button
aria-labelledby={labelId}
minimal={true}
small={true}
icon={isExpanded ? 'chevron-down' : 'chevron-right'}
onClick={() => setIsExpanded((v) => !v)}
/>
<span id={labelId}>{i18n.translate(I18nId.TechnicalInfo)}</span>
</span>
<Collapse isOpen={isExpanded}>
<pre className="pb-alerts-stack-trace">{error.stack}</pre>
</Collapse>
<div>
<ButtonGroup minimal={true} fill={true}>
<Button
intent={Intent.DANGER}
icon="duplicate"
onClick={() =>
navigator.clipboard.writeText(
`\`\`\`\n${error.stack || error.message}\n\`\`\``,
)
}
>
{i18n.translate(I18nId.CopyErrorMessage)}
</Button>
<AnchorButton
intent={Intent.DANGER}
icon="virus"
href={`https://github.com/pybricks/support/issues?q=${encodeURIComponent(
'is:issue',
)}+${encodeURIComponent(error.message)}`}
target="_blank"
>
{i18n.translate(I18nId.ReportBug)}
</AnchorButton>
</ButtonGroup>
</div>
</>
);
};
export const unexpectedError: CreateToast<{ error: Error }> = (onAction, { error }) => {
return {
message: <UnexpectedErrorAlert error={error} />,
icon: 'error',
intent: Intent.DANGER,
onDismiss: () => onAction('dismiss'),
};
};