-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathpage-icon.tsx
108 lines (98 loc) · 2.56 KB
/
page-icon.tsx
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
import { type Block, type CalloutBlock, type PageBlock } from 'notion-types'
import { getBlockIcon, getBlockTitle } from 'notion-utils'
import * as React from 'react'
import { useNotionContext } from '../context'
import { DefaultPageIcon } from '../icons/default-page-icon'
import { cs, isUrl } from '../utils'
import { LazyImage } from './lazy-image'
const isIconBlock = (value: Block): value is PageBlock | CalloutBlock => {
return (
value.type === 'page' ||
value.type === 'callout' ||
value.type === 'collection_view' ||
value.type === 'collection_view_page'
)
}
export function PageIconImpl({
block,
className,
inline = true,
hideDefaultIcon = false,
defaultIcon
}: {
block: Block
className?: string
inline?: boolean
hideDefaultIcon?: boolean
defaultIcon?: string | null
}) {
const { alignCenter, mapImageUrl, recordMap, darkMode } = useNotionContext()
let isImage = false
let content: any = null
if (isIconBlock(block)) {
const icon = getBlockIcon(block, recordMap)?.trim() || defaultIcon
const title = getBlockTitle(block, recordMap)
if (icon && isUrl(icon)) {
const url = mapImageUrl(icon, block)
isImage = true
content = (
<LazyImage
src={url}
alt={title || 'page icon'}
className={cs(className, 'notion-page-icon')}
/>
)
} else if (icon && icon.startsWith('/icons/')) {
const url =
'https://www.notion.so' +
icon +
'?mode=' +
(darkMode ? 'dark' : 'light')
content = (
<LazyImage
src={url}
alt={title || 'page icon'}
className={cs(className, 'notion-page-icon')}
/>
)
} else if (!icon) {
if (!hideDefaultIcon) {
isImage = true
content = (
<DefaultPageIcon
className={cs(className, 'notion-page-icon')}
alt={title || 'page icon'}
/>
)
}
} else {
isImage = false
content = (
<span
className={cs(className, 'notion-page-icon')}
role='img'
aria-label={icon}
>
{icon}
</span>
)
}
}
if (!content) {
return null
}
return (
<div
className={cs(
inline ? 'notion-page-icon-inline' : 'notion-page-icon-hero',
isImage ? 'notion-page-icon-image' : 'notion-page-icon-span',
alignCenter
? 'notion-page-icon--align-center'
: 'notion-page-icon--align-left'
)}
>
{content}
</div>
)
}
export const PageIcon = React.memo(PageIconImpl)