Files
pybricks-code/src/firmware/alerts/FlashProgress.tsx
T
David Lechner fa27649fe3 toasterTypes: change toaster to ref
Instead of rendering the toaster separate from the main index, add it
there so we can inherit all of the context providers. This requires
passing the ref object instead of the toaster instance itself, so
sagas have to be updated.
2022-10-19 18:26:37 -05:00

53 lines
1.4 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { Intent, ProgressBar } from '@blueprintjs/core';
import React from 'react';
import type { CreateToast } from '../../toasterTypes';
import { useI18n } from './i18n';
type FlashProgressProps = {
action: 'erase' | 'flash';
progress: number | undefined;
};
const FlashProgress: React.VoidFunctionComponent<FlashProgressProps> = ({
action,
progress,
}) => {
const i18n = useI18n();
return (
<>
{action === 'erase' && (
<p>
{i18n.translate('flashProgress.erasing', {
percent: progress ? i18n.formatPercentage(progress) : '',
})}
</p>
)}
{action === 'flash' && (
<p>
{i18n.translate('flashProgress.flashing', {
percent: progress ? i18n.formatPercentage(progress) : '',
})}
</p>
)}
<ProgressBar value={progress} />
</>
);
};
export const flashProgress: CreateToast<FlashProgressProps> = (onAction, props) => {
return {
message: <FlashProgress {...props} />,
icon: 'download',
intent: Intent.PRIMARY,
// close one second after progress is complete
timeout: (props.progress ?? 0) < 1 ? 0 : 1000,
onDismiss: () => onAction('dismiss'),
};
};