This PR fixes minor bugs on advanced filters : - We couldn't close the advanced filter dropdown after removing a rule, because the rule options dropdown wasn't closed, so focus dropdown id was in a corrupted state. - The text filter input in filter dropdown and the search input were the same component, which was causing conflicts with state management but this conflict didn't happen with the simple filter dropdown implementation, the advanced filter dropdown brought this bug to light. - The chevron down icon disappeared from the filter update button group, this PR fixes it. Fixes https://github.com/twentyhq/core-team-issues/issues/557 Fixes https://github.com/twentyhq/core-team-issues/issues/558
148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
import styled from '@emotion/styled';
|
|
import {
|
|
Button,
|
|
ButtonGroup,
|
|
IconButton,
|
|
IconChevronDown,
|
|
IconPlus,
|
|
MenuItem,
|
|
} from 'twenty-ui';
|
|
|
|
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
|
|
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';
|
|
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
|
|
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
|
|
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
|
|
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
|
|
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
|
|
import { UPDATE_VIEW_BUTTON_DROPDOWN_ID } from '@/views/constants/UpdateViewButtonDropdownId';
|
|
import { useViewFromQueryParams } from '@/views/hooks/internal/useViewFromQueryParams';
|
|
import { useAreViewFilterGroupsDifferentFromRecordFilterGroups } from '@/views/hooks/useAreViewFilterGroupsDifferentFromRecordFilterGroups';
|
|
import { useAreViewFiltersDifferentFromRecordFilters } from '@/views/hooks/useAreViewFiltersDifferentFromRecordFilters';
|
|
import { useAreViewSortsDifferentFromRecordSorts } from '@/views/hooks/useAreViewSortsDifferentFromRecordSorts';
|
|
import { useGetCurrentViewOnly } from '@/views/hooks/useGetCurrentViewOnly';
|
|
import { useSaveCurrentViewFiltersAndSorts } from '@/views/hooks/useSaveCurrentViewFiltersAndSorts';
|
|
import { VIEW_PICKER_DROPDOWN_ID } from '@/views/view-picker/constants/ViewPickerDropdownId';
|
|
import { useViewPickerMode } from '@/views/view-picker/hooks/useViewPickerMode';
|
|
import { viewPickerReferenceViewIdComponentState } from '@/views/view-picker/states/viewPickerReferenceViewIdComponentState';
|
|
import { t } from '@lingui/core/macro';
|
|
|
|
const StyledContainer = styled.div`
|
|
border-radius: ${({ theme }) => theme.border.radius.md};
|
|
display: inline-flex;
|
|
margin-right: ${({ theme }) => theme.spacing(2)};
|
|
position: relative;
|
|
`;
|
|
|
|
export type UpdateViewButtonGroupProps = {
|
|
hotkeyScope: HotkeyScope;
|
|
};
|
|
|
|
export const UpdateViewButtonGroup = ({
|
|
hotkeyScope,
|
|
}: UpdateViewButtonGroupProps) => {
|
|
const { saveCurrentViewFilterAndSorts } = useSaveCurrentViewFiltersAndSorts();
|
|
|
|
const { setViewPickerMode } = useViewPickerMode();
|
|
|
|
const currentViewId = useRecoilComponentValueV2(
|
|
contextStoreCurrentViewIdComponentState,
|
|
);
|
|
|
|
const { closeDropdown: closeUpdateViewButtonDropdown } = useDropdown(
|
|
UPDATE_VIEW_BUTTON_DROPDOWN_ID,
|
|
);
|
|
const { openDropdown: openViewPickerDropdown } = useDropdown(
|
|
VIEW_PICKER_DROPDOWN_ID,
|
|
);
|
|
const { currentView } = useGetCurrentViewOnly();
|
|
|
|
const setViewPickerReferenceViewId = useSetRecoilComponentStateV2(
|
|
viewPickerReferenceViewIdComponentState,
|
|
);
|
|
|
|
const openViewPickerInCreateMode = () => {
|
|
if (!currentViewId) {
|
|
return;
|
|
}
|
|
|
|
openViewPickerDropdown();
|
|
setViewPickerReferenceViewId(currentViewId);
|
|
setViewPickerMode('create-from-current');
|
|
|
|
closeUpdateViewButtonDropdown();
|
|
};
|
|
|
|
const handleCreateViewClick = () => {
|
|
openViewPickerInCreateMode();
|
|
};
|
|
|
|
const handleSaveAsNewViewClick = () => {
|
|
openViewPickerInCreateMode();
|
|
};
|
|
|
|
const handleUpdateViewClick = async () => {
|
|
await saveCurrentViewFilterAndSorts();
|
|
};
|
|
|
|
const { hasFiltersQueryParams } = useViewFromQueryParams();
|
|
|
|
const { viewFilterGroupsAreDifferentFromRecordFilterGroups } =
|
|
useAreViewFilterGroupsDifferentFromRecordFilterGroups();
|
|
|
|
const { viewFiltersAreDifferentFromRecordFilters } =
|
|
useAreViewFiltersDifferentFromRecordFilters();
|
|
|
|
const { viewSortsAreDifferentFromRecordSorts } =
|
|
useAreViewSortsDifferentFromRecordSorts();
|
|
|
|
const canShowButton =
|
|
(viewFiltersAreDifferentFromRecordFilters ||
|
|
viewSortsAreDifferentFromRecordSorts ||
|
|
viewFilterGroupsAreDifferentFromRecordFilterGroups) &&
|
|
!hasFiltersQueryParams;
|
|
|
|
if (!canShowButton) {
|
|
return <></>;
|
|
}
|
|
|
|
return (
|
|
<StyledContainer>
|
|
{currentView?.key !== 'INDEX' ? (
|
|
<ButtonGroup size="small" accent="blue">
|
|
<Button title="Update view" onClick={handleUpdateViewClick} />
|
|
<Dropdown
|
|
dropdownId={UPDATE_VIEW_BUTTON_DROPDOWN_ID}
|
|
dropdownHotkeyScope={hotkeyScope}
|
|
clickableComponent={
|
|
<IconButton
|
|
size="small"
|
|
accent="blue"
|
|
Icon={IconChevronDown}
|
|
position="right"
|
|
/>
|
|
}
|
|
dropdownComponents={
|
|
<DropdownMenuItemsContainer>
|
|
<MenuItem
|
|
onClick={handleCreateViewClick}
|
|
LeftIcon={IconPlus}
|
|
text={t`Create view`}
|
|
/>
|
|
</DropdownMenuItemsContainer>
|
|
}
|
|
/>
|
|
</ButtonGroup>
|
|
) : (
|
|
<Button
|
|
title={t`Save as new view`}
|
|
onClick={handleSaveAsNewViewClick}
|
|
accent="blue"
|
|
size="small"
|
|
variant="secondary"
|
|
/>
|
|
)}
|
|
</StyledContainer>
|
|
);
|
|
};
|