From ae8b44e7c0a220a8cc65f4ce446051e583ec8009 Mon Sep 17 00:00:00 2001 From: Prateek Jain Date: Tue, 30 Jul 2024 21:33:01 +0530 Subject: [PATCH] Fix total count in show page --- .../hooks/useRecordShowPagePagination.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/twenty-front/src/modules/object-record/record-show/hooks/useRecordShowPagePagination.ts b/packages/twenty-front/src/modules/object-record/record-show/hooks/useRecordShowPagePagination.ts index d9dc4004e5337..51319b1662b3c 100644 --- a/packages/twenty-front/src/modules/object-record/record-show/hooks/useRecordShowPagePagination.ts +++ b/packages/twenty-front/src/modules/object-record/record-show/hooks/useRecordShowPagePagination.ts @@ -60,7 +60,8 @@ export const useRecordShowPagePagination = ( const cursorFromRequest = currentRecordsPageInfo?.endCursor; - const [totalCount, setTotalCount] = useState(0); + const [totalCountBefore, setTotalCountBefore] = useState(0); + const [totalCountAfter, setTotalCountAfter] = useState(0); const { loading: loadingRecordBefore, records: recordsBefore } = useFindManyRecords({ @@ -78,7 +79,7 @@ export const useRecordShowPagePagination = ( objectNameSingular, recordGqlFields, onCompleted: (_, pagination) => { - setTotalCount(pagination?.totalCount ?? 0); + setTotalCountBefore(pagination?.totalCount ?? 0); }, }); @@ -98,7 +99,7 @@ export const useRecordShowPagePagination = ( objectNameSingular, recordGqlFields, onCompleted: (_, pagination) => { - setTotalCount(pagination?.totalCount ?? 0); + setTotalCountAfter(pagination?.totalCount ?? 0); }, }); @@ -147,6 +148,19 @@ export const useRecordShowPagePagination = ( const objectLabel = capitalize(objectMetadataItem.namePlural); + let totalCount = 0; + + if (totalCountBefore !== 0 && totalCountAfter !== 0) { + totalCount = Math.max(totalCountBefore, totalCountAfter); + } else { + totalCount = + totalCountBefore === 0 + ? totalCountAfter === 0 + ? 1 + : totalCountAfter + : totalCountBefore; + } + const viewNameWithCount = rankFoundInFiew ? `${rankInView + 1} of ${totalCount} in ${objectLabel}` : `${objectLabel} (${totalCount})`;