|
| 1 | +import { controlled } from '@under-control/forms'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import { pipe } from 'fp-ts/lib/function'; |
| 4 | +import { UploadIcon } from 'lucide-react'; |
| 5 | + |
| 6 | +import { tapTaskOption } from '@llm/commons'; |
| 7 | +import { useConfig } from '~/config'; |
| 8 | +import { useI18n } from '~/i18n'; |
| 9 | +import { useGoogleDriveFilePicker } from '~/modules/google-drive'; |
| 10 | +import { GoogleDriveSVG } from '~/ui'; |
| 11 | + |
| 12 | +import { selectChatFile } from '../select-chat-file'; |
| 13 | +import { AttachFileButton } from './attach-file-button'; |
| 14 | + |
| 15 | +type Props = { |
| 16 | + disabled?: boolean; |
| 17 | +}; |
| 18 | + |
| 19 | +export const AttachFileDropdown = controlled<File[], Props>(({ disabled, control: { value, setValue } }) => { |
| 20 | + const config = useConfig(); |
| 21 | + const t = useI18n().pack.chat.actions.files; |
| 22 | + |
| 23 | + const [selectGoogleDriveFile, attachGoogleFileStatus] = useGoogleDriveFilePicker(); |
| 24 | + |
| 25 | + const onAttachGoogleDriveFile = async () => { |
| 26 | + const files = await selectGoogleDriveFile(); |
| 27 | + |
| 28 | + setValue({ |
| 29 | + value: [...(value ?? []), ...files ?? []], |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + const onAttachLocalFile = pipe( |
| 34 | + selectChatFile, |
| 35 | + tapTaskOption((file: File) => { |
| 36 | + setValue({ |
| 37 | + value: [...(value ?? []), file], |
| 38 | + }); |
| 39 | + }), |
| 40 | + ); |
| 41 | + |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <AttachFileButton disabled={disabled} /> |
| 45 | + |
| 46 | + <div className="uk-drop uk-dropdown" uk-dropdown="mode: click"> |
| 47 | + <ul className="uk-dropdown-nav uk-nav"> |
| 48 | + <li> |
| 49 | + <a |
| 50 | + className="justify-between uk-drop-close" |
| 51 | + type="button" |
| 52 | + href="#" |
| 53 | + onClick={(e) => { |
| 54 | + e.preventDefault(); |
| 55 | + void onAttachLocalFile(); |
| 56 | + }} |
| 57 | + > |
| 58 | + <span className="flex items-center gap-2"> |
| 59 | + <UploadIcon size={16} /> |
| 60 | + {t.attachLocalFile} |
| 61 | + </span> |
| 62 | + </a> |
| 63 | + </li> |
| 64 | + |
| 65 | + {config.googleDrive && ( |
| 66 | + <li> |
| 67 | + <a |
| 68 | + className={clsx( |
| 69 | + 'justify-between uk-drop-close', |
| 70 | + attachGoogleFileStatus.isLoading && 'opacity-50 cursor-not-allowed pointer-events-none', |
| 71 | + )} |
| 72 | + type="button" |
| 73 | + href="#" |
| 74 | + onClick={(e) => { |
| 75 | + e.preventDefault(); |
| 76 | + void onAttachGoogleDriveFile(); |
| 77 | + }} |
| 78 | + > |
| 79 | + <span className="flex items-center gap-2"> |
| 80 | + <GoogleDriveSVG width={16} height={16} /> |
| 81 | + {t.attachGoogleDriveFile} |
| 82 | + </span> |
| 83 | + </a> |
| 84 | + </li> |
| 85 | + )} |
| 86 | + </ul> |
| 87 | + </div> |
| 88 | + </> |
| 89 | + ); |
| 90 | +}); |
0 commit comments