Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Cookie table settings should be made persistent #134

Merged
merged 50 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
53b2adf
Update package-lock
amovar18 Aug 22, 2023
6712906
Move cookieStore to new folder.
amovar18 Aug 22, 2023
fb38efd
Add preference store.
amovar18 Aug 22, 2023
e93eb94
Update cookieStore references.
amovar18 Aug 22, 2023
8bb60a1
Move cookieStore tests to cookieStore folder.
amovar18 Aug 22, 2023
d9c8025
Add ability to store preferences in the storage.
amovar18 Aug 22, 2023
edc7bda
Update types.
amovar18 Aug 23, 2023
00b7451
Set persistence of selectedFrame.
amovar18 Aug 23, 2023
48ea605
Make column sorting persistent
amovar18 Aug 23, 2023
cb33a5d
Make column visibility preference persistent
amovar18 Aug 24, 2023
cd67bdf
Add persistence for selectedFilters
amovar18 Aug 24, 2023
6a1517a
Conditionally setFilters
amovar18 Aug 24, 2023
91a54cc
Modify preference updating function.
amovar18 Aug 24, 2023
e56bc13
Merge branch 'develop' of https://github.com/GoogleChromeLabs/ps-anal…
amovar18 Aug 31, 2023
2662686
Fix selection of the frames.
amovar18 Aug 31, 2023
00d7765
Fix sorting problem in react-table.
amovar18 Sep 5, 2023
ce4ecb6
Fix tests.
amovar18 Sep 5, 2023
e09d745
Merge branch 'develop' of https://github.com/GoogleChromeLabs/ps-anal…
amovar18 Sep 6, 2023
3ea9b28
Fix accordionState according to set Preference.
amovar18 Sep 7, 2023
82f00f4
Fix selectedFilters persistence problem.
amovar18 Sep 7, 2023
3d1e436
Merge branch 'develop' of https://github.com/GoogleChromeLabs/ps-anal…
amovar18 Sep 19, 2023
590ce88
Merge branch 'develop' of https://github.com/GoogleChromeLabs/ps-anal…
amovar18 Sep 27, 2023
208929e
Move SelectedFilters and PreferenceDataValues to common package
amovar18 Sep 27, 2023
565384c
Add column Resizing and column sorting state
amovar18 Sep 27, 2023
69ab213
Prettify code
amovar18 Sep 27, 2023
c91d43a
Add column visibility.
amovar18 Sep 27, 2023
ffa933e
Prettify and Select accordion
amovar18 Sep 27, 2023
e54e4f2
Revert tabFrames condition
amovar18 Sep 28, 2023
19eaf2c
Fix infinite rerender.
amovar18 Sep 28, 2023
5815447
Fix multiple frame filter selection.
amovar18 Sep 28, 2023
4912a6c
Revert accidental removal of setSelectedFrame.
amovar18 Sep 28, 2023
763f9db
Fix sorting state setter.
amovar18 Sep 28, 2023
ab437e5
Add preference provider to test.
amovar18 Sep 28, 2023
e5f6543
Add promise for getAllFrames mock.
amovar18 Sep 28, 2023
086925d
Fix failing test
amovar18 Sep 28, 2023
ae94b7d
Remoce screen.debug
amovar18 Sep 28, 2023
2cf3264
Handle contextInvalidated error in preference provider
amovar18 Sep 28, 2023
e48b26c
Fix column sizing reverting back to original
amovar18 Sep 28, 2023
7e689e9
Fix sorting issue.
amovar18 Sep 28, 2023
4e93a72
Fix sorting issue.
amovar18 Sep 28, 2023
157a647
Fix toggle on and off of columns
amovar18 Sep 28, 2023
3562525
Fix column visibility state
amovar18 Sep 29, 2023
906f300
Fix column resizing, remove unnecessary dependency
amovar18 Sep 29, 2023
2f838e4
Fix persistence problem
amovar18 Sep 29, 2023
f7936e4
Merge branch 'develop' of https://github.com/GoogleChromeLabs/ps-anal…
amovar18 Oct 3, 2023
1c81085
Fix column resizing and visibility issue
amovar18 Oct 3, 2023
c36de2d
Small improvements
mohdsayed Oct 4, 2023
d06729b
Make stores as single file.
amovar18 Oct 4, 2023
4a38634
Merge branch 'feat/persistent-settings' of https://github.com/GoogleC…
amovar18 Oct 4, 2023
79fc02c
Add missing default as
mohdsayed Oct 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16,821 changes: 15,920 additions & 901 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions packages/common/src/cookies.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ export interface CookieStatsComponents {
export interface FramesWithCookies {
[key: string]: { frameIds: number[] };
}

export type SortingState = {
defaultSortKey?: string;
defaultSortOrder?: 'asc' | 'desc';
};

export type SelectedFilters = {
[key: string]: Set<string>;
};

export type PreferenceDataValues =
| Record<string, number | boolean>
| string
| SelectedFilters
| SortingState[];
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,47 @@
/**
* External dependencies.
*/
import React from 'react';
import React, { useEffect } from 'react';
import { PreferenceDataValues } from '@cookie-analysis-tool/common';

/**
* Internal dependencies.
*/
import ColumnListItem from './columnListItem';

import type { TableOutput } from '../useTable';

interface ColumnListProps {
table: TableOutput;
toggleVisibility: (key: string) => void;
updatePreference: (
key: string,
updater: (prevStatePreference: {
[key: string]: unknown;
}) => PreferenceDataValues
) => void;
handleClose: () => void;
}

const ColumnList = ({
table,
toggleVisibility,
handleClose,
updatePreference,
}: ColumnListProps) => {
useEffect(() => {
return () => {
const visibleColumns: Record<string, boolean> = {};

table.hideableColumns.forEach((column) => {
visibleColumns[column.header] = table.isColumnHidden(
column.accessorKey
);
});

updatePreference('selectedColumns', () => visibleColumns);
};
}, [table, table.hideableColumns, updatePreference]);

return (
<ul className="text-basic mt-1.5">
{table.hideableColumns.map((column, key) => (
Expand Down
17 changes: 15 additions & 2 deletions packages/design-system/src/components/table/columnMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';

import { type PreferenceDataValues } from '@cookie-analysis-tool/common';
/**
* Internal dependencies.
*/
Expand All @@ -32,9 +32,21 @@ interface ColumnMenuProps {
position: { x: number; y: number };
open: boolean;
onClose: (value: boolean) => void;
updatePreference: (
key: string,
updater: (prevStatePreference: {
[key: string]: unknown;
}) => PreferenceDataValues
) => void;
}

const ColumnMenu = ({ table, position, open, onClose }: ColumnMenuProps) => {
const ColumnMenu = ({
table,
position,
open,
onClose,
updatePreference,
}: ColumnMenuProps) => {
const [startAnimation, setStartAnimation] = useState(false);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

Expand Down Expand Up @@ -95,6 +107,7 @@ const ColumnMenu = ({ table, position, open, onClose }: ColumnMenuProps) => {
<div>
<ColumnList
table={table}
updatePreference={updatePreference}
toggleVisibility={(key: string) => {
table.isColumnHidden(key)
? table.showColumn(key)
Expand Down
15 changes: 13 additions & 2 deletions packages/design-system/src/components/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
* External dependencies.
*/
import React, { useCallback, useEffect, useRef, useState } from 'react';

import {
CookieTableData,
PreferenceDataValues,
} from '@cookie-analysis-tool/common';
/**
* Internal dependencies.
*/
import TableHeader from './tableHeader';
import TableBody from './tableBody';
import ColumnMenu from './columnMenu';
import { TableOutput, TableRow } from './useTable';
import { CookieTableData } from '@cookie-analysis-tool/common';

export type TableData = CookieTableData;

Expand All @@ -35,13 +37,20 @@ interface TableProps {
selectedKey: string | undefined | null;
getRowObjectKey: (row: TableRow) => string;
onRowClick: (row: TableData | null) => void;
updatePreference: (
key: string,
updater: (prevStatePreference: {
[key: string]: unknown;
}) => PreferenceDataValues
) => void;
}

const Table = ({
table,
selectedKey,
getRowObjectKey,
onRowClick,
updatePreference,
}: TableProps) => {
const [showColumnsMenu, setShowColumnsMenu] = useState(false);
const [columnPosition, setColumnPosition] = useState({
Expand Down Expand Up @@ -89,13 +98,15 @@ const Table = ({
className="relative h-full w-full overflow-auto"
>
<ColumnMenu
updatePreference={updatePreference}
table={table}
open={showColumnsMenu}
onClose={setShowColumnsMenu}
position={columnPosition}
/>
<div className="h-full w-full" ref={tableRef}>
<TableHeader
updatePreference={updatePreference}
table={table}
setColumnPosition={setColumnPosition}
onRightClick={handleRightClick}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,73 @@
/**
* External dependencies.
*/
import React, { useRef } from 'react';

import React, { useCallback, useEffect, useRef } from 'react';
import { PreferenceDataValues } from '@cookie-analysis-tool/common';
/**
* Internal dependencies.
*/
import type { TableColumn, TableOutput } from '../useTable';
import HeaderResizer from './headerResizer';
import { ArrowDown } from '@cookie-analysis-tool/design-system';
import { ArrowDown } from '../../../icons';

interface HeaderCellProps {
table: TableOutput;
index: number;
cell: TableColumn;
setIsRowFocused: (state: boolean) => void;
updatePreference: (
key: string,
callback: (prevStatePreference: {
[key: string]: unknown;
}) => PreferenceDataValues
) => void;
}

const HeaderCell = ({
table,
index,
cell,
setIsRowFocused,
updatePreference,
}: HeaderCellProps) => {
// Table data is updated on mouseup.
const resizeHandler = useCallback(() => {
updatePreference('columnSizing', () => {
const currentSizes: { [key: string]: number } = {};

table.columns.map((column) => {
currentSizes[column.accessorKey] = column.width as number;
return column;
});
return currentSizes;
});
}, [table, updatePreference]);

useEffect(() => {
if (columnRef.current) {
columnRef.current.addEventListener('mouseup', resizeHandler);
}

const tempRef = columnRef.current;

return () => {
if (tempRef) {
tempRef.removeEventListener('mouseup', resizeHandler);
}
};
}, [resizeHandler]);

const handleOnClick = useCallback(() => {
table.setSortKey(cell.accessorKey, updatePreference);
}, [cell.accessorKey, table, updatePreference]);

const columnRef = useRef<HTMLTableHeaderCellElement>(null);

return (
<div
ref={columnRef}
style={{ width: cell.width }}
onClick={() => table.setSortKey(cell.accessorKey)}
onClick={handleOnClick}
className="relative hover:bg-gainsboro dark:hover:bg-outer-space select-none touch-none font-normal truncate"
data-testid="header-cell"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@
* External dependencies.
*/
import React from 'react';

import { PreferenceDataValues } from '@cookie-analysis-tool/common';
/**
* Internal dependencies.
*/
import HeaderCell from './headerCell';
import type { TableOutput } from '../useTable';

interface HeaderRowProps {
table: TableOutput;
setIsRowFocused: (state: boolean) => void;
updatePreference: (
key: string,
callback: (prevStatePreference: {
[key: string]: unknown;
}) => PreferenceDataValues
) => void;
}

const HeaderRow = ({ table, setIsRowFocused }: HeaderRowProps) => {
const HeaderRow = ({
table,
setIsRowFocused,
updatePreference,
}: HeaderRowProps) => {
return (
<div className="bg-anti-flash-white dark:bg-charleston-green border-b border-american-silver dark:border-quartz divide-x divide-american-silver dark:divide-quartz flex">
{table.columns?.map((cell, idx) => (
<HeaderCell
updatePreference={updatePreference}
key={idx}
index={idx}
table={table}
Expand Down
15 changes: 13 additions & 2 deletions packages/design-system/src/components/table/tableHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* External dependencies.
*/
import React, { useCallback } from 'react';

import { PreferenceDataValues } from '@cookie-analysis-tool/common';
/**
* Internal dependencies.
*/
Expand All @@ -32,13 +32,20 @@ interface TableHeaderProps {
event: React.MouseEvent<HTMLTableSectionElement, MouseEvent>
) => void;
setIsRowFocused: (state: boolean) => void;
updatePreference: (
key: string,
updater: (prevStatePreference: {
[key: string]: unknown;
}) => PreferenceDataValues
) => void;
}

const TableHeader = ({
table,
setColumnPosition,
onRightClick,
setIsRowFocused,
updatePreference,
}: TableHeaderProps) => {
const handleRightClick = useCallback(
(e: React.MouseEvent<HTMLTableSectionElement>) => {
Expand All @@ -53,7 +60,11 @@ const TableHeader = ({

return (
<div onContextMenu={handleRightClick} className="sticky top-0 z-10">
<HeaderRow table={table} setIsRowFocused={setIsRowFocused} />
<HeaderRow
updatePreference={updatePreference}
table={table}
setIsRowFocused={setIsRowFocused}
/>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,49 @@ export type ColumnResizingOutput = {
};

const useColumnResizing = (
tableColumns: TableColumn[]
tableColumns: TableColumn[],
options?: Record<string, number>
): ColumnResizingOutput => {
const tableContainerRef = useRef<HTMLDivElement>(null);
const [columns, setColumns] = useState<TableColumn[]>([]);

useEffect(() => {
const tableWidth =
tableContainerRef.current?.getBoundingClientRect().width || 0;
if (options) {
const tableWidth =
tableContainerRef.current?.getBoundingClientRect().width || 0;
let newColumns = tableColumns.map((column) => ({
...column,
width: options[column.accessorKey],
}));

if (Object.keys(options).length === tableColumns.length) {
newColumns = tableColumns.map((column) => ({
...column,
width: options[column.accessorKey],
}));
} else {
newColumns = tableColumns.map((column) => ({
...column,
width: tableWidth / tableColumns.length,
}));
}

setColumns(newColumns);
}
}, [options, tableColumns]);

const newColumns = tableColumns.map((column) => {
return {
useEffect(() => {
if (!options) {
const tableWidth =
tableContainerRef.current?.getBoundingClientRect().width || 0;
const newColumns = tableColumns.map((column) => ({
...column,
width: tableWidth / tableColumns.length,
};
});
}));

setColumns(newColumns);
}, [tableColumns]);
setColumns(newColumns);
}
}, [options, tableColumns]);

const onMouseDown = useCallback(
(
Expand Down
Loading
Loading