-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotesInfoBar.tsx
108 lines (92 loc) · 3.06 KB
/
NotesInfoBar.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 React, { useEffect, useRef, useState } from 'react'
import searchLine from '@iconify/icons-ri/search-line'
import { debounce } from 'lodash'
import { useTheme } from 'styled-components'
import { Infobox } from '@workduck-io/mex-components'
import { BASE_TASKS_PATH, isParent, mog } from '@mexit/core'
import {
SidebarListFilterWrapper,
SidebarListFilter,
Input,
SnippetCards,
MexIcon,
NotesInfoBarHelp,
List,
CenteredColumn
} from '@mexit/shared'
import { useLinks } from '../../Hooks/useLinks'
import useRaju from '../../Hooks/useRaju'
import { useRecentsStore } from '../../Stores/useRecentsStore'
import { getElementById } from '../../contentScript'
import { NodeCard } from './NodeCard'
export const NotesInfoBar = () => {
const [search, setSearch] = useState('')
const [searchedNodes, setSearchedNodes] = useState<string[]>()
const { dispatch } = useRaju()
const recentNotes = useRecentsStore((s) => s.lastOpened)
const theme = useTheme()
const { getILinkFromNodeid } = useLinks()
const inputRef = useRef<HTMLInputElement>(null)
const onSearchChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
setSearch(e.target.value)
}
const onSearch = async (newSearchTerm: string) => {
try {
const res = await dispatch('SEARCH', ['node'], newSearchTerm)
const results = res?.map((item) => item.id) ?? []
setSearchedNodes(results)
} catch (err) {
mog('[NOTE SEARCH]: Unable to search', { err })
}
}
const getRecentList = (noteIds: Array<string>, limit = 5) => {
const recentList = []
noteIds?.forEach((noteId) => {
const noteLink = getILinkFromNodeid(noteId, true)
if (noteLink && !isParent(noteLink.path, BASE_TASKS_PATH)) {
recentList.push(noteId)
}
})
if (recentList.length > limit) {
return recentList.reverse().slice(0, limit)
}
return recentList?.reverse()
}
useEffect(() => {
if (search !== '') {
onSearch(search)
} else {
const defaultList = getRecentList(recentNotes)
setSearchedNodes(defaultList)
}
}, [search, recentNotes])
return (
<SnippetCards>
<SidebarListFilterWrapper>
<SidebarListFilter noMargin>
<MexIcon $noHover height={20} width={20} icon={searchLine} margin="0.6rem 0" />
<Input
autoFocus
fontSize="1rem"
placeholder={'Search notes'}
onChange={debounce((e) => onSearchChange(e), 250)}
ref={inputRef}
/>
</SidebarListFilter>
<Infobox text={NotesInfoBarHelp} root={getElementById('ext-side-nav')} />
</SidebarListFilterWrapper>
{!search && !searchedNodes?.length ? (
<CenteredColumn>
<MexIcon color={theme.colors.primary} $noHover width="32" height="32" icon="gg:file-document" />
<p>All your recents will shown here!</p>
</CenteredColumn>
) : (
<List scrollable>
{searchedNodes?.map((nodeId) => (
<NodeCard key={nodeId} nodeId={nodeId} />
))}
</List>
)}
</SnippetCards>
)
}