|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { type ChangeEvent, useRef, useTransition } from "react"; |
| 4 | +import { FilePdfIcon, UploadSimpleIcon } from "@phosphor-icons/react"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | +import type { Application } from "@/lib/applications"; |
| 7 | +import { RESUME_ACCEPT, checkResumeFile, formatBytes, resumeUrl } from "@/lib/resumes"; |
| 8 | +import { removeResume, uploadResume } from "@/app/(application-record)/actions"; |
| 9 | +import { toast } from "@/components/ui/toast"; |
| 10 | +import { Button, buttonVariants } from "@/components/ui/button"; |
| 11 | +import { Spinner } from "@/components/ui/spinner"; |
| 12 | +import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Owns the hidden file input and the upload/remove transitions. Render |
| 16 | + * `input` somewhere in the tree, then call `pick()` from any button or menu item. |
| 17 | + */ |
| 18 | +export function useResumeUpload(application: Application) { |
| 19 | + const inputRef = useRef<HTMLInputElement>(null); |
| 20 | + const [pending, startTransition] = useTransition(); |
| 21 | + const attached = application.resume !== null; |
| 22 | + |
| 23 | + const pick = () => inputRef.current?.click(); |
| 24 | + |
| 25 | + const onChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 26 | + const file = event.target.files?.[0]; |
| 27 | + event.target.value = ""; |
| 28 | + if (!file) return; |
| 29 | + |
| 30 | + const problem = checkResumeFile(file); |
| 31 | + if (problem) { |
| 32 | + toast.add({ type: "error", title: "Resume not uploaded", description: problem }); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + startTransition(async () => { |
| 37 | + const body = new FormData(); |
| 38 | + body.append("file", file); |
| 39 | + const result = await uploadResume(application.id, body); |
| 40 | + if (!result.ok) { |
| 41 | + toast.add({ type: "error", title: "Resume not uploaded", description: result.error }); |
| 42 | + return; |
| 43 | + } |
| 44 | + toast.add({ |
| 45 | + type: "success", |
| 46 | + title: attached ? "Resume replaced" : "Resume attached", |
| 47 | + description: file.name, |
| 48 | + }); |
| 49 | + }); |
| 50 | + }; |
| 51 | + |
| 52 | + const remove = () => { |
| 53 | + startTransition(async () => { |
| 54 | + const result = await removeResume(application.id); |
| 55 | + if (!result.ok) { |
| 56 | + toast.add({ type: "error", title: "Resume not removed", description: result.error }); |
| 57 | + return; |
| 58 | + } |
| 59 | + toast.add({ type: "success", title: "Resume removed" }); |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + const input = ( |
| 64 | + <input |
| 65 | + ref={inputRef} |
| 66 | + type="file" |
| 67 | + accept={RESUME_ACCEPT} |
| 68 | + className="hidden" |
| 69 | + tabIndex={-1} |
| 70 | + aria-hidden="true" |
| 71 | + onChange={onChange} |
| 72 | + /> |
| 73 | + ); |
| 74 | + |
| 75 | + return { attached, pending, pick, remove, input }; |
| 76 | +} |
| 77 | + |
| 78 | +/** Card view: a chip that opens the PDF, or a quiet "Add resume" button. */ |
| 79 | +export function ResumeChip({ application }: { application: Application }) { |
| 80 | + const { pending, pick, input } = useResumeUpload(application); |
| 81 | + const resume = application.resume; |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + {input} |
| 86 | + {resume ? ( |
| 87 | + <a |
| 88 | + href={resumeUrl(application.id)} |
| 89 | + target="_blank" |
| 90 | + rel="noreferrer" |
| 91 | + title={`${resume.file_name} (${formatBytes(resume.size)})`} |
| 92 | + aria-label={`Open resume ${resume.file_name}`} |
| 93 | + className={cn( |
| 94 | + "inline-flex h-6 max-w-full items-center gap-1.5 rounded-2xl bg-brand/15 pr-2.5 pl-2 text-xs font-medium text-brand-ink transition-colors outline-none hover:bg-brand/25 focus-visible:ring-3 focus-visible:ring-ring/30", |
| 95 | + pending && "opacity-60", |
| 96 | + )} |
| 97 | + > |
| 98 | + {pending ? ( |
| 99 | + <Spinner className="size-3.5" /> |
| 100 | + ) : ( |
| 101 | + <FilePdfIcon weight="fill" className="size-3.5 shrink-0" aria-hidden="true" /> |
| 102 | + )} |
| 103 | + <span className="truncate">{resume.file_name}</span> |
| 104 | + </a> |
| 105 | + ) : ( |
| 106 | + <Button |
| 107 | + variant="ghost" |
| 108 | + size="xs" |
| 109 | + className="-ml-2 text-muted-foreground hover:text-foreground" |
| 110 | + onClick={pick} |
| 111 | + disabled={pending} |
| 112 | + > |
| 113 | + {pending ? ( |
| 114 | + <Spinner data-icon="inline-start" /> |
| 115 | + ) : ( |
| 116 | + <UploadSimpleIcon data-icon="inline-start" weight="bold" /> |
| 117 | + )} |
| 118 | + Add resume |
| 119 | + </Button> |
| 120 | + )} |
| 121 | + </> |
| 122 | + ); |
| 123 | +} |
| 124 | + |
| 125 | +/** Table view: one icon that either opens the PDF or starts an upload. */ |
| 126 | +export function ResumeCell({ application }: { application: Application }) { |
| 127 | + const { pending, pick, input } = useResumeUpload(application); |
| 128 | + const resume = application.resume; |
| 129 | + |
| 130 | + if (pending) { |
| 131 | + return ( |
| 132 | + <span className="inline-flex size-7 items-center justify-center"> |
| 133 | + <Spinner className="text-muted-foreground" /> |
| 134 | + </span> |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + return ( |
| 139 | + <> |
| 140 | + {input} |
| 141 | + {resume ? ( |
| 142 | + <Tooltip> |
| 143 | + <TooltipTrigger |
| 144 | + render={ |
| 145 | + <a |
| 146 | + href={resumeUrl(application.id)} |
| 147 | + target="_blank" |
| 148 | + rel="noreferrer" |
| 149 | + aria-label={`Open resume ${resume.file_name}`} |
| 150 | + /> |
| 151 | + } |
| 152 | + className={cn( |
| 153 | + buttonVariants({ variant: "ghost", size: "icon-sm" }), |
| 154 | + "text-brand-ink hover:text-brand-ink", |
| 155 | + )} |
| 156 | + > |
| 157 | + <FilePdfIcon weight="fill" /> |
| 158 | + </TooltipTrigger> |
| 159 | + <TooltipContent> |
| 160 | + {resume.file_name} |
| 161 | + <span className="opacity-60">{formatBytes(resume.size)}</span> |
| 162 | + </TooltipContent> |
| 163 | + </Tooltip> |
| 164 | + ) : ( |
| 165 | + <Tooltip> |
| 166 | + <TooltipTrigger |
| 167 | + render={<Button variant="ghost" size="icon-sm" aria-label="Upload resume" />} |
| 168 | + className="text-muted-foreground/60 hover:text-foreground" |
| 169 | + onClick={pick} |
| 170 | + > |
| 171 | + <UploadSimpleIcon /> |
| 172 | + </TooltipTrigger> |
| 173 | + <TooltipContent>Upload resume</TooltipContent> |
| 174 | + </Tooltip> |
| 175 | + )} |
| 176 | + </> |
| 177 | + ); |
| 178 | +} |
0 commit comments