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

85 lines
3.2 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-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-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-02-12 08:35:10 +08:00
const renderViewLayoutKey = useChatLayoutContextSelector((state) => state.renderViewLayoutKey);
2025-01-26 02:20:19 +08:00
2025-02-13 02:01:54 +08:00
const showCollapseButton = renderViewLayoutKey !== 'file';
2025-01-26 02:20:19 +08:00
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-22 05:01:29 +08:00
const { data: hasAccess } = useAssetCheck(
{ assetId: selectedFileId, fileType: selectedFileType },
(x) => x.has_access
);
2025-03-18 12:40:09 +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-03-18 12:40:09 +08:00
)}
2025-01-31 06:33:06 +08:00
</div>
2025-03-18 12:40:09 +08:00
{hasAccess && <SelectedFileButtons selectedFileView={selectedFileView} />}
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
};