1
1
import React , { useEffect , useRef , useState } from 'react'
2
2
3
3
import searchLine from '@iconify/icons-ri/search-line'
4
- import { Icon } from '@iconify/react'
5
4
import { debounce } from 'lodash'
5
+ import { useTheme } from 'styled-components'
6
6
7
- import { mog } from '@mexit/core'
8
- import { SidebarListFilterWrapper , SidebarListFilter , Input , SnippetCards } from '@mexit/shared'
7
+ import { Infobox } from '@workduck-io/mex-components'
9
8
9
+ import { BASE_TASKS_PATH , isParent , mog } from '@mexit/core'
10
+ import {
11
+ SidebarListFilterWrapper ,
12
+ SidebarListFilter ,
13
+ Input ,
14
+ SnippetCards ,
15
+ MexIcon ,
16
+ NotesInfoBarHelp ,
17
+ List ,
18
+ CenteredColumn
19
+ } from '@mexit/shared'
20
+
21
+ import { useLinks } from '../../Hooks/useLinks'
10
22
import useRaju from '../../Hooks/useRaju'
11
- import useDataStore from '../../Stores/useDataStore'
23
+ import { useRecentsStore } from '../../Stores/useRecentsStore'
24
+ import { getElementById } from '../../contentScript'
12
25
import { NodeCard } from './NodeCard'
13
26
14
27
export const NotesInfoBar = ( ) => {
15
- const publicNodes = useDataStore ( ( state ) => state . publicNodes )
16
28
const [ search , setSearch ] = useState ( '' )
17
29
const [ searchedNodes , setSearchedNodes ] = useState < string [ ] > ( )
18
30
const { dispatch } = useRaju ( )
31
+ const recentNotes = useRecentsStore ( ( s ) => s . lastOpened )
32
+
33
+ const theme = useTheme ( )
34
+ const { getILinkFromNodeid } = useLinks ( )
19
35
20
36
const inputRef = useRef < HTMLInputElement > ( null )
21
37
@@ -24,35 +40,69 @@ export const NotesInfoBar = () => {
24
40
}
25
41
26
42
const onSearch = async ( newSearchTerm : string ) => {
27
- const res = await dispatch ( 'SEARCH' , [ 'node' ] , newSearchTerm )
43
+ try {
44
+ const res = await dispatch ( 'SEARCH' , [ 'node' ] , newSearchTerm )
45
+ const results = res ?. map ( ( item ) => item . id ) ?? [ ]
46
+ setSearchedNodes ( results )
47
+ } catch ( err ) {
48
+ mog ( '[NOTE SEARCH]: Unable to search' , { err } )
49
+ }
50
+ }
51
+
52
+ const getRecentList = ( noteIds : Array < string > , limit = 5 ) => {
53
+ const recentList = [ ]
54
+
55
+ noteIds ?. forEach ( ( noteId ) => {
56
+ const noteLink = getILinkFromNodeid ( noteId , true )
57
+
58
+ if ( noteLink && ! isParent ( noteLink . path , BASE_TASKS_PATH ) ) {
59
+ recentList . push ( noteId )
60
+ }
61
+ } )
62
+
63
+ if ( recentList . length > limit ) {
64
+ return recentList . reverse ( ) . slice ( 0 , limit )
65
+ }
28
66
29
- setSearchedNodes ( res ?. map ( ( item ) => item . id ) )
67
+ return recentList ?. reverse ( )
30
68
}
31
69
32
70
useEffect ( ( ) => {
33
71
if ( search !== '' ) {
34
72
onSearch ( search )
35
73
} else {
36
- setSearchedNodes ( publicNodes )
74
+ const defaultList = getRecentList ( recentNotes )
75
+ setSearchedNodes ( defaultList )
37
76
}
38
- } , [ search ] )
77
+ } , [ search , recentNotes ] )
39
78
40
79
return (
41
80
< SnippetCards >
42
81
< SidebarListFilterWrapper >
43
- < SidebarListFilter >
44
- < Icon icon = { searchLine } />
82
+ < SidebarListFilter noMargin >
83
+ < MexIcon $noHover height = { 20 } width = { 20 } icon = { searchLine } margin = "0.6rem 0" />
45
84
< Input
46
85
autoFocus
86
+ fontSize = "1rem"
47
87
placeholder = { 'Search notes' }
48
88
onChange = { debounce ( ( e ) => onSearchChange ( e ) , 250 ) }
49
89
ref = { inputRef }
50
90
/>
51
91
</ SidebarListFilter >
92
+ < Infobox text = { NotesInfoBarHelp } root = { getElementById ( 'ext-side-nav' ) } />
52
93
</ SidebarListFilterWrapper >
53
- { searchedNodes ?. map ( ( nodeId ) => (
54
- < NodeCard key = { nodeId } nodeId = { nodeId } />
55
- ) ) }
94
+ { ! search && ! searchedNodes ?. length ? (
95
+ < CenteredColumn >
96
+ < MexIcon color = { theme . colors . primary } $noHover width = "32" height = "32" icon = "gg:file-document" />
97
+ < p > All your recents will shown here!</ p >
98
+ </ CenteredColumn >
99
+ ) : (
100
+ < List scrollable >
101
+ { searchedNodes ?. map ( ( nodeId ) => (
102
+ < NodeCard key = { nodeId } nodeId = { nodeId } />
103
+ ) ) }
104
+ </ List >
105
+ ) }
56
106
</ SnippetCards >
57
107
)
58
108
}
0 commit comments