buster/web/src/layouts/ChatLayout/FileContainer/FileContainerHeader/FileContainerHeader.tsx

93 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-03-05 04:52:33 +08:00
'use client';
2025-01-26 02:20:19 +08:00
import React from 'react';
import { CollapseFileButton } from './CollapseFileButton';
2025-03-12 07:01:39 +08:00
import type { FileType } from '@/api/asset_interfaces/chat';
2025-01-31 06:33:06 +08:00
import { FileContainerSegmentProps, FileContainerButtonsProps } from './interfaces';
2025-02-08 13:24:18 +08:00
import { DashboardContainerHeaderButtons } from './DashboardContainerHeaderButtons';
2025-01-31 06:33:06 +08:00
import { DashboardContainerHeaderSegment } from './DashboardContainerHeaderSegment';
2025-02-08 13:24:18 +08:00
import { MetricContainerHeaderSegment } from './MetricContainerHeaderSegment';
import { MetricContainerHeaderButtons } from './MetricContainerHeaderButtons';
2025-01-31 06:33:06 +08:00
import { useChatLayoutContextSelector } from '../../ChatLayoutContext';
2025-02-08 13:24:18 +08:00
import { ReasoningContainerHeaderSegment } from './ReasoningContainerHeaderSegment';
2025-03-18 12:40:09 +08:00
import { useAssetCheck } from '@/api/buster_rest/assets/queryRequests';
2025-03-27 00:40:26 +08:00
import { FileContainerHeaderVersionHistory } from './FileContainerHeaderVersionHistory';
2025-01-26 02:20:19 +08:00
export const FileContainerHeader: React.FC = React.memo(() => {
2025-03-18 12:40:09 +08:00
const selectedFileType = useChatLayoutContextSelector((x) => x.selectedFile?.type);
2025-01-31 06:33:06 +08:00
const selectedFileView = useChatLayoutContextSelector((x) => x.selectedFileView);
2025-03-26 11:56:21 +08:00
const isVersionHistoryMode = useChatLayoutContextSelector((x) => x.isVersionHistoryMode);
2025-03-18 12:40:09 +08:00
const selectedFileId = useChatLayoutContextSelector((x) => x.selectedFile?.id);
2025-02-04 01:31:15 +08:00
const onCollapseFileClick = useChatLayoutContextSelector((state) => state.onCollapseFileClick);
2025-03-28 05:05:06 +08:00
const selectedLayout = useChatLayoutContextSelector((x) => x.selectedLayout);
const showCollapseButton = selectedLayout === 'both';
2025-04-02 01:04:19 +08:00
const chatId = useChatLayoutContextSelector((x) => x.chatId);
2025-01-31 06:33:06 +08:00
const SelectedFileSegment = React.useMemo(
2025-02-08 13:24:18 +08:00
() =>
selectedFileType && SelectedFileSegmentRecord[selectedFileType]
? SelectedFileSegmentRecord[selectedFileType]
2025-03-14 09:51:45 +08:00
: () => <></>,
2025-01-31 06:33:06 +08:00
[selectedFileType]
);
const SelectedFileButtons = React.useMemo(
2025-02-08 13:24:18 +08:00
() =>
selectedFileType && SelectedFileButtonsRecord[selectedFileType]
? SelectedFileButtonsRecord[selectedFileType]
2025-03-12 07:01:39 +08:00
: () => null,
2025-01-31 06:33:06 +08:00
[selectedFileType]
);
2025-03-29 03:16:39 +08:00
const { data: hasAccessQuery } = useAssetCheck(
2025-03-22 05:01:29 +08:00
{ assetId: selectedFileId, fileType: selectedFileType },
(x) => x.has_access
);
2025-03-29 03:16:39 +08:00
const hasAccess = hasAccessQuery ?? true; // we assume access until it is revoked
2025-03-18 12:40:09 +08:00
2025-03-27 00:40:26 +08:00
if (isVersionHistoryMode) return <FileContainerHeaderVersionHistory />;
2025-03-26 11:56:21 +08:00
2025-01-26 02:20:19 +08:00
return (
2025-03-05 04:52:33 +08:00
<>
2025-01-31 06:33:06 +08:00
<div className="flex items-center gap-1.5">
2025-02-04 01:31:15 +08:00
<CollapseFileButton
showCollapseButton={showCollapseButton}
onCollapseFileClick={onCollapseFileClick}
/>
2025-03-18 12:40:09 +08:00
{hasAccess && selectedFileView && (
2025-03-26 06:43:34 +08:00
<SelectedFileSegment
selectedFileView={selectedFileView}
selectedFileId={selectedFileId}
2025-04-02 01:04:19 +08:00
chatId={chatId}
2025-03-26 06:43:34 +08:00
/>
2025-03-18 12:40:09 +08:00
)}
2025-01-31 06:33:06 +08:00
</div>
2025-03-26 11:56:21 +08:00
2025-04-02 01:36:57 +08:00
{hasAccess && (
<SelectedFileButtons selectedFileView={selectedFileView} selectedFileId={selectedFileId} />
)}
2025-03-05 04:52:33 +08:00
</>
2025-01-26 02:20:19 +08:00
);
});
FileContainerHeader.displayName = 'FileContainerHeader';
2025-01-31 06:33:06 +08:00
const SelectedFileButtonsRecord: Record<FileType, React.FC<FileContainerButtonsProps>> = {
metric: MetricContainerHeaderButtons,
dashboard: DashboardContainerHeaderButtons,
2025-02-08 13:24:18 +08:00
reasoning: () => null
// value: ValueContainerHeaderButtons,
// term: TermContainerHeaderButtons,
// dataset: DatasetContainerHeaderButtons,
// collection: CollectionContainerHeaderButtons
2025-01-31 06:33:06 +08:00
};
const SelectedFileSegmentRecord: Record<FileType, React.FC<FileContainerSegmentProps>> = {
metric: MetricContainerHeaderSegment,
dashboard: DashboardContainerHeaderSegment,
2025-02-08 13:24:18 +08:00
reasoning: ReasoningContainerHeaderSegment
// value: ValueContainerHeaderSegment,
// term: TermContainerHeaderSegment,
// dataset: DatasetContainerHeaderSegment,
// collection: CollectionContainerHeaderSegment
2025-01-31 06:33:06 +08:00
};