Repository navigation
Expand file tree
/
Copy pathroot.tsx
More file actions
145 lines (135 loc) · 4.28 KB
/
Copy pathroot.tsx
File metadata and controls
145 lines (135 loc) · 4.28 KB
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
import * as C from '@/constants'
import * as Constants from '@/constants/fs'
import * as React from 'react'
import * as T from '@/constants/types'
import * as Kb from '@/common-adapters'
import TlfType from './rows/tlf-type-container'
import Tlf from './rows/tlf-container'
import SfmiBanner from '../banner/system-file-manager-integration-banner/container'
import {WrapRow} from './rows/rows'
type Props = {
destinationPickerIndex?: number
}
type SectionListItem = {
name: string
tlfType: T.FS.TlfType
}
const rootRows: Array<SectionListItem> = [
{
name: T.FS.TlfType.Private,
tlfType: T.FS.TlfType.Private,
},
{
name: T.FS.TlfType.Public,
tlfType: T.FS.TlfType.Public,
},
{
name: T.FS.TlfType.Team,
tlfType: T.FS.TlfType.Team,
},
]
const getRenderItem =
(destinationPickerIndex?: number) =>
({item, section}: {item: SectionListItem; section: {key: string}}) =>
section.key === 'section-top' ? (
<WrapRow>
<TlfType name={item.name as T.FS.TlfType} destinationPickerIndex={destinationPickerIndex} />
</WrapRow>
) : (
<WrapRow>
<Tlf
disabled={false}
name={item.name}
tlfType={item.tlfType}
mixedMode={true}
destinationPickerIndex={destinationPickerIndex}
/>
</WrapRow>
)
const renderSectionHeader = ({section}: {section: {key: string; title: string}}) =>
section.key === 'banner-sfmi' ? <SfmiBanner /> : <Kb.SectionDivider label={section.title} />
const useTopNTlfs = (
tlfType: T.FS.TlfType,
tlfs: T.FS.TlfList,
n: number
): Array<{
name: string
tlfMtime: number
tlfType: T.FS.TlfType
}> =>
// TODO move these sorting to Go HOTPOT-433
React.useMemo(
() =>
[...tlfs.values()]
.filter(({isIgnored}) => !isIgnored)
.sort((tlf1, tlf2) => tlf2.tlfMtime - tlf1.tlfMtime)
.slice(0, n)
.map(({name, tlfMtime}) => ({
name,
tlfMtime,
tlfType,
})),
[tlfs, n, tlfType]
)
const useRecentTlfs = (n: number, destinationPickerIndex?: number): Array<SectionListItem> => {
const tlfs = C.useFSState(s => s.tlfs)
const username = C.useCurrentUserState(s => s.username)
const privateTopN = useTopNTlfs(T.FS.TlfType.Private, tlfs.private, n)
const publicTopN = useTopNTlfs(T.FS.TlfType.Public, tlfs.public, n)
const teamTopN = useTopNTlfs(T.FS.TlfType.Team, tlfs.team, n)
return React.useMemo(() => {
const recent = [...privateTopN, ...publicTopN, ...teamTopN]
.sort(({tlfMtime: t1}, {tlfMtime: t2}) => t2 - t1)
.map(({name, tlfType}) => ({name, tlfType}))
const afterFilter =
// This isn't perfect since it doesn't cover the case where a team TLF
// could be readonly. But to do that we'd need some new caching in KBFS
// to plumb it into the Tlfs structure without awful overhead.
typeof destinationPickerIndex === 'number'
? recent.filter(
({name, tlfType}) =>
!Constants.hideOrDisableInDestinationPicker(tlfType, name, username, destinationPickerIndex)
)
: recent
return afterFilter.slice(0, n)
}, [destinationPickerIndex, privateTopN, publicTopN, teamTopN, n, username])
}
const Root = React.memo(function Root({destinationPickerIndex}: Props) {
const top10 = useRecentTlfs(10, destinationPickerIndex)
const sections = [
...(destinationPickerIndex
? [] // don't show sfmi banner in destination picker
: [
{
data: new Array<SectionListItem>(),
key: 'banner-sfmi',
keyExtractor: () => 'banner-sfmi-item',
title: '',
},
]),
{
data: rootRows,
key: 'section-top',
keyExtractor: (item: SectionListItem) => `top:${item.name}`,
title: ' ',
},
{
data: top10,
key: 'section-recent-tlfs',
keyExtractor: (item: SectionListItem) => `recent-tlfs:${item.tlfType}-${item.name}`,
title: 'Recent folders',
},
]
const renderItem = React.useMemo(() => getRenderItem(destinationPickerIndex), [destinationPickerIndex])
return (
<Kb.BoxGrow>
<Kb.SectionList
sections={sections}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}
stickySectionHeadersEnabled={false}
/>
</Kb.BoxGrow>
)
})
export default Root