Skip to content

Commit

Permalink
fix: deactivated object should get fallback when going back from sett…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
Nabhag8848 committed Aug 18, 2024
1 parent c490435 commit e39b6c4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
13 changes: 3 additions & 10 deletions packages/twenty-front/src/hooks/useDefaultHomePagePath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { isDefined } from '~/utils/isDefined';

export const useDefaultHomePagePath = () => {
const currentUser = useRecoilValue(currentUserState);
const { activeObjectMetadataItems } = useFilteredObjectMetadataItems();
const { activeObjectMetadataItems, alphaSortedActiveObjectMetadataItems } =
useFilteredObjectMetadataItems();
const { records } = usePrefetchedData(PrefetchKey.AllViews);
const { lastVisitedObjectMetadataId } = useLastVisitedPageOrView();
let objectMetadata: {
Expand All @@ -32,15 +33,7 @@ export const useDefaultHomePagePath = () => {
};

const getFirstObjectInfo = () => {
const [metadata] = activeObjectMetadataItems.sort((a, b) => {
if (a.nameSingular < b.nameSingular) {
return -1;
}
if (a.nameSingular > b.nameSingular) {
return 1;
}
return 0;
});
const [metadata] = alphaSortedActiveObjectMetadataItems;

const view = getViewMatchingObjectId(metadata.id);
return { metadata, view };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { lastVisitedPageOrViewStateSelector } from '@/navigation/states/selectors/lastVisitedPageOrViewStateSelector';
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
import { navigationMemorizedUrlState } from '@/ui/navigation/states/navigationMemorizedUrlState';
import { extractComponentState } from '@/ui/utilities/state/component-state/utils/extractComponentState';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';

export const useLastVisitedPageOrView = () => {
const currentWorkspace = useRecoilValue(currentWorkspaceState);
Expand All @@ -12,11 +14,44 @@ export const useLastVisitedPageOrView = () => {
scopeId,
);
const [currentPages, setCurrentPages] = useRecoilState(currentPagesState);
const { findActiveObjectMetadataItemBySlug } =
useFilteredObjectMetadataItems();
const {
findActiveObjectMetadataItemBySlug,
alphaSortedActiveObjectMetadataItems,
} = useFilteredObjectMetadataItems();

const resetNavigationMemorizedUrl = useSetRecoilState(
navigationMemorizedUrlState,
);

const lastVisitedObjectMetadataId = currentPages?.['DEFAULT'] ?? null;

const removeMatchingIdInCaseLastVisited = ({
objectMetadataId,
}: {
objectMetadataId: string;
}) => {
const isDeactivateDefault = isDeeplyEqual(
lastVisitedObjectMetadataId,
objectMetadataId,
);

const [newFallbackObjectMetadataItem] =
alphaSortedActiveObjectMetadataItems.filter(
(item) => item.id !== objectMetadataId,
);

setCurrentPages({
...(isDeactivateDefault && { DEFAULT: newFallbackObjectMetadataItem.id }),
[objectMetadataId]: undefined,
});

if (isDeactivateDefault) {
resetNavigationMemorizedUrl(
`/objects/${newFallbackObjectMetadataItem.namePlural}`,
);
}
};

const setLastVisitedObjectOrView = (
{
objectMetadataId,
Expand Down Expand Up @@ -55,5 +90,6 @@ export const useLastVisitedPageOrView = () => {
setLastVisitedObjectOrView,
getLastVisitedViewId,
getLastVisitedViewIdFromObjectId,
removeMatchingIdInCaseLastVisited,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ export const useFilteredObjectMetadataItems = () => {
const activeObjectMetadataItems = objectMetadataItems.filter(
({ isActive, isSystem }) => isActive && !isSystem,
);

const alphaSortedActiveObjectMetadataItems = activeObjectMetadataItems.sort(
(a, b) => {
if (a.nameSingular < b.nameSingular) {
return -1;
}
if (a.nameSingular > b.nameSingular) {
return 1;
}
return 0;
},
);

const inactiveObjectMetadataItems = objectMetadataItems.filter(
({ isActive, isSystem }) => !isActive && !isSystem,
);
Expand Down Expand Up @@ -37,5 +50,6 @@ export const useFilteredObjectMetadataItems = () => {
findObjectMetadataItemByNamePlural,
inactiveObjectMetadataItems,
objectMetadataItems,
alphaSortedActiveObjectMetadataItems,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { IconArchive, IconDotsVertical, IconPencil, useIcons } from 'twenty-ui';

import { useLastVisitedPageOrView } from '@/navigation/hooks/useLastVisitedPageOrView';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { SettingsSummaryCard } from '@/settings/components/SettingsSummaryCard';
import { SettingsDataModelObjectTypeTag } from '@/settings/data-model/objects/SettingsDataModelObjectTypeTag';
Expand Down Expand Up @@ -38,15 +39,18 @@ export const SettingsObjectSummaryCard = ({
const theme = useTheme();
const { getIcon } = useIcons();
const Icon = getIcon(iconKey);
const objectMetadataId = objectMetadataItem.id;

const { closeDropdown } = useDropdown(dropdownId);
const { removeMatchingIdInCaseLastVisited } = useLastVisitedPageOrView();

const handleEdit = () => {
onEdit();
closeDropdown();
};

const handleDeactivate = () => {
removeMatchingIdInCaseLastVisited({ objectMetadataId });
onDeactivate();
closeDropdown();
};
Expand Down

0 comments on commit e39b6c4

Please sign in to comment.