aboutsummaryrefslogtreecommitdiff
path: root/apps/web/lib/document-icon.tsx
blob: edeaf76b1f9349f27b02f5dde109685b7e4b2349 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { MCPIcon } from "@/components/menu"
import { colors } from "@repo/ui/memory-graph/constants"
import {
	GoogleDocs,
	MicrosoftWord,
	NotionDoc,
	GoogleDrive,
	GoogleSheets,
	GoogleSlides,
	PDF,
	OneDrive,
	MicrosoftOneNote,
	MicrosoftPowerpoint,
	MicrosoftExcel,
} from "@ui/assets/icons"
import { FileText, Globe } from "lucide-react"
import { useState } from "react"

const getFaviconUrl = (url: string): string => {
	try {
		const domain = new URL(url).hostname
		return `https://www.google.com/s2/favicons?domain=${domain}&sz=32`
	} catch {
		return ""
	}
}

const FaviconIcon = ({
	url,
	className,
	iconProps,
}: {
	url: string
	className: string
	iconProps: { className: string; style: { color: string } }
}) => {
	const [hasError, setHasError] = useState(false)
	const faviconUrl = getFaviconUrl(url)

	if (hasError || !faviconUrl) {
		return <Globe {...iconProps} />
	}

	return (
		<img
			src={faviconUrl}
			alt="Website favicon"
			className={className}
			style={{
				width: "2em",
				height: "2em",
				objectFit: "contain",
			}}
			onError={() => setHasError(true)}
		/>
	)
}

export const getDocumentIcon = (
	type: string,
	className: string,
	source?: string,
	url?: string,
) => {
	const iconProps = {
		className,
		style: { color: colors.text.muted },
	}

	if (source === "mcp") {
		return <MCPIcon {...iconProps} />
	}

	if (
		type === "webpage" ||
		type === "url" ||
		(url && (type === "unknown" || !type))
	) {
		if (url) {
			return (
				<FaviconIcon url={url} className={className} iconProps={iconProps} />
			)
		}

		return <Globe {...iconProps} />
	}

	switch (type) {
		case "google_doc":
			return <GoogleDocs {...iconProps} />
		case "google_sheet":
			return <GoogleSheets {...iconProps} />
		case "google_slide":
			return <GoogleSlides {...iconProps} />
		case "google_drive":
			return <GoogleDrive {...iconProps} />
		case "notion":
		case "notion_doc":
			return <NotionDoc {...iconProps} />
		case "word":
		case "microsoft_word":
			return <MicrosoftWord {...iconProps} />
		case "excel":
		case "microsoft_excel":
			return <MicrosoftExcel {...iconProps} />
		case "powerpoint":
		case "microsoft_powerpoint":
			return <MicrosoftPowerpoint {...iconProps} />
		case "onenote":
		case "microsoft_onenote":
			return <MicrosoftOneNote {...iconProps} />
		case "onedrive":
			return <OneDrive {...iconProps} />
		case "pdf":
			return <PDF {...iconProps} />
		default:
			return <FileText {...iconProps} />
	}
}