-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighlightGroup.tsx
210 lines (176 loc) · 5.99 KB
/
HighlightGroup.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { useMemo } from 'react'
import { useTheme } from 'styled-components'
import {
appendQueryParams,
camelCase,
deleteQueryParams,
DrawerType,
getFavicon,
Highlight,
Highlights,
mog,
useHighlightStore,
useLayoutStore
} from '@mexit/core'
import {
CardFooter,
Container,
DefaultMIcons,
DrawerHeaderDesc,
FlexBetween,
FooterFlexButton,
GenericFlex,
getMIcon,
Group,
HighlightGroupWrapper,
IconDisplay,
MexIcon,
PrimaryText,
SingleHighlightWrapper,
SnippetContentPreview,
StickyHeader,
VerticalSeperator
} from '@mexit/shared'
import { useHighlights } from '../../Hooks/useHighlights'
import { useLinks } from '../../Hooks/useLinks'
import { NodeCardHeader } from './NodeCard'
import { DomainWithHighlight } from './styled'
const HIGHLIGHT_TEXT_MAX_LENGTH = 300
const LinkedNotes: React.FC<{ highlight: Highlight }> = ({ highlight }) => {
const openDrawer = useLayoutStore((store) => store.setDrawer)
const highlightBlockMap = useHighlightStore((store) => store.highlightBlockMap)
const { getEditableMap } = useHighlights()
const { getILinkFromNodeid } = useLinks()
const linkedNotes = useMemo(() => {
const editableMap = getEditableMap(highlight.entityId)
return Object.keys(editableMap).map((nodeId) => {
const node = getILinkFromNodeid(nodeId, true)
return node
})
}, [highlight?.entityId, highlightBlockMap])
const handleOnClick = (e) => {
e.stopPropagation()
// * Open Quick Action Drawer
openDrawer({ type: DrawerType.LINKED_NOTES, data: highlight })
}
if (!linkedNotes?.length) return null
return (
<>
<FooterFlexButton onClick={handleOnClick}>
<IconDisplay icon={getMIcon('ICON', 'ri:eye-line')} /> Linked Notes ({linkedNotes?.length})
</FooterFlexButton>
<VerticalSeperator />
</>
)
}
const AddToNote: React.FC<{ highlight: Highlight }> = ({ highlight }) => {
const theme = useTheme()
const openDrawer = useLayoutStore((store) => store.setDrawer)
const handleOnClick = (e) => {
e.stopPropagation()
// Open Quick Action Drawer
openDrawer({
type: DrawerType.ADD_TO_NOTE,
data: highlight
})
}
return (
<FooterFlexButton onClick={handleOnClick}>
<IconDisplay color={theme.tokens.colors.primary.default} icon={DefaultMIcons.ADD} />{' '}
<PrimaryText>Add To Note</PrimaryText>
</FooterFlexButton>
)
}
export const SingleHighlightWithToggle = ({ highlight }: { highlight: Highlight }) => {
const [open, setOpen] = React.useState(false)
const highlightText = highlight.properties.saveableRange.text
const theme = useTheme()
const willCollapse = highlightText?.length > HIGHLIGHT_TEXT_MAX_LENGTH
const strippedText = highlightText?.substring(0, HIGHLIGHT_TEXT_MAX_LENGTH) + (willCollapse ? '...' : '')
const toShowText = willCollapse ? (open ? highlightText : strippedText) : highlightText
const handleOpenHighlight = (e) => {
e.stopPropagation()
const onSamePage = highlight.properties?.sourceUrl == deleteQueryParams(window.location.href)
if (onSamePage) {
const element = document.querySelector(`[data-highlight-id="${highlight.entityId}"]`)
if (element) {
element?.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
} else {
if (highlight.properties?.sourceUrl == null) return
window.open(appendQueryParams(highlight.properties.sourceUrl, { scrollToCapture: highlight.entityId }))
}
}
const title = camelCase(toShowText.slice(0, 35))
return (
<SingleHighlightWrapper onClick={handleOpenHighlight}>
<Container>
<NodeCardHeader>
<GenericFlex>
<MexIcon color={theme.tokens.colors.primary.default} icon={DefaultMIcons.HIGHLIGHT.value} />
<PrimaryText>{title}</PrimaryText>
</GenericFlex>
</NodeCardHeader>
<SnippetContentPreview>{toShowText}</SnippetContentPreview>
</Container>
<CardFooter>
<LinkedNotes highlight={highlight} />
<AddToNote highlight={highlight} />
</CardFooter>
</SingleHighlightWrapper>
)
}
export const HighlightGroups = ({ highlights, all }: { highlights: Highlights; all?: boolean }) => {
// group by sourceUrl
const groupedHighlights = useMemo(() => {
const groupedHighlights = {} as Record<string, Highlight[]>
highlights.forEach((highlight) => {
if (!highlight) return
try {
const sourceUrl = new URL(highlight.properties?.sourceUrl)?.origin
if (!groupedHighlights[sourceUrl]) {
groupedHighlights[sourceUrl] = []
}
groupedHighlights[sourceUrl].push(highlight)
} catch (err) {
mog('Unable to group highlights', { highlight })
console.error('Unable to group highlights', err)
}
})
return groupedHighlights
}, [highlights])
const highlightWithDomains = Object.entries(groupedHighlights)
return (
<>
{highlightWithDomains.map(([origin, highlightList]) => {
if (highlightList.length === 0) return null
const faviconURL = getFavicon(origin)
return (
<DomainWithHighlight>
{highlightWithDomains.length > 1 && (
<StickyHeader>
<FlexBetween>
<Group
onClick={(e) => {
e.stopPropagation()
window.open(highlightList.at(0).properties.sourceUrl, '_blank')
}}
>
<img src={faviconURL} />
<DrawerHeaderDesc>{origin}</DrawerHeaderDesc>
</Group>
<DrawerHeaderDesc fade>{highlightList.length} </DrawerHeaderDesc>
</FlexBetween>
</StickyHeader>
)}
<HighlightGroupWrapper>
{highlightList.map((highlight) => {
return <SingleHighlightWithToggle highlight={highlight} />
})}
</HighlightGroupWrapper>
</DomainWithHighlight>
)
})}
</>
)
}