From 40bfde8e57405295489afb906acad5e6294f6390 Mon Sep 17 00:00:00 2001 From: typist Date: Wed, 29 Oct 2025 09:24:01 +0800 Subject: [PATCH] feat: implement ID generator component for UUIDs and Nano ID - Added IDGenerator component to handle the display and regeneration of UUIDs and Nano ID. - Integrated clipboard functionality to copy generated IDs with user feedback via toast notifications. - Refactored the Tool component to utilize the new IDGenerator for improved user interaction and code organization. --- src/components/tool/uuid.tsx | 98 +++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 13 deletions(-) diff --git a/src/components/tool/uuid.tsx b/src/components/tool/uuid.tsx index dd67bd1..721e5b9 100644 --- a/src/components/tool/uuid.tsx +++ b/src/components/tool/uuid.tsx @@ -1,22 +1,94 @@ -import { type FC } from "react"; - +import { type FC, useState } from "react"; +import { RefreshCw, Copy } from "lucide-react"; import * as uuid from 'uuid' import { nanoid } from 'nanoid' +import { Button } from "@/components/ui/button"; +import { toast } from "sonner"; + +interface IDGeneratorProps { + label: string; + value: string; + onRegenerate: () => void; +} + +const IDGenerator: FC = ({ label, value, onRegenerate }) => { + const copyToClipboard = async () => { + try { + await navigator.clipboard.writeText(value); + toast(`${label} has been copied to clipboard`); + } catch (err) { + toast.error("Copy failed"); + } + }; + + return ( +
+ +
+ + {value} + + + +
+
+ ); +}; const Tool: FC = () => { + const [uuidV1, setUuidV1] = useState(() => uuid.v1()); + const [uuidV4, setUuidV4] = useState(() => uuid.v4()); + const [uuidV6, setUuidV6] = useState(() => uuid.v6()); + const [uuidV7, setUuidV7] = useState(() => uuid.v7()); + const [nanoId, setNanoId] = useState(() => nanoid()); + return (
- Refresh the page to generate new UUID - - {uuid.v1()} - - {uuid.v4()} - - {uuid.v6()} - - {uuid.v7()} - - {nanoid()} + Click the refresh button to regenerate the corresponding ID + + setUuidV1(uuid.v1())} + /> + + setUuidV4(uuid.v4())} + /> + + setUuidV6(uuid.v6())} + /> + + setUuidV7(uuid.v7())} + /> + + setNanoId(nanoid())} + />
); };