Skip to content

armgit / mapcn

Public Imported from GitHub
0

Beautiful map components. 100% Free, Zero config, one command setup.

TypeScript
2.6 KB
Raw Blame
"use client";

import { useState } from "react";
import { Check, Copy } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { getCopyLabel, useCopy } from "@/hooks/use-copy";
import { trackEvent } from "@/lib/events";

interface InstallCommandProps {
  /** What to add, e.g. "@mapcn/map" */
  name: string;
  /** The CLI and subcommand to run it with */
  cli?: string;
}

const PACKAGE_MANAGERS = [
  { manager: "pnpm", exec: "pnpm dlx" },
  { manager: "npm", exec: "npx" },
  { manager: "yarn", exec: "yarn dlx" },
  { manager: "bun", exec: "bunx --bun" },
] as const;

export function InstallCommand({
  name,
  cli = "shadcn@latest add",
}: InstallCommandProps) {
  const tabs = PACKAGE_MANAGERS.map(({ manager, exec }) => ({
    manager,
    command: `${exec} ${cli} ${name}`,
  }));

  const [active, setActive] = useState<string>(tabs[0].manager);
  const { status, copy } = useCopy();

  const copyCommand = async () => {
    const current = tabs.find((tab) => tab.manager === active);
    if (!current || !(await copy(current.command))) return;
    trackEvent({
      name: "copy_install_command",
      properties: { name, cli, packageManager: active },
    });
  };

  return (
    <div className="bg-surface relative w-full overflow-hidden rounded-lg">
      <Tabs value={active} onValueChange={setActive} className="gap-0">
        <div className="flex items-center justify-between border-b pr-2 pl-[5px]">
          <TabsList
            variant="line"
            className="h-10 bg-transparent group-data-[orientation=horizontal]/tabs:h-10"
          >
            {tabs.map((tab) => (
              <TabsTrigger
                key={tab.manager}
                value={tab.manager}
                className="font-mono text-xs"
              >
                {tab.manager}
              </TabsTrigger>
            ))}
          </TabsList>
          <Button
            variant="ghost"
            size="icon-sm"
            onClick={copyCommand}
            aria-label={getCopyLabel(status, "Copy command")}
            className="text-muted-foreground"
          >
            {status === "copied" ? <Check /> : <Copy />}
          </Button>
        </div>
        {tabs.map((tab) => (
          <TabsContent key={tab.manager} value={tab.manager}>
            <pre className="overflow-x-auto p-4 text-sm">
              <code className="font-mono" data-language="bash">
                {tab.command}
              </code>
            </pre>
          </TabsContent>
        ))}
      </Tabs>
    </div>
  );
}