-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy paththumbnail.tsx
More file actions
74 lines (67 loc) · 2.16 KB
/
Copy paththumbnail.tsx
File metadata and controls
74 lines (67 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use client";
import { useState, useEffect } from "react";
import { getRawUrl } from "@/lib/github-image";
import { useRepo } from "@/contexts/repo-context";
import { useConfig } from "@/contexts/config-context";
import { cn } from "@/lib/utils";
import { Ban, ImageOff, Loader } from "lucide-react";
export function Thumbnail({
name,
path,
className
}: {
name: string,
path: string | null;
className?: string;
}) {
const [rawUrl, setRawUrl] = useState<string | null>(null);
const [error, setError] = useState(null);
const { owner, repo, isPrivate } = useRepo();
const { config } = useConfig();
const branch = config?.branch!;
useEffect(() => {
const fetchRawUrl = async () => {
if (path) {
setError(null);
if (!rawUrl) setRawUrl(null);
try {
const url = await getRawUrl(owner, repo, branch, name, path, isPrivate);
setRawUrl(url);
} catch (error: any) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.warn(errorMessage);
setError(error.message);
}
}
};
fetchRawUrl();
}, [path, owner, repo, branch, isPrivate, name, rawUrl]);
return (
<div
className={cn(
"bg-muted w-full aspect-square overflow-hidden relative",
className
)}
>
{path
? rawUrl
? <img
src={rawUrl}
alt={path.split("/").pop() || "thumbnail"}
loading="lazy"
className="absolute inset-0 w-full h-full object-cover"
/>
: error
? <div className="flex justify-center items-center absolute inset-0 text-muted-foreground" title={error}>
<Ban className="h-4 w-4"/>
</div>
: <div className="flex justify-center items-center absolute inset-0 text-muted-foreground" title="Loading...">
<Loader className="h-4 w-4 animate-spin"/>
</div>
: <div className="flex justify-center items-center absolute inset-0 text-muted-foreground" title="No image">
<ImageOff className="h-4 w-4"/>
</div>
}
</div>
);
};