Address six QA findings on the offline-sync feature: 1. HIGH — silent data loss: a paused mutation persisted to IndexedDB and reloaded while still offline never resumed on reconnect. Seed TanStack onlineManager from navigator.onLine at boot (it defaults to online:true and only flips on events, so a cold-boot-offline tab wrongly believed it was online and never got a true online transition), and call resumePausedMutations() in PersistQueryClientProvider onSuccess after the persister rehydrates (defaults are registered before, so the restored mutation has a mutationFn). New offline-resume.test.ts reproduces the full persist -> reload -> reconnect path. 2. MEDIUM (security) — logout did not durably clear gitmost-rq-cache: the throttled persister re-wrote the key ~1s after del() with the still-in-memory snapshot, resurrecting the previous user's data. Freeze the persister (persistClient becomes a no-op) before clearing/deleting so neither the clear()-triggered nor any in-flight write can repopulate the key; re-enable afterwards for the next sign-in session. 3. MEDIUM (UX) — offline create spun forever: the create-note button awaited a mutateAsync that stays pending while paused. Detect offline, fire-and-forget the (queued) mutation, show a "saved offline" notice, and gate the spinner on !isPaused so it no longer hangs. 4. LOW — an uncached page opened offline showed the generic "Error fetching page data." instead of the offline fallback (offline fetch yields no HTTP status). Render OfflineFallback when navigator is offline or the error has no status. 5. LOW — logout teardown threw "Cannot read properties of null (reading 'settings')" in full-editor.tsx: optional-chain the (transiently null) user. 6. Tab title "Untitled": investigated — the tab-title derivation in page.tsx is byte-identical to develop and already reads page.title from REST/cache (the recommended source); live edits keep it in sync via updatePageData. Not a tab-title-derivation regression introduced by this PR; no change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
280 lines
9.2 KiB
TypeScript
280 lines
9.2 KiB
TypeScript
import classes from "@/features/editor/styles/editor.module.css";
|
|
import React, { useEffect } from "react";
|
|
import { TitleEditor } from "@/features/editor/title-editor";
|
|
import PageEditor from "@/features/editor/page-editor";
|
|
import {
|
|
ActionIcon,
|
|
Container,
|
|
Divider,
|
|
Group,
|
|
Popover,
|
|
Stack,
|
|
Text,
|
|
Tooltip,
|
|
UnstyledButton,
|
|
} from "@mantine/core";
|
|
import { IconInfoCircle } from "@tabler/icons-react";
|
|
import { useAtom, useAtomValue } from "jotai";
|
|
import {
|
|
userAtom,
|
|
workspaceAtom,
|
|
} from "@/features/user/atoms/current-user-atom.ts";
|
|
import { CustomAvatar } from "@/components/ui/custom-avatar.tsx";
|
|
import { useTranslation } from "react-i18next";
|
|
import { IContributor } from "@/features/page/types/page.types.ts";
|
|
import { FixedToolbar } from "@/features/editor/components/fixed-toolbar/fixed-toolbar";
|
|
import { PageEditMode } from "@/features/user/types/user.types.ts";
|
|
import { useAsideTriggerProps } from "@/hooks/use-toggle-aside.tsx";
|
|
import { DeletedPageBanner } from "@/features/page/trash/components/deleted-page-banner.tsx";
|
|
import { TemporaryNoteBanner } from "@/features/page/components/temporary-note-banner.tsx";
|
|
import clsx from "clsx";
|
|
import {
|
|
currentPageEditModeAtom,
|
|
pageEditorAtom,
|
|
} from "@/features/editor/atoms/editor-atoms.ts";
|
|
import { DictationGroup } from "@/features/editor/components/fixed-toolbar/groups/dictation-group";
|
|
import { GenerateTitleGroup } from "@/features/editor/components/fixed-toolbar/groups/generate-title-group";
|
|
import { usePageCollabProviders } from "@/features/editor/hooks/use-page-collab-providers";
|
|
import { EditorProvidersContext } from "@/features/editor/contexts/editor-providers-context";
|
|
|
|
const MemoizedTitleEditor = React.memo(TitleEditor);
|
|
const MemoizedPageEditor = React.memo(PageEditor);
|
|
const MemoizedFixedToolbar = React.memo(FixedToolbar);
|
|
const MemoizedDeletedPageBanner = React.memo(DeletedPageBanner);
|
|
const MemoizedTemporaryNoteBanner = React.memo(TemporaryNoteBanner);
|
|
|
|
type PageUser = {
|
|
id: string;
|
|
name: string;
|
|
avatarUrl: string;
|
|
};
|
|
|
|
// Module-level flag: survives component unmount/remount on page navigation,
|
|
// reset only on full page reload (i.e. a new app session).
|
|
let defaultEditModeApplied = false;
|
|
|
|
export interface FullEditorProps {
|
|
pageId: string;
|
|
slugId: string;
|
|
title: string;
|
|
content: string;
|
|
spaceSlug: string;
|
|
editable: boolean;
|
|
creator?: PageUser;
|
|
contributors?: IContributor[];
|
|
canComment?: boolean;
|
|
}
|
|
|
|
export function FullEditor({
|
|
pageId,
|
|
title,
|
|
slugId,
|
|
content,
|
|
spaceSlug,
|
|
editable,
|
|
creator,
|
|
contributors,
|
|
canComment,
|
|
}: FullEditorProps) {
|
|
const [user] = useAtom(userAtom);
|
|
const workspace = useAtomValue(workspaceAtom);
|
|
const isDictationEnabled = workspace?.settings?.ai?.dictation === true;
|
|
// AI title generation is gated by the general AI chat flag (the same toggle
|
|
// that enables the chat agent); the server enforces it too (#199).
|
|
const isTitleGenEnabled = workspace?.settings?.ai?.chat === true;
|
|
// `user` can momentarily be null during logout teardown (the currentUser atom
|
|
// is reset before this subtree unmounts). Optional-chain every access so the
|
|
// teardown render does not throw "Cannot read properties of null (reading
|
|
// 'settings')".
|
|
const fullPageWidth = user?.settings?.preferences?.fullPageWidth;
|
|
const editorToolbarEnabled =
|
|
user?.settings?.preferences?.editorToolbar ?? false;
|
|
const [currentPageEditMode, setCurrentPageEditMode] = useAtom(
|
|
currentPageEditModeAtom,
|
|
);
|
|
const userPageEditMode =
|
|
user?.settings?.preferences?.pageEditMode ?? PageEditMode.Edit;
|
|
const isEditMode = currentPageEditMode === PageEditMode.Edit;
|
|
|
|
// Single shared Y.Doc + HocuspocusProvider for both the title and body
|
|
// editors (title lives in the 'title' fragment of the same doc).
|
|
const { ydoc, remote, providersReady } = usePageCollabProviders(pageId);
|
|
|
|
// Apply the user's saved preference only once on initial load, not on every
|
|
// page navigation — so the mode sticks across navigations within a session.
|
|
useEffect(() => {
|
|
if (!defaultEditModeApplied) {
|
|
setCurrentPageEditMode(userPageEditMode as PageEditMode);
|
|
defaultEditModeApplied = true;
|
|
}
|
|
}, [userPageEditMode, setCurrentPageEditMode]);
|
|
|
|
return (
|
|
<Container
|
|
fluid={fullPageWidth}
|
|
size={!fullPageWidth && 900}
|
|
className={classes.editor}
|
|
>
|
|
{editorToolbarEnabled && editable && isEditMode && (
|
|
<MemoizedFixedToolbar />
|
|
)}
|
|
<MemoizedDeletedPageBanner slugId={slugId} />
|
|
<MemoizedTemporaryNoteBanner slugId={slugId} />
|
|
<EditorProvidersContext.Provider
|
|
value={ydoc && remote ? { ydoc, remote, providersReady } : null}
|
|
>
|
|
<MemoizedTitleEditor
|
|
pageId={pageId}
|
|
slugId={slugId}
|
|
title={title}
|
|
spaceSlug={spaceSlug}
|
|
editable={editable}
|
|
/>
|
|
<PageByline
|
|
pageId={pageId}
|
|
creator={creator}
|
|
contributors={contributors}
|
|
editable={editable}
|
|
isEditMode={isEditMode}
|
|
isDictationEnabled={isDictationEnabled}
|
|
isTitleGenEnabled={isTitleGenEnabled}
|
|
/>
|
|
<MemoizedPageEditor
|
|
pageId={pageId}
|
|
editable={editable}
|
|
content={content}
|
|
canComment={canComment}
|
|
/>
|
|
</EditorProvidersContext.Provider>
|
|
</Container>
|
|
);
|
|
}
|
|
|
|
type PageBylineProps = {
|
|
pageId: string;
|
|
creator?: PageUser;
|
|
contributors?: IContributor[];
|
|
editable?: boolean;
|
|
isEditMode?: boolean;
|
|
isDictationEnabled?: boolean;
|
|
isTitleGenEnabled?: boolean;
|
|
};
|
|
|
|
function PageByline({
|
|
pageId,
|
|
creator,
|
|
contributors,
|
|
editable,
|
|
isEditMode,
|
|
isDictationEnabled,
|
|
isTitleGenEnabled,
|
|
}: PageBylineProps) {
|
|
const { t } = useTranslation();
|
|
const detailsTriggerProps = useAsideTriggerProps("details");
|
|
const editor = useAtomValue(pageEditorAtom);
|
|
const showDictation = Boolean(
|
|
isDictationEnabled && editable && isEditMode && editor,
|
|
);
|
|
const showTitleGen = Boolean(
|
|
isTitleGenEnabled && editable && isEditMode && editor,
|
|
);
|
|
|
|
const otherContributors = (contributors ?? []).filter(
|
|
(c) => c.id !== creator?.id,
|
|
);
|
|
|
|
return (
|
|
<Group
|
|
gap="sm"
|
|
mb="md"
|
|
className={clsx("print-hide", classes.byline)}
|
|
style={{ marginTop: "-0.5em" }}
|
|
>
|
|
{creator && (
|
|
<Popover position="bottom-start" shadow="md" width={280} withArrow>
|
|
<Popover.Target>
|
|
<UnstyledButton
|
|
aria-label={t("Created by {{name}}", { name: creator.name })}
|
|
>
|
|
<Group gap={6}>
|
|
<CustomAvatar
|
|
avatarUrl={creator.avatarUrl}
|
|
name={creator.name}
|
|
size={22}
|
|
/>
|
|
<Text size="sm" c="dimmed">
|
|
{t("By {{name}}", { name: creator.name })}
|
|
</Text>
|
|
</Group>
|
|
</UnstyledButton>
|
|
</Popover.Target>
|
|
<Popover.Dropdown>
|
|
<Stack gap="xs">
|
|
<Group gap="sm">
|
|
<CustomAvatar
|
|
avatarUrl={creator.avatarUrl}
|
|
name={creator.name}
|
|
size={36}
|
|
/>
|
|
<div>
|
|
<Text size="sm" fw={500}>
|
|
{creator.name}
|
|
</Text>
|
|
<Text size="xs" c="dimmed">
|
|
{otherContributors.length === 0
|
|
? t("Owner, no contributors")
|
|
: t("Owner")}
|
|
</Text>
|
|
</div>
|
|
</Group>
|
|
|
|
{otherContributors.length > 0 && (
|
|
<>
|
|
<Divider />
|
|
<Text size="xs" fw={500} c="dimmed" tt="uppercase">
|
|
{t("Contributors")}
|
|
</Text>
|
|
<Stack gap={6}>
|
|
{otherContributors.map((contributor) => (
|
|
<Group gap="sm" key={contributor.id}>
|
|
<CustomAvatar
|
|
avatarUrl={contributor.avatarUrl}
|
|
name={contributor.name}
|
|
size={28}
|
|
/>
|
|
<Text size="sm">{contributor.name}</Text>
|
|
</Group>
|
|
))}
|
|
</Stack>
|
|
</>
|
|
)}
|
|
</Stack>
|
|
</Popover.Dropdown>
|
|
</Popover>
|
|
)}
|
|
<Group gap={4} wrap="nowrap">
|
|
<Tooltip label={t("Details")} withArrow openDelay={250}>
|
|
<ActionIcon
|
|
variant="subtle"
|
|
color="gray"
|
|
aria-label={t("Details")}
|
|
{...detailsTriggerProps}
|
|
>
|
|
<IconInfoCircle size={20} stroke={1.5} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
{/* Shown only in edit mode when workspace dictation is enabled, so
|
|
dictation stays reachable even when the fixed toolbar is hidden. */}
|
|
{showDictation && editor && (
|
|
<DictationGroup editor={editor} color="gray" iconSize={20} />
|
|
)}
|
|
{/* Shown only in edit mode when the workspace's AI chat flag is on,
|
|
so AI title generation stays reachable from the byline (#199). */}
|
|
{showTitleGen && (
|
|
<GenerateTitleGroup pageId={pageId} color="gray" iconSize={20} />
|
|
)}
|
|
</Group>
|
|
</Group>
|
|
);
|
|
}
|