From 80ca065d12ee309faf566b590f6a286ec26f6330 Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Thu, 10 Apr 2025 22:16:16 +0100 Subject: [PATCH] Add copy button to pre components --- components/icons/CheckIcon.tsx | 10 ++++++++++ components/icons/CopyIcon.tsx | 11 +++++++++++ components/layouts/ArticleLayout.tsx | 4 +--- components/ui/Code.tsx | 29 ++++++++++++++++++++++++++++ components/ui/Comments.tsx | 2 +- lib/spotify.ts | 9 ++------- mdx-components.tsx | 3 +++ 7 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 components/icons/CheckIcon.tsx create mode 100644 components/icons/CopyIcon.tsx create mode 100644 components/ui/Code.tsx diff --git a/components/icons/CheckIcon.tsx b/components/icons/CheckIcon.tsx new file mode 100644 index 0000000..8ec2f65 --- /dev/null +++ b/components/icons/CheckIcon.tsx @@ -0,0 +1,10 @@ +import {Props} from '@/types' + +export function CheckIcon(props: Props) { + return ( + + ) +} \ No newline at end of file diff --git a/components/icons/CopyIcon.tsx b/components/icons/CopyIcon.tsx new file mode 100644 index 0000000..6b08196 --- /dev/null +++ b/components/icons/CopyIcon.tsx @@ -0,0 +1,11 @@ +import {Props} from '@/types' + +export function CopyIcon(props: Props) { + return ( + + ) +} \ No newline at end of file diff --git a/components/layouts/ArticleLayout.tsx b/components/layouts/ArticleLayout.tsx index 00f9d96..5009755 100644 --- a/components/layouts/ArticleLayout.tsx +++ b/components/layouts/ArticleLayout.tsx @@ -5,13 +5,11 @@ import {Prose} from '@/components/ui/Prose' import {Views} from '@/components/ui/Views' import {ArrowDownIcon} from '@/components/icons/ArrowDownIcon' import ArticleNav from '@/components/ui/ArticleNav' -import Comments from '@/components/ui/Comments' +import {Comments} from '@/components/ui/Comments' import {getAllArticles} from '@/lib/getAllArticles' import {getComments} from '@/lib/getComments' import {format} from 'date-fns' -import type {Comment} from '@/types' - type ArticleLayout = { title: string date: string diff --git a/components/ui/Code.tsx b/components/ui/Code.tsx new file mode 100644 index 0000000..08c6002 --- /dev/null +++ b/components/ui/Code.tsx @@ -0,0 +1,29 @@ +'use client' + +import React, {ReactNode, useRef, useState} from 'react' +import {CopyIcon} from '@/components/icons/CopyIcon' +import {CheckIcon} from '@/components/icons/CheckIcon' + +export function Code({children}: { children: ReactNode }) { + const [copied, setCopied] = useState(false) + const preRef = useRef(null) + + const handleCopy = async (e: React.MouseEvent) => { + e.preventDefault() + await navigator.clipboard.writeText(preRef.current?.innerText ?? '') + setCopied(true) + setTimeout(() => setCopied(false), 1000) + } + + return ( +
+                
+            
+ {children} +
+
+ ) +} \ No newline at end of file diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index f90df4f..d52e060 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -205,7 +205,7 @@ type CommentsProps = { comments?: any } -export default function Comments({slug, comments}: CommentsProps) { +export function Comments({slug, comments}: CommentsProps) { return ( diff --git a/lib/spotify.ts b/lib/spotify.ts index b5dd0ae..6b7f334 100644 --- a/lib/spotify.ts +++ b/lib/spotify.ts @@ -45,13 +45,8 @@ export const getCurrentlyPlaying = async () => { export const getRecentlyPlayed = async () => { - try { - const {access_token}: Response = await getAccessToken() as Response - } catch(e) { - console.error(e) - return null - } - + const {access_token}: Response = await getAccessToken() as Response + return await fetch(SPOTIFY_RECENTLY_PLAYED, { headers: { Authorization: `Bearer ${access_token}` diff --git a/mdx-components.tsx b/mdx-components.tsx index 4c2e529..f6bcbbf 100644 --- a/mdx-components.tsx +++ b/mdx-components.tsx @@ -1,4 +1,6 @@ import type {MDXComponents} from 'mdx/types' +import React from 'react' +import {Code} from '@/components/ui/Code' import {Heading} from './components/ui/Heading' // This file allows you to provide custom React components @@ -11,6 +13,7 @@ export function useMDXComponents(components: MDXComponents): MDXComponents { return { h2: ({children}) => {children}, h3: ({children}) => {children}, + pre: ({children}) => {children}, // Allows customizing built-in components, e.g. to add styling. // h1: ({ children }) =>

{children}

, ...components