Files
DeepTutor/web/components/reading/workspace/useLearningMode.ts
Bingxi Zhao (Frank) 53436f8190 feat(reading): collections as colored folders; rework reader interactions
Collections vs workspaces: a workspace holds many collections, and a
collection's files and reading conversations live in the workspace it
was created in. The library now shows collections as folders grouped
under their workspace, and creating one asks which workspace it goes
into.

- Folder page at /learning/reading/folders/[folderId]: breadcrumb,
  files, the collection's conversations, and "Start reading" that opens
  the whole folder in the reader. The reader's back link returns to it.
- Collections carry a user-picked color (new `color` column, migrated);
  tones are --folder-* tokens with dark variants.
- One shared "New collection" / "Edit collection" dialog with consistent
  type sizes.
- Selection popover gains AI actions; results land as cards at the foot
  of the companion. Read-aloud is a single header toggle.
- The three ⋯ menus (collection, document, conversation) merge into one.
- Outline toggle moves to the left and starts closed; navigator has
  Outline / Annotations tabs.
- Reading conversations live in the main sidebar (with a page-shaped idle
  mark) instead of a history dropdown in the companion.
- Fix sidebar rows never showing a selected state
  (bg-[var(--background)]/60 compiled to nothing).
- Composer floats over the conversation (no divider, bottom fade); the
  toolbar wraps by content instead of splitting under 420px.
2026-09-23 18:59:17 +08:00

144 lines
4.4 KiB
TypeScript

'use client'
import { browserStorage } from '@/shared/storage'
import { useCallback, useEffect, useRef, useState } from 'react'
interface ReadingPanelState {
companionOpen: boolean
navigatorOpen: boolean
}
const READING_LEARNING_KEY = 'reading-learning'
/** Learning mode owns the entry-time panel state, not just the boolean. */
function restoreReadingLearning(
value: string | null,
workspaceId: string
): ReadingPanelState | null {
if (!value) return null
try {
const preference = JSON.parse(value) as {
panels?: Partial<ReadingPanelState> & {
navigatorCollapsed?: boolean
}
workspaceId?: string
}
const panels = preference.workspaceId === workspaceId ? preference.panels : null
if (!panels || typeof panels !== 'object') return null
// Normalize the previous two-flag session schema to one panel state.
return {
companionOpen: panels.companionOpen === true,
navigatorOpen: panels.navigatorOpen === true || panels.navigatorCollapsed === false,
}
} catch {
return null
}
}
export function useReadingLearningMode(workspaceId: string) {
const [learning, setLearning] = useState(
() =>
restoreReadingLearning(
browserStorage.readRaw('session', READING_LEARNING_KEY),
workspaceId
) !== null
)
const [learningSnapshot, setLearningSnapshot] = useState<ReadingPanelState | null>(() =>
restoreReadingLearning(browserStorage.readRaw('session', READING_LEARNING_KEY), workspaceId)
)
const [companionOpen, setCompanionOpen] = useState(() =>
learning ? false : (learningSnapshot?.companionOpen ?? true)
)
// The outline starts closed: a learner opens a collection to read, and the
// page is the thing to show. Learning mode still opens it on entry.
const [navigatorOpen, setNavigatorOpen] = useState(() =>
learning ? true : (learningSnapshot?.navigatorOpen ?? false)
)
const mainRef = useRef<HTMLElement | null>(null)
const openLearning = useCallback(() => {
const snapshot: ReadingPanelState = {
companionOpen,
navigatorOpen,
}
setLearningSnapshot(snapshot)
browserStorage.writeRaw(
'session',
READING_LEARNING_KEY,
JSON.stringify({ panels: snapshot, workspaceId })
)
setLearning(true)
setCompanionOpen(false)
setNavigatorOpen(true)
}, [companionOpen, navigatorOpen, workspaceId])
const closeLearning = useCallback(() => {
browserStorage.removeRaw('session', READING_LEARNING_KEY)
setLearning(false)
setLearningSnapshot(null)
if (learningSnapshot) {
setCompanionOpen(learningSnapshot.companionOpen)
setNavigatorOpen(learningSnapshot.navigatorOpen)
}
}, [learningSnapshot])
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
if (event.key !== 'Escape' || !learning || document.fullscreenElement) return
const hasVisibleDialog = Array.from(
document.querySelectorAll<HTMLElement>('[role="dialog"],[role="alertdialog"]')
).some(
dialog =>
dialog.getBoundingClientRect().width > 0 &&
getComputedStyle(dialog).visibility !== 'hidden'
)
if (hasVisibleDialog) return
closeLearning()
}
document.addEventListener('keydown', onKeyDown)
return () => document.removeEventListener('keydown', onKeyDown)
}, [closeLearning, learning])
useEffect(() => {
if (!learning) return
const root = mainRef.current
if (!root) return
const siblings = new Map<HTMLElement, boolean>()
const isolate = () => {
let node: HTMLElement = root
while (node.parentElement && node.parentElement !== document.body) {
for (const sibling of Array.from(node.parentElement.children)) {
if (sibling !== node && sibling instanceof HTMLElement) {
if (!siblings.has(sibling)) siblings.set(sibling, sibling.inert)
sibling.inert = true
}
}
node = node.parentElement
}
return siblings
}
isolate()
const observer = new MutationObserver(isolate)
observer.observe(document.body, { childList: true, subtree: true })
return () => {
observer.disconnect()
for (const [element, inert] of siblings) element.inert = inert
}
}, [learning])
return {
closeLearning,
companionOpen,
learning,
mainRef,
navigatorOpen,
openLearning,
setCompanionOpen,
setNavigatorOpen,
}
}