From 61fb434a54ee17c8bcd6b7b57d50db9adf7f767d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 22 Mar 2023 16:12:05 -0500 Subject: [PATCH] Improve API of parseEntitiesPath --- app/soapbox/entity-store/hooks/useCreateEntity.ts | 3 +-- app/soapbox/entity-store/hooks/useDismissEntity.ts | 3 +-- app/soapbox/entity-store/hooks/useEntities.ts | 4 +--- app/soapbox/entity-store/hooks/useEntityActions.ts | 3 +-- app/soapbox/entity-store/hooks/utils.ts | 10 ++++++++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/soapbox/entity-store/hooks/useCreateEntity.ts b/app/soapbox/entity-store/hooks/useCreateEntity.ts index 719bed971..373434e73 100644 --- a/app/soapbox/entity-store/hooks/useCreateEntity.ts +++ b/app/soapbox/entity-store/hooks/useCreateEntity.ts @@ -35,8 +35,7 @@ function useCreateEntity, opts: UseCreateEntityOpts = {}, ) { - const path = parseEntitiesPath(expandedPath); - const [entityType, listKey] = path; + const { entityType, listKey } = parseEntitiesPath(expandedPath); const dispatch = useAppDispatch(); diff --git a/app/soapbox/entity-store/hooks/useDismissEntity.ts b/app/soapbox/entity-store/hooks/useDismissEntity.ts index c887e9490..1ba5f4a60 100644 --- a/app/soapbox/entity-store/hooks/useDismissEntity.ts +++ b/app/soapbox/entity-store/hooks/useDismissEntity.ts @@ -13,8 +13,7 @@ type DismissFn = (entityId: string) => Promise | T; * To remove an entity globally from all lists, see `useDeleteEntity`. */ function useDismissEntity(expandedPath: ExpandedEntitiesPath, dismissFn: DismissFn) { - const path = parseEntitiesPath(expandedPath); - const [entityType, listKey] = path; + const { entityType, listKey } = parseEntitiesPath(expandedPath); const dispatch = useAppDispatch(); diff --git a/app/soapbox/entity-store/hooks/useEntities.ts b/app/soapbox/entity-store/hooks/useEntities.ts index dec6aefd4..309accf1b 100644 --- a/app/soapbox/entity-store/hooks/useEntities.ts +++ b/app/soapbox/entity-store/hooks/useEntities.ts @@ -39,9 +39,7 @@ function useEntities( const dispatch = useAppDispatch(); const getState = useGetState(); - const path = parseEntitiesPath(expandedPath); - const [entityType, listKey] = path; - + const { entityType, listKey, path } = parseEntitiesPath(expandedPath); const entities = useAppSelector(state => selectEntities(state, path)); const isEnabled = opts.enabled ?? true; diff --git a/app/soapbox/entity-store/hooks/useEntityActions.ts b/app/soapbox/entity-store/hooks/useEntityActions.ts index 96def69ab..9406b3809 100644 --- a/app/soapbox/entity-store/hooks/useEntityActions.ts +++ b/app/soapbox/entity-store/hooks/useEntityActions.ts @@ -22,8 +22,7 @@ function useEntityActions( opts: UseEntityActionsOpts = {}, ) { const api = useApi(); - const path = parseEntitiesPath(expandedPath); - const [entityType] = path; + const { entityType, path } = parseEntitiesPath(expandedPath); const deleteEntity = useDeleteEntity(entityType, (entityId) => { if (!endpoints.delete) return Promise.reject(endpoints); diff --git a/app/soapbox/entity-store/hooks/utils.ts b/app/soapbox/entity-store/hooks/utils.ts index 69568b25a..d137ca1fb 100644 --- a/app/soapbox/entity-store/hooks/utils.ts +++ b/app/soapbox/entity-store/hooks/utils.ts @@ -1,9 +1,15 @@ import type { EntitiesPath, ExpandedEntitiesPath } from './types'; -function parseEntitiesPath(expandedPath: ExpandedEntitiesPath): EntitiesPath { +function parseEntitiesPath(expandedPath: ExpandedEntitiesPath) { const [entityType, ...listKeys] = expandedPath; const listKey = (listKeys || []).join(':'); - return [entityType, listKey]; + const path: EntitiesPath = [entityType, listKey]; + + return { + entityType, + listKey, + path, + }; } export { parseEntitiesPath }; \ No newline at end of file