2025-04-04 06:03:05 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import {
|
2025-04-05 09:50:07 +00:00
|
|
|
BotIcon,
|
|
|
|
CircleCheckBigIcon,
|
|
|
|
FileTextIcon,
|
|
|
|
FlaskConicalIcon,
|
|
|
|
SquareCheckIcon,
|
|
|
|
SquarePenIcon,
|
|
|
|
TerminalIcon,
|
2025-04-04 06:03:05 +00:00
|
|
|
} from "lucide-react";
|
|
|
|
import "@/styles/dockview.css";
|
2025-04-05 09:50:07 +00:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
import { DockviewReact, themeAbyssSpaced } from "dockview";
|
|
|
|
import type { AddPanelOptions, DockviewReadyEvent, DockviewApi } from "dockview";
|
|
|
|
|
|
|
|
const iconMap = {
|
|
|
|
FileTextIcon,
|
|
|
|
FlaskConicalIcon,
|
|
|
|
CircleCheckBigIcon,
|
|
|
|
SquarePenIcon,
|
|
|
|
SquareCheckIcon,
|
|
|
|
TerminalIcon,
|
|
|
|
BotIcon,
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
type IconKey = keyof typeof iconMap;
|
2025-04-04 06:03:05 +00:00
|
|
|
|
|
|
|
interface DockviewProps {
|
2025-04-05 09:50:07 +00:00
|
|
|
options: (AddPanelOptions & {
|
|
|
|
node: React.ReactNode;
|
|
|
|
icon: IconKey;
|
|
|
|
})[];
|
|
|
|
storageKey: string;
|
2025-04-04 06:03:05 +00:00
|
|
|
}
|
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
const DockView = ({ options, storageKey }: DockviewProps) => {
|
|
|
|
const [api, setApi] = useState<DockviewApi>();
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
const { components, tabComponents } = useMemo(() => {
|
|
|
|
const comps: Record<string, () => React.ReactNode> = {};
|
|
|
|
const tabs: Record<string, () => React.ReactNode> = {};
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
options.forEach((option) => {
|
|
|
|
const { id, icon, node, title } = option;
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
comps[id] = () => <>{node}</>;
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
const Icon = iconMap[icon];
|
|
|
|
tabs[id] = () => (
|
|
|
|
<div className="flex items-center px-1 text-sm font-medium">
|
|
|
|
<Icon
|
|
|
|
className="-ms-0.5 me-1.5 opacity-60"
|
|
|
|
size={16}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
<span>{title}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
return { components: comps, tabComponents: tabs };
|
|
|
|
}, [options]);
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!api) return;
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
const disposable = api.onDidLayoutChange(() => {
|
|
|
|
localStorage.setItem(storageKey, JSON.stringify(api.toJSON()));
|
|
|
|
});
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
return () => disposable.dispose();
|
|
|
|
}, [api, storageKey]);
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
const handleReady = (event: DockviewReadyEvent) => {
|
|
|
|
setApi(event.api);
|
|
|
|
const serializedLayout = localStorage.getItem(storageKey);
|
|
|
|
|
|
|
|
const addDefaultPanels = () => {
|
|
|
|
options.forEach((option) => {
|
|
|
|
event.api.addPanel({ ...option });
|
|
|
|
});
|
2025-04-04 06:03:05 +00:00
|
|
|
};
|
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
if (serializedLayout) {
|
|
|
|
try {
|
|
|
|
event.api.fromJSON(JSON.parse(serializedLayout));
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to parse layout:", error);
|
|
|
|
localStorage.removeItem(storageKey);
|
|
|
|
addDefaultPanels();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addDefaultPanels();
|
|
|
|
}
|
2025-04-04 06:03:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DockviewReact
|
|
|
|
theme={themeAbyssSpaced}
|
|
|
|
onReady={handleReady}
|
|
|
|
components={components}
|
|
|
|
tabComponents={tabComponents}
|
|
|
|
/>
|
|
|
|
);
|
2025-04-05 09:50:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default DockView;
|