-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathuseTableLayout.ts
More file actions
59 lines (52 loc) · 1.98 KB
/
Copy pathuseTableLayout.ts
File metadata and controls
59 lines (52 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import { useMemo } from 'react';
import type { DefaultLayout, ColumnPreferences, SlicingPreferences } from 'components/common/EntityDataTable/types';
import useUserLayoutPreferences from './useUserLayoutPreferences';
export type LayoutConfig = {
attributes: ColumnPreferences;
order: Array<string>;
pageSize: number;
slicing?: SlicingPreferences;
sort: DefaultLayout['defaultSort'];
};
const useTableLayout = ({
entityTableId,
layoutVariant,
defaultSort,
defaultSlicing,
defaultPageSize,
}: DefaultLayout): {
isInitialLoading: boolean;
layoutConfig: LayoutConfig;
} => {
const { data: userLayoutPreferences = {}, isInitialLoading } = useUserLayoutPreferences(entityTableId, layoutVariant);
return useMemo(() => {
const hasSlicingPreference = Object.prototype.hasOwnProperty.call(userLayoutPreferences, 'slicing');
return {
layoutConfig: {
attributes: userLayoutPreferences?.attributes,
order: userLayoutPreferences.order,
pageSize: userLayoutPreferences.perPage ?? defaultPageSize,
slicing: hasSlicingPreference ? (userLayoutPreferences.slicing ?? undefined) : defaultSlicing,
sort: userLayoutPreferences.sort ?? defaultSort,
},
isInitialLoading,
};
}, [defaultPageSize, defaultSlicing, defaultSort, isInitialLoading, userLayoutPreferences]);
};
export default useTableLayout;