feat(app): sign-out warns before losing editor work, local copies purged
This commit is contained in:
1 parent
8b6adcb7df
commit
bd4ba338af
12 files changed
+417
-16
No files matched your search
@@ -9,6 +9,7 @@ import MobileDrawer from './MobileDrawer'
|
||||
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
|
||||
import SignOutButton from './SignOutButton'
|
||||
import UserBadge, { AvatarCircle } from './UserBadge'
|
||||
import { usePurgeForeignCopies } from '@/app/usePurgeForeignCopies'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
@@ -41,6 +42,7 @@ function followedLink(event: MouseEvent): HTMLAnchorElement | null {
|
||||
*/
|
||||
export default function AppLayout() {
|
||||
const t = useT()
|
||||
usePurgeForeignCopies()
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const [drawerOpen, setDrawerOpen] = useState(false)
|
||||
|
||||
@@ -2,16 +2,39 @@ import { useState } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import SignOutLossDialog from './SignOutLossDialog'
|
||||
import { hasUserCopies, removeUserCopies } from '@/editor/state/recovery'
|
||||
import { leaveGuardArmed, useEditorStore } from '@/editor/state/editorStore'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export default function SignOutButton({ className = '' }: { className?: string }) {
|
||||
export default function SignOutButton({
|
||||
className = '',
|
||||
}: {
|
||||
className?: string
|
||||
}) {
|
||||
const t = useT()
|
||||
const navigate = useNavigate()
|
||||
const signOut = useAuthStore((s) => s.signOut)
|
||||
const userId = useAuthStore((s) => s.user?.id)
|
||||
const unsavedWork = useEditorStore(leaveGuardArmed)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [confirming, setConfirming] = useState(false)
|
||||
|
||||
async function onClick() {
|
||||
function onClick() {
|
||||
const atStake =
|
||||
unsavedWork || (userId !== undefined && hasUserCopies(userId))
|
||||
if (atStake) setConfirming(true)
|
||||
else void leave()
|
||||
}
|
||||
|
||||
function onConfirm() {
|
||||
setConfirming(false)
|
||||
if (userId) removeUserCopies(userId)
|
||||
void leave()
|
||||
}
|
||||
|
||||
async function leave() {
|
||||
setBusy(true)
|
||||
try {
|
||||
await signOut()
|
||||
@@ -25,15 +48,22 @@ export default function SignOutButton({ className = '' }: { className?: string }
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
onClick={onClick}
|
||||
disabled={busy}
|
||||
className={`justify-start gap-3 text-[var(--color-ink-muted)] hover:text-[var(--color-ink)] ${className}`}
|
||||
>
|
||||
<Icon name="logout" size={20} />
|
||||
{busy ? t('shell.signing_out') : t('shell.sign_out')}
|
||||
</Button>
|
||||
<>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
onClick={onClick}
|
||||
disabled={busy}
|
||||
className={`justify-start gap-3 text-[var(--color-ink-muted)] hover:text-[var(--color-ink)] ${className}`}
|
||||
>
|
||||
<Icon name="logout" size={20} />
|
||||
{busy ? t('shell.signing_out') : t('shell.sign_out')}
|
||||
</Button>
|
||||
<SignOutLossDialog
|
||||
open={confirming}
|
||||
onConfirm={onConfirm}
|
||||
onCancel={() => setConfirming(false)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export interface SignOutLossDialogProps {
|
||||
open: boolean
|
||||
onConfirm: () => void
|
||||
onCancel: () => void
|
||||
}
|
||||
|
||||
/** Warns that signing out drops the unsaved changes and this tab's local copies. */
|
||||
export default function SignOutLossDialog({
|
||||
open,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}: SignOutLossDialogProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onClose={onCancel}
|
||||
title={t('shell.sign_out_loss_title')}
|
||||
description={t('shell.sign_out_loss_body')}
|
||||
>
|
||||
<div className="mt-4 flex flex-wrap justify-end gap-2">
|
||||
<Button type="button" variant="ghost" onClick={onCancel}>
|
||||
{t('shell.sign_out_loss_cancel')}
|
||||
</Button>
|
||||
<Button type="button" onClick={onConfirm}>
|
||||
{t('shell.sign_out_loss_confirm')}
|
||||
</Button>
|
||||
</div>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user