-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/todo tags && handling error while copying (#100)
Co-authored-by: Ryczko <konradryczko1@gmail.com>
- Loading branch information
1 parent
1dce63d
commit edfcd4a
Showing
6 changed files
with
25 additions
and
27 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
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
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,29 +1,30 @@ | ||
import { useState } from 'react'; | ||
import toast from 'react-hot-toast'; | ||
|
||
type CopiedValue = string | null; | ||
type CopyFn = (text: string) => Promise<boolean>; | ||
function useCopyToClipboard() { | ||
const [copiedText, setCopiedText] = useState<string | null>(null); | ||
|
||
function useCopyToClipboard(): [CopiedValue, CopyFn] { | ||
const [copiedText, setCopiedText] = useState<CopiedValue>(null); | ||
|
||
const copy: CopyFn = async (text) => { | ||
const copy = async (text: string, silient = false) => { | ||
if (!navigator?.clipboard) { | ||
console.warn('Clipboard not supported'); | ||
toast.error('Clipboard not supported'); | ||
return false; | ||
} | ||
|
||
try { | ||
await navigator.clipboard.writeText(text); | ||
setCopiedText(text); | ||
if (!silient) { | ||
toast.success('Copied to clipboard'); | ||
} | ||
return true; | ||
} catch (error) { | ||
console.warn('Copy failed', error); | ||
toast.error('Copy failed'); | ||
setCopiedText(null); | ||
return false; | ||
} | ||
}; | ||
|
||
return [copiedText, copy]; | ||
return { copiedText, copy }; | ||
} | ||
|
||
export default useCopyToClipboard; |