Files
twenty/front/src/modules/companies/table/hooks/useSetCompanyEntityTable.ts
Lucas Bordeau a2ccb643ff Optimize table loading (#866)
* wip

* wip

* Ok

* Deleted unused code

* Fixed lint

* Minor fixes

* Minor fixes

* Minor Fixes

* Minor merge fixes

* Ok

* Fix storybook tests

* Removed console.log

* Fix login

* asd

* Fixed storybook

* Added await

* Fixed await

* Added sleep for failing test

* Fix sleep

* Fix test

* Fix tests

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2023-07-25 11:00:15 -07:00

142 lines
5.4 KiB
TypeScript

import { useLocation } from 'react-router-dom';
import { useRecoilCallback } from 'recoil';
import { companyAccountOwnerFamilyState } from '@/companies/states/companyAccountOwnerFamilyState';
import { companyAddressFamilyState } from '@/companies/states/companyAddressFamilyState';
import { companyCommentCountFamilyState } from '@/companies/states/companyCommentCountFamilyState';
import { companyCreatedAtFamilyState } from '@/companies/states/companyCreatedAtFamilyState';
import { companyDomainNameFamilyState } from '@/companies/states/companyDomainNameFamilyState';
import { companyEmployeesFamilyState } from '@/companies/states/companyEmployeesFamilyState';
import { companyLinkedinUrlFamilyState } from '@/companies/states/companyLinkedinUrlFamilyState';
import { companyNameFamilyState } from '@/companies/states/companyNameFamilyState';
import { GetCompaniesQuery } from '~/generated/graphql';
import { companiesFilters } from '../../../../pages/companies/companies-filters';
import { availableFiltersScopedState } from '../../../ui/filter-n-sort/states/availableFiltersScopedState';
import { useContextScopeId } from '../../../ui/recoil-scope/hooks/useContextScopeId';
import { currentPageLocationState } from '../../../ui/states/currentPageLocationState';
import { useResetTableRowSelection } from '../../../ui/table/hooks/useResetTableRowSelection';
import { entityTableDimensionsState } from '../../../ui/table/states/entityTableDimensionsState';
import { isFetchingEntityTableDataState } from '../../../ui/table/states/isFetchingEntityTableDataState';
import { TableContext } from '../../../ui/table/states/TableContext';
import { tableRowIdsState } from '../../../ui/table/states/tableRowIdsState';
import { companyColumns } from '../components/companyColumns';
export function useSetCompanyEntityTable() {
const resetTableRowSelection = useResetTableRowSelection();
const tableContextScopeId = useContextScopeId(TableContext);
const currentLocation = useLocation().pathname;
return useRecoilCallback(
({ set, snapshot }) =>
(newCompanyArray: GetCompaniesQuery['companies']) => {
for (const company of newCompanyArray) {
const currentName = snapshot
.getLoadable(companyNameFamilyState(company.id))
.valueOrThrow();
if (currentName !== company.name) {
set(companyNameFamilyState(company.id), company.name);
}
const currentDomainName = snapshot
.getLoadable(companyDomainNameFamilyState(company.id))
.valueOrThrow();
if (currentDomainName !== company.domainName) {
set(companyDomainNameFamilyState(company.id), company.domainName);
}
const currentLinkedinUrl = snapshot
.getLoadable(companyLinkedinUrlFamilyState(company.id))
.valueOrThrow();
if (currentLinkedinUrl !== company.linkedinUrl) {
set(
companyLinkedinUrlFamilyState(company.id),
company.linkedinUrl ?? '',
);
}
const currentEmployees = snapshot
.getLoadable(companyEmployeesFamilyState(company.id))
.valueOrThrow();
if (currentEmployees !== company.employees) {
set(
companyEmployeesFamilyState(company.id),
company.employees?.toString() ?? '',
);
}
const currentAddress = snapshot
.getLoadable(companyAddressFamilyState(company.id))
.valueOrThrow();
if (currentAddress !== company.address) {
set(companyAddressFamilyState(company.id), company.address);
}
const currentCommentCount = snapshot
.getLoadable(companyCommentCountFamilyState(company.id))
.valueOrThrow();
if (currentCommentCount !== company._commentThreadCount) {
set(
companyCommentCountFamilyState(company.id),
company._commentThreadCount,
);
}
const currentAccountOwner = snapshot
.getLoadable(companyAccountOwnerFamilyState(company.id))
.valueOrThrow();
if (
JSON.stringify(currentAccountOwner) !==
JSON.stringify(company.accountOwner)
) {
set(
companyAccountOwnerFamilyState(company.id),
company.accountOwner,
);
}
const currentCreatedAt = snapshot
.getLoadable(companyCreatedAtFamilyState(company.id))
.valueOrThrow();
if (currentCreatedAt !== company.createdAt) {
set(companyCreatedAtFamilyState(company.id), company.createdAt);
}
}
const companyIds = newCompanyArray.map((company) => company.id);
set(tableRowIdsState, (currentRowIds) => {
if (JSON.stringify(currentRowIds) !== JSON.stringify(companyIds)) {
return companyIds;
}
return currentRowIds;
});
resetTableRowSelection();
set(entityTableDimensionsState, {
numberOfColumns: companyColumns.length,
numberOfRows: companyIds.length,
});
set(availableFiltersScopedState(tableContextScopeId), companiesFilters);
set(currentPageLocationState, currentLocation);
set(isFetchingEntityTableDataState, false);
},
[resetTableRowSelection, tableContextScopeId, currentLocation],
);
}