buster/web/src/layouts/ChatLayout/ChatLayoutContext/useChatFileLayout/useChatFileLayout.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-02-01 02:04:49 +08:00
import { FileType } from '@/api/asset_interfaces';
2025-01-31 06:33:06 +08:00
import { useMemo, useState } from 'react';
import { FileConfig, FileView, FileViewConfig, FileViewSecondary } from './interfaces';
2025-03-08 07:02:56 +08:00
import { useMemoizedFn } from '@/hooks';
2025-02-08 05:11:05 +08:00
import { create } from 'mutative';
2025-01-31 06:33:06 +08:00
export const useChatFileLayout = ({
selectedFileId,
selectedFileType
}: {
selectedFileId: string | undefined;
selectedFileType: FileType | undefined;
}) => {
const [fileViews, setFileViews] = useState<Record<string, FileConfig>>({});
const onSetFileView = useMemoizedFn(
({
fileView,
fileId,
secondaryView
}: {
fileView?: FileView;
fileId?: string;
secondaryView?: FileViewSecondary;
}) => {
const id = fileId ?? selectedFileId;
if (!id) return;
setFileViews((prev) => {
const newFileConfig: FileConfig = { ...prev[id] };
const usedFileView =
fileView ??
newFileConfig.selectedFileView ??
defaultFileView[selectedFileType as FileType];
if (fileView !== undefined) {
newFileConfig.selectedFileView = fileView;
}
if (secondaryView !== undefined) {
newFileConfig.fileViewConfig = {
...newFileConfig.fileViewConfig,
[usedFileView]: {
...newFileConfig.fileViewConfig?.[usedFileView],
secondaryView
}
};
}
return { ...prev, [id]: newFileConfig };
});
}
);
const selectedFileView: FileView | undefined = useMemo(() => {
if (!selectedFileId) return undefined;
return (
fileViews[selectedFileId]?.selectedFileView || defaultFileView[selectedFileType as FileType]
);
2025-03-06 06:37:22 +08:00
}, [fileViews, selectedFileId, selectedFileType]);
2025-01-31 06:33:06 +08:00
const selectedFileViewConfig: FileViewConfig | undefined = useMemo(() => {
if (!selectedFileId) return undefined;
return fileViews[selectedFileId]?.fileViewConfig;
}, [fileViews, selectedFileId]);
const selectedFileViewSecondary: FileViewSecondary | null = useMemo(() => {
if (!selectedFileId || !selectedFileViewConfig || !selectedFileView) return null;
return selectedFileViewConfig?.[selectedFileView]?.secondaryView ?? null;
}, [selectedFileViewConfig, selectedFileId, selectedFileView]);
2025-02-04 02:24:32 +08:00
const closeSecondaryView = useMemoizedFn(() => {
if (!selectedFileId || !selectedFileViewConfig || !selectedFileView) return;
2025-03-08 07:02:56 +08:00
setFileViews((prev) => {
return create(prev, (draft) => {
2025-02-04 02:24:32 +08:00
if (!draft[selectedFileId]?.fileViewConfig?.[selectedFileView]) return;
draft[selectedFileId].fileViewConfig[selectedFileView].secondaryView = null;
2025-03-08 07:02:56 +08:00
});
});
2025-02-04 02:24:32 +08:00
});
2025-01-31 06:33:06 +08:00
return {
selectedFileView,
selectedFileViewSecondary,
2025-02-04 02:24:32 +08:00
onSetFileView,
closeSecondaryView
2025-01-31 06:33:06 +08:00
};
};
const defaultFileView: Record<FileType, FileView> = {
metric: 'chart',
2025-02-08 13:24:18 +08:00
dashboard: 'dashboard',
reasoning: 'reasoning'
// collection: 'results',
// value: 'results',
// term: 'results',
// dataset: 'results',
2025-01-31 06:33:06 +08:00
};