"use client" import * as React from "react" import { IconCheck, IconCopy, IconPlus } from "@tabler/icons-react" import { useConfig } from "@/hooks/use-config" import { useIsMobile } from "@/hooks/use-mobile" import { copyToClipboardWithMeta } from "@/components/copy-button" import { Button } from "@/styles/base-nova/ui/button" import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/styles/base-nova/ui/dialog" import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, } from "@/styles/base-nova/ui/drawer" import { Tabs, TabsContent, TabsList, TabsTrigger, } from "@/styles/base-nova/ui/tabs" import { Tooltip, TooltipContent, TooltipTrigger, } from "@/styles/base-nova/ui/tooltip" const DirectoryAddContext = React.createContext<{ open: (registry: { name: string }) => void }>({ open: () => {}, }) export function useDirectoryAdd() { return React.useContext(DirectoryAddContext) } export function DirectoryAddButton({ registry, }: { registry: { name: string } }) { const { open } = useDirectoryAdd() return ( open(registry)} > Add ) } export function DirectoryAddProvider({ children, }: { children: React.ReactNode }) { const [config, setConfig] = useConfig() const [hasCopied, setHasCopied] = React.useState(false) const [isOpen, setIsOpen] = React.useState(false) const [selectedRegistry, setSelectedRegistry] = React.useState<{ name: string } | null>(null) const isMobile = useIsMobile() const packageManager = config.packageManager || "pnpm" const commands = React.useMemo(() => { if (!selectedRegistry) return null return { pnpm: `pnpm dlx shadcn@latest registry add ${selectedRegistry.name}`, npm: `npx shadcn@latest registry add ${selectedRegistry.name}`, yarn: `yarn dlx shadcn@latest registry add ${selectedRegistry.name}`, bun: `bunx --bun shadcn@latest registry add ${selectedRegistry.name}`, } }, [selectedRegistry]) const command = commands?.[packageManager] ?? "" React.useEffect(() => { if (hasCopied) { const timer = setTimeout(() => setHasCopied(false), 2000) return () => clearTimeout(timer) } }, [hasCopied]) const handleCopy = React.useCallback(() => { copyToClipboardWithMeta(command, { name: "copy_registry_add_command", properties: { command, registry: selectedRegistry?.name ?? "", }, }) setHasCopied(true) }, [command, selectedRegistry?.name]) const contextValue = React.useMemo( () => ({ open: (registry: { name: string }) => { setSelectedRegistry(registry) setIsOpen(true) }, }), [] ) const Content = commands ? ( { setConfig({ ...config, packageManager: value as "pnpm" | "npm" | "yarn" | "bun", }) }} className="gap-0 overflow-hidden rounded-xl border" > pnpm npm yarn bun } > {hasCopied ? : } Copy command {hasCopied ? "Copied!" : "Copy command"} {Object.entries(commands).map(([key, cmd]) => ( {cmd} ))} ) : null return ( {children} {isMobile ? ( Add Registry Run this command to add {selectedRegistry?.name} to your project. {Content} }>Done ) : ( Add Registry Run this command to add {selectedRegistry?.name} to your project. {Content} }> Done )} ) }
{cmd}