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: Add context menu to copy network filter string to filter network requests #333

Merged
merged 7 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
3 changes: 3 additions & 0 deletions packages/design-system/src/components/cookieTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface CookieTableProps {
columnSorting?: SortingState[];
columnSizing?: Record<string, number>;
selectedColumns?: Record<string, boolean>;
isExtension?: boolean;
}

const CookieTable = ({
Expand All @@ -78,6 +79,7 @@ const CookieTable = ({
columnSorting,
columnSizing,
selectedColumns,
isExtension = false,
}: CookieTableProps) => {
useEffect(() => {
if (selectedFrame && selectedFrameCookie) {
Expand Down Expand Up @@ -149,6 +151,7 @@ const CookieTable = ({
(row?.originalData as CookieTableData).parsedCookie
) as string
}
isExtension={isExtension}
onRowClick={onRowClick}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface TableProps {
) => void;
showTopBar?: boolean;
disableFiltering?: boolean;
isExtension?: boolean;
}

const Table = ({
Expand All @@ -55,6 +56,7 @@ const Table = ({
updatePreference = noop,
showTopBar = false,
disableFiltering = false,
isExtension = false,
}: TableProps) => {
const [showColumnsMenu, setShowColumnsMenu] = useState(false);
const [showFilterSidebar, setShowFilterSidebar] = useState(false);
Expand Down Expand Up @@ -152,6 +154,7 @@ const Table = ({
setIsRowFocused={setIsRowFocused}
/>
<TableBody
isExtension={isExtension}
table={table}
getRowObjectKey={getRowObjectKey}
isRowFocused={isRowFocused}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,78 @@
/**
* External dependencies.
*/
import React from 'react';
import React, { useState, useCallback } from 'react';
import { createPortal } from 'react-dom';

/**
* Internal dependencies.
*/
import type { InfoType } from '../../useTable';
import type { InfoType, TableRow } from '../../useTable';
import { CookieTableData } from '@ps-analysis-tool/common';
interface BodyCellProps {
cell: React.JSX.Element | InfoType;
width: number;
isHighlighted?: boolean;
isRowFocused: boolean;
isExtension?: boolean;
row: TableRow;
onRowClick: () => void;
}

const BodyCell = ({
onRowClick,
amovar18 marked this conversation as resolved.
Show resolved Hide resolved
row,
cell,
width,
isRowFocused,
isHighlighted = false,
isExtension = false,
}: BodyCellProps) => {
const [contextMenuOpen, setContextMenuOpen] = useState(false);
const [columnPosition, setColumnPosition] = useState({
x: 0,
y: 0,
});

const handleRightClick = useCallback(
(e: React.MouseEvent<HTMLElement>) => {
if (isExtension) {
e.preventDefault();
const x = e.clientX,
y = e.clientY;
onRowClick();
setColumnPosition({ x, y });
document.body.style.overflow = contextMenuOpen ? 'auto' : 'hidden';
setContextMenuOpen(!contextMenuOpen);
}
},
[contextMenuOpen, isExtension, onRowClick]
);

const handleCopy = useCallback(() => {
try {
const domain = (row?.originalData as CookieTableData)?.parsedCookie
?.domain;
const name = (row?.originalData as CookieTableData)?.parsedCookie?.name;

// Need to do this since chrome doesnt allow the clipboard access in extension.
const copyFrom = document.createElement('textarea');
copyFrom.textContent = `cookie-domain:${domain} cookie-name:${name}`;
document.body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
copyFrom.blur();
document.body.removeChild(copyFrom);
setContextMenuOpen(false);
} catch (error) {
//Fail silently
}
}, [row?.originalData]);

return (
<div
tabIndex={0}
onContextMenu={handleRightClick}
style={{ maxWidth: width }}
className={`box-border outline-0 px-1 py-px truncate h-5 text-xs ${
isHighlighted
Expand All @@ -49,6 +99,38 @@ const BodyCell = ({
} cursor-default flex-1`}
>
{cell}
<>
{isExtension &&
amovar18 marked this conversation as resolved.
Show resolved Hide resolved
contextMenuOpen &&
createPortal(
<div className="transition duration-100" data-testid="column-menu">
<div
className="absolute z-50 text-raisin-black dark:text-bright-gray rounded-md backdrop-blur-2xl w-screen max-w-[13rem] p-1.5 mr-2 divide-neutral-300 dark:divide-neutral-500 max-h-[78vh] overflow-auto bg-stone-200 dark:bg-neutral-700 shadow-3xl"
style={{
left:
'min( calc( 100vw - 15rem),' + columnPosition.x + 'px )',
top: columnPosition.y + 'px',
border: '0.5px solid rgba(0, 0, 0, 0.20)',
}}
>
<button
onClick={handleCopy}
className="w-full text-xs rounded px-1 py-[3px] mb-1.5 flex items-center hover:bg-royal-blue hover:text-white cursor-default"
>
<span>Copy network filter string.</span>
</button>
<div></div>
</div>

<div
data-testid="column-menu-overlay"
onClick={() => setContextMenuOpen(false)}
className="absolute w-screen h-screen z-10 top-0 left-0"
/>
</div>,
document.body
)}
</>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface BodyRowProps {
getRowObjectKey: (row: TableRow) => string;
onRowClick: () => void;
onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>, index: number) => void;
isExtension?: boolean;
}

const BodyRow = ({
Expand All @@ -47,6 +48,7 @@ const BodyRow = ({
isRowFocused,
onRowClick,
onKeyDown,
isExtension = false,
}: BodyRowProps) => {
const cookieKey = getRowObjectKey(row);
const isHighlighted = (row.originalData as CookieTableData)?.highlighted;
Expand Down Expand Up @@ -80,11 +82,14 @@ const BodyRow = ({
>
{columns.map(({ accessorKey, width }, idx) => (
<BodyCell
isExtension={isExtension}
key={idx}
onRowClick={onRowClick}
cell={row[accessorKey]?.value || ''}
width={width || 0}
isHighlighted={isHighlighted}
isRowFocused={cookieKey === selectedKey}
row={row}
/>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface TableBodyProps {
setIsRowFocused: (state: boolean) => void;
selectedKey: string | undefined | null;
onRowClick: (key: TableData | null) => void;
isExtension?: boolean;
}

const TableBody = ({
Expand All @@ -42,6 +43,7 @@ const TableBody = ({
setIsRowFocused,
selectedKey,
onRowClick,
isExtension = false,
}: TableBodyProps) => {
const tableBodyRef = useRef(null);

Expand Down Expand Up @@ -126,6 +128,7 @@ const TableBody = ({
<div ref={tableBodyRef} className="h-full flex flex-col">
{table.rows.map((row, index) => (
<BodyRow
isExtension={isExtension}
key={index}
index={index}
row={row}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ const CookieTableContainer = ({

return (
<CookieTable
isExtension
tableColumns={tableColumns}
data={Object.values(tableData)}
selectedFrame={selectedFrame}
Expand Down
Loading