diff --git a/components/common/gist-code.tsx b/components/common/gist-code.tsx new file mode 100644 index 0000000..9908a9d --- /dev/null +++ b/components/common/gist-code.tsx @@ -0,0 +1,25 @@ +import '@catppuccin/highlightjs/sass/catppuccin.variables.scss'; +import GistsCode from 'components/pages/gists/gists-code'; +import { getSignalGist } from 'lib/fetcher'; + +const GistCode = async ({ id }: { id: string }) => { + const gist = await getSignalGist(id); + + if (!gist?.files) { + return
+{children} -> ); diff --git a/components/pages/gists/gists-code.tsx b/components/pages/gists/gists-code.tsx index 63945a3..321c4de 100644 --- a/components/pages/gists/gists-code.tsx +++ b/components/pages/gists/gists-code.tsx @@ -15,6 +15,12 @@ interface Props { showFileName?: boolean; } +/** + * Render GitHub gists code. + * + * @params file + * @params showaFileName determine show full content or not + */ const GistsCode = ({ file, showFileName = false }: Props) => { const fileContent = showFileName ? file.content @@ -33,7 +39,7 @@ const GistsCode = ({ file, showFileName = false }: Props) => { return ( <> {showFileName ? ( -+ +void; }; -const CopyButton = ({ onCopy: onClick, className }: CopyButtonProps) => { - const [copied, setCopied] = useState(false); - const handleClick = () => { - onClick?.(); - setCopied(true); - setTimeout(() => { - setCopied(false); - }, 1000); - }; +const CopyButton = forwardRef( + ({ onCopy: onClick, className }, ref) => { + const [copied, setCopied] = useState(false); + const handleClick = () => { + onClick?.(); + setCopied(true); + setTimeout(() => { + setCopied(false); + }, 1000); + }; - return ( - <> - - > - ); -}; + + + + + > + ); + }, +); + +CopyButton.displayName = 'CopyButton'; export default memo(CopyButton); diff --git a/components/post/copy-code.tsx b/components/post/copy-code.tsx new file mode 100644 index 0000000..adfe7f1 --- /dev/null +++ b/components/post/copy-code.tsx @@ -0,0 +1,29 @@ +'use client'; + +import clsx from 'clsx'; +import CopyButton from 'components/post/copy-button'; +import useCopyToClipboard from 'lib/hooks/use-copy-to-clipboard'; +import { useCallback, useRef } from 'react'; + +const CopyCode = () => { + const btnRef = useRef (null); + const { copy } = useCopyToClipboard(); + const handleCopy = useCallback(() => { + if (!btnRef.current?.parentElement) + throw new Error('Can not access pre element.'); + if (btnRef.current.parentElement.textContent == null) return; + copy(btnRef.current.parentElement.textContent); + }, [copy]); + + return ( + <> + + > + ); +}; + +export default CopyCode;