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
Error
; + } + + return ( +
+ {Object.keys(gist.files).map((f) => ( + + ))} +
+ ); +}; + +export default GistCode; diff --git a/components/mdx/components.ts b/components/mdx/components.ts index 5239dee..0733f7f 100644 --- a/components/mdx/components.ts +++ b/components/mdx/components.ts @@ -6,6 +6,7 @@ import RUACodeSandbox from 'components/rua/rua-code-sandbox'; import RUASandpack from 'components/rua/rua-sandpack'; import Tab from 'components/rua/tab'; import TabItem from 'components/rua/tab/tab-item'; +import GistCode from 'components/common/gist-code'; const components = { RUASandpack, @@ -16,6 +17,7 @@ const components = { TabItem, RUACodeSandbox, RUACodepen, + GistCode, }; export default components; diff --git a/components/mdx/pre.tsx b/components/mdx/pre.tsx index 0a93f2a..a3bc088 100644 --- a/components/mdx/pre.tsx +++ b/components/mdx/pre.tsx @@ -1,7 +1,6 @@ -'use client'; - import clsx from 'clsx'; import CopyButton from 'components/post/copy-button'; +import CopyCode from 'components/post/copy-code'; import useCopyToClipboard from 'lib/hooks/use-copy-to-clipboard'; import { DetailedHTMLProps, @@ -19,26 +18,11 @@ type Props = {} & DetailedHTMLProps< const Pre = ({ ...rest }: Props) => { const { children, className, ...props } = rest; - const preRef = useRef(null); - const { copy } = useCopyToClipboard(); - const handleCopy = useCallback(() => { - if (!preRef.current) throw new Error('Can not access pre element.'); - if (preRef.current.textContent == null) return; - copy(preRef.current.textContent); - }, [copy]); - 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;