2023-05-23 08:43:35 +01:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
|
|
|
export const useClipboard = () => {
|
2023-09-15 10:08:21 +02:00
|
|
|
const copy = useCallback(async (value: string | number) => {
|
2023-05-23 08:43:35 +01:00
|
|
|
try {
|
|
|
|
// only strings and numbers casted to strings can be copied to clipboard
|
|
|
|
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
|
|
throw new Error(`Cannot copy typeof ${typeof value} to clipboard, must be a string`);
|
|
|
|
}
|
|
|
|
// empty strings are also considered invalid
|
|
|
|
else if (value === '') {
|
|
|
|
throw new Error(`Cannot copy empty string to clipboard.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const stringifiedValue = value.toString();
|
|
|
|
|
|
|
|
await navigator.clipboard.writeText(stringifiedValue);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
/**
|
|
|
|
* Realistically this isn't useful in production as there's nothing the user can do.
|
|
|
|
*/
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
console.warn('Copy failed', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { copy };
|
|
|
|
};
|