From 6dd7a4960f141b5d970ab365ee5b18a96b6806d3 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Wed, 14 Aug 2024 10:55:31 +0500 Subject: [PATCH] web: show warning if there are unsynced changes on logout --- apps/web/src/components/error-text/index.tsx | 9 +++-- apps/web/src/dialogs/confirm.tsx | 35 +++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/apps/web/src/components/error-text/index.tsx b/apps/web/src/components/error-text/index.tsx index a9f92e022..28faf37d2 100644 --- a/apps/web/src/components/error-text/index.tsx +++ b/apps/web/src/components/error-text/index.tsx @@ -30,8 +30,13 @@ export function ErrorText(props: ErrorTextProps) { diff --git a/apps/web/src/dialogs/confirm.tsx b/apps/web/src/dialogs/confirm.tsx index 9343194a7..2367b3313 100644 --- a/apps/web/src/dialogs/confirm.tsx +++ b/apps/web/src/dialogs/confirm.tsx @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import { Box, Checkbox, Label, Text } from "@theme-ui/components"; +import { Checkbox, Flex, Label, Text } from "@theme-ui/components"; import { useRef } from "react"; import { mdToHtml } from "../utils/md"; import Dialog from "../components/dialog"; @@ -25,6 +25,7 @@ import { BaseDialogProps, DialogManager } from "../common/dialog-manager"; import { db } from "../common/db"; import { getChangelog } from "../utils/version"; import { downloadUpdate } from "../utils/updater"; +import { ErrorText } from "../components/error-text"; type Check = { text: string; default?: boolean }; export type ConfirmDialogProps = BaseDialogProps< @@ -36,6 +37,7 @@ export type ConfirmDialogProps = BaseDialogProps< positiveButtonText?: string; negativeButtonText?: string; message?: string; + warnings?: string[]; checks?: Partial>; }; @@ -50,6 +52,7 @@ export const ConfirmDialog = DialogManager.register(function ConfirmDialog< negativeButtonText, positiveButtonText, message, + warnings, checks } = props; const checkedItems = useRef>({} as any); @@ -62,6 +65,12 @@ export const ConfirmDialog = DialogManager.register(function ConfirmDialog< width={width} description={subtitle} onClose={() => onClose(false)} + onOpen={() => { + for (const checkId in checks) { + checkedItems.current[checkId as TCheckId] = + checks[checkId as TCheckId]?.default || false; + } + }} positiveButton={ positiveButtonText ? { @@ -80,8 +89,10 @@ export const ConfirmDialog = DialogManager.register(function ConfirmDialog< : undefined } > - ) : null} + {warnings?.map((text) => ( + + ))} {checks ? Object.entries(checks).map( ([id, check]) => @@ -101,7 +115,7 @@ export const ConfirmDialog = DialogManager.register(function ConfirmDialog< key={id} id={id} variant="text.body" - sx={{ fontWeight: "bold", mt: 1 }} + sx={{ fontWeight: "bold" }} > + ); }); @@ -148,16 +162,21 @@ export function showMultiPermanentDeleteConfirmation(length: number) { }); } -export function showLogoutConfirmation() { - return ConfirmDialog.show({ +export async function showLogoutConfirmation() { + return await ConfirmDialog.show({ title: `Logout?`, message: - "Logging out will clear all data stored on THIS DEVICE. Are you sure you want to log out?", + "Are you sure you want to log out and clear all data stored on THIS DEVICE?", positiveButtonText: "Yes", negativeButtonText: "No", + warnings: (await db.hasUnsyncedChanges()) + ? [ + "You have unsynced notes. Take a backup or sync your notes to avoid losing critical data." + ] + : [], checks: { backup: { - text: "Take a backup before logging out to prevent data loss?", + text: "Take a backup before logging out?", default: true } }