-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseFetchContexts.ts
More file actions
47 lines (42 loc) · 1.26 KB
/
useFetchContexts.ts
File metadata and controls
47 lines (42 loc) · 1.26 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
import { Dropdown } from './../types/dropdown.interface';
import React from 'react';
import { Post } from 'shared/utils/request';
export const getContexts = async (spaceId: string, roomId: string, after: number, before: number, baseUrl: string) => {
const response = await Post({
path: `/v3/spaces/${spaceId}/rooms/${roomId}/contexts`,
baseUrl,
data: {
scope: {
contexts: ['*'],
nodes: [],
},
selectors: {
contexts: [],
nodes: [],
},
window: {
after,
before,
},
},
});
const { contexts = {} } = response?.data || {};
return Object.keys(contexts) as string[];
};
export const useFetchContexts = (baseUrl: string) => {
const [isError, setIsError] = React.useState(false);
const [contexts, setContexts] = React.useState<Dropdown[]>([]);
const fetchContexts = React.useCallback(
async (spaceId: string, roomId: string, after: number, before: number) => {
setIsError(false);
try {
const result = await getContexts(spaceId, roomId, after, before, baseUrl);
setContexts(result.map((c) => ({ label: c, value: c })));
} catch (error) {
setIsError(true);
}
},
[baseUrl]
);
return { isError, contexts, fetchContexts };
};