-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
249 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
apps/chat/src/modules/chats/conversation/files/file-card.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import clsx from 'clsx'; | ||
import { | ||
FileAxis3DIcon, | ||
FileIcon, | ||
FileSpreadsheetIcon, | ||
FileTextIcon, | ||
ImageIcon, | ||
XIcon, | ||
} from 'lucide-react'; | ||
|
||
import { | ||
isImageFileUrl, | ||
isLegacyExcelMimetype, | ||
isLegacyWordMimetype, | ||
isPDFFileUrl, | ||
isPDFMimeType, | ||
isSpreadsheetFileUrl, | ||
isWordFileUrl, | ||
} from '@llm/commons'; | ||
import { useI18n } from '~/i18n'; | ||
|
||
type Props = { | ||
file: File; | ||
onRemove: () => void; | ||
}; | ||
|
||
export function FileCard({ file, onRemove }: Props) { | ||
const { pack } = useI18n(); | ||
const { type, bgColor, icon: IconComponent } = getFileTypeAndColor(file); | ||
|
||
const isImage = isImageFileUrl(file.name); | ||
const imageUrl = isImage ? URL.createObjectURL(file) : null; | ||
|
||
return ( | ||
<div | ||
className={clsx( | ||
'relative gap-2 p-3 border rounded-lg h-[57px] group', | ||
isImage | ||
? 'p-0 w-[128px]' | ||
: 'bg-gray-50 border-gray-200 w-[200px]', | ||
)} | ||
> | ||
<button | ||
title={pack.buttons.delete} | ||
type="button" | ||
onClick={onRemove} | ||
className="-top-2 -right-2 absolute bg-black p-1 rounded-full" | ||
> | ||
<XIcon className="w-3 h-3 text-white" /> | ||
</button> | ||
|
||
{isImage && imageUrl | ||
? ( | ||
<img | ||
src={imageUrl} | ||
alt={file.name} | ||
className="rounded w-full h-full aspect-square object-contain" | ||
/> | ||
) | ||
: ( | ||
<div className="gap-2 grid grid-cols-[auto,1fr] min-w-0 overflow-hidden"> | ||
<div className={clsx('flex items-center p-1 rounded', bgColor)}> | ||
<IconComponent className="w-5 h-5 text-white" /> | ||
</div> | ||
|
||
<div className="flex flex-col min-w-0"> | ||
<span | ||
className="font-semibold text-gray-700 text-xs truncate" | ||
title={file.name} | ||
> | ||
{file.name} | ||
</span> | ||
|
||
<span className="text-[10px] text-gray-500"> | ||
{type} | ||
</span> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
function getFileTypeAndColor(file: File) { | ||
if (isSpreadsheetFileUrl(file.name) || isLegacyExcelMimetype(file.type)) { | ||
return { | ||
type: 'Excel', | ||
bgColor: 'bg-[#217346]', | ||
icon: FileSpreadsheetIcon, | ||
}; | ||
} | ||
|
||
if (isLegacyWordMimetype(file.type) || isWordFileUrl(file.name)) { | ||
return { | ||
type: 'Word', | ||
bgColor: 'bg-[#2B579A]', | ||
icon: FileTextIcon, | ||
}; | ||
} | ||
|
||
if (isPDFMimeType(file.type) || isPDFFileUrl(file.name)) { | ||
return { | ||
type: 'PDF', | ||
bgColor: 'bg-[#D93F3F]', | ||
icon: FileAxis3DIcon, | ||
}; | ||
} | ||
|
||
if (isImageFileUrl(file.name)) { | ||
return { | ||
type: 'Image', | ||
bgColor: 'bg-purple-500', | ||
icon: ImageIcon, | ||
}; | ||
} | ||
|
||
return { | ||
type: 'TXT', | ||
bgColor: 'bg-gray-400', | ||
icon: FileIcon, | ||
}; | ||
} |
59 changes: 59 additions & 0 deletions
59
apps/chat/src/modules/chats/conversation/files/files-cards-list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { controlled } from '@under-control/forms'; | ||
import clsx from 'clsx'; | ||
import { useMemo } from 'react'; | ||
import { v4 } from 'uuid'; | ||
|
||
import { without } from '@llm/commons'; | ||
|
||
import { FileCard } from './file-card'; | ||
|
||
type Props = { | ||
className?: string; | ||
}; | ||
|
||
export const FilesCardsList = controlled<File[], Props>(({ className, control: { value, setValue } }) => { | ||
const mappedFiles = useMemo(() => { | ||
const fileNames = new Set<string>(); | ||
const duplicateNames = new Set<string>(); | ||
|
||
value.forEach((file) => { | ||
if (fileNames.has(file.name)) { | ||
duplicateNames.add(file.name); | ||
} | ||
|
||
fileNames.add(file.name); | ||
}); | ||
|
||
return value.map(file => ({ | ||
id: duplicateNames.has(file.name) ? v4() : file.name, | ||
file, | ||
})); | ||
}, [value]); | ||
|
||
if (!value?.length) { | ||
return null; | ||
} | ||
|
||
const handleRemove = (file: File) => { | ||
setValue({ | ||
value: without([file])(value), | ||
}); | ||
}; | ||
|
||
return ( | ||
<div | ||
className={clsx( | ||
'flex flex-row gap-5', | ||
className, | ||
)} | ||
> | ||
{mappedFiles.map(({ file, id }) => ( | ||
<FileCard | ||
key={id} | ||
file={file} | ||
onRemove={() => handleRemove(file)} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './file-card'; | ||
export * from './files-cards-list'; | ||
export * from './select-chat-file'; |
3 changes: 3 additions & 0 deletions
3
apps/chat/src/modules/chats/conversation/files/select-chat-file.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { selectFile } from '@llm/commons'; | ||
|
||
export const selectChatFile = selectFile('.pdf,.csv,.doc,.docx,.xls,.xlsx,.png,.jpg,.jpeg,.bmp'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
export * from './chat-attached-app'; | ||
export * from './chat-background'; | ||
export * from './chat-conversation-panel'; | ||
export * from './chat-conversation-with-sidebar'; | ||
export * from './config-panel'; | ||
export * from './content-parse'; | ||
export * from './files'; | ||
export * from './hooks'; | ||
export * from './input-toolbar'; | ||
export * from './messages'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isPDFFileUrl(url: string): boolean { | ||
return url.endsWith('.pdf'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isWordFileUrl(url: string): boolean { | ||
return /\.(?:doc|docx)$/i.test(url); | ||
} |