|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { Copy, Check } from 'lucide-react'; |
| 5 | + |
| 6 | +export function UrlConverter() { |
| 7 | + const [mode, setMode] = useState<'google-drive' | 'custom-domain'>('google-drive'); |
| 8 | + const [inputUrl, setInputUrl] = useState(''); |
| 9 | + const [inputPassword, setInputPassword] = useState(''); |
| 10 | + const [outputUrl, setOutputUrl] = useState(''); |
| 11 | + const [copied, setCopied] = useState(false); |
| 12 | + |
| 13 | + const encodeBase64Utf8 = (str: string) => { |
| 14 | + try { |
| 15 | + return btoa(encodeURIComponent(str)); |
| 16 | + } catch { |
| 17 | + try { |
| 18 | + return btoa(str); |
| 19 | + } catch { |
| 20 | + return ''; |
| 21 | + } |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + const extractAndConvert = (url: string) => { |
| 26 | + const match = url.match(/\/d\/([a-zA-Z0-9-_]+)/); |
| 27 | + if (match && match[1]) { |
| 28 | + const fileId = match[1]; |
| 29 | + let downloadUrl = `https://drive.google.com/uc?export=download&id=${fileId}`; |
| 30 | + if (inputPassword && inputPassword.length > 0) { |
| 31 | + const encoded = encodeBase64Utf8(inputPassword); |
| 32 | + if (encoded) downloadUrl = `${downloadUrl}#${encoded}`; |
| 33 | + } |
| 34 | + setOutputUrl(downloadUrl); |
| 35 | + return downloadUrl; |
| 36 | + } |
| 37 | + setOutputUrl(''); |
| 38 | + return null; |
| 39 | + }; |
| 40 | + |
| 41 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 42 | + const value = e.target.value; |
| 43 | + setInputUrl(value); |
| 44 | + if (mode === 'google-drive') { |
| 45 | + extractAndConvert(value); |
| 46 | + } else { |
| 47 | + // For custom domain, just update the output URL directly |
| 48 | + let customUrl = value; |
| 49 | + if (inputPassword && inputPassword.length > 0) { |
| 50 | + const encoded = encodeBase64Utf8(inputPassword); |
| 51 | + if (encoded) customUrl = `${customUrl}#${encoded}`; |
| 52 | + } |
| 53 | + setOutputUrl(customUrl || ''); |
| 54 | + } |
| 55 | + setCopied(false); |
| 56 | + }; |
| 57 | + |
| 58 | + const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 59 | + const value = e.target.value; |
| 60 | + setInputPassword(value); |
| 61 | + // Recompute output using the existing URL and new password |
| 62 | + if (inputUrl) { |
| 63 | + if (mode === 'google-drive') { |
| 64 | + extractAndConvert(inputUrl); |
| 65 | + } else { |
| 66 | + // For custom domain, recompute with new password |
| 67 | + let customUrl = inputUrl; |
| 68 | + if (value && value.length > 0) { |
| 69 | + const encoded = encodeBase64Utf8(value); |
| 70 | + if (encoded) customUrl = `${inputUrl}#${encoded}`; |
| 71 | + } |
| 72 | + setOutputUrl(customUrl || ''); |
| 73 | + } |
| 74 | + } |
| 75 | + setCopied(false); |
| 76 | + }; |
| 77 | + |
| 78 | + const handleModeChange = (e: React.ChangeEvent<HTMLSelectElement>) => { |
| 79 | + const newMode = e.target.value as 'google-drive' | 'custom-domain'; |
| 80 | + setMode(newMode); |
| 81 | + setInputUrl(''); |
| 82 | + setInputPassword(''); |
| 83 | + setOutputUrl(''); |
| 84 | + setCopied(false); |
| 85 | + }; |
| 86 | + |
| 87 | + const copyToClipboard = async () => { |
| 88 | + if (outputUrl) { |
| 89 | + await navigator.clipboard.writeText(outputUrl); |
| 90 | + setCopied(true); |
| 91 | + setTimeout(() => setCopied(false), 2000); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + return ( |
| 96 | + <div className="rounded-lg border border-fd-border bg-fd-card p-6 my-6"> |
| 97 | + <h3 className="font-semibold mb-4"> |
| 98 | + {mode === 'google-drive' |
| 99 | + ? 'Google Drive Direct Link Converter' |
| 100 | + : 'Custom URL Converter'} |
| 101 | + </h3> |
| 102 | + |
| 103 | + <div className="space-y-4"> |
| 104 | + <div> |
| 105 | + <label className="block text-sm font-medium mb-2"> |
| 106 | + Conversion Mode: |
| 107 | + </label> |
| 108 | + <select |
| 109 | + value={mode} |
| 110 | + onChange={handleModeChange} |
| 111 | + className="w-full px-3 py-2 border border-fd-border rounded-md bg-fd-background text-fd-foreground focus:outline-none focus:ring-2 focus:ring-fd-primary" |
| 112 | + > |
| 113 | + <option value="google-drive">Google Drive Link</option> |
| 114 | + <option value="custom-domain">Custom Domain</option> |
| 115 | + </select> |
| 116 | + </div> |
| 117 | + |
| 118 | + <div> |
| 119 | + <label className="block text-sm font-medium mb-2"> |
| 120 | + {mode === 'google-drive' |
| 121 | + ? 'Paste your Google Drive Share Link:' |
| 122 | + : 'Paste your Custom Domain URL:'} |
| 123 | + </label> |
| 124 | + <input |
| 125 | + type="text" |
| 126 | + value={inputUrl} |
| 127 | + onChange={handleInputChange} |
| 128 | + placeholder={ |
| 129 | + mode === 'google-drive' |
| 130 | + ? 'https://drive.google.com/file/d/YOUR_FILE_ID/view?usp=sharing' |
| 131 | + : 'https://example.com/path/to/file' |
| 132 | + } |
| 133 | + className="w-full px-3 py-2 border border-fd-border rounded-md bg-fd-background text-fd-foreground placeholder-fd-muted-foreground focus:outline-none focus:ring-2 focus:ring-fd-primary" |
| 134 | + /> |
| 135 | + </div> |
| 136 | + |
| 137 | + <div> |
| 138 | + <label className="block text-sm font-medium mb-2"> |
| 139 | + Optional avatar password (will be base64-encoded and appended) |
| 140 | + </label> |
| 141 | + <input |
| 142 | + type="password" |
| 143 | + value={inputPassword} |
| 144 | + onChange={handlePasswordChange} |
| 145 | + placeholder="Optional password" |
| 146 | + className="w-full px-3 py-2 border border-fd-border rounded-md bg-fd-background text-fd-foreground placeholder-fd-muted-foreground focus:outline-none focus:ring-2 focus:ring-fd-primary" |
| 147 | + /> |
| 148 | + </div> |
| 149 | + |
| 150 | + {outputUrl && ( |
| 151 | + <div> |
| 152 | + <label className="block text-sm font-medium mb-2"> |
| 153 | + Direct Download Link: |
| 154 | + </label> |
| 155 | + <div className="flex gap-2"> |
| 156 | + <input |
| 157 | + type="text" |
| 158 | + value={outputUrl} |
| 159 | + readOnly |
| 160 | + className="flex-1 px-3 py-2 border border-fd-border rounded-md bg-fd-background text-fd-foreground" |
| 161 | + /> |
| 162 | + <button |
| 163 | + onClick={copyToClipboard} |
| 164 | + className="flex items-center gap-2 px-4 py-2 bg-fd-primary text-fd-primary-foreground rounded-md hover:opacity-90 transition-opacity" |
| 165 | + > |
| 166 | + {copied ? ( |
| 167 | + <> |
| 168 | + <Check size={18} /> |
| 169 | + <span>Copied!</span> |
| 170 | + </> |
| 171 | + ) : ( |
| 172 | + <> |
| 173 | + <Copy size={18} /> |
| 174 | + <span>Copy</span> |
| 175 | + </> |
| 176 | + )} |
| 177 | + </button> |
| 178 | + </div> |
| 179 | + <p className="text-sm text-fd-muted-foreground mt-2"> |
| 180 | + Use this link as your <strong>BEE File URL</strong> in Basis |
| 181 | + </p> |
| 182 | + </div> |
| 183 | + )} |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + ); |
| 187 | +} |
0 commit comments