Skip to content

Commit

Permalink
Preserve navigation state when adding custom fields (#6399)
Browse files Browse the repository at this point in the history
@lucasbordeau
Issue #6374 
Fixed the navigation state issue! I also found and resolved a similar
bug with the "Edit Fields" functionality. The
`setNavigationMemorizedUrl` state now correctly updates on navigation to
settings, ensuring users return to the correct page.

Please review.
  • Loading branch information
ehconitin authored Aug 5, 2024
1 parent 2073d8e commit eba79d2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ import { UndecoratedLink } from '@/ui/navigation/link/components/UndecoratedLink
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { MenuItemNavigate } from '@/ui/navigation/menu-item/components/MenuItemNavigate';
import { MenuItemToggle } from '@/ui/navigation/menu-item/components/MenuItemToggle';
import { navigationMemorizedUrlState } from '@/ui/navigation/states/navigationMemorizedUrlState';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { ViewFieldsVisibilityDropdownSection } from '@/views/components/ViewFieldsVisibilityDropdownSection';
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { ViewType } from '@/views/types/ViewType';
import { useLocation } from 'react-router-dom';
import { useSetRecoilState } from 'recoil';

type RecordIndexOptionsMenu = 'fields' | 'hiddenFields';

Expand Down Expand Up @@ -124,6 +127,11 @@ export const RecordIndexOptionsDropdownContent = ({
recordIndexId,
});

const location = useLocation();
const setNavigationMemorizedUrl = useSetRecoilState(
navigationMemorizedUrlState,
);

return (
<>
{!currentMenu && (
Expand Down Expand Up @@ -190,7 +198,12 @@ export const RecordIndexOptionsDropdownContent = ({
)}
<DropdownMenuSeparator />

<UndecoratedLink to={settingsUrl}>
<UndecoratedLink
to={settingsUrl}
onClick={() => {
setNavigationMemorizedUrl(location.pathname + location.search);
}}
>
<DropdownMenuItemsContainer>
<MenuItem LeftIcon={IconSettings} text="Edit Fields" />
</DropdownMenuItemsContainer>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback, useContext } from 'react';
import { Link } from 'react-router-dom';
import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';
import { useLocation } from 'react-router-dom';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { IconSettings, useIcons } from 'twenty-ui';

import { getObjectSlug } from '@/object-metadata/utils/getObjectSlug';
Expand All @@ -13,7 +12,9 @@ import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefin
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { UndecoratedLink } from '@/ui/navigation/link/components/UndecoratedLink';
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { navigationMemorizedUrlState } from '@/ui/navigation/states/navigationMemorizedUrlState';

export const RecordTableHeaderPlusButtonContent = () => {
const { objectMetadataItem } = useContext(RecordTableContext);
Expand All @@ -34,10 +35,10 @@ export const RecordTableHeaderPlusButtonContent = () => {
[handleColumnVisibilityChange, closeDropdown],
);

const StyledMenuItemLink = styled(Link)`
text-decoration: none;
width: 100%;
`;
const location = useLocation();
const setNavigationMemorizedUrl = useSetRecoilState(
navigationMemorizedUrlState,
);

return (
<>
Expand All @@ -57,11 +58,14 @@ export const RecordTableHeaderPlusButtonContent = () => {
</>
)}
<DropdownMenuItemsContainer>
<StyledMenuItemLink
<UndecoratedLink
to={`/settings/objects/${getObjectSlug(objectMetadataItem)}`}
onClick={() => {
setNavigationMemorizedUrl(location.pathname + location.search);
}}
>
<MenuItem LeftIcon={IconSettings} text="Customize fields" />
</StyledMenuItemLink>
</UndecoratedLink>
</DropdownMenuItemsContainer>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import styled from '@emotion/styled';
import React from 'react';
import { Link } from 'react-router-dom';
import styled from '@emotion/styled';

const StyledUndecoratedLink = styled(Link)`
text-decoration: none;
width: 100%;
`;

type UndecoratedLinkProps = {
to: string | number;
children: React.ReactNode;
replace?: boolean;
onClick?: React.MouseEventHandler<HTMLAnchorElement>;
};

export const UndecoratedLink = ({
children,
to,
replace = false,
onClick,
}: UndecoratedLinkProps) => {
return (
<StyledUndecoratedLink to={to as string} replace={replace}>
<StyledUndecoratedLink
to={to as string}
replace={replace}
onClick={onClick}
>
{children}
</StyledUndecoratedLink>
);
Expand Down

0 comments on commit eba79d2

Please sign in to comment.