-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseNamespaceAPI.ts
323 lines (295 loc) · 8.78 KB
/
useNamespaceAPI.ts
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import toast from 'react-hot-toast'
import { client } from '@workduck-io/dwindle'
import {
apiURLs,
generateNamespaceId,
MIcon,
mog,
WORKSPACE_HEADER,
AccessLevel,
iLinksToUpdate,
extractMetadata
} from '@mexit/core'
import { useApiStore } from '../../Stores/useApiStore'
import { useAuthStore } from '../../Stores/useAuth'
import { useDataStore } from '../../Stores/useDataStore'
import '../../Utils/apiClient'
import { deserializeContent } from '../../Utils/serializer'
import { WorkerRequestType } from '../../Utils/worker'
import { runBatchWorker } from '../../Workers/controller'
import { useUpdater } from '../useUpdater'
export const useNamespaceApi = () => {
const getWorkspaceId = useAuthStore((store) => store.getWorkspaceId)
const { setNamespaces, setIlinks } = useDataStore()
const setRequest = useApiStore.getState().setRequest
const { updateFromContent } = useUpdater()
const workspaceHeaders = () => ({
[WORKSPACE_HEADER]: getWorkspaceId(),
Accept: 'application/json, text/plain, */*'
})
const getAllNamespaces = async () => {
const namespaces = await client
.get(apiURLs.namespaces.getAll(), {
headers: workspaceHeaders()
})
.then((d: any) => {
// mog('namespaces all', d.data)
return d.data.map((item: any) => {
// metadata is json string parse to object
return {
ns: {
id: item.id,
name: item.name,
icon: item.metadata?.icon ?? undefined,
access: item.accessType,
createdAt: item.createdAt,
updatedAt: item.updatedAt,
granterID: item.granterID ?? undefined,
publicAccess: item.publicAccess
},
nodeHierarchy: item.nodeHierarchy.map((i) => ({ ...i, namespace: item.id })),
archiveHierarchy: item?.archivedNodeHierarchyInformation
}
})
})
.catch((e) => {
mog('Error fetching all namespaces', e)
return undefined
})
if (namespaces) {
const newILinks = namespaces.reduce((arr, { nodeHierarchy }) => {
return [...arr, ...nodeHierarchy]
}, [])
const localILinks = useDataStore.getState().ilinks
mog('update namespaces and ILinks', { namespaces, newILinks })
// SetILinks once middleware is integrated
setNamespaces(namespaces.map((n) => n.ns))
// TODO: Also set archive links
setIlinks(newILinks)
const { toUpdateLocal } = iLinksToUpdate(localILinks, newILinks)
const ids = toUpdateLocal.map((i) => i.nodeid)
const { fulfilled } = await runBatchWorker(WorkerRequestType.GET_NODES, 6, ids)
const requestData = { time: Date.now(), method: 'GET' }
fulfilled.forEach((node) => {
if (node) {
const { rawResponse, nodeid } = node
setRequest(apiURLs.getNode(nodeid), { ...requestData, url: apiURLs.getNode(nodeid) })
const content = deserializeContent(rawResponse.data)
const metadata = extractMetadata(rawResponse) // added by Varshitha
updateFromContent(nodeid, content, metadata)
}
})
}
}
const getNamespace = async (id: string) => {
const namespace = await client
.get(apiURLs.namespaces.get(id), {
headers: workspaceHeaders()
})
.then((d: any) => {
mog('namespaces specific', { data: d.data, id })
// return d.data?.nodeHierarchy
return {
id: d.data?.id,
name: d.data?.name,
icon: d.data?.metadata?.icon ?? undefined,
nodeHierarchy: d.data?.nodeHierarchy,
createdAt: d.data?.createdAt,
updatedAt: d.data?.updatedAt,
publicAccess: d.data?.publicAccess
}
})
.catch((e) => {
mog('Save error', e)
return undefined
})
return namespace
}
const getPublicNamespaceAPI = async (namespaceID: string) => {
const res = await client
.get(apiURLs.namespaces.getPublic(namespaceID), {
headers: workspaceHeaders()
})
.then((response: any) => {
return response.data
})
return res
}
const makeNamespacePublic = async (namespaceID: string) => {
const res = await client
.patch(apiURLs.namespaces.makePublic(namespaceID), null, {
headers: workspaceHeaders()
})
.then((response: any) => {
return response.data
})
return res
}
const makeNamespacePrivate = async (namespaceID: string) => {
const res = await client
.patch(apiURLs.namespaces.makePrivate(namespaceID), null, {
headers: workspaceHeaders()
})
.then((response: any) => {
return response.data
})
return res
}
const createNewNamespace = async (name: string) => {
try {
const req = {
type: 'NamespaceRequest',
name,
id: generateNamespaceId(),
metadata: {
icon: {
type: 'ICON',
value: 'heroicons-outline:view-grid'
}
}
}
const res = await client
.post(apiURLs.namespaces.create, req, {
headers: workspaceHeaders()
})
.then((d: any) => ({
id: req.id,
name: name,
iconUrl: req.metadata.icon,
access: 'MANAGE' as const,
createdAt: Date.now(),
updatedAt: Date.now()
}))
mog('We created a namespace', { res })
return res
} catch (err) {
toast('Unable to Create New Namespace')
}
}
const changeNamespaceName = async (id: string, name: string) => {
try {
const res = await client
.patch(
apiURLs.namespaces.update,
{
type: 'NamespaceRequest',
id,
name
},
{
headers: workspaceHeaders()
}
)
.then(() => true)
return res
} catch (err) {
throw new Error('Unable to update namespace')
}
}
const changeNamespaceIcon = async (id: string, name: string, icon: MIcon) => {
try {
const res = await client
.patch(
apiURLs.namespaces.update,
{
type: 'NamespaceRequest',
id,
name,
metadata: { icon }
},
{
headers: workspaceHeaders()
}
)
.then(() => icon)
return res
} catch (err) {
throw new Error('Unable to update namespace icon')
}
}
const shareNamespace = async (id: string, userIDs: string[], accessType: AccessLevel) => {
try {
const res = await client.post(
apiURLs.namespaces.share,
{
type: 'SharedNamespaceRequest',
namespaceID: id,
userIDToAccessTypeMap: userIDs.reduce((acc, userID) => {
acc[userID] = accessType
return acc
}, {} as Record<string, AccessLevel>)
},
{
headers: workspaceHeaders()
}
)
mog('Shared a namespace', { res })
return res
} catch (err) {
throw new Error(`Unable to share namespace: ${err}`)
}
}
const revokeNamespaceShare = async (id: string, userIDs: string[]) => {
try {
const res = await client.delete(apiURLs.namespaces.update, {
data: {
type: 'SharedNamespaceRequest',
namespaceID: id,
userIDs
},
headers: workspaceHeaders()
})
mog('revoke access users', res)
return res
} catch (err) {
throw new Error(`Unable to revoke namespace access: ${err}`)
}
}
const updateNamespaceShare = async (id: string, userIDToAccessTypeMap: { [userid: string]: AccessLevel }) => {
try {
const payload = {
namespaceID: id,
userIDToAccessTypeMap
}
return await client
.post(apiURLs.namespaces.share, payload, {
headers: workspaceHeaders()
})
.then((resp) => {
mog('changeUsers resp', { resp })
return resp
})
} catch (err) {
throw new Error(`Unable to update namespace access: ${err}`)
}
}
const getAllSharedUsers = async (id: string): Promise<{ users: Record<string, string> }> => {
try {
return await client
.get(apiURLs.namespaces.getUsersOfShared(id), {
headers: workspaceHeaders()
})
.then((resp: any) => {
mog('get all shared users', resp)
return { users: resp.data }
})
} catch (err) {
mog(`Unable to get shared namespace users: ${err}`)
return { users: {} }
}
}
return {
createNewNamespace,
getAllNamespaces,
getNamespace,
changeNamespaceName,
changeNamespaceIcon,
shareNamespace,
revokeNamespaceShare,
getAllSharedUsers,
updateNamespaceShare,
getPublicNamespaceAPI,
makeNamespacePublic,
makeNamespacePrivate
}
}