Skip to content

Commit 7089e7f

Browse files
authoredJul 4, 2024
Fix copy all template feedback (#62)
* fix(template): use notification as feedback for copied templates instead of changing button text * fix(workflows): upgrade pnpm setup from `v2` to `v4`
1 parent 9939cd2 commit 7089e7f

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed
 

‎.github/workflows/root.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: actions/checkout@v3
1717

1818
- name: Setup Pnpm
19-
uses: pnpm/action-setup@v2
19+
uses: pnpm/action-setup@v4
2020
with:
2121
version: 8
2222

‎apps/web/pages/templates/index.tsx

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ const StyledSelect = styled(Select)`
8686
}
8787
`;
8888

89-
const useCopyToClipboard = () => {
89+
const useCopyToClipboard = (timeout?: number) => {
9090
const [copied, setCopied] = React.useState(false);
9191

9292
React.useEffect(() => {
9393
if (!copied) {
9494
return;
9595
}
9696

97+
if (!timeout) {
98+
return setCopied(false);
99+
}
100+
97101
const timeOut = setTimeout(() => {
98102
setCopied(false);
99103
}, 1000 * 2);
@@ -106,9 +110,7 @@ const useCopyToClipboard = () => {
106110
return {
107111
copied,
108112
copy: (text: string) => {
109-
if (navigator?.clipboard?.writeText) {
110-
setCopied(true);
111-
113+
if (navigator?.clipboard.writeText) {
112114
navigator.clipboard.writeText(text);
113115
} else {
114116
const element = document.createElement('textarea');
@@ -128,9 +130,9 @@ const useCopyToClipboard = () => {
128130
document.execCommand('copy');
129131

130132
document.body.removeChild(element);
131-
132-
setCopied(true);
133133
}
134+
135+
setCopied(true);
134136
},
135137
};
136138
};
@@ -148,7 +150,7 @@ const TemplatePreview = (
148150
)
149151
>
150152
) => {
151-
const clipboard = useCopyToClipboard();
153+
const clipboard = useCopyToClipboard(2000);
152154

153155
return (
154156
<Box
@@ -385,16 +387,16 @@ const QuerySection = (
385387
}, [updateDependency]);
386388
};
387389

388-
const useNotification = () => {
390+
const useTemplateNotification = () => {
389391
const [type, setType] = React.useState(
390-
'none' as 'loading' | 'succeed' | 'none' | 'failed'
392+
undefined as 'loading' | 'succeed' | 'failed' | undefined
391393
);
392394

393395
React.useEffect(() => {
394396
toast.dismiss();
395397

396398
switch (type) {
397-
case 'none': {
399+
case undefined: {
398400
break;
399401
}
400402
case 'loading': {
@@ -425,6 +427,33 @@ const useNotification = () => {
425427
};
426428
};
427429

430+
const useCopyNotification = () => {
431+
const [type, setType] = React.useState(undefined as 'succeed' | undefined);
432+
433+
React.useEffect(() => {
434+
toast.dismiss();
435+
436+
switch (type) {
437+
case undefined: {
438+
break;
439+
}
440+
case 'succeed': {
441+
toast.success('🎉 Copied All');
442+
break;
443+
}
444+
}
445+
}, [type]);
446+
447+
return {
448+
succeed: () => {
449+
setType('succeed');
450+
},
451+
unset: () => {
452+
setType(undefined);
453+
},
454+
};
455+
};
456+
428457
const Templates = () => {
429458
const delimiter = ',';
430459

@@ -438,7 +467,9 @@ const Templates = () => {
438467

439468
const clipboard = useCopyToClipboard();
440469

441-
const notification = useNotification();
470+
const templateNotification = useTemplateNotification();
471+
472+
const copyNotification = useCopyNotification();
442473

443474
const [templates, setTemplate] = React.useState([] as Templates);
444475

@@ -447,15 +478,15 @@ const Templates = () => {
447478
});
448479

449480
React.useEffect(() => {
450-
notification.loading();
481+
templateNotification.loading();
451482

452483
trpcClient.template.findAllTemplates
453484
.query()
454485
.then((result) => {
455486
if (!result.hadSucceed) {
456-
notification.failed();
487+
templateNotification.failed();
457488
} else {
458-
notification.succeed();
489+
templateNotification.succeed();
459490
}
460491

461492
return result;
@@ -470,6 +501,12 @@ const Templates = () => {
470501
.then(setTemplate);
471502
}, []);
472503

504+
React.useEffect(() => {
505+
if (clipboard.copied) {
506+
copyNotification.succeed();
507+
}
508+
}, [clipboard.copied]);
509+
473510
return (
474511
<Layout title="Templates">
475512
<Seo
@@ -492,13 +529,18 @@ const Templates = () => {
492529
minHeight="30vh"
493530
boxSizing="border-box"
494531
>
495-
<Box display="flex" justifyContent="space-between">
532+
<Box
533+
display="flex"
534+
justifyContent="space-between"
535+
alignItems="center"
536+
gap={8}
537+
>
496538
<QuerySection
497539
templates={{
498540
all: templates,
499541
selected,
500542
updateSelected: (selected) => {
501-
const params =
543+
const query =
502544
formQueryParamStringFromRecord({
503545
names: selected
504546
.map(({ name }) => {
@@ -510,7 +552,7 @@ const Templates = () => {
510552
router.push(
511553
{
512554
pathname: '/templates',
513-
query: params,
555+
query,
514556
},
515557
undefined,
516558
{
@@ -525,12 +567,11 @@ const Templates = () => {
525567
colorScheme="messenger"
526568
isDisabled={isFalsy(selected.length)}
527569
onClick={() => {
570+
copyNotification.unset();
528571
clipboard.copy(combineTemplates(selected));
529572
}}
530573
>
531-
{!clipboard.copied
532-
? 'Copy All'
533-
: '🎉 Copied All'}
574+
Copy All
534575
</Button>
535576
<Divider orientation="vertical" />
536577
<Button

0 commit comments

Comments
 (0)