|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import { ArrowLeftIcon } from '@phosphor-icons/react'; |
| 4 | +import colors from '../../config/colors'; |
| 5 | +import usePageTitle from '../../utils/usePageTitle'; |
| 6 | +import { useToast } from '../../hooks/useToast'; |
| 7 | + |
| 8 | +function UuidGeneratorPage() { |
| 9 | + usePageTitle('UUID Generator'); |
| 10 | + const [uuid, setUuid] = useState(''); |
| 11 | + const { addToast } = useToast(); |
| 12 | + |
| 13 | + const generateUuidV4 = () => { |
| 14 | + try { |
| 15 | + // RFC 4122 v4 UUID generation |
| 16 | + const uuidV4 = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
| 17 | + const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16; // Use crypto.getRandomValues for better randomness |
| 18 | + const v = c === 'x' ? r : (r & 0x3 | 0x8); |
| 19 | + return v.toString(16); |
| 20 | + }); |
| 21 | + setUuid(uuidV4); |
| 22 | + } catch (error) { |
| 23 | + addToast({ |
| 24 | + title: 'Error', |
| 25 | + message: 'Failed to generate UUID.', |
| 26 | + duration: 3000, |
| 27 | + }); |
| 28 | + setUuid(''); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + const copyToClipboard = (text) => { |
| 33 | + navigator.clipboard.writeText(text) |
| 34 | + .then(() => { |
| 35 | + addToast({ |
| 36 | + title: 'Success', |
| 37 | + message: 'Copied to clipboard!', |
| 38 | + duration: 2000, |
| 39 | + }); |
| 40 | + }) |
| 41 | + .catch(() => { |
| 42 | + addToast({ |
| 43 | + title: 'Error', |
| 44 | + message: 'Failed to copy!', |
| 45 | + duration: 2000, |
| 46 | + }); |
| 47 | + }); |
| 48 | + }; |
| 49 | + |
| 50 | + const cardStyle = { |
| 51 | + backgroundColor: colors['app-alpha-10'], |
| 52 | + borderColor: colors['app-alpha-50'], |
| 53 | + color: colors.app, |
| 54 | + }; |
| 55 | + |
| 56 | + const detailTextColor = colors['app-light']; |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="py-16 sm:py-24"> |
| 60 | + <div className="mx-auto max-w-7xl px-6 lg:px-8 text-gray-300"> |
| 61 | + <Link |
| 62 | + to="/apps" |
| 63 | + className="text-article hover:underline flex items-center justify-center gap-2 text-lg mb-4" |
| 64 | + > |
| 65 | + <ArrowLeftIcon size={24} /> Back to Apps |
| 66 | + </Link> |
| 67 | + <h1 className="text-4xl font-bold tracking-tight sm:text-6xl mb-4 flex items-center"> |
| 68 | + <span className="codex-color">fc</span> |
| 69 | + <span className="separator-color">::</span> |
| 70 | + <span className="apps-color">apps</span> |
| 71 | + <span className="separator-color">::</span> |
| 72 | + <span className="single-app-color">uuid</span> |
| 73 | + </h1> |
| 74 | + <hr className="border-gray-700" /> |
| 75 | + <div className="flex justify-center items-center mt-16"> |
| 76 | + <div |
| 77 | + className="group bg-transparent border rounded-lg shadow-2xl p-6 flex flex-col justify-between relative transform transition-all duration-300 ease-in-out scale-105 overflow-hidden h-full w-full max-w-4xl" |
| 78 | + style={cardStyle} |
| 79 | + > |
| 80 | + <div |
| 81 | + className="absolute top-0 left-0 w-full h-full opacity-10" |
| 82 | + style={{ |
| 83 | + backgroundImage: |
| 84 | + 'radial-gradient(circle, white 1px, transparent 1px)', |
| 85 | + backgroundSize: '10px 10px', |
| 86 | + }} |
| 87 | + ></div> |
| 88 | + <div className="relative z-10 p-1"> |
| 89 | + <div className="mb-4"> |
| 90 | + <label className="block text-lg font-semibold mb-2" style={{ color: cardStyle.color }}>Generated UUID v4</label> |
| 91 | + <div className="relative"> |
| 92 | + <textarea |
| 93 | + readOnly |
| 94 | + className="w-full h-24 p-4 bg-gray-800/50 font-mono resize-y border rounded-md" |
| 95 | + style={{ borderColor: cardStyle.borderColor, color: detailTextColor }} |
| 96 | + value={uuid} |
| 97 | + placeholder="Click 'Generate UUID' to create one..." |
| 98 | + /> |
| 99 | + <button |
| 100 | + onClick={() => copyToClipboard(uuid)} |
| 101 | + className="absolute top-2 right-2 px-3 py-1 bg-gray-700 text-white text-sm rounded hover:bg-gray-600" |
| 102 | + > |
| 103 | + Copy |
| 104 | + </button> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + <div className="flex justify-center gap-4 mb-4"> |
| 108 | + <button |
| 109 | + onClick={generateUuidV4} |
| 110 | + className="px-6 py-2 rounded-md text-lg font-arvo font-normal transition-colors duration-300 ease-in-out" |
| 111 | + style={{ |
| 112 | + backgroundColor: 'rgba(0, 0, 0, 0.2)', |
| 113 | + color: cardStyle.color, |
| 114 | + borderColor: cardStyle.borderColor, |
| 115 | + border: '1px solid', |
| 116 | + '--hover-bg-color': colors['app-alpha-50'], |
| 117 | + }} |
| 118 | + onMouseEnter={(e) => e.currentTarget.style.backgroundColor = 'var(--hover-bg-color)'} |
| 119 | + onMouseLeave={(e) => e.currentTarget.style.backgroundColor = 'rgba(0, 0, 0, 0.2)'} |
| 120 | + > |
| 121 | + Generate UUID v4 |
| 122 | + </button> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +export default UuidGeneratorPage; |
0 commit comments