1import { useState, useEffect, useRef, useCallback } from 'react';
2import copy from 'copy-to-clipboard';
4const COPY_DELAY = 2 * 1000; // ms
6export default function useCopiable(text: string): [doCopy: VoidFunction, didCopy: boolean] {
7 const [didCopy, setDidCopy] = useState<boolean>(false);
9 // what we really want here is a useDebouncedCallback from use-debounced
10 // but that's a relatively large dep for a single simple usage so ¯\_(ツ)_/¯
11 const listener = useRef<ReturnType<typeof window.setTimeout>>(); // <NodeJS.Listener>
12 useEffect(() => () => clearTimeout(Number(listener.current)), []);
14 const doCopy = useCallback(() => {
18 clearTimeout(Number(listener.current));
19 listener.current = setTimeout(() => setDidCopy(false), COPY_DELAY);
22 return [doCopy, didCopy];
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list