copy.tsx
This file
Path
apps/web/src/features/code/copy.tsx
42 lines · 981 bytes · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243import { Effect } from "effect";import { Check, Copy } from "lucide-react";import { useState } from "react";
import { Button } from "@/components/ui/button";
const copyFor = (text: string, shown: (copied: boolean) => void) => Effect.tryPromise(() => navigator.clipboard.writeText(text)).pipe( Effect.andThen( Effect.sync(() => { shown(true); }) ), Effect.andThen(Effect.sleep("2 seconds")), Effect.andThen( Effect.sync(() => { shown(false); }) ), Effect.ignore );
export const CopyButton = ({ label, text,}: { readonly label: string; readonly text: string;}) => { const [copied, setCopied] = useState(false);
const copy = () => { Effect.runFork(copyFor(text, setCopied)); };
return ( <Button onClick={copy} size="sm" type="button" variant="ghost"> {copied ? <Check aria-hidden="true" /> : <Copy aria-hidden="true" />} {copied ? "Copied" : label} </Button> );};