Preserve navigation state when adding custom fields (#6399)

@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.
This commit is contained in:
nitin
2024-08-05 20:20:08 +05:30
committed by GitHub
parent 2073d8e6e1
commit eba79d2ea5
3 changed files with 36 additions and 12 deletions

View File

@ -30,10 +30,13 @@ import { UndecoratedLink } from '@/ui/navigation/link/components/UndecoratedLink
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem'; import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { MenuItemNavigate } from '@/ui/navigation/menu-item/components/MenuItemNavigate'; import { MenuItemNavigate } from '@/ui/navigation/menu-item/components/MenuItemNavigate';
import { MenuItemToggle } from '@/ui/navigation/menu-item/components/MenuItemToggle'; 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 { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { ViewFieldsVisibilityDropdownSection } from '@/views/components/ViewFieldsVisibilityDropdownSection'; import { ViewFieldsVisibilityDropdownSection } from '@/views/components/ViewFieldsVisibilityDropdownSection';
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView'; import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { ViewType } from '@/views/types/ViewType'; import { ViewType } from '@/views/types/ViewType';
import { useLocation } from 'react-router-dom';
import { useSetRecoilState } from 'recoil';
type RecordIndexOptionsMenu = 'fields' | 'hiddenFields'; type RecordIndexOptionsMenu = 'fields' | 'hiddenFields';
@ -124,6 +127,11 @@ export const RecordIndexOptionsDropdownContent = ({
recordIndexId, recordIndexId,
}); });
const location = useLocation();
const setNavigationMemorizedUrl = useSetRecoilState(
navigationMemorizedUrlState,
);
return ( return (
<> <>
{!currentMenu && ( {!currentMenu && (
@ -190,7 +198,12 @@ export const RecordIndexOptionsDropdownContent = ({
)} )}
<DropdownMenuSeparator /> <DropdownMenuSeparator />
<UndecoratedLink to={settingsUrl}> <UndecoratedLink
to={settingsUrl}
onClick={() => {
setNavigationMemorizedUrl(location.pathname + location.search);
}}
>
<DropdownMenuItemsContainer> <DropdownMenuItemsContainer>
<MenuItem LeftIcon={IconSettings} text="Edit Fields" /> <MenuItem LeftIcon={IconSettings} text="Edit Fields" />
</DropdownMenuItemsContainer> </DropdownMenuItemsContainer>

View File

@ -1,7 +1,6 @@
import { useCallback, useContext } from 'react'; import { useCallback, useContext } from 'react';
import { Link } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import styled from '@emotion/styled'; import { useRecoilValue, useSetRecoilState } from 'recoil';
import { useRecoilValue } from 'recoil';
import { IconSettings, useIcons } from 'twenty-ui'; import { IconSettings, useIcons } from 'twenty-ui';
import { getObjectSlug } from '@/object-metadata/utils/getObjectSlug'; import { getObjectSlug } from '@/object-metadata/utils/getObjectSlug';
@ -13,7 +12,9 @@ import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefin
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer'; import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator'; import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown'; 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 { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { navigationMemorizedUrlState } from '@/ui/navigation/states/navigationMemorizedUrlState';
export const RecordTableHeaderPlusButtonContent = () => { export const RecordTableHeaderPlusButtonContent = () => {
const { objectMetadataItem } = useContext(RecordTableContext); const { objectMetadataItem } = useContext(RecordTableContext);
@ -34,10 +35,10 @@ export const RecordTableHeaderPlusButtonContent = () => {
[handleColumnVisibilityChange, closeDropdown], [handleColumnVisibilityChange, closeDropdown],
); );
const StyledMenuItemLink = styled(Link)` const location = useLocation();
text-decoration: none; const setNavigationMemorizedUrl = useSetRecoilState(
width: 100%; navigationMemorizedUrlState,
`; );
return ( return (
<> <>
@ -57,11 +58,14 @@ export const RecordTableHeaderPlusButtonContent = () => {
</> </>
)} )}
<DropdownMenuItemsContainer> <DropdownMenuItemsContainer>
<StyledMenuItemLink <UndecoratedLink
to={`/settings/objects/${getObjectSlug(objectMetadataItem)}`} to={`/settings/objects/${getObjectSlug(objectMetadataItem)}`}
onClick={() => {
setNavigationMemorizedUrl(location.pathname + location.search);
}}
> >
<MenuItem LeftIcon={IconSettings} text="Customize fields" /> <MenuItem LeftIcon={IconSettings} text="Customize fields" />
</StyledMenuItemLink> </UndecoratedLink>
</DropdownMenuItemsContainer> </DropdownMenuItemsContainer>
</> </>
); );

View File

@ -1,24 +1,31 @@
import styled from '@emotion/styled';
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import styled from '@emotion/styled';
const StyledUndecoratedLink = styled(Link)` const StyledUndecoratedLink = styled(Link)`
text-decoration: none; text-decoration: none;
width: 100%;
`; `;
type UndecoratedLinkProps = { type UndecoratedLinkProps = {
to: string | number; to: string | number;
children: React.ReactNode; children: React.ReactNode;
replace?: boolean; replace?: boolean;
onClick?: React.MouseEventHandler<HTMLAnchorElement>;
}; };
export const UndecoratedLink = ({ export const UndecoratedLink = ({
children, children,
to, to,
replace = false, replace = false,
onClick,
}: UndecoratedLinkProps) => { }: UndecoratedLinkProps) => {
return ( return (
<StyledUndecoratedLink to={to as string} replace={replace}> <StyledUndecoratedLink
to={to as string}
replace={replace}
onClick={onClick}
>
{children} {children}
</StyledUndecoratedLink> </StyledUndecoratedLink>
); );