Skip to content

Commit 8ea4e6a

Browse files
feat: reset table column resizing on ViewBar Cancel button click (#1520)
* feat: reset table column resizing on ViewBar Cancel button click Closes #1500 * Fix according to PR --------- Co-authored-by: Charles Bochet <[email protected]>
1 parent c808eec commit 8ea4e6a

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

front/src/modules/ui/table/table-header/components/TableHeader.tsx

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useCallback } from 'react';
2-
import { useRecoilCallback, useRecoilValue, useSetRecoilState } from 'recoil';
2+
import { useRecoilCallback, useRecoilState, useRecoilValue } from 'recoil';
33

44
import { DropdownRecoilScopeContext } from '@/ui/dropdown/states/recoil-scope-contexts/DropdownRecoilScopeContext';
55
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
66
import { useContextScopeId } from '@/ui/utilities/recoil-scope/hooks/useContextScopeId';
7+
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
78
import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue';
8-
import { ViewBar, type ViewBarProps } from '@/ui/view-bar/components/ViewBar';
9+
import { ViewBar, ViewBarProps } from '@/ui/view-bar/components/ViewBar';
910
import { currentViewIdScopedState } from '@/ui/view-bar/states/currentViewIdScopedState';
1011

1112
import { TableOptionsDropdown } from '../../options/components/TableOptionsDropdown';
@@ -38,14 +39,18 @@ export function TableHeader<SortField>({
3839
const canPersistTableColumns = useRecoilValue(
3940
canPersistTableColumnsScopedFamilySelector([tableScopeId, currentViewId]),
4041
);
41-
const tableColumns = useRecoilScopedValue(
42+
const [tableColumns, setTableColumns] = useRecoilScopedState(
4243
tableColumnsScopedState,
4344
TableRecoilScopeContext,
4445
);
45-
const setSavedTableColumns = useSetRecoilState(
46+
const [savedTableColumns, setSavedTableColumns] = useRecoilState(
4647
savedTableColumnsFamilyState(currentViewId),
4748
);
4849

50+
function handleViewBarReset() {
51+
setTableColumns(savedTableColumns);
52+
}
53+
4954
const handleViewSelect = useRecoilCallback(
5055
({ set, snapshot }) =>
5156
async (viewId: string) => {
@@ -57,11 +62,13 @@ export function TableHeader<SortField>({
5762
[tableScopeId],
5863
);
5964

60-
const handleViewSubmit = async () => {
61-
if (canPersistTableColumns) setSavedTableColumns(tableColumns);
65+
async function handleViewSubmit() {
66+
if (canPersistTableColumns) {
67+
setSavedTableColumns(tableColumns);
68+
}
6269

6370
await onViewSubmit?.();
64-
};
71+
}
6572

6673
const OptionsDropdownButton = useCallback(
6774
() => (
@@ -79,6 +86,7 @@ export function TableHeader<SortField>({
7986
<ViewBar
8087
{...props}
8188
canPersistViewFields={canPersistTableColumns}
89+
onReset={handleViewBarReset}
8290
onViewSelect={handleViewSelect}
8391
onViewSubmit={handleViewSubmit}
8492
OptionsDropdownButton={OptionsDropdownButton}

front/src/modules/ui/view-bar/components/ViewBar.tsx

+10-28
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
import { ComponentProps, type ComponentType, type Context } from 'react';
2-
import { useRecoilValue } from 'recoil';
1+
import type { ComponentProps, ComponentType, Context } from 'react';
32

43
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton';
54
import { TopBar } from '@/ui/top-bar/TopBar';
6-
import { useContextScopeId } from '@/ui/utilities/recoil-scope/hooks/useContextScopeId';
7-
import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue';
85

9-
import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
10-
import { canPersistFiltersScopedFamilySelector } from '../states/selectors/canPersistFiltersScopedFamilySelector';
11-
import { canPersistSortsScopedFamilySelector } from '../states/selectors/canPersistSortsScopedFamilySelector';
126
import { FiltersHotkeyScope } from '../types/FiltersHotkeyScope';
137
import { ViewsHotkeyScope } from '../types/ViewsHotkeyScope';
148

159
import { FilterDropdownButton } from './FilterDropdownButton';
1610
import {
1711
SortDropdownButton,
18-
SortDropdownButtonProps,
12+
type SortDropdownButtonProps,
1913
} from './SortDropdownButton';
2014
import {
2115
UpdateViewButtonGroup,
22-
UpdateViewButtonGroupProps,
16+
type UpdateViewButtonGroupProps,
2317
} from './UpdateViewButtonGroup';
24-
import ViewBarDetails from './ViewBarDetails';
18+
import ViewBarDetails, { type ViewBarDetailsProps } from './ViewBarDetails';
2519
import {
2620
ViewsDropdownButton,
27-
ViewsDropdownButtonProps,
21+
type ViewsDropdownButtonProps,
2822
} from './ViewsDropdownButton';
2923

3024
export type ViewBarProps<SortField> = ComponentProps<'div'> & {
31-
canPersistViewFields?: boolean;
3225
OptionsDropdownButton: ComponentType;
3326
optionsDropdownKey: string;
3427
scopeContext: Context<string | null>;
@@ -37,12 +30,14 @@ export type ViewBarProps<SortField> = ComponentProps<'div'> & {
3730
'defaultViewName' | 'onViewsChange' | 'onViewSelect'
3831
> &
3932
Pick<SortDropdownButtonProps<SortField>, 'availableSorts'> &
33+
Pick<ViewBarDetailsProps, 'canPersistViewFields' | 'onReset'> &
4034
Pick<UpdateViewButtonGroupProps, 'onViewSubmit'>;
4135

4236
export const ViewBar = <SortField,>({
4337
availableSorts,
4438
canPersistViewFields,
4539
defaultViewName,
40+
onReset,
4641
onViewsChange,
4742
onViewSelect,
4843
onViewSubmit,
@@ -51,19 +46,6 @@ export const ViewBar = <SortField,>({
5146
scopeContext,
5247
...props
5348
}: ViewBarProps<SortField>) => {
54-
const recoilScopeId = useContextScopeId(scopeContext);
55-
56-
const currentViewId = useRecoilScopedValue(
57-
currentViewIdScopedState,
58-
scopeContext,
59-
);
60-
const canPersistFilters = useRecoilValue(
61-
canPersistFiltersScopedFamilySelector([recoilScopeId, currentViewId]),
62-
);
63-
const canPersistSorts = useRecoilValue(
64-
canPersistSortsScopedFamilySelector([recoilScopeId, currentViewId]),
65-
);
66-
6749
const { openDropdownButton: openOptionsDropdownButton } = useDropdownButton({
6850
key: optionsDropdownKey,
6951
});
@@ -100,13 +82,13 @@ export const ViewBar = <SortField,>({
10082
}
10183
bottomComponent={
10284
<ViewBarDetails
103-
canPersistView={
104-
canPersistViewFields || canPersistFilters || canPersistSorts
105-
}
85+
canPersistViewFields={canPersistViewFields}
10686
context={scopeContext}
10787
hasFilterButton
88+
onReset={onReset}
10889
rightComponent={
10990
<UpdateViewButtonGroup
91+
canPersistViewFields={canPersistViewFields}
11092
onViewEditModeChange={openOptionsDropdownButton}
11193
onViewSubmit={onViewSubmit}
11294
hotkeyScope={ViewsHotkeyScope.CreateDropdown}

front/src/modules/ui/view-bar/components/ViewBarDetails.tsx

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import type { Context, ReactNode } from 'react';
22
import { useTheme } from '@emotion/react';
33
import styled from '@emotion/styled';
4+
import { useRecoilValue } from 'recoil';
45

56
import {
67
IconArrowNarrowDown,
78
IconArrowNarrowUp,
89
IconPlus,
910
} from '@/ui/icon/index';
11+
import { useContextScopeId } from '@/ui/utilities/recoil-scope/hooks/useContextScopeId';
1012
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
13+
import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue';
1114

1215
import { useRemoveFilter } from '../hooks/useRemoveFilter';
1316
import { availableFiltersScopedState } from '../states/availableFiltersScopedState';
17+
import { currentViewIdScopedState } from '../states/currentViewIdScopedState';
1418
import { filtersScopedState } from '../states/filtersScopedState';
1519
import { isViewBarExpandedScopedState } from '../states/isViewBarExpandedScopedState';
20+
import { canPersistFiltersScopedFamilySelector } from '../states/selectors/canPersistFiltersScopedFamilySelector';
21+
import { canPersistSortsScopedFamilySelector } from '../states/selectors/canPersistSortsScopedFamilySelector';
1622
import { sortsScopedState } from '../states/sortsScopedState';
1723
import { FiltersHotkeyScope } from '../types/FiltersHotkeyScope';
1824
import { SelectedSortType } from '../types/interface';
@@ -21,10 +27,11 @@ import { getOperandLabelShort } from '../utils/getOperandLabel';
2127
import { FilterDropdownButton } from './FilterDropdownButton';
2228
import SortOrFilterChip from './SortOrFilterChip';
2329

24-
type OwnProps = {
25-
canPersistView?: boolean;
30+
export type ViewBarDetailsProps = {
31+
canPersistViewFields?: boolean;
2632
context: Context<string | null>;
2733
hasFilterButton?: boolean;
34+
onReset?: () => void;
2835
rightComponent?: ReactNode;
2936
};
3037

@@ -96,13 +103,18 @@ const StyledAddFilterContainer = styled.div`
96103
`;
97104

98105
function ViewBarDetails<SortField>({
99-
canPersistView,
106+
canPersistViewFields,
100107
context,
101108
hasFilterButton = false,
109+
onReset,
102110
rightComponent,
103-
}: OwnProps) {
111+
}: ViewBarDetailsProps) {
104112
const theme = useTheme();
105113

114+
const recoilScopeId = useContextScopeId(context);
115+
116+
const currentViewId = useRecoilScopedValue(currentViewIdScopedState, context);
117+
106118
const [filters, setFilters] = useRecoilScopedState(
107119
filtersScopedState,
108120
context,
@@ -111,11 +123,20 @@ function ViewBarDetails<SortField>({
111123
availableFiltersScopedState,
112124
context,
113125
);
126+
const canPersistFilters = useRecoilValue(
127+
canPersistFiltersScopedFamilySelector([recoilScopeId, currentViewId]),
128+
);
114129

115130
const [sorts, setSorts] = useRecoilScopedState<SelectedSortType<SortField>[]>(
116131
sortsScopedState,
117132
context,
118133
);
134+
const canPersistSorts = useRecoilValue(
135+
canPersistSortsScopedFamilySelector([recoilScopeId, currentViewId]),
136+
);
137+
138+
const canPersistView =
139+
canPersistViewFields || canPersistFilters || canPersistSorts;
119140

120141
const [isViewBarExpanded] = useRecoilScopedState(
121142
isViewBarExpandedScopedState,
@@ -136,6 +157,7 @@ function ViewBarDetails<SortField>({
136157
const removeFilter = useRemoveFilter(context);
137158

138159
function handleCancelClick() {
160+
onReset?.();
139161
setFilters([]);
140162
setSorts([]);
141163
}
@@ -207,7 +229,7 @@ function ViewBarDetails<SortField>({
207229
</StyledAddFilterContainer>
208230
)}
209231
</StyledFilterContainer>
210-
{filters.length + sorts.length > 0 && (
232+
{(filters.length + sorts.length > 0 || canPersistViewFields) && (
211233
<StyledCancelButton
212234
data-testid="cancel-button"
213235
onClick={handleCancelClick}

0 commit comments

Comments
 (0)