import { type ReactNode } from "react";
import { LuFileDiff } from "react-icons/lu";
import { RiBookmarkLine, RiCloseLine } from "react-icons/ri";
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js";
import { useComponentsContext } from "../../editor/ComponentsContext.js";
import { useVersioning } from "./useVersioning.js";
import { useDictionary } from "../../i18n/dictionary.js";
import { usePreviewRow } from "./usePreviewRow.js";
import { useVersioningSidebar } from "./VersioningSidebarContext.js";
import { getShownVersionRow, getVersionList } from "./visibleHistory.js";
/** A header button, whose tooltip and accessible name are the same string. */
function HeaderButton(props: {
label: string;
isSelected?: boolean;
onClick: () => void;
children: ReactNode;
}) {
const Components = useComponentsContext()!;
return (
{props.children}
);
}
export function VersioningSidebarHeader(props: { onClose?: () => void }) {
const editor = useBlockNoteEditor();
const Components = useComponentsContext()!;
const dict = useDictionary();
const { store, canCompare, canRename } = useVersioning();
const {
comparisonMode,
setComparisonMode,
namedOnly,
setNamedOnly,
run,
close,
} = useVersioningSidebar();
const previewRow = usePreviewRow();
// Toggling comparison re-previews whatever is on screen with the new
// baseline, so the toggle takes effect immediately instead of waiting for the
// next row click.
function toggleComparison() {
void run(async () => {
const view = store.state;
const next = !comparisonMode;
setComparisonMode(next);
const list = getVersionList(view);
if (view.mode === "live" || !list.loaded) {
return { status: "unavailable" };
}
const shown = getShownVersionRow(list, view);
if (shown) {
return previewRow(shown.snapshot, {
compareTo: { type: next ? "previous" : "none" },
});
}
return { status: "unavailable" };
});
}
function toggleNamedOnly() {
void run(async () => {
const view = store.state;
const next = !namedOnly;
setNamedOnly(next);
if (!comparisonMode) {
return { status: "unavailable" };
}
const list = getVersionList(view);
if (view.mode === "live" || !list.loaded) {
return { status: "unavailable" };
}
const shown = getShownVersionRow(list, view);
// Reset the baseline for the new filter, keeping the displayed source.
if (shown) {
return previewRow(shown.snapshot, { namedOnly: next });
}
return { status: "unavailable" };
});
}
return (
{(canRename || namedOnly) && (
)}
{canCompare && (
)}
}
closeAction={
props.onClose ? (
{
close();
editor.focus();
props.onClose?.();
}}
>
) : undefined
}
/>
);
}