-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: better error feedback on copy to clipboard
- Loading branch information
Showing
2 changed files
with
66 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,47 @@ | ||
import { useState } from 'react' | ||
import { useState } from 'react'; | ||
|
||
type CopiedValue = null | string | ||
type CopyFn = (text: string) => Promise<boolean> // Return success | ||
type CopyToClipboard = { | ||
data: string; | ||
isError: boolean; | ||
message?: string; | ||
}; | ||
|
||
export function useCopyToClipboard(): [CopiedValue, CopyFn] { | ||
const [copiedText, setCopiedText] = useState<CopiedValue>(null) | ||
export const useCopyToClipboard = (): [ | ||
(text: string) => Promise<void>, | ||
CopyToClipboard, | ||
] => { | ||
const [copyResult, setCopyResult] = useState<CopyToClipboard>({ | ||
data: '', | ||
isError: false, | ||
}); | ||
|
||
const copy: CopyFn = async text => { | ||
if (!navigator?.clipboard) { | ||
console.warn('Clipboard not supported') | ||
return false | ||
const copyToClipboard = async (text: string) => { | ||
if (!navigator.clipboard) { | ||
setCopyResult({ | ||
data: text, | ||
isError: true, | ||
message: 'Clipboard API not available', | ||
}); | ||
throw new Error('Clipboard API not available'); | ||
} | ||
|
||
// Try to save to clipboard then save it in the state if worked | ||
try { | ||
await navigator.clipboard.writeText(text) | ||
setCopiedText(text) | ||
return true | ||
} catch (error) { | ||
console.warn('Copy failed', error) | ||
setCopiedText(null) | ||
return false | ||
await navigator.clipboard.writeText(text); | ||
setCopyResult({ | ||
data: text, | ||
isError: false, | ||
message: 'Copied to clipboard', | ||
}); | ||
} catch (err) { | ||
setCopyResult({ | ||
data: text, | ||
isError: true, | ||
message: | ||
err instanceof Error ? err.message : 'Failed to copy to clipboard', | ||
}); | ||
throw err; | ||
} | ||
} | ||
}; | ||
|
||
return [copiedText, copy] | ||
} | ||
return [copyToClipboard, copyResult]; | ||
}; |