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';
|
|
|
|
import { useMemoizedFn } from 'ahooks';
|
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]
|
|
|
|
);
|
|
|
|
}, [fileViews, selectedFileId]);
|
|
|
|
|
|
|
|
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;
|
|
|
|
setFileViews(
|
2025-02-08 05:11:05 +08:00
|
|
|
create((draft) => {
|
2025-02-04 02:24:32 +08:00
|
|
|
if (!draft[selectedFileId]?.fileViewConfig?.[selectedFileView]) return;
|
|
|
|
draft[selectedFileId].fileViewConfig[selectedFileView].secondaryView = null;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-03-06 05:45:17 +08:00
|
|
|
console.log({ selectedFileType, selectedFileView, selectedFileId });
|
|
|
|
|
2025-01-31 06:33:06 +08:00
|
|
|
return {
|
|
|
|
selectedFileType,
|
|
|
|
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
|
|
|
};
|