fix(editor): a sign-out with a dirty dialog is said at the next sign-in
This commit is contained in:
1 parent
7ecaf1998e
commit
28e298ac56
4 files changed
+178
-53
No files matched your search
@@ -1,10 +1,4 @@
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import { useEffect, useMemo, useState, type ReactNode } from 'react'
|
||||
import { useLocation } from 'react-router-dom'
|
||||
import type { OfficialSheetAdmin } from '@faunosteo/supabase'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
@@ -21,7 +15,7 @@ import {
|
||||
import useEditorHistory from '@/editor/history/useEditorHistory'
|
||||
import useLeaveConfirm from '@/editor/history/useLeaveConfirm'
|
||||
import { useSessionTag, useSessionTagStore } from '@/editor/history/sessionTag'
|
||||
import { useLeftDraft } from '@/editor/leftDraft'
|
||||
import { useLeftDraft, type LeaveFacts } from '@/editor/leftDraft'
|
||||
import { citationsOfView, defaultView } from '@/editor/navigation'
|
||||
import { findingLayerOf, referenceLayerOf } from '@/editor/findingLayer'
|
||||
import { initialTab } from '@/editor/tabs'
|
||||
@@ -31,6 +25,7 @@ import {
|
||||
} from '@/editor/useEditorNavigation'
|
||||
import type { TabTier } from '@/editor/useViewport'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import ConflictDialog from './ConflictDialog'
|
||||
import CopyIssueNotice from './CopyIssueNotice'
|
||||
import DraftHeader from './DraftHeader'
|
||||
@@ -131,20 +126,35 @@ function Places({ nav, readOnly, reloadSheet }: PlacesProps) {
|
||||
)
|
||||
}
|
||||
|
||||
/** The signed-in account's session tag, and its id (kept in memory only). */
|
||||
interface Who {
|
||||
tag: string
|
||||
account: string
|
||||
}
|
||||
|
||||
/** The sheet and the session tag the history entries of this editor belong to. */
|
||||
function useOwner(sheetId: string, tag: string): HistoryOwner {
|
||||
return useMemo(() => ({ sheetId, tag }), [sheetId, tag])
|
||||
}
|
||||
|
||||
/**
|
||||
* A leave that leaves no left-draft flag: one the user confirmed before a history jump,
|
||||
* or one under a tag already rotated (a sign-out): nobody would read that flag.
|
||||
* How this editor leaves, for its left-draft flag: a leave the user confirmed before a
|
||||
* history jump leaves none; one under a tag already rotated (a sign-out, a session
|
||||
* ending) leaves it keyed by the account, so its next sign-in in this tab is told.
|
||||
*/
|
||||
function useUnflagged(nav: EditorNavigation, tag: string) {
|
||||
function useLeaveFacts(
|
||||
nav: EditorNavigation,
|
||||
tag: string,
|
||||
account: string,
|
||||
): LeaveFacts {
|
||||
const confirmed = useLeaveConfirm(nav)
|
||||
return useCallback(
|
||||
() => confirmed() || useSessionTagStore.getState().held?.tag !== tag,
|
||||
[confirmed, tag],
|
||||
return useMemo(
|
||||
() => ({
|
||||
confirmed,
|
||||
rotated: () => useSessionTagStore.getState().held?.tag !== tag,
|
||||
account,
|
||||
}),
|
||||
[confirmed, tag, account],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -177,14 +187,15 @@ function useShowRestored(nav: EditorNavigation) {
|
||||
* confirmation before a history jump leaves with a dirty dialog, and whether the last
|
||||
* editor left a dirty dialog behind.
|
||||
*/
|
||||
function useWorkspaceNavigation(sheetId: string, tier: TabTier, tag: string) {
|
||||
function useWorkspaceNavigation(sheetId: string, tier: TabTier, who: Who) {
|
||||
const previewState = useEditorStore((s) => s.previewState)
|
||||
const tab = previewState ? initialTab(previewState, tier) : 'annotations'
|
||||
const owner = useOwner(sheetId, tag)
|
||||
const owner = useOwner(sheetId, who.tag)
|
||||
const nav = useEditorNavigation(tab, useRestoredView(owner))
|
||||
useEditorHistory(nav, owner, defaultView(tab))
|
||||
useShowRestored(nav)
|
||||
return { nav, left: useLeftDraft(nav, owner, useUnflagged(nav, tag)) }
|
||||
const leave = useLeaveFacts(nav, who.tag, who.account)
|
||||
return { nav, left: useLeftDraft(nav, owner, leave) }
|
||||
}
|
||||
|
||||
/** The last editor of this sheet was left with a dirty dialog: said once. */
|
||||
@@ -199,12 +210,12 @@ function LeftDraftNotice({ shown }: { shown: boolean }) {
|
||||
* panel and the shown place. Owns the navigation state, so the first tab is known at
|
||||
* mount and later edits never move the user off their tab.
|
||||
*/
|
||||
function Workspace(props: Props & { tag: string }) {
|
||||
function Workspace(props: Props & Who) {
|
||||
const { sheet, mode, readOnly } = props
|
||||
const { nav, left } = useWorkspaceNavigation(
|
||||
sheet.sheet_id,
|
||||
props.tier,
|
||||
props.tag,
|
||||
props,
|
||||
)
|
||||
return (
|
||||
<EditingState readOnly={readOnly} mode={mode}>
|
||||
@@ -244,5 +255,7 @@ function Workspace(props: Props & { tag: string }) {
|
||||
*/
|
||||
export default function EditorWorkspace(props: Props) {
|
||||
const tag = useSessionTag()
|
||||
return tag ? <Workspace key={tag} {...props} tag={tag} /> : null
|
||||
const account = useAuthStore((s) => s.user?.id ?? null)
|
||||
if (!tag || !account) return null
|
||||
return <Workspace key={tag} {...props} tag={tag} account={account} />
|
||||
}
|
||||
Reference in new issue
Block a user