|
| 1 | +import { useMemo, useState } from "react"; |
1 | 2 | import Image from "next/image";
|
2 | 3 | import Link from "next/link";
|
| 4 | +import { |
| 5 | + Select, |
| 6 | + SelectContent, |
| 7 | + SelectGroup, |
| 8 | + SelectItem, |
| 9 | + SelectTrigger, |
| 10 | + SelectValue, |
| 11 | +} from "@/components/ui/select"; |
| 12 | +import { useTranslation } from "@/lib/i18n/client"; |
3 | 13 |
|
| 14 | +import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils"; |
4 | 15 | import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks";
|
5 | 16 |
|
6 |
| -export function AssetContentSection({ bookmark }: { bookmark: ZBookmark }) { |
| 17 | +// 20 MB |
| 18 | +const BIG_FILE_SIZE = 20 * 1024 * 1024; |
| 19 | + |
| 20 | +function PDFContentSection({ bookmark }: { bookmark: ZBookmark }) { |
7 | 21 | if (bookmark.content.type != BookmarkTypes.ASSET) {
|
8 | 22 | throw new Error("Invalid content type");
|
9 | 23 | }
|
| 24 | + const { t } = useTranslation(); |
10 | 25 |
|
11 |
| - switch (bookmark.content.assetType) { |
12 |
| - case "image": { |
13 |
| - return ( |
14 |
| - <div className="relative h-full min-w-full"> |
15 |
| - <Link |
16 |
| - href={`/api/assets/${bookmark.content.assetId}`} |
17 |
| - target="_blank" |
18 |
| - > |
19 |
| - <Image |
20 |
| - alt="asset" |
21 |
| - fill={true} |
22 |
| - className="object-contain" |
23 |
| - src={`/api/assets/${bookmark.content.assetId}`} |
24 |
| - /> |
25 |
| - </Link> |
26 |
| - </div> |
27 |
| - ); |
| 26 | + const initialSection = useMemo(() => { |
| 27 | + if (bookmark.content.type != BookmarkTypes.ASSET) { |
| 28 | + throw new Error("Invalid content type"); |
28 | 29 | }
|
29 |
| - case "pdf": { |
30 |
| - return ( |
31 |
| - <iframe |
32 |
| - title={bookmark.content.assetId} |
33 |
| - className="h-full w-full" |
34 |
| - src={`/api/assets/${bookmark.content.assetId}`} |
35 |
| - /> |
36 |
| - ); |
| 30 | + |
| 31 | + const screenshot = bookmark.assets.find( |
| 32 | + (item) => item.assetType === "assetScreenshot", |
| 33 | + ); |
| 34 | + const bigSize = |
| 35 | + bookmark.content.size && bookmark.content.size > BIG_FILE_SIZE; |
| 36 | + if (bigSize && screenshot) { |
| 37 | + return "screenshot"; |
37 | 38 | }
|
38 |
| - default: { |
| 39 | + return "pdf"; |
| 40 | + }, [bookmark]); |
| 41 | + const [section, setSection] = useState(initialSection); |
| 42 | + |
| 43 | + const screenshot = bookmark.assets.find( |
| 44 | + (r) => r.assetType === "assetScreenshot", |
| 45 | + )?.id; |
| 46 | + |
| 47 | + const content = |
| 48 | + section === "screenshot" && screenshot ? ( |
| 49 | + <div className="relative h-full min-w-full"> |
| 50 | + <Image |
| 51 | + alt="screenshot" |
| 52 | + src={getAssetUrl(screenshot)} |
| 53 | + fill={true} |
| 54 | + className="object-contain" |
| 55 | + /> |
| 56 | + </div> |
| 57 | + ) : ( |
| 58 | + <iframe |
| 59 | + title={bookmark.content.assetId} |
| 60 | + className="h-full w-full" |
| 61 | + src={getAssetUrl(bookmark.content.assetId)} |
| 62 | + /> |
| 63 | + ); |
| 64 | + |
| 65 | + return ( |
| 66 | + <div className="flex h-full flex-col items-center gap-2"> |
| 67 | + <div className="flex w-full items-center justify-center gap-4"> |
| 68 | + <Select onValueChange={setSection} value={section}> |
| 69 | + <SelectTrigger className="w-fit"> |
| 70 | + <SelectValue /> |
| 71 | + </SelectTrigger> |
| 72 | + <SelectContent> |
| 73 | + <SelectGroup> |
| 74 | + <SelectItem value="screenshot" disabled={!screenshot}> |
| 75 | + {t("common.screenshot")} |
| 76 | + </SelectItem> |
| 77 | + <SelectItem value="pdf">PDF</SelectItem> |
| 78 | + </SelectGroup> |
| 79 | + </SelectContent> |
| 80 | + </Select> |
| 81 | + </div> |
| 82 | + {content} |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +function ImageContentSection({ bookmark }: { bookmark: ZBookmark }) { |
| 88 | + if (bookmark.content.type != BookmarkTypes.ASSET) { |
| 89 | + throw new Error("Invalid content type"); |
| 90 | + } |
| 91 | + return ( |
| 92 | + <div className="relative h-full min-w-full"> |
| 93 | + <Link href={getAssetUrl(bookmark.content.assetId)} target="_blank"> |
| 94 | + <Image |
| 95 | + alt="asset" |
| 96 | + fill={true} |
| 97 | + className="object-contain" |
| 98 | + src={getAssetUrl(bookmark.content.assetId)} |
| 99 | + /> |
| 100 | + </Link> |
| 101 | + </div> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +export function AssetContentSection({ bookmark }: { bookmark: ZBookmark }) { |
| 106 | + if (bookmark.content.type != BookmarkTypes.ASSET) { |
| 107 | + throw new Error("Invalid content type"); |
| 108 | + } |
| 109 | + switch (bookmark.content.assetType) { |
| 110 | + case "image": |
| 111 | + return <ImageContentSection bookmark={bookmark} />; |
| 112 | + case "pdf": |
| 113 | + return <PDFContentSection bookmark={bookmark} />; |
| 114 | + default: |
39 | 115 | return <div>Unsupported asset type</div>;
|
40 |
| - } |
41 | 116 | }
|
42 | 117 | }
|
0 commit comments