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
@@ -0,0 +1,15 @@
|
||||
import { useEffect } from 'react'
|
||||
import { removeOtherUsersCopies } from '@/editor/state/recovery'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
|
||||
/**
|
||||
* Local recovery copies are only offered to their own account (ruling R24): once another
|
||||
* account is signed in within this tab, the previous accounts' copies are removed, before
|
||||
* any editor screen reads them. An expired session alone removes nothing.
|
||||
*/
|
||||
export function usePurgeForeignCopies(): void {
|
||||
const userId = useAuthStore((s) => s.user?.id ?? null)
|
||||
useEffect(() => {
|
||||
if (userId) removeOtherUsersCopies(userId)
|
||||
}, [userId])
|
||||
}
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
@@ -12,7 +12,8 @@ export const COPY_PREFIX = 'osteo:sheet-draft:'
|
||||
export const MAX_OTHER_COPIES = 2
|
||||
|
||||
const DocumentObject = z.custom<Record<string, unknown>>(
|
||||
(value) => typeof value === 'object' && value !== null && !Array.isArray(value),
|
||||
(value) =>
|
||||
typeof value === 'object' && value !== null && !Array.isArray(value),
|
||||
)
|
||||
|
||||
const CopySchema = z.object({
|
||||
@@ -55,7 +56,11 @@ function storage(): Storage | null {
|
||||
}
|
||||
}
|
||||
|
||||
function parseCopy(raw: string, userId: string, versionId: string): RecoveryCopy | null {
|
||||
function parseCopy(
|
||||
raw: string,
|
||||
userId: string,
|
||||
versionId: string,
|
||||
): RecoveryCopy | null {
|
||||
try {
|
||||
const parsed = CopySchema.safeParse(JSON.parse(raw))
|
||||
const matches = parsed.success && parsed.data.user_id === userId
|
||||
@@ -90,7 +95,12 @@ function keysOf(store: Storage, prefix: string): string[] {
|
||||
return keys.sort()
|
||||
}
|
||||
|
||||
function infoOf(store: Storage, key: string, userId: string, versionId: string) {
|
||||
function infoOf(
|
||||
store: Storage,
|
||||
key: string,
|
||||
userId: string,
|
||||
versionId: string,
|
||||
) {
|
||||
const copy = parseCopy(store.getItem(key) ?? '', userId, versionId)
|
||||
return {
|
||||
versionId,
|
||||
@@ -101,7 +111,10 @@ function infoOf(store: Storage, key: string, userId: string, versionId: string)
|
||||
}
|
||||
|
||||
/** The account's copies of other versions; another account's keys never match the prefix. */
|
||||
export function listOtherCopies(userId: string, versionId: string): StoredCopyInfo[] {
|
||||
export function listOtherCopies(
|
||||
userId: string,
|
||||
versionId: string,
|
||||
): StoredCopyInfo[] {
|
||||
const store = storage()
|
||||
if (!store) return []
|
||||
const prefix = `${COPY_PREFIX}${userId}:`
|
||||
@@ -124,3 +137,27 @@ export function writeCopy(copy: RecoveryCopy): WriteResult {
|
||||
export function removeCopy(userId: string, versionId: string): void {
|
||||
writeOrRemove(copyKey(userId, versionId), null)
|
||||
}
|
||||
|
||||
/** Whether this tab holds at least one copy of the account's own. */
|
||||
export function hasUserCopies(userId: string): boolean {
|
||||
const store = storage()
|
||||
return store !== null && keysOf(store, `${COPY_PREFIX}${userId}:`).length > 0
|
||||
}
|
||||
|
||||
function removeKeys(keys: string[]): void {
|
||||
keys.forEach((key) => writeOrRemove(key, null))
|
||||
}
|
||||
|
||||
/** Voluntary sign-out: every copy of the account goes, other keys stay (plan R24). */
|
||||
export function removeUserCopies(userId: string): void {
|
||||
const store = storage()
|
||||
if (store) removeKeys(keysOf(store, `${COPY_PREFIX}${userId}:`))
|
||||
}
|
||||
|
||||
/** Another account signed in: the previous accounts' copies go, the account's own stay. */
|
||||
export function removeOtherUsersCopies(userId: string): void {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
const own = `${COPY_PREFIX}${userId}:`
|
||||
removeKeys(keysOf(store, COPY_PREFIX).filter((key) => !key.startsWith(own)))
|
||||
}
|
||||
@@ -369,6 +369,11 @@ export const en: Dict = {
|
||||
'shell.signing_out': 'Signing out…',
|
||||
'shell.sign_out_failed':
|
||||
'Signed out on this device, but the server did not confirm: your other sessions may stay open.',
|
||||
'shell.sign_out_loss_title': 'Sign out and lose the local work?',
|
||||
'shell.sign_out_loss_body':
|
||||
'Unsaved changes or the local copies held in this tab would be lost. Saving them to the server before signing out keeps them.',
|
||||
'shell.sign_out_loss_cancel': 'Cancel',
|
||||
'shell.sign_out_loss_confirm': 'Sign out anyway',
|
||||
'shell.loading_session': 'Checking your session…',
|
||||
'shell.role.owner': 'Platform owner',
|
||||
'shell.role.admin': 'Administrator',
|
||||
|
||||
@@ -385,6 +385,11 @@ export const fr: Dict = {
|
||||
'shell.signing_out': 'Déconnexion…',
|
||||
'shell.sign_out_failed':
|
||||
"Déconnecté sur cet appareil, mais le serveur n'a pas confirmé : vos autres sessions peuvent rester ouvertes.",
|
||||
'shell.sign_out_loss_title': 'Se déconnecter et perdre le travail local ?',
|
||||
'shell.sign_out_loss_body':
|
||||
'Des modifications non enregistrées ou des copies locales de cet onglet seraient perdues. Les enregistrer sur le serveur avant de vous déconnecter les conserve.',
|
||||
'shell.sign_out_loss_cancel': 'Annuler',
|
||||
'shell.sign_out_loss_confirm': 'Se déconnecter quand même',
|
||||
'shell.loading_session': 'Vérification de la session…',
|
||||
'shell.role.owner': 'Propriétaire plateforme',
|
||||
'shell.role.admin': 'Administrateur',
|
||||
|
||||
Reference in new issue
Block a user