From 66e37d80d62d12cb5390ae51022c5a54c3a499a5 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 17 Dec 2024 16:07:11 +0700 Subject: [PATCH 1/3] fix: attachment preview issue --- web/hooks/usePath.ts | 26 +++++++--- .../ThreadCenterPanel/ChatInput/index.tsx | 2 +- .../TextMessage/DocMessage.tsx | 47 +++++++++---------- .../TextMessage/ImageMessage.tsx | 27 +---------- .../TextMessage/RelativeImage.tsx | 9 ++-- .../ThreadCenterPanel/TextMessage/index.tsx | 4 +- web/utils/file.ts | 9 ++++ 7 files changed, 61 insertions(+), 63 deletions(-) diff --git a/web/hooks/usePath.ts b/web/hooks/usePath.ts index afdafe11ff..315072000a 100644 --- a/web/hooks/usePath.ts +++ b/web/hooks/usePath.ts @@ -1,6 +1,8 @@ -import { openFileExplorer, joinPath, baseName } from '@janhq/core' +import { openFileExplorer, joinPath, baseName, fs } from '@janhq/core' import { useAtomValue } from 'jotai' +import { getFileInfo } from '@/utils/file' + import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom' import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom' import { selectedModelAtom } from '@/helpers/atoms/Model.atom' @@ -47,13 +49,23 @@ export const usePath = () => { const onViewFile = async (id: string) => { if (!activeThread) return - let filePath = undefined - id = await baseName(id) - filePath = await joinPath(['threads', `${activeThread.id}/files`, `${id}`]) - if (!filePath) return - const fullPath = await joinPath([janDataFolderPath, filePath]) - openFileExplorer(fullPath) + + // New ID System + if (!id.startsWith('file-')) { + const threadFilePath = await joinPath([ + janDataFolderPath, + 'threads', + `${activeThread.id}/files`, + id, + ]) + openFileExplorer(threadFilePath) + } else { + id = id.split('.')[0] + const fileName = (await getFileInfo(id)).filename + const filesPath = await joinPath([janDataFolderPath, 'files', fileName]) + openFileExplorer(filesPath) + } } const onViewFileContainer = async () => { diff --git a/web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx b/web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx index b3246a26b7..4f00bbc595 100644 --- a/web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx +++ b/web/screens/Thread/ThreadCenterPanel/ChatInput/index.tsx @@ -126,7 +126,7 @@ const ChatInput = () => { const renderPreview = (fileUpload: any) => { if (fileUpload) { if (fileUpload.type === 'image') { - return + return } else { return } diff --git a/web/screens/Thread/ThreadCenterPanel/TextMessage/DocMessage.tsx b/web/screens/Thread/ThreadCenterPanel/TextMessage/DocMessage.tsx index 69d61d0d58..0051f832e3 100644 --- a/web/screens/Thread/ThreadCenterPanel/TextMessage/DocMessage.tsx +++ b/web/screens/Thread/ThreadCenterPanel/TextMessage/DocMessage.tsx @@ -1,44 +1,39 @@ -import { memo } from 'react' - -import { Tooltip } from '@janhq/joi' - -import { FolderOpenIcon } from 'lucide-react' +import { memo, useEffect, useState } from 'react' import { usePath } from '@/hooks/usePath' -import { toGibibytes } from '@/utils/converter' -import { openFileTitle } from '@/utils/titleUtils' +import { getFileInfo } from '@/utils/file' import Icon from '../FileUploadPreview/Icon' -const DocMessage = ({ id, name }: { id: string; name?: string }) => { - const { onViewFile, onViewFileContainer } = usePath() +const DocMessage = ({ id }: { id: string }) => { + const { onViewFile } = usePath() + const [fileInfo, setFileInfo] = useState< + { filename: string; id: string } | undefined + >() + useEffect(() => { + if (!fileInfo) { + getFileInfo(id).then((data) => { + setFileInfo(data) + }) + } + }, [fileInfo, id]) return (
onViewFile(`${id}.pdf`)} /> - - -
- } - content={{openFileTitle()}} - /> +
-
- {name?.replaceAll(/[-._]/g, ' ')} +
+ {fileInfo?.filename}
- {/*

- {toGibibytes(Number(size))} -

*/} +

+ {fileInfo?.id ?? id} +

) diff --git a/web/screens/Thread/ThreadCenterPanel/TextMessage/ImageMessage.tsx b/web/screens/Thread/ThreadCenterPanel/TextMessage/ImageMessage.tsx index e83d35fbbb..14041721bb 100644 --- a/web/screens/Thread/ThreadCenterPanel/TextMessage/ImageMessage.tsx +++ b/web/screens/Thread/ThreadCenterPanel/TextMessage/ImageMessage.tsx @@ -1,34 +1,11 @@ import { memo } from 'react' -import { Tooltip } from '@janhq/joi' - -import { FolderOpenIcon } from 'lucide-react' - -import { usePath } from '@/hooks/usePath' - -import { openFileTitle } from '@/utils/titleUtils' - import { RelativeImage } from '../TextMessage/RelativeImage' const ImageMessage = ({ image }: { image: string }) => { - const { onViewFile, onViewFileContainer } = usePath() - return ( -
-
- onViewFile(image)} /> -
- - -
- } - content={{openFileTitle()}} - /> +
+
) } diff --git a/web/screens/Thread/ThreadCenterPanel/TextMessage/RelativeImage.tsx b/web/screens/Thread/ThreadCenterPanel/TextMessage/RelativeImage.tsx index 72d2a9365b..bfd13b0efc 100644 --- a/web/screens/Thread/ThreadCenterPanel/TextMessage/RelativeImage.tsx +++ b/web/screens/Thread/ThreadCenterPanel/TextMessage/RelativeImage.tsx @@ -7,7 +7,7 @@ export const RelativeImage = ({ onClick, }: { src: string - onClick: () => void + onClick?: () => void }) => { const [path, setPath] = useState('') @@ -17,9 +17,12 @@ export const RelativeImage = ({ }) }, []) return ( -