2025-04-04 06:03:05 +00:00
|
|
|
"use client";
|
|
|
|
|
2025-04-06 10:16:43 +00:00
|
|
|
import type {
|
|
|
|
AddPanelOptions,
|
|
|
|
DockviewApi,
|
|
|
|
DockviewReadyEvent,
|
|
|
|
IDockviewPanelHeaderProps,
|
|
|
|
IDockviewPanelProps,
|
|
|
|
} from "dockview";
|
2025-04-04 06:03:05 +00:00
|
|
|
import "@/styles/dockview.css";
|
2025-04-07 02:32:58 +00:00
|
|
|
import * as Icons from "lucide-react";
|
2025-04-06 10:16:43 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import type { LucideIcon } from "lucide-react";
|
2025-04-05 09:50:07 +00:00
|
|
|
import { DockviewReact, themeAbyssSpaced } from "dockview";
|
2025-04-04 06:03:05 +00:00
|
|
|
|
|
|
|
interface DockviewProps {
|
2025-04-05 09:50:07 +00:00
|
|
|
storageKey: string;
|
2025-04-06 10:16:43 +00:00
|
|
|
options: AddPanelOptions[];
|
|
|
|
components: Record<string, React.FunctionComponent<IDockviewPanelProps>>;
|
|
|
|
tabComponents?: Record<string, React.FunctionComponent<IDockviewPanelHeaderProps>>;
|
|
|
|
onApiReady?: (api: DockviewApi) => void;
|
2025-04-04 06:03:05 +00:00
|
|
|
}
|
|
|
|
|
2025-04-06 10:16:43 +00:00
|
|
|
const DefaultTab = (
|
2025-04-07 02:32:58 +00:00
|
|
|
props: IDockviewPanelHeaderProps<{ icon?: string }>
|
2025-04-06 10:16:43 +00:00
|
|
|
) => {
|
2025-04-07 02:32:58 +00:00
|
|
|
const { icon } = props.params;
|
|
|
|
const Icon =
|
|
|
|
icon && icon in Icons
|
|
|
|
? (Icons[icon as keyof typeof Icons] as LucideIcon)
|
|
|
|
: null;
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-06 10:16:43 +00:00
|
|
|
return (
|
|
|
|
<div className="flex items-center px-1 text-sm font-medium">
|
|
|
|
{Icon && (
|
|
|
|
<Icon
|
|
|
|
className="-ms-0.5 me-1.5 opacity-60"
|
|
|
|
size={16}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{props.api.title}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-06 10:16:43 +00:00
|
|
|
export default function DockView({
|
|
|
|
storageKey,
|
|
|
|
options,
|
|
|
|
components,
|
|
|
|
tabComponents,
|
|
|
|
onApiReady,
|
|
|
|
}: DockviewProps) {
|
|
|
|
const [api, setApi] = useState<DockviewApi>();
|
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(() => {
|
2025-04-06 10:16:43 +00:00
|
|
|
const layout = api.toJSON();
|
|
|
|
localStorage.setItem(storageKey, JSON.stringify(layout));
|
2025-04-05 09:50:07 +00:00
|
|
|
});
|
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-06 10:16:43 +00:00
|
|
|
const onReady = (event: DockviewReadyEvent) => {
|
2025-04-05 09:50:07 +00:00
|
|
|
setApi(event.api);
|
2025-04-06 10:16:43 +00:00
|
|
|
onApiReady?.(event.api);
|
2025-04-05 09:50:07 +00:00
|
|
|
|
2025-04-06 10:16:43 +00:00
|
|
|
let success = false;
|
|
|
|
const serializedLayout = localStorage.getItem(storageKey);
|
2025-04-04 06:03:05 +00:00
|
|
|
|
2025-04-05 09:50:07 +00:00
|
|
|
if (serializedLayout) {
|
|
|
|
try {
|
2025-04-06 10:16:43 +00:00
|
|
|
const layout = JSON.parse(serializedLayout);
|
|
|
|
event.api.fromJSON(layout);
|
|
|
|
success = true;
|
2025-04-05 09:50:07 +00:00
|
|
|
} catch (error) {
|
2025-04-06 10:16:43 +00:00
|
|
|
console.error("Failed to load layout:", error);
|
2025-04-05 09:50:07 +00:00
|
|
|
localStorage.removeItem(storageKey);
|
|
|
|
}
|
2025-04-06 10:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
options.forEach((option) => {
|
|
|
|
event.api.addPanel({ ...option });
|
|
|
|
});
|
2025-04-05 09:50:07 +00:00
|
|
|
}
|
2025-04-04 06:03:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DockviewReact
|
|
|
|
theme={themeAbyssSpaced}
|
2025-04-06 10:16:43 +00:00
|
|
|
onReady={onReady}
|
2025-04-04 06:03:05 +00:00
|
|
|
components={components}
|
2025-04-06 10:16:43 +00:00
|
|
|
defaultTabComponent={DefaultTab}
|
2025-04-04 06:03:05 +00:00
|
|
|
tabComponents={tabComponents}
|
|
|
|
/>
|
|
|
|
);
|
2025-04-06 10:16:43 +00:00
|
|
|
}
|