-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathsidebar-list.tsx
121 lines (113 loc) · 3.16 KB
/
sidebar-list.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
import SidebarListItem from './sidebar-list-item'
import NoteTreeState from 'libs/web/state/tree'
import Tree from '@atlaskit/tree'
import { useCallback } from 'react'
import router from 'next/router'
import HotkeyTooltip from 'components/hotkey-tooltip'
import IconButton from 'components/icon-button'
import useI18n from 'libs/web/hooks/use-i18n'
import { CircularProgress, Tooltip } from '@material-ui/core'
import { Favorites } from './favorites'
const SideBarList = () => {
const { t } = useI18n()
const {
tree,
moveItem,
mutateItem,
initLoaded,
collapseAllItems,
} = NoteTreeState.useContainer()
const onExpand = useCallback(
(id) => {
mutateItem(id, {
isExpanded: true,
})
},
[mutateItem]
)
const onCollapse = useCallback(
(id) => {
mutateItem(id, {
isExpanded: false,
})
},
[mutateItem]
)
const onDragEnd = useCallback(
(source, destination) => {
moveItem({
source,
destination,
}).catch((e) => {
// todo: toast
console.error('更新错误', e)
})
},
[moveItem]
)
const onCreateNote = useCallback(() => {
router.push('/new', undefined, { shallow: true })
}, [])
return (
<section className="h-full flex text-sm flex-col flex-grow bg-gray-100 overflow-y-auto">
{/* Favorites */}
<Favorites />
{/* My Pages */}
<div className="p-2 text-gray-500 flex items-center sticky top-0 bg-gray-100 z-10">
<div className="flex-auto flex items-center">
<span>{t('My Pages')}</span>
{initLoaded ? null : (
<CircularProgress className="ml-4" size="14px" color="inherit" />
)}
</div>
<Tooltip title={t('Collapse all pages')}>
<IconButton
icon="ChevronDoubleUp"
onClick={collapseAllItems}
className="text-gray-700 w-5 h-5 md:w-5 md:h-5"
></IconButton>
</Tooltip>
<HotkeyTooltip
text={t('Create page')}
commandKey
onHotkey={onCreateNote}
keys={['O']}
>
<IconButton
icon="Plus"
onClick={onCreateNote}
className="text-gray-700"
></IconButton>
</HotkeyTooltip>
</div>
<div className="flex-grow pb-10">
<Tree
onExpand={onExpand}
onCollapse={onCollapse}
onDragEnd={onDragEnd}
tree={tree}
isDragEnabled
isNestingEnabled
offsetPerLevel={10}
renderItem={({ provided, item, onExpand, onCollapse, snapshot }) => (
<SidebarListItem
{...provided.draggableProps}
{...provided.dragHandleProps}
onExpand={onExpand}
onCollapse={onCollapse}
isExpanded={item.isExpanded}
innerRef={provided.innerRef}
hasChildren={!!item.children.length}
item={{
...item.data,
id: item.id,
}}
snapshot={snapshot}
></SidebarListItem>
)}
></Tree>
</div>
</section>
)
}
export default SideBarList