MCPcopy Create free account
hub / github.com/dailydotdev/apps / useSourceStack

Function useSourceStack

packages/shared/src/hooks/source/useSourceStack.ts:21–93  ·  view source on GitHub ↗
(squad: Squad | null)

Source from the content-addressed store, hash-verified

19import { SourcePermissions as SourcePermissionsEnum } from '../../graphql/sources';
20
21export function useSourceStack(squad: Squad | null) {
22 const queryClient = useQueryClient();
23 const canEdit = squad
24 ? verifyPermission(squad, SourcePermissionsEnum.Edit)
25 : false;
26
27 const queryKey = generateQueryKey(
28 RequestKey.SourceStack,
29 undefined,
30 squad?.id,
31 );
32
33 const query = useQuery({
34 queryKey,
35 queryFn: () => getSourceStack(squad?.id as string),
36 staleTime: StaleTime.Default,
37 enabled: !!squad?.id,
38 });
39
40 const stackItems = useMemo(
41 () => query.data?.edges?.map(({ node }) => node) ?? [],
42 [query.data],
43 );
44 const canAddMore = stackItems.length < MAX_STACK_ITEMS;
45
46 const invalidateQuery = useCallback(() => {
47 queryClient.invalidateQueries({ queryKey });
48 }, [queryClient, queryKey]);
49
50 const addMutation = useMutation({
51 mutationFn: (input: AddSourceStackInput) =>
52 addSourceStack(squad?.id as string, input),
53 onSuccess: invalidateQuery,
54 });
55
56 const updateMutation = useMutation({
57 mutationFn: ({
58 id,
59 input,
60 }: {
61 id: string;
62 input: UpdateSourceStackInput;
63 }) => updateSourceStack(id, input),
64 onSuccess: invalidateQuery,
65 });
66
67 const deleteMutation = useMutation({
68 mutationFn: (id: string) => deleteSourceStack(id),
69 onSuccess: invalidateQuery,
70 });
71
72 const reorderMutation = useMutation({
73 mutationFn: (items: ReorderSourceStackInput[]) =>
74 reorderSourceStack(squad?.id as string, items),
75 onSuccess: invalidateQuery,
76 });
77
78 return {

Callers 1

SquadStackFunction · 0.90

Calls 7

verifyPermissionFunction · 0.90
generateQueryKeyFunction · 0.90
getSourceStackFunction · 0.90
addSourceStackFunction · 0.90
updateSourceStackFunction · 0.90
deleteSourceStackFunction · 0.90
reorderSourceStackFunction · 0.90

Tested by

no test coverage detected