From d883f2f5bdcbdee12920361eb39fe67f65091bf5 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 9 Mar 2023 12:36:20 -0600 Subject: [PATCH] Group hooks: use new parser opt --- app/soapbox/hooks/useGroups.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/soapbox/hooks/useGroups.ts b/app/soapbox/hooks/useGroups.ts index 1a69da120..7af1fa024 100644 --- a/app/soapbox/hooks/useGroups.ts +++ b/app/soapbox/hooks/useGroups.ts @@ -5,8 +5,13 @@ import { normalizeGroup, normalizeGroupRelationship } from 'soapbox/normalizers' import type { Group, GroupRelationship } from 'soapbox/types/entities'; +// HACK: normalizers currently don't have the desired API. +// TODO: rewrite normalizers as Zod parsers. +const parseGroup = (entity: unknown) => normalizeGroup(entity as Record); +const parseGroupRelationship = (entity: unknown) => normalizeGroupRelationship(entity as Record); + function useGroups() { - const result = useEntities(['Group', ''], '/api/v1/groups'); + const result = useEntities(['Group', ''], '/api/v1/groups', { parser: parseGroup }); const { entities, isLoading, fetchEntities } = result; const { entities: relationships } = useGroupRelationships(entities.map(entity => entity.id)); @@ -18,8 +23,7 @@ function useGroups() { } }, []); - const groups = entities.map((entity) => { - const group = normalizeGroup(entity); + const groups = entities.map((group) => { // TODO: a generalistic useRelationships() hook that returns a map of values (would be faster). const relationship = relationships.find(r => r.id === group.id); if (relationship) { @@ -35,8 +39,9 @@ function useGroups() { } function useGroup(groupId: string) { - const result = useEntity(['Group', groupId], `/api/v1/groups/${groupId}`); + const result = useEntity(['Group', groupId], `/api/v1/groups/${groupId}`, { parser: parseGroup }); const { entity, isLoading, fetchEntity } = result; + const { relationship } = useGroupRelationship(groupId); useEffect(() => { if (!isLoading) { @@ -46,13 +51,21 @@ function useGroup(groupId: string) { return { ...result, - group: entity ? normalizeGroup(entity) : undefined, + group: entity?.set('relationship', relationship), + }; +} + +function useGroupRelationship(groupId: string) { + const { relationships, ...rest } = useGroupRelationships([groupId]); + return { + ...rest, + relationship: relationships[0], }; } function useGroupRelationships(groupIds: string[]) { const q = groupIds.map(id => `id[]=${id}`).join('&'); - const result = useEntities(['GroupRelationship', ''], `/api/v1/groups/relationships?${q}`); + const result = useEntities(['GroupRelationship', ''], `/api/v1/groups/relationships?${q}`, { parser: parseGroupRelationship }); const { entities, isLoading, fetchEntities } = result; useEffect(() => { @@ -63,7 +76,7 @@ function useGroupRelationships(groupIds: string[]) { return { ...result, - relationships: entities.map(normalizeGroupRelationship), + relationships: entities, }; }