-
Notifications
You must be signed in to change notification settings - Fork 597
Expand file tree
/
Copy pathproject-icon.tsx
More file actions
64 lines (58 loc) · 1.8 KB
/
project-icon.tsx
File metadata and controls
64 lines (58 loc) · 1.8 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
import { useCallback, useEffect, useMemo, useState } from "react"
import { FolderOpen } from "lucide-react"
import { useProjectIcon } from "../../lib/hooks/use-project-icon"
import { cn } from "../../lib/utils"
interface ProjectIconProps {
project:
| {
id?: string | null
name?: string | null
gitRepo?: string | null
iconPath?: string | null
updatedAt?: string | Date | null
}
| null
| undefined
className?: string
}
export function ProjectIcon({ project, className }: ProjectIconProps) {
const { src, hasError } = useProjectIcon(project)
const [imgError, setImgError] = useState(false)
const handleError = useCallback(() => setImgError(true), [])
const fallbackInitial = useMemo(() => {
const label = (project?.gitRepo || project?.name || "").trim()
const initial = label.replace(/^[^a-zA-Z0-9]+/, "").charAt(0)
return initial ? initial.toUpperCase() : "?"
}, [project?.gitRepo, project?.name])
useEffect(() => {
setImgError(false)
}, [src, project?.id, project?.iconPath, project?.updatedAt])
if (!project) {
return (
<FolderOpen
className={cn("text-muted-foreground flex-shrink-0", className)}
/>
)
}
if (hasError || !src || imgError) {
return (
<div
aria-hidden="true"
className={cn(
"rounded-sm bg-input-background border text-muted-foreground flex-shrink-0 flex items-center justify-center font-medium uppercase",
className,
)}
>
<span className="text-[0.65em] leading-none">{fallbackInitial}</span>
</div>
)
}
return (
<img
src={src}
alt={project.gitRepo || project.name || "Project icon"}
className={cn("rounded-sm flex-shrink-0 object-cover", className)}
onError={handleError}
/>
)
}