|
| 1 | +// Files the bot shared with `share_file` (spec 10, 15.3): one card each, |
| 2 | +// with the file's tile or picture, its name, kind and size, and buttons to |
| 3 | +// preview it, open it and save a copy anywhere. |
| 4 | + |
| 5 | +import { Download, ExternalLink } from "lucide-react"; |
| 6 | +import { useContext } from "react"; |
| 7 | +import { useT } from "../../i18n"; |
| 8 | +import { fileSize } from "../../lib/format"; |
| 9 | +import type { BotFile, ChatItem } from "../../lib/protocol.gen"; |
| 10 | +import { useHost } from "../../store/context"; |
| 11 | +import { Button } from "../../ui/Button"; |
| 12 | +import { useArrival } from "../../ui/motion"; |
| 13 | +import { attempt } from "../../ui/toast"; |
| 14 | +import { fileKind, kindIcon } from "../files/kinds"; |
| 15 | +import { ShowFile } from "../files/showFile"; |
| 16 | +import { useFileImage } from "./images"; |
| 17 | +import { sharedFiles } from "./shared"; |
| 18 | + |
| 19 | +/** "PDF" from report.pdf; nothing for a name without one. */ |
| 20 | +function extension(name: string): string { |
| 21 | + const dot = name.lastIndexOf("."); |
| 22 | + return dot > 0 ? name.slice(dot + 1).toUpperCase() : ""; |
| 23 | +} |
| 24 | + |
| 25 | +function Picture({ botId, file }: { botId: string; file: BotFile }) { |
| 26 | + const source = useFileImage(botId, file); |
| 27 | + if (source.kind !== "ready") { |
| 28 | + return null; |
| 29 | + } |
| 30 | + return ( |
| 31 | + <img |
| 32 | + src={source.url} |
| 33 | + alt={file.name} |
| 34 | + className="block max-h-56 w-full bg-sunken object-contain" |
| 35 | + /> |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +function FileCard({ botId, file }: { botId: string; file: BotFile }) { |
| 40 | + const t = useT().files; |
| 41 | + const host = useHost(); |
| 42 | + const showFile = useContext(ShowFile); |
| 43 | + const kind = fileKind(file.mediaType, file.name); |
| 44 | + const Icon = kindIcon(kind); |
| 45 | + const what = [extension(file.name), fileSize(file.size)].filter(Boolean).join(" · "); |
| 46 | + const about = ( |
| 47 | + <> |
| 48 | + <span aria-hidden className="file-tile" data-kind={kind}> |
| 49 | + <Icon size={18} /> |
| 50 | + </span> |
| 51 | + <span className="min-w-0 flex-1"> |
| 52 | + <span className="block truncate font-medium text-sm" title={file.path}> |
| 53 | + {file.name} |
| 54 | + </span> |
| 55 | + <span className="block truncate text-muted text-xs">{what}</span> |
| 56 | + </span> |
| 57 | + </> |
| 58 | + ); |
| 59 | + return ( |
| 60 | + <div className="@container flex w-96 max-w-full flex-col overflow-hidden rounded-xl border border-line bg-panel shadow-sm"> |
| 61 | + <Picture botId={botId} file={file} /> |
| 62 | + <div className="flex items-center gap-1 p-1.5"> |
| 63 | + {/* Where there is a files panel, the file itself opens its preview. */} |
| 64 | + {showFile ? ( |
| 65 | + <button |
| 66 | + type="button" |
| 67 | + aria-label={`${t.previewOne}: ${file.name}`} |
| 68 | + title={t.previewOne} |
| 69 | + onClick={() => showFile(file.path, file)} |
| 70 | + className="flex min-w-0 flex-1 items-center gap-2.5 rounded-lg p-1 text-left transition-colors hover:bg-sunken" |
| 71 | + > |
| 72 | + {about} |
| 73 | + </button> |
| 74 | + ) : ( |
| 75 | + <div className="flex min-w-0 flex-1 items-center gap-2.5 p-1">{about}</div> |
| 76 | + )} |
| 77 | + <Button |
| 78 | + variant="ghost" |
| 79 | + size="sm" |
| 80 | + icon={ExternalLink} |
| 81 | + label={t.open} |
| 82 | + onClick={() => attempt(t.failed.open, () => host.openFile(file.path))} |
| 83 | + /> |
| 84 | + {/* A narrow card keeps the name and drops the words on the button. */} |
| 85 | + <Button |
| 86 | + size="sm" |
| 87 | + icon={Download} |
| 88 | + aria-label={t.save} |
| 89 | + title={t.saveOne(file.name)} |
| 90 | + onClick={() => attempt(t.failed.save, () => host.saveFileAs(file.path))} |
| 91 | + > |
| 92 | + <span className="hidden @[22rem]:inline">{t.save}</span> |
| 93 | + </Button> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +export function SharedFiles({ item, botId, bot }: { item: ChatItem; botId: string; bot: string }) { |
| 100 | + const t = useT().files; |
| 101 | + const arrival = useArrival(item.createdAt); |
| 102 | + if (item.body.kind !== "tool") { |
| 103 | + return null; |
| 104 | + } |
| 105 | + const files = sharedFiles(item.body); |
| 106 | + if (files.length === 0) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + return ( |
| 110 | + <ul aria-label={t.shared(bot)} className={`flex flex-wrap gap-2 ${arrival}`}> |
| 111 | + {files.map((file) => ( |
| 112 | + <li key={file.path} className="max-w-full"> |
| 113 | + <FileCard botId={botId} file={file} /> |
| 114 | + </li> |
| 115 | + ))} |
| 116 | + </ul> |
| 117 | + ); |
| 118 | +} |
0 commit comments