"use client";
import "./reading-bar.css";
import {
Expand,
FileText,
Maximize2,
Minimize2,
PanelLeftClose,
PanelLeftOpen,
PanelRightClose,
PanelRightOpen,
SquarePen,
} from "lucide-react";
import { useCallback, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import Tooltip from "@/shared/ui/Tooltip";
import { FeatureCrumb, InstanceCrumb, type CrumbItem } from "@/components/layout/LocationCrumbs";
import { useShelfFeature } from "@/components/sidebar/PageBar";
import { fetchReadingCollectionIndex, type ReadingWorkspaceTab } from "@/lib/reading-workspace-api";
import { READING_HOME, readingCollectionRoute } from "@/lib/learning-routes";
import type { WorkspaceMenuItem } from "@/components/reading/workspace-menu-context";
import { PageToolButtons, ReadAloudButton } from "./ReadAloudButton";
import { WorkspaceMenu } from "./WorkspaceMenu";
const ICON_BUTTON =
"flex size-7 shrink-0 items-center justify-center rounded-md transition hover:bg-[var(--muted)] disabled:cursor-not-allowed disabled:opacity-40 disabled:hover:bg-transparent";
function BarButton({
label,
onClick,
active = false,
disabled = false,
pressed,
expanded,
children,
}: {
label: string;
onClick: () => void;
active?: boolean;
disabled?: boolean;
pressed?: boolean;
expanded?: boolean;
children: ReactNode;
}) {
return (
);
}
/**
* The reading workspace's one row of chrome, over the document only.
*
* Its left half is the location every page states, 「沉浸式阅读 / the
* collection / the document」: the feature opens onto the other features of
* the learning shelf, the collection onto the other collections (double-click
* renames it, as does its menu; its folder page is at the bottom), the document onto the other
* files in it (adding one is at the top). The reader pane hands its place,
* history and bookmark up into `docSlot`, after the document. The right half
* is what can be done with it. The companion keeps its own controls on its
* own sheet; only the switch that brings it back lives here, and only while
* it is away.
*/
export function ReadingBar({
backHref,
collectionId,
title,
onRename,
onAdd,
documents,
activeDocumentId,
onSwitchDocument,
navigatorOpen,
onToggleNavigator,
docSlot,
notice,
materialReady,
busy,
onQuiz,
learning,
onToggleLearning,
menuSections,
collection,
companionOpen,
onOpenCompanion,
}: {
backHref: string;
collectionId: string;
title: string;
onRename: () => void;
onAdd: () => void;
documents: ReadingWorkspaceTab[];
activeDocumentId: string | null;
onSwitchDocument: (tab: ReadingWorkspaceTab) => void;
navigatorOpen: boolean;
onToggleNavigator: () => void;
docSlot: (node: HTMLDivElement | null) => void;
notice?: string | null;
materialReady: boolean;
busy: boolean;
onQuiz: () => void;
learning: boolean;
onToggleLearning: () => void;
menuSections: Parameters[0]["sections"];
collection: WorkspaceMenuItem[];
companionOpen: boolean;
onOpenCompanion: () => void;
}) {
const { t } = useTranslation();
const feature = useShelfFeature(READING_HOME);
const loadCollections = useCallback(
async (): Promise =>
(await fetchReadingCollectionIndex()).map((collection) => ({
id: collection.workspace_id,
label: collection.title || t("Untitled collection"),
href: readingCollectionRoute(collection.workspace_id),
})),
[t],
);
const documentItems = useCallback(
async (): Promise =>
documents.map((tab) => ({
id: tab.material.material_id,
label: tab.material.title || tab.material.filename,
icon: FileText,
})),
[documents],
);
const activeDocument = documents.find((tab) => tab.material.material_id === activeDocumentId);
return (
{/* The outline opens on the left, so its switch lives on the left. */}
{navigatorOpen ? : }
{/* The open document's place, history and bookmark render here. */}
);
}
/**
* The top of the companion's sheet: what it is, and its own three controls —
* start over, take the whole width, put it away.
*/
export function CompanionHead({
title,
canStartOver,
onStartOver,
focus,
onClose,
}: {
title?: string;
canStartOver: boolean;
onStartOver: () => void;
/** Present only where the companion can take the reader's width. */
focus?: { active: boolean; toggle: () => void };
onClose: () => void;
}) {
const { t } = useTranslation();
return (