Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RecordShowPage Header performances #6440

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { buildShowPageURL } from '@/object-record/record-show/utils/buildShowPag
import { buildIndexTablePageURL } from '@/object-record/record-table/utils/buildIndexTableURL';
import { useQueryVariablesFromActiveFieldsOfViewOrDefaultView } from '@/views/hooks/useQueryVariablesFromActiveFieldsOfViewOrDefaultView';
import { isNonEmptyString } from '@sniptt/guards';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { capitalize } from '~/utils/string/capitalize';

export const useRecordShowPagePagination = (
Expand Down Expand Up @@ -60,55 +60,47 @@ export const useRecordShowPagePagination = (

const cursorFromRequest = currentRecordsPageInfo?.endCursor;

const {
loading: loadingRecordBefore,
records: recordsBefore,
totalCount: totalCountBefore,
} = useFindManyRecords({
skip: loadingCursor,
fetchPolicy: 'network-only',
filter,
orderBy,
cursorFilter: isNonEmptyString(cursorFromRequest)
? {
cursorDirection: 'before',
cursor: cursorFromRequest,
limit: 1,
}
: undefined,
objectNameSingular,
recordGqlFields,
});

const {
loading: loadingRecordAfter,
records: recordsAfter,
totalCount: totalCountAfter,
} = useFindManyRecords({
skip: loadingCursor,
filter,
fetchPolicy: 'network-only',
orderBy,
cursorFilter: cursorFromRequest
? {
cursorDirection: 'after',
cursor: cursorFromRequest,
limit: 1,
}
: undefined,
objectNameSingular,
recordGqlFields,
});
const [totalCount, setTotalCount] = useState<number>(0);

const [totalCount, setTotalCount] = useState(
Math.max(totalCountBefore ?? 0, totalCountAfter ?? 0),
);
const { loading: loadingRecordBefore, records: recordsBefore } =
useFindManyRecords({
skip: loadingCursor,
fetchPolicy: 'network-only',
filter,
orderBy,
cursorFilter: isNonEmptyString(cursorFromRequest)
? {
cursorDirection: 'before',
cursor: cursorFromRequest,
limit: 1,
}
: undefined,
objectNameSingular,
recordGqlFields,
onCompleted: (_, pagination) => {
setTotalCount(pagination?.totalCount ?? 0);
},
});

useEffect(() => {
if (totalCountBefore !== undefined || totalCountAfter !== undefined) {
setTotalCount(Math.max(totalCountBefore ?? 0, totalCountAfter ?? 0));
}
}, [totalCountBefore, totalCountAfter]);
const { loading: loadingRecordAfter, records: recordsAfter } =
useFindManyRecords({
skip: loadingCursor,
filter,
fetchPolicy: 'network-only',
orderBy,
cursorFilter: cursorFromRequest
? {
cursorDirection: 'after',
cursor: cursorFromRequest,
limit: 1,
}
: undefined,
objectNameSingular,
recordGqlFields,
onCompleted: (_, pagination) => {
setTotalCount(pagination?.totalCount ?? 0);
},
});

const loading = loadingRecordAfter || loadingRecordBefore || loadingCursor;

Expand Down
31 changes: 6 additions & 25 deletions packages/twenty-front/src/pages/object-record/RecordShowPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { useParams } from 'react-router-dom';
import { TimelineActivityContext } from '@/activities/timelineActivities/contexts/TimelineActivityContext';
import { RecordShowContainer } from '@/object-record/record-show/components/RecordShowContainer';
import { useRecordShowPage } from '@/object-record/record-show/hooks/useRecordShowPage';
import { useRecordShowPagePagination } from '@/object-record/record-show/hooks/useRecordShowPagePagination';
import { RecordValueSetterEffect } from '@/object-record/record-store/components/RecordValueSetterEffect';
import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext';
import { PageBody } from '@/ui/layout/page/PageBody';
import { PageContainer } from '@/ui/layout/page/PageContainer';
import { PageFavoriteButton } from '@/ui/layout/page/PageFavoriteButton';
import { PageHeader } from '@/ui/layout/page/PageHeader';
import { ShowPageAddButton } from '@/ui/layout/show-page/components/ShowPageAddButton';
import { ShowPageMoreButton } from '@/ui/layout/show-page/components/ShowPageMoreButton';
import { PageTitle } from '@/ui/utilities/page-title/PageTitle';
import { RecordShowPageHeader } from '~/pages/object-record/RecordShowPageHeader';

export const RecordShowPage = () => {
const parameters = useParams<{
Expand All @@ -36,33 +35,15 @@ export const RecordShowPage = () => {
parameters.objectRecordId ?? '',
);

const {
viewName,
hasPreviousRecord,
hasNextRecord,
navigateToPreviousRecord,
navigateToNextRecord,
navigateToIndexView,
} = useRecordShowPagePagination(
parameters.objectNameSingular ?? '',
parameters.objectRecordId ?? '',
);

return (
<RecordFieldValueSelectorContextProvider>
<RecordValueSetterEffect recordId={objectRecordId} />
<PageContainer>
<PageTitle title={pageTitle} />
<PageHeader
title={viewName}
hasPaginationButtons
hasClosePageButton
onClosePage={navigateToIndexView}
hasPreviousRecord={hasPreviousRecord}
navigateToPreviousRecord={navigateToPreviousRecord}
hasNextRecord={hasNextRecord}
navigateToNextRecord={navigateToNextRecord}
Icon={headerIcon}
<RecordShowPageHeader
objectNameSingular={objectNameSingular}
objectRecordId={objectRecordId}
headerIcon={headerIcon}
>
<>
<PageFavoriteButton
Expand All @@ -82,7 +63,7 @@ export const RecordShowPage = () => {
objectNameSingular={objectNameSingular}
/>
</>
</PageHeader>
</RecordShowPageHeader>
<PageBody>
<TimelineActivityContext.Provider
value={{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useRecordShowPage } from '@/object-record/record-show/hooks/useRecordShowPage';
import { useRecordShowPagePagination } from '@/object-record/record-show/hooks/useRecordShowPagePagination';
import { PageHeader } from '@/ui/layout/page/PageHeader';

export const RecordShowPageHeader = ({
objectNameSingular,
objectRecordId,
children,
}: {
objectNameSingular: string;
objectRecordId: string;
headerIcon: React.ComponentType;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: The headerIcon prop is defined but not used in the component. It should be removed from the props list.

children?: React.ReactNode;
}) => {
const {
viewName,
hasPreviousRecord,
hasNextRecord,
navigateToPreviousRecord,
navigateToNextRecord,
navigateToIndexView,
} = useRecordShowPagePagination(objectNameSingular, objectRecordId);

const { headerIcon } = useRecordShowPage(objectNameSingular, objectRecordId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic: Redundant call to useRecordShowPage for headerIcon. This can be optimized by passing headerIcon directly as a prop from the parent component.


return (
<PageHeader
title={viewName}
hasPaginationButtons
hasClosePageButton
onClosePage={navigateToIndexView}
hasPreviousRecord={hasPreviousRecord}
navigateToPreviousRecord={navigateToPreviousRecord}
hasNextRecord={hasNextRecord}
navigateToNextRecord={navigateToNextRecord}
Icon={headerIcon}
>
{children}
</PageHeader>
);
};
Loading