-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathWorkspaceSwitcher.tsx
More file actions
277 lines (241 loc) · 10.8 KB
/
WorkspaceSwitcher.tsx
File metadata and controls
277 lines (241 loc) · 10.8 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { Sheet, useSheetRef } from '@components/nativewindui/Sheet'
import { Text } from '@components/nativewindui/Text'
import { BottomSheetScrollView, BottomSheetView } from '@gorhom/bottom-sheet'
import { Pressable, TouchableOpacity, View } from 'react-native'
import UserAvatar from '@components/layout/UserAvatar'
import useFetchWorkspaces, { WorkspaceFields } from '@raven/lib/hooks/useFetchWorkspaces';
import { useCallback, useMemo } from 'react';
import { Divider } from '@components/layout/Divider';
import CheckFilledIcon from '@assets/icons/CheckFilledIcon.svg';
import { useColorScheme } from '@hooks/useColorScheme';
import ChevronDownIcon from '@assets/icons/ChevronDownIcon.svg'
import { COLORS } from '@theme/colors'
import useSiteContext from '@hooks/useSiteContext'
import SiteSwitcher from '../auth/SiteSwitcher'
import { getSiteNameFromUrl } from '@raven/lib/utils/operations'
import ServerIcon from '@assets/icons/ServerIcon.svg'
import AddSite from '../auth/AddSite'
import { FrappeError, useFrappePostCall, useSWRConfig } from 'frappe-react-sdk'
import { toast } from 'sonner-native'
import { getErrorMessage } from '@components/common/ErrorBanner'
import { ActivityIndicator } from '@components/nativewindui/ActivityIndicator'
const WorkspaceSwitcher = ({ workspace, setWorkspace }: { workspace: string, setWorkspace: (workspace: string) => Promise<void> }) => {
const { data: workspaces } = useFetchWorkspaces()
const selectedWorkspace = workspaces?.message.find((w: WorkspaceFields) => w.name === workspace)
if (!selectedWorkspace) return null
return (
<WorkSpaceSwitcherMenu
selectedWorkspace={selectedWorkspace}
setWorkspace={setWorkspace}
workspaces={workspaces?.message || []} />
)
}
const WorkSpaceSwitcherMenu = ({ selectedWorkspace, workspaces, setWorkspace }: { selectedWorkspace: WorkspaceFields, workspaces: WorkspaceFields[], setWorkspace: (workspace: string) => Promise<void> }) => {
const bottomSheetRef = useSheetRef()
const onSetWorkspace = useCallback(async (workspace: string) => {
await setWorkspace(workspace)
bottomSheetRef.current?.dismiss()
}, [setWorkspace])
const logo = getLogo(selectedWorkspace)
const siteInfo = useSiteContext()
const urlWithoutProtocol = useMemo(() => {
return getSiteNameFromUrl(siteInfo?.url)
}, [siteInfo])
const addSiteSheetRef = useSheetRef()
const openAddSiteSheet = useCallback(() => {
bottomSheetRef.current?.dismiss()
addSiteSheetRef.current?.present()
}, [addSiteSheetRef])
return (
<View className='flex-1 gap-3'>
<TouchableOpacity onPress={() => bottomSheetRef.current?.present()}>
<View className='flex-row items-center gap-2'>
<UserAvatar
key={logo}
alt={selectedWorkspace?.workspace_name ?? 'Workspace Logo'}
src={logo}
avatarProps={{ className: 'h-10 w-10' }}
/>
<View>
<View className='flex-row items-center gap-1'>
<Text className="text-white font-bold">{selectedWorkspace?.workspace_name}</Text>
<ChevronDownIcon height={20} width={20} fill={COLORS.white} />
</View>
<Text className='text-xs text-white/90 overflow-hidden text-ellipsis line-clamp-1'>{urlWithoutProtocol}</Text>
</View>
</View>
</TouchableOpacity>
<Sheet enableDynamicSizing snapPoints={['60%', '80%']} ref={bottomSheetRef}>
<BottomSheetScrollView>
<View className='flex flex-col gap-4 px-4 pb-24'>
<SelectWorkspaceSheet
selectedWorkspace={selectedWorkspace}
setWorkspace={onSetWorkspace}
workspaces={workspaces} />
<Divider />
<SiteSwitcher openAddSiteSheet={openAddSiteSheet} />
</View>
</BottomSheetScrollView>
</Sheet>
<Sheet enableDynamicSizing ref={addSiteSheetRef}>
<BottomSheetView className='flex-1 pb-16'>
<View className='flex-1 gap-2 px-4'>
<Text className='text-lg font-semibold'>Add a new site</Text>
<AddSite useBottomSheet={true} />
</View>
</BottomSheetView>
</Sheet>
</View>
)
}
type Workspace = WorkspaceFields & { isSelected?: boolean }
interface SelectWorkspaceSheetProps {
selectedWorkspace: Workspace | undefined,
workspaces: Workspace[],
setWorkspace: (workspace: string) => Promise<void>
}
const SelectWorkspaceSheet = ({ selectedWorkspace, workspaces, setWorkspace }: SelectWorkspaceSheetProps) => {
const { call: joinWorkspace, loading: joiningWorkspace } = useFrappePostCall('raven.api.workspaces.join_workspace')
const { mutate } = useSWRConfig()
const { myWorkspaces, otherWorkspaces } = useMemo(() => {
const myWorkspaces: Workspace[] = []
const otherWorkspaces: Workspace[] = []
if (workspaces) {
workspaces.forEach((workspace) => {
if (workspace.workspace_member_name) {
myWorkspaces.push({
...workspace,
isSelected: workspace.name === selectedWorkspace?.name
})
} else {
otherWorkspaces.push(workspace)
}
})
}
return { myWorkspaces, otherWorkspaces }
}, [workspaces, selectedWorkspace])
const handleJoinOtherWorkspace = useCallback(
(workspaceName: string) => {
const displayName =
workspaces.find((w) => w.name === workspaceName)?.workspace_name ?? workspaceName
joinWorkspace({ workspace: workspaceName })
.then(() => Promise.all([mutate('workspaces_list'), mutate('channel_list')]))
.then(() => setWorkspace(workspaceName))
.then(() => {
toast.success(`You have joined ${displayName}.`)
})
.catch((error: unknown) => {
toast.error(
getErrorMessage(error as FrappeError) || 'Failed to join the workspace.'
)
})
},
[joinWorkspace, setWorkspace, workspaces]
)
const siteInfo = useSiteContext()
const urlWithoutProtocol = useMemo(() => {
return getSiteNameFromUrl(siteInfo?.url)
}, [siteInfo])
const { colors } = useColorScheme()
return (
<View className='flex flex-col gap-2'>
<View className='flex flex-col gap-2'>
<View className='flex flex-row items-center gap-2'>
<ServerIcon height={16} width={16} color={colors.grey} />
<Text className='text-sm font-medium text-muted-foreground'>{urlWithoutProtocol}</Text>
</View>
<View className='flex flex-col gap-2 border border-border p-2 rounded-xl'>
{myWorkspaces.map((workspace, index) => (
<WorkspaceRow
key={workspace.name}
workspace={workspace}
setWorkspace={setWorkspace}
isLast={index === myWorkspaces.length - 1}
/>
))}
</View>
</View>
{otherWorkspaces.length > 0 &&
<View className='flex flex-col gap-2'>
<Text className='text-sm font-medium text-muted-foreground'>Other workspaces</Text>
<View className='flex flex-col gap-2 border border-border p-2 rounded-xl'>
{otherWorkspaces.map((workspace, index) => (
<WorkspaceRow key={workspace.name}
workspace={workspace}
setWorkspace={setWorkspace}
isOtherWorkspace
isJoining={joiningWorkspace}
onJoinOtherWorkspace={handleJoinOtherWorkspace}
isLast={index === otherWorkspaces.length - 1}
/>
))}
</View>
</View>
}
</View>
)
}
type WorkspaceRowProps = {
workspace: Workspace
isLast: boolean
setWorkspace: (workspace: string) => Promise<void>
isOtherWorkspace?: boolean
/** True while this row's join request is in flight */
isJoining?: boolean
onJoinOtherWorkspace?: (workspaceName: string) => void
}
const WorkspaceRow = ({
workspace,
isLast,
setWorkspace,
isOtherWorkspace = false,
isJoining = false,
onJoinOtherWorkspace,
}: WorkspaceRowProps) => {
const { colors } = useColorScheme()
const onPress = () => {
if (isOtherWorkspace && onJoinOtherWorkspace) {
void onJoinOtherWorkspace(workspace.name)
return
}
if (!isOtherWorkspace) {
void setWorkspace(workspace.name)
}
}
const disabled = isOtherWorkspace && isJoining
return (
<Pressable
className='flex flex-col gap-2'
onPress={onPress}
disabled={disabled}
>
<View className={`flex-row items-center gap-2 ${disabled ? 'opacity-60' : ''}`}>
<UserAvatar
alt={workspace.workspace_name}
src={getLogo(workspace)}
avatarProps={{ className: 'h-10 w-10 rounded-lg dark:border-border dark:border' }}
/>
<View className='flex-1'>
<Text className='text-sm font-semibold'>{workspace.workspace_name}</Text>
<Text className='text-sm text-gray-500'>{workspace.type}</Text>
</View>
{isJoining ? (
<ActivityIndicator size="small" color={colors.primary} />
) : workspace.isSelected ? (
<View className='mr-2'>
<CheckFilledIcon fill={colors.primary} height={20} width={20} />
</View>
) : null}
</View>
{!isLast && <Divider className='mx-0' />}
</Pressable>
)
}
const getLogo = (workspace: WorkspaceFields) => {
let logo = workspace?.logo || undefined
if (!logo && workspace?.workspace_name === 'Raven') {
logo = '/assets/raven/raven-logo.png'
}
return logo
}
export default WorkspaceSwitcher