Skip to content

Commit e95fa0c

Browse files
committed
refactor(web): remove unnecessary re-rendering CopyPasteControls
1 parent 3d5ed63 commit e95fa0c

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

webdriver-ts-results/src/components/SelectionToolbar/CopyPasteControls/CopyPasteControls.tsx

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { Button } from "antd";
77
const CopyPasteControls = () => {
88
console.log("CopyPasteControls");
99

10-
const state = useRootStore((state) => state);
1110
const setStateFromClipboard = useRootStore((state) => state.setStateFromClipboard);
11+
const copyStateToClipboard = useRootStore((state) => state.copyStateToClipboard);
1212

1313
const handlePasteError = (error: Error) => {
1414
alert("Sorry - couldn't parse pasted selection");
1515
console.error("Pasting state failed", error);
1616
};
1717

18-
const handlePaste = useCallback(
18+
const pasteStateFromText = useCallback(
1919
(text: string) => {
2020
try {
2121
const parsedState = JSON.parse(text);
@@ -32,10 +32,10 @@ const CopyPasteControls = () => {
3232
event.preventDefault();
3333
const text = event.clipboardData?.getData("text/plain");
3434
if (text) {
35-
handlePaste(text);
35+
pasteStateFromText(text);
3636
}
3737
},
38-
[handlePaste]
38+
[pasteStateFromText]
3939
);
4040

4141
useEffect(() => {
@@ -45,37 +45,24 @@ const CopyPasteControls = () => {
4545
};
4646
}, [handleClipboardPaste]);
4747

48-
const copy = () => {
49-
const serializedState = {
50-
frameworks: state.frameworks.filter((f) => state.selectedFrameworks.has(f)).map((f) => f.dir),
51-
benchmarks: state.benchmarks.filter((f) => state.selectedBenchmarks.has(f)).map((f) => f.id),
52-
displayMode: state.displayMode,
53-
};
54-
55-
const json = JSON.stringify(serializedState);
56-
57-
try {
58-
navigator.clipboard.writeText(json);
59-
window.location.hash = btoa(json);
60-
} catch (error) {
61-
console.error("Copying state failed", error);
62-
}
63-
};
64-
6548
const handlePasteFromClipboard = useCallback(async () => {
6649
try {
6750
const text = await navigator.clipboard.readText();
68-
handlePaste(text);
51+
pasteStateFromText(text);
6952
} catch (error) {
7053
handlePasteError(error as Error);
7154
}
72-
}, [handlePaste]);
55+
}, [pasteStateFromText]);
7356

7457
return (
7558
<div className="copy-paste-panel">
7659
<div>Copy/paste current selection</div>
7760
<div className="copy-paste-panel__buttons">
78-
<Button onClick={copy} icon={<CopyIcon size={20} />} aria-label="Copy selected frameworks and benchmarks" />
61+
<Button
62+
onClick={copyStateToClipboard}
63+
icon={<CopyIcon size={20} />}
64+
aria-label="Copy selected frameworks and benchmarks"
65+
/>
7966
<Button
8067
onClick={handlePasteFromClipboard}
8168
icon={<ClipboardPasteIcon size={20} />}

webdriver-ts-results/src/reducer.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ interface Actions {
100100
compare: (framework: Framework) => void;
101101
stopCompare: (framework: Framework) => void;
102102
sort: (sortKey: string) => void;
103+
copyStateToClipboard: () => void;
103104
setStateFromClipboard: (arg: unknown) => void;
104105
}
105106

@@ -309,6 +310,24 @@ export const useRootStore = create<State & Actions>((set, get) => ({
309310
const t = { ...get(), sortKey };
310311
return set(() => ({ ...t, resultTables: updateResultTable(t) }));
311312
},
313+
copyStateToClipboard: () => {
314+
const currentState = get();
315+
316+
const serializedState = {
317+
frameworks: currentState.frameworks.filter((f) => currentState.selectedFrameworks.has(f)).map((f) => f.dir),
318+
benchmarks: currentState.benchmarks.filter((f) => currentState.selectedBenchmarks.has(f)).map((f) => f.id),
319+
displayMode: currentState.displayMode,
320+
};
321+
322+
const json = JSON.stringify(serializedState);
323+
324+
try {
325+
navigator.clipboard.writeText(json);
326+
window.location.hash = btoa(json);
327+
} catch (error) {
328+
console.error("Copying state failed", error);
329+
}
330+
},
312331
setStateFromClipboard: (arg) => {
313332
if (!arg) {
314333
console.log("no state found");

0 commit comments

Comments
 (0)