(user: PublicProfile | null)
| 23 | const selectStack = (data: ProfileShowcase) => data.userStack; |
| 24 | |
| 25 | export function useUserStack(user: PublicProfile | null) { |
| 26 | const { isOwner } = useProfilePreview(user); |
| 27 | const { logEvent } = useLogContext(); |
| 28 | |
| 29 | const { |
| 30 | queryKey, |
| 31 | invalidate: invalidateQuery, |
| 32 | ...query |
| 33 | } = useProfileShowcase(user, selectStack); |
| 34 | |
| 35 | const stackItems = useMemo( |
| 36 | () => query.data?.edges?.map(({ node }) => node) ?? [], |
| 37 | [query.data], |
| 38 | ); |
| 39 | const canAddMore = stackItems.length < MAX_STACK_ITEMS; |
| 40 | |
| 41 | // Group items by section |
| 42 | const groupedBySection = useMemo(() => { |
| 43 | const groups: Record<string, UserStack[]> = {}; |
| 44 | stackItems.forEach((item) => { |
| 45 | if (!groups[item.section]) { |
| 46 | groups[item.section] = []; |
| 47 | } |
| 48 | groups[item.section].push(item); |
| 49 | }); |
| 50 | return groups; |
| 51 | }, [stackItems]); |
| 52 | |
| 53 | const addMutation = useMutation({ |
| 54 | mutationFn: (input: AddUserStackInput) => addUserStack(input), |
| 55 | onSuccess: (_, input) => { |
| 56 | invalidateQuery(); |
| 57 | logEvent({ |
| 58 | event_name: LogEvent.AddUserStack, |
| 59 | target_id: input.title, |
| 60 | extra: JSON.stringify({ section: input.section }), |
| 61 | }); |
| 62 | }, |
| 63 | }); |
| 64 | |
| 65 | const updateMutation = useMutation({ |
| 66 | mutationFn: ({ id, input }: { id: string; input: UpdateUserStackInput }) => |
| 67 | updateUserStack(id, input), |
| 68 | onSuccess: (_, { id }) => { |
| 69 | invalidateQuery(); |
| 70 | logEvent({ |
| 71 | event_name: LogEvent.UpdateUserStack, |
| 72 | extra: JSON.stringify({ id }), |
| 73 | }); |
| 74 | }, |
| 75 | }); |
| 76 | |
| 77 | const deleteMutation = useMutation({ |
| 78 | mutationFn: (id: string) => deleteUserStack(id), |
| 79 | onSuccess: (_, id) => { |
| 80 | invalidateQuery(); |
| 81 | logEvent({ |
| 82 | event_name: LogEvent.RemoveUserStack, |
no test coverage detected