|
| 1 | +import type { SelectDownload } from '@main/stores/queue-database.helpers' |
| 2 | +import { Button } from '@renderer/components/ui/button' |
| 3 | +import { ScrollArea } from '@renderer/components/ui/scroll-area' |
| 4 | +import { Sheet, SheetContent, SheetTitle, SheetTrigger } from '@renderer/components/ui/sheet' |
| 5 | +import { trpc } from '@renderer/lib/trpc-link' |
| 6 | +import { formatDistanceToNow } from 'date-fns' |
| 7 | +import prettyBytes from 'pretty-bytes' |
| 8 | +import { PropsWithChildren, useMemo } from 'react' |
| 9 | +const isYTDomain = /^((www|music)\.)?youtube.com/ |
| 10 | +export default function FileSheet({ |
| 11 | + item: { title, source, url, metaId, filepath, filesize, meta, type, created }, |
| 12 | + children |
| 13 | +}: PropsWithChildren<{ item: SelectDownload }>) { |
| 14 | + const { isYoutube, frameSource } = useMemo(() => { |
| 15 | + const isYoutube = isYTDomain.test(source) |
| 16 | + const frameSource = |
| 17 | + (isYoutube && |
| 18 | + metaId && |
| 19 | + `https://www.youtube.com/embed/${metaId}?rel=0&showinfo=0&controls=1&iv_load_policy=3&playsinline=1&fs=0`) || |
| 20 | + null |
| 21 | + return { isYoutube, frameSource } |
| 22 | + }, [source]) |
| 23 | + const { mutateAsync: openFile } = trpc.internals.openFile.useMutation() |
| 24 | + const createdAt = useMemo( |
| 25 | + () => (created && formatDistanceToNow(new Date(created), { includeSeconds: false })) || null, |
| 26 | + [created] |
| 27 | + ) |
| 28 | + return ( |
| 29 | + <> |
| 30 | + <Sheet modal> |
| 31 | + <SheetTrigger asChild>{children}</SheetTrigger> |
| 32 | + <SheetContent className="flex flex-col gap-6 sm:max-w-2xl h-full"> |
| 33 | + <SheetTitle className="text-lg font-semibold tracking-wider whitespace-pre-wrap mr-8 break-words line-clamp-2 flex-shrink-0"> |
| 34 | + {title} |
| 35 | + </SheetTitle> |
| 36 | + {isYoutube && frameSource && ( |
| 37 | + <div className="-mx-6 flex-shrink-0"> |
| 38 | + <iframe |
| 39 | + className="w-full aspect-video" |
| 40 | + src={frameSource} |
| 41 | + title="YouTube video player" |
| 42 | + allow="encrypted-media;" |
| 43 | + referrerPolicy="strict-origin-when-cross-origin" |
| 44 | + ></iframe> |
| 45 | + </div> |
| 46 | + )} |
| 47 | + <ScrollArea className="flex-auto flex flex-col -m-6 h-full" plain> |
| 48 | + <div className="file-info-row"> |
| 49 | + <span>Filename</span> |
| 50 | + <span>{meta?.filename || '-'}</span> |
| 51 | + </div> |
| 52 | + <div className="file-info-row"> |
| 53 | + <span>Filesize</span> |
| 54 | + <span>{filesize && prettyBytes(filesize) || '0B'}</span> |
| 55 | + </div> |
| 56 | + <div className="file-info-row"> |
| 57 | + <span>Type</span> |
| 58 | + <span>{type || '-'}</span> |
| 59 | + </div> |
| 60 | + <div className="file-info-row"> |
| 61 | + <span>Source</span> |
| 62 | + <span>{source || '-'}</span> |
| 63 | + </div> |
| 64 | + <div className="file-info-row"> |
| 65 | + <span>Uploader</span> |
| 66 | + <div className="flex items-center gap-2"> |
| 67 | + {meta?.uploader && meta?.uploader !== meta?.channel && ( |
| 68 | + <> |
| 69 | + <span>{meta?.uploader || '-'}</span> |
| 70 | + <span className='bg-muted-foreground size-1 rounded-full'></span> |
| 71 | + </> |
| 72 | + )} |
| 73 | + <span>{meta?.channel || '-'}</span> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + <div className="file-info-row"> |
| 77 | + <span></span> |
| 78 | + <span>downloaded {createdAt || '?'} ago</span> |
| 79 | + </div> |
| 80 | + </ScrollArea> |
| 81 | + <div className="flex items-center gap-3 -mx-6 px-6 pt-6 border-t border-t-muted justify-end flex-shrink-0"> |
| 82 | + <Button asChild variant={'outline'}> |
| 83 | + <a href={url} target="_blank"> |
| 84 | + Open in Browser |
| 85 | + </a> |
| 86 | + </Button> |
| 87 | + <Button variant={'outline'} onClick={() => openFile({ path: filepath })}> |
| 88 | + Open File |
| 89 | + </Button> |
| 90 | + </div> |
| 91 | + </SheetContent> |
| 92 | + </Sheet> |
| 93 | + <style jsx> |
| 94 | + {` |
| 95 | + .file-info-row { |
| 96 | + @apply h-10 grid grid-cols-[160px_1fr] gap-6 items-center text-sm flex-shrink-0; |
| 97 | + } |
| 98 | + .file-info-row:nth-child(even) { |
| 99 | + @apply bg-muted/20; |
| 100 | + } |
| 101 | + .file-info-row > span:first-of-type { |
| 102 | + @apply flex justify-end; |
| 103 | + } |
| 104 | + .file-info-row > span:nth-child(2) { |
| 105 | + @apply truncate pr-8; |
| 106 | + } |
| 107 | +
|
| 108 | + `} |
| 109 | + </style> |
| 110 | + </> |
| 111 | + ) |
| 112 | +} |
0 commit comments