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

feat: record group fetch more #8868

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -84,6 +84,8 @@ export const useFindManyParams = (
...recordGroupFilter,
},
orderBy,
// If we have a current record group definition, we only want to fetch 8 records by page
...(currentRecordGroupDefinition ? { limit: 8 } : {}),
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider making the 8 record limit configurable via a constant rather than hardcoding the value

};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';
import { createComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/utils/createComponentFamilyStateV2';
import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewComponentInstanceContext';

export const recordIndexHasFetchedAllRecordsByGroupComponentState =
createComponentFamilyStateV2<boolean, RecordGroupDefinition['id']>({
key: 'recordIndexHasFetchedAllRecordsByGroupComponentState',
componentInstanceContext: ViewComponentInstanceContext,
defaultValue: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';
import { createComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/utils/createComponentFamilyStateV2';
import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewComponentInstanceContext';

export const recordIndexShouldFetchMoreRecordsByGroupComponentState =
createComponentFamilyStateV2<boolean, RecordGroupDefinition['id']>({
key: 'recordIndexShouldFetchMoreRecordsByGroupComponentState',
componentInstanceContext: ViewComponentInstanceContext,
defaultValue: false,
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { useRecoilState } from 'recoil';
import { lastShowPageRecordIdState } from '@/object-record/record-field/states/lastShowPageRecordId';
import { useCurrentRecordGroupId } from '@/object-record/record-group/hooks/useCurrentRecordGroupId';
import { useLoadRecordIndexTable } from '@/object-record/record-index/hooks/useLoadRecordIndexTable';
import { recordIndexHasFetchedAllRecordsByGroupComponentState } from '@/object-record/record-index/states/recordIndexHasFetchedAllRecordsByGroupComponentState';
import { recordIndexShouldFetchMoreRecordsByGroupComponentState } from '@/object-record/record-index/states/recordIndexShouldFetchMoreRecordsByGroupComponentState';
import { ROW_HEIGHT } from '@/object-record/record-table/constants/RowHeight';
import { RecordTableContext } from '@/object-record/record-table/contexts/RecordTableContext';
import { hasRecordTableFetchedAllRecordsComponentStateV2 } from '@/object-record/record-table/states/hasRecordTableFetchedAllRecordsComponentStateV2';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import { useRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentFamilyStateV2';
import { useSetRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentFamilyStateV2';
import { isNonEmptyString } from '@sniptt/guards';
import { useScrollToPosition } from '~/hooks/useScrollToPosition';

Expand All @@ -18,12 +20,25 @@ export const RecordTableRecordGroupBodyEffect = () => {

const [hasInitializedScroll, setHasInitializedScroll] = useState(false);

const { records, totalCount, setRecordTableData, loading, hasNextPage } =
useLoadRecordIndexTable(objectNameSingular);
const [shouldFetchMoreRecords, setShouldFetchMoreRecords] =
useRecoilComponentFamilyStateV2(
recordIndexShouldFetchMoreRecordsByGroupComponentState,
recordGroupId,
);

const {
fetchMoreRecords,
records,
totalCount,
setRecordTableData,
loading,
hasNextPage,
} = useLoadRecordIndexTable(objectNameSingular);

const setHasRecordTableFetchedAllRecordsComponents =
useSetRecoilComponentStateV2(
hasRecordTableFetchedAllRecordsComponentStateV2,
const setHasRecordFetchedAllRecordsComponents =
useSetRecoilComponentFamilyStateV2(
recordIndexHasFetchedAllRecordsByGroupComponentState,
recordGroupId,
);

const [lastShowPageRecordId] = useRecoilState(lastShowPageRecordIdState);
Expand Down Expand Up @@ -65,8 +80,16 @@ export const RecordTableRecordGroupBodyEffect = () => {
useEffect(() => {
const allRecordsHaveBeenFetched = !hasNextPage;

setHasRecordTableFetchedAllRecordsComponents(allRecordsHaveBeenFetched);
}, [hasNextPage, setHasRecordTableFetchedAllRecordsComponents]);
setHasRecordFetchedAllRecordsComponents(allRecordsHaveBeenFetched);
}, [hasNextPage, setHasRecordFetchedAllRecordsComponents]);

useEffect(() => {
if (shouldFetchMoreRecords) {
fetchMoreRecords().finally(() => {
setShouldFetchMoreRecords(false);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Missing error handling for failed fetch operations. The state should be reset even if the fetch fails.

}, [fetchMoreRecords, setShouldFetchMoreRecords, shouldFetchMoreRecords]);

return <></>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RecordTableBodyLoading } from '@/object-record/record-table/record-tabl
import { RecordTableBodyRecordGroupDragDropContextProvider } from '@/object-record/record-table/record-table-body/components/RecordTableBodyRecordGroupDragDropContextProvider';
import { RecordTablePendingRow } from '@/object-record/record-table/record-table-row/components/RecordTablePendingRow';
import { RecordTableRecordGroupSection } from '@/object-record/record-table/record-table-section/components/RecordTableRecordGroupSection';
import { RecordTableRecordGroupSectionLoadMore } from '@/object-record/record-table/record-table-section/components/RecordTableRecordGroupSectionLoadMore';
import { isRecordTableInitialLoadingComponentState } from '@/object-record/record-table/states/isRecordTableInitialLoadingComponentState';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';

Expand Down Expand Up @@ -40,6 +41,7 @@ export const RecordTableRecordGroupsBody = () => {
<RecordTableBodyDroppable recordGroupId={recordGroupId}>
<RecordTableRecordGroupSection />
<RecordTableRecordGroupRows />
<RecordTableRecordGroupSectionLoadMore />
</RecordTableBodyDroppable>
</RecordGroupContext.Provider>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const RecordTableTd = ({
hasRightBorder = true,
hasBottomBorder = true,
width,
colSpan,
...dragHandleProps
}: {
className?: string;
Expand All @@ -74,6 +75,7 @@ export const RecordTableTd = ({
hasBottomBorder?: boolean;
left?: number;
width?: number;
colSpan?: number;
} & (Partial<DraggableProvidedDragHandleProps> | null)) => {
const { theme } = useContext(ThemeContext);

Expand All @@ -97,6 +99,7 @@ export const RecordTableTd = ({
hasRightBorder={hasRightBorder}
hasBottomBorder={hasBottomBorder}
width={width}
colSpan={colSpan}
// eslint-disable-next-line react/jsx-props-no-spreading
{...dragHandleProps}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import styled from '@emotion/styled';

import { RecordTableTd } from '@/object-record/record-table/record-table-cell/components/RecordTableTd';
import { visibleTableColumnsComponentSelector } from '@/object-record/record-table/states/selectors/visibleTableColumnsComponentSelector';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useTheme } from '@emotion/react';
import { IconComponent } from 'twenty-ui';

const StyledTrContainer = styled.tr`
cursor: pointer;
`;

const StyledIconContainer = styled(RecordTableTd)`
border-right: none;
color: ${({ theme }) => theme.font.color.secondary};
text-align: center;
vertical-align: middle;
padding-top: 3px;
`;

const StyledRecordTableTdTextContainer = styled(RecordTableTd)`
border-right: none;
height: 32px;
`;

const StyledText = styled.span`
color: ${({ theme }) => theme.font.color.tertiary};
margin-left: ${({ theme }) => theme.spacing(2)};
font-size: ${({ theme }) => theme.font.size.md};
text-align: center;
vertical-align: middle;
`;

const StyledEmptyTd = styled.td`
border-bottom: 1px solid ${({ theme }) => theme.border.color.light};
`;

type RecordTableActionRowProps = {
LeftIcon: IconComponent;
text: string;
onClick?: (event?: React.MouseEvent<HTMLTableRowElement>) => void;
};
Comment on lines +38 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: onClick handler should be required since this is an interactive component - remove the optional '?' from the type definition


export const RecordTableActionRow = ({
LeftIcon,
text,
onClick,
}: RecordTableActionRowProps) => {
const theme = useTheme();

const visibleColumns = useRecoilComponentValueV2(
visibleTableColumnsComponentSelector,
);

return (
<StyledTrContainer onClick={onClick}>
<td aria-hidden />
<StyledIconContainer>
<LeftIcon size={theme.icon.size.sm} color={theme.font.color.tertiary} />
</StyledIconContainer>
<StyledRecordTableTdTextContainer colSpan={visibleColumns.length}>
<StyledText>{text}</StyledText>
</StyledRecordTableTdTextContainer>
<StyledEmptyTd />
<StyledEmptyTd />
</StyledTrContainer>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useCurrentRecordGroupId } from '@/object-record/record-group/hooks/useC
import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/states/recordGroupDefinitionFamilyState';
import { RecordGroupDefinitionType } from '@/object-record/record-group/types/RecordGroupDefinition';
import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
import { RecordTableTd } from '@/object-record/record-table/record-table-cell/components/RecordTableTd';
import { isRecordGroupTableSectionToggledComponentState } from '@/object-record/record-table/record-table-section/states/isRecordGroupTableSectionToggledComponentState';
import { visibleTableColumnsComponentSelector } from '@/object-record/record-table/states/selectors/visibleTableColumnsComponentSelector';
import { useRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentFamilyStateV2';
Expand All @@ -19,8 +20,8 @@ const StyledTrContainer = styled.tr`
cursor: pointer;
`;

const StyledChevronContainer = styled.td`
border-bottom: 1px solid ${({ theme }) => theme.border.color.light};
const StyledChevronContainer = styled(RecordTableTd)`
border-right: none;
color: ${({ theme }) => theme.font.color.secondary};
text-align: center;
vertical-align: middle;
Expand All @@ -33,10 +34,9 @@ const StyledTotalRow = styled.span`
vertical-align: middle;
`;

const StyledRecordGroupSection = styled.td`
border-bottom: 1px solid ${({ theme }) => theme.border.color.light};
padding-bottom: 6px;
padding-top: 6px;
const StyledRecordGroupSection = styled(RecordTableTd)`
border-right: none;
height: 32px;
`;

const StyledEmptyTd = styled.td`
Expand Down Expand Up @@ -79,7 +79,7 @@ export const RecordTableRecordGroupSection = () => {

return (
<StyledTrContainer onClick={handleDropdownToggle}>
<td aria-hidden></td>
<td aria-hidden />
<StyledChevronContainer>
<motion.span
animate={{ rotate: isRecordGroupTableSectionToggled ? 180 : 0 }}
Expand Down Expand Up @@ -108,8 +108,8 @@ export const RecordTableRecordGroupSection = () => {
/>
<StyledTotalRow>{recordIdsByGroup.length}</StyledTotalRow>
Copy link
Contributor

Choose a reason for hiding this comment

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

style: recordIdsByGroup.length may not reflect total records when paginated. Consider showing total count instead

</StyledRecordGroupSection>
<StyledEmptyTd></StyledEmptyTd>
<StyledEmptyTd></StyledEmptyTd>
<StyledEmptyTd />
<StyledEmptyTd />
Comment on lines +111 to +112
Copy link
Contributor

Choose a reason for hiding this comment

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

style: empty td elements should have aria-hidden for better accessibility

</StyledTrContainer>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useCurrentRecordGroupId } from '@/object-record/record-group/hooks/useCurrentRecordGroupId';
import { recordIndexHasFetchedAllRecordsByGroupComponentState } from '@/object-record/record-index/states/recordIndexHasFetchedAllRecordsByGroupComponentState';
import { recordIndexShouldFetchMoreRecordsByGroupComponentState } from '@/object-record/record-index/states/recordIndexShouldFetchMoreRecordsByGroupComponentState';
import { RecordTableActionRow } from '@/object-record/record-table/record-table-row/components/RecordTableActionRow';
import { useRecoilComponentFamilyValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentFamilyValueV2';
import { useSetRecoilComponentFamilyStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentFamilyStateV2';
import { IconArrowDown } from 'twenty-ui';

export const RecordTableRecordGroupSectionLoadMore = () => {
const currentRecordGroupId = useCurrentRecordGroupId();

const hasFetchedAllRecords = useRecoilComponentFamilyValueV2(
recordIndexHasFetchedAllRecordsByGroupComponentState,
currentRecordGroupId,
);

const setShouldFetchMoreRecords = useSetRecoilComponentFamilyStateV2(
recordIndexShouldFetchMoreRecordsByGroupComponentState,
currentRecordGroupId,
);

const handleLoadMore = () => {
setShouldFetchMoreRecords(true);
};

if (hasFetchedAllRecords) {
return null;
}

return (
<RecordTableActionRow
LeftIcon={IconArrowDown}
text="Load more"
onClick={handleLoadMore}
/>
);
};
Loading