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

89 lines
3.0 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-01-31 06:33:06 +08:00
import { useChatLayoutContextSelector } from '../../ChatLayoutContext';
2025-03-27 00:40:26 +08:00
import { FileContainerHeaderVersionHistory } from './FileContainerHeaderVersionHistory';
2025-04-09 07:15:47 +08:00
import { SelectedFileButtonsRecord, SelectedFileSegmentRecord } from './config';
import { FileType } from '@/api/asset_interfaces/chat';
2025-04-21 22:27:09 +08:00
import { useRouter } from 'next/navigation';
2025-04-20 13:22:24 +08:00
import { useMount } from '@/hooks';
import { BusterRoutes, createBusterRoute } from '@/routes';
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);
const selectedFileId = useChatLayoutContextSelector((x) => x.selectedFile?.id);
2025-04-09 07:15:47 +08:00
const isVersionHistoryMode = useChatLayoutContextSelector((x) => x.isVersionHistoryMode);
if (isVersionHistoryMode) return <FileContainerHeaderVersionHistory />;
return (
<FileContainerHeaderStandard
selectedFileId={selectedFileId}
selectedFileType={selectedFileType}
/>
);
});
FileContainerHeader.displayName = 'FileContainerHeader';
const FileContainerHeaderStandard: React.FC<{
selectedFileId: string | undefined;
selectedFileType: FileType | undefined;
}> = ({ selectedFileId, selectedFileType }) => {
2025-04-20 13:22:24 +08:00
const router = useRouter();
2025-04-09 07:15:47 +08:00
const chatId = useChatLayoutContextSelector((x) => x.chatId);
const selectedFileView = useChatLayoutContextSelector((x) => x.selectedFileView);
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);
2025-04-16 01:02:08 +08:00
const showCollapseButton = selectedLayout === 'both' || selectedLayout === 'chat-hidden';
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-04-20 13:22:24 +08:00
useMount(() => {
if (chatId) {
router.prefetch(
createBusterRoute({
route: BusterRoutes.APP_CHAT_ID,
chatId
})
);
}
});
2025-01-26 02:20:19 +08:00
return (
2025-03-05 04:52:33 +08:00
<>
2025-04-09 08:39:27 +08:00
<div className="flex min-w-0 shrink items-center gap-1.5 overflow-hidden">
2025-02-04 01:31:15 +08:00
<CollapseFileButton
showCollapseButton={showCollapseButton}
onCollapseFileClick={onCollapseFileClick}
/>
2025-04-09 00:58:20 +08:00
{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-04-09 12:15:35 +08:00
<div className="flex flex-1 items-center justify-end">
2025-04-09 08:39:27 +08:00
<SelectedFileButtons selectedFileView={selectedFileView} selectedFileId={selectedFileId} />
</div>
2025-03-05 04:52:33 +08:00
</>
2025-01-26 02:20:19 +08:00
);
2025-01-31 06:33:06 +08:00
};