mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-24 04:00:59 +01:00
web: migrate loading dialog to typescript
This commit is contained in:
@@ -305,23 +305,26 @@ export function showMigrationDialog() {
|
||||
));
|
||||
}
|
||||
|
||||
type LoadingDialogProps = {
|
||||
type LoadingDialogProps<T> = {
|
||||
title: string;
|
||||
message?: string;
|
||||
subtitle: string;
|
||||
action: () => void;
|
||||
action: () => T | Promise<T>;
|
||||
};
|
||||
export function showLoadingDialog(dialogData: LoadingDialogProps) {
|
||||
export function showLoadingDialog<T>(dialogData: LoadingDialogProps<T>) {
|
||||
const { title, message, subtitle, action } = dialogData;
|
||||
return showDialog("LoadingDialog", (Dialog, perform) => (
|
||||
<Dialog
|
||||
title={title}
|
||||
subtitle={subtitle}
|
||||
message={message}
|
||||
action={action}
|
||||
onDone={(e: boolean) => perform(e)}
|
||||
/>
|
||||
));
|
||||
return showDialog<"LoadingDialog", T | boolean>(
|
||||
"LoadingDialog",
|
||||
(Dialog, perform) => (
|
||||
<Dialog
|
||||
title={title}
|
||||
description={subtitle}
|
||||
message={message}
|
||||
action={action}
|
||||
onClose={(e) => perform(e as T | boolean)}
|
||||
/>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
type ProgressDialogProps = {
|
||||
|
||||
@@ -22,27 +22,32 @@ import { Box, Text } from "@theme-ui/components";
|
||||
import Dialog from "./dialog";
|
||||
import * as Icon from "../icons";
|
||||
|
||||
function LoadingDialog(props) {
|
||||
type LoadingDialogProps<T> = {
|
||||
onClose: (result: T | boolean) => void;
|
||||
action: () => T | Promise<T>;
|
||||
title: string;
|
||||
description: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
function LoadingDialog<T>(props: LoadingDialogProps<T>) {
|
||||
const { onClose, action, description, message, title } = props;
|
||||
useEffect(() => {
|
||||
(async function () {
|
||||
try {
|
||||
props.onDone(await props.action());
|
||||
onClose(await action());
|
||||
} catch (e) {
|
||||
props.onDone(e);
|
||||
onClose(false);
|
||||
throw e;
|
||||
}
|
||||
})();
|
||||
}, [props]);
|
||||
}, [onClose, action]);
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
isOpen={true}
|
||||
title={props.title}
|
||||
description={props.subtitle}
|
||||
onClose={() => {}}
|
||||
>
|
||||
<Dialog isOpen={true} title={title} description={description}>
|
||||
<Box>
|
||||
<Text as="span" variant="body">
|
||||
{props.message}
|
||||
{message}
|
||||
</Text>
|
||||
<Icon.Loading rotate sx={{ my: 2 }} color="primary" />
|
||||
</Box>
|
||||
Reference in New Issue
Block a user