-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Kinfe123/feat/code_preview
Feat/code preview
- Loading branch information
Showing
10 changed files
with
218 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,95 @@ | ||
'use client' | ||
"use client"; | ||
|
||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "@/components/ui/dropdown-menu" | ||
import { Button, ButtonProps } from "@/components/ui/button" | ||
import { CheckIcon, ClipboardIcon , } from "lucide-react" | ||
import React from "react" | ||
import { cn } from "@/lib/utils" | ||
import { CommandList } from "cmdk" | ||
import { Separator } from "@/components/ui/separator" | ||
|
||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "@/components/ui/dropdown-menu"; | ||
import { Button, ButtonProps } from "@/components/ui/button"; | ||
import { CheckIcon, ClipboardIcon } from "lucide-react"; | ||
import React from "react"; | ||
import { cn } from "@/lib/utils"; | ||
import { CommandList } from "cmdk"; | ||
import { Separator } from "@/components/ui/separator"; | ||
import { TrackClick } from "@loglib/tracker/react"; | ||
|
||
interface CopyButtonProps extends ButtonProps { | ||
value: string | ||
src?: string | ||
} | ||
value: string; | ||
src?: string; | ||
} | ||
|
||
function copyToClipboardWithMeta(value: string) { | ||
navigator.clipboard.writeText(value); | ||
} | ||
export function CopyNpmCommandButton({ | ||
commands, | ||
className, | ||
}: { | ||
commands: string; | ||
className?: string; | ||
}) { | ||
const [hasCopied, setHasCopied] = React.useState(false); | ||
|
||
function copyToClipboardWithMeta(value: string,) { | ||
navigator.clipboard.writeText(value) | ||
} | ||
export function CopyNpmCommandButton({commands , className} : {commands: string , className?:string}) { | ||
const [hasCopied, setHasCopied] = React.useState(false) | ||
|
||
React.useEffect(() => { | ||
setTimeout(() => { | ||
setHasCopied(false) | ||
}, 2000) | ||
}, [hasCopied]) | ||
|
||
const copyCommand = React.useCallback( | ||
(value: string, pm: "npm" | "pnpm" | "yarn" | "bun") => { | ||
copyToClipboardWithMeta(pm + " " + value) | ||
setHasCopied(true) | ||
}, | ||
[] | ||
) | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button | ||
size="icon" | ||
variant="ghost" | ||
className={cn( | ||
"relative z-20 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50", | ||
className | ||
)} | ||
> | ||
{hasCopied ? ( | ||
<CheckIcon className="h-4 w-4 mx-auto" /> | ||
) : ( | ||
<ClipboardIcon className="h-4 w-4 mx-auto" /> | ||
)} | ||
<span className="sr-only">Copy</span> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="start" className="z-20 bg-black/80"> | ||
<DropdownMenuItem | ||
onClick={() => copyCommand(commands, "npm")} | ||
> | ||
React.useEffect(() => { | ||
setTimeout(() => { | ||
setHasCopied(false); | ||
}, 2000); | ||
}, [hasCopied]); | ||
|
||
npm | ||
</DropdownMenuItem> | ||
<Separator className="border-[1px] border-white/10"/> | ||
<DropdownMenuItem | ||
onClick={() => copyCommand(commands, "yarn")} | ||
> | ||
yarn | ||
</DropdownMenuItem> | ||
<Separator className="border-[1px] border-white/10"/> | ||
const copyCommand = React.useCallback( | ||
(value: string, pm: "npm" | "pnpm" | "yarn" | "bun") => { | ||
copyToClipboardWithMeta(pm + " " + value); | ||
setHasCopied(true); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button | ||
size="icon" | ||
variant="ghost" | ||
className={cn( | ||
"relative z-20 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50", | ||
className | ||
)} | ||
> | ||
{hasCopied ? ( | ||
<CheckIcon className="h-4 w-4 mx-auto" /> | ||
) : ( | ||
<TrackClick | ||
name="Installing components" | ||
payload={{ | ||
click: "command copied", | ||
}} | ||
> | ||
<ClipboardIcon className="h-4 w-4 mx-auto" /> | ||
</TrackClick> | ||
)} | ||
<span className="sr-only">Copy</span> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="start" className="z-20 bg-black/80"> | ||
<DropdownMenuItem onClick={() => copyCommand(commands, "npm")}> | ||
npm | ||
</DropdownMenuItem> | ||
<Separator className="border-[1px] border-white/10" /> | ||
<DropdownMenuItem onClick={() => copyCommand(commands, "yarn")}> | ||
yarn | ||
</DropdownMenuItem> | ||
<Separator className="border-[1px] border-white/10" /> | ||
|
||
<DropdownMenuItem | ||
onClick={() => copyCommand(commands, "pnpm")} | ||
> | ||
pnpm | ||
</DropdownMenuItem> | ||
<Separator className="border-[1px] border-white/10"/> | ||
<DropdownMenuItem onClick={() => copyCommand(commands, "pnpm")}> | ||
pnpm | ||
</DropdownMenuItem> | ||
<Separator className="border-[1px] border-white/10" /> | ||
|
||
<DropdownMenuItem | ||
onClick={() => copyCommand(commands, "bun")} | ||
> | ||
bun | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
) | ||
} | ||
<DropdownMenuItem onClick={() => copyCommand(commands, "bun")}> | ||
bun | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.