-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuse-pending-reflections.ts
40 lines (36 loc) · 1.42 KB
/
use-pending-reflections.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
import { type ReactiveVar, useReactiveVar } from '@apollo/client';
import type { ReflectEntryData } from '@akashaorg/typings/lib/ui';
/**
* Hook that makes use of the Apollo Client's reactivity model to allow the adding,
* removing and returning of pending Reflections to a component (i.e. the Beam Page)
*
* @example usePendingReflections hook
* ```typescript
* // createReactiveVar is another function that makes use of the `makeVar` method
* from Apollo Client to create a reactive variable.
* const pendingReflectionsVar = createReactiveVar<ReflectEntryData[]>([]);
const { pendingReflections } = usePendingReflections(pendingReflectionsVar);
* ```
*/
export const usePendingReflections = (
pendingReflectionsReactiveVar: ReactiveVar<ReflectEntryData[]>,
) => {
const pendingReflections = useReactiveVar<ReflectEntryData[]>(pendingReflectionsReactiveVar);
const addPendingReflection = (pendingReflection: ReflectEntryData) => {
pendingReflectionsReactiveVar([...pendingReflectionsReactiveVar(), pendingReflection]);
};
const removePendingReflection = (tempId: string) => {
pendingReflectionsReactiveVar([
...pendingReflectionsReactiveVar().filter(refl => refl.id === tempId),
]);
};
const removePendingReflections = () => {
pendingReflectionsReactiveVar([]);
};
return {
addPendingReflection,
removePendingReflection,
removePendingReflections,
pendingReflections,
};
};