Skip to content

Commit

Permalink
Handle copy button timeouts better
Browse files Browse the repository at this point in the history
Less glitchy by clearing out old timeouts when creating new ones.
  • Loading branch information
ivarnakken committed Jul 22, 2023
1 parent 30e43ad commit 1c8c866
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/routes/surveys/components/AdminSideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Props = {

type State = {
copied: boolean;
timeoutId: number | null;
timeoutId: NodeJS.Timeout | null;
generatedCSV:
| {
url: string;
Expand Down
25 changes: 19 additions & 6 deletions app/routes/users/components/UserSettingsOAuth2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ type Props = {

const UserSettingsOAuth2 = (props: Props) => {
const [copiedClientId, setCopiedClientId] = useState<string>('');
const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout | null>(null);

const handleCopyButtonClick = (shareLink: string) => {
navigator.clipboard.writeText(shareLink).then(() => {
if (timeoutId) {
clearTimeout(timeoutId);
}

setCopiedClientId(shareLink);

const newTimeoutId = setTimeout(() => {
setCopiedClientId('');
}, 2000);

setTimeoutId(newTimeoutId);
});
};

const applicationColumns = [
{
Expand All @@ -46,17 +63,13 @@ const UserSettingsOAuth2 = (props: Props) => {
return (
<Flex wrap gap={10}>
{application.clientId}
<Tooltip content="Kopier client ID">
<Tooltip content={copied ? 'Kopiert!' : 'Kopier client ID'}>
<Icon
name={copied ? 'copy' : 'copy-outline'}
size={20}
success={copied}
className={styles.copyIcon}
onClick={() => {
navigator.clipboard.writeText(application.clientId);
setCopiedClientId(application.clientId);
setTimeout(() => setCopiedClientId(''), 2000);
}}
onClick={() => handleCopyButtonClick(application.clientId)}
/>
</Tooltip>
</Flex>
Expand Down

0 comments on commit 1c8c866

Please sign in to comment.