|
| 1 | +import { useState } from "react"; |
| 2 | +import { Card, PageHeader, CopyButton, s } from "./shared/index.jsx"; |
| 3 | + |
| 4 | +const labelStyle = { |
| 5 | + display: "block", fontSize: "var(--xs)", fontFamily: "var(--font-mono)", |
| 6 | + textTransform: "uppercase", letterSpacing: "0.1em", color: "var(--text-faint)", marginBottom: 6, |
| 7 | +}; |
| 8 | + |
| 9 | +function ResultBox({ label, value, color, error }) { |
| 10 | + return ( |
| 11 | + <div> |
| 12 | + <label style={labelStyle}>{label}</label> |
| 13 | + <div style={{ |
| 14 | + background: "var(--surface-2)", border: `2px solid ${error ? "var(--red-dim)" : "var(--border-2)"}`, |
| 15 | + borderRadius: 10, padding: "12px 14px", |
| 16 | + display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 10, minHeight: 52, |
| 17 | + }}> |
| 18 | + <span style={{ |
| 19 | + fontFamily: "var(--font-mono)", fontSize: "var(--sm)", fontWeight: 500, |
| 20 | + color: error ? "var(--red)" : value ? color : "var(--text-faint)", |
| 21 | + wordBreak: "break-all", flex: 1, lineHeight: 1.6, |
| 22 | + }}> |
| 23 | + {value || "—"} |
| 24 | + </span> |
| 25 | + {value && !error && <CopyButton text={value} />} |
| 26 | + </div> |
| 27 | + </div> |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +export default function Base64Codec() { |
| 32 | + const [encoded, setEncoded] = useState(""); |
| 33 | + const [decoded, setDecoded] = useState(""); |
| 34 | + |
| 35 | + const decodedResult = (() => { |
| 36 | + if (!encoded) return ""; |
| 37 | + try { |
| 38 | + return decodeURIComponent(escape(atob(encoded))); |
| 39 | + } catch { |
| 40 | + return "Error: invalid Base64 input"; |
| 41 | + } |
| 42 | + })(); |
| 43 | + |
| 44 | + const encodedResult = (() => { |
| 45 | + if (!decoded) return ""; |
| 46 | + try { |
| 47 | + return btoa(unescape(encodeURIComponent(decoded))); |
| 48 | + } catch { |
| 49 | + return "Error: unable to encode input"; |
| 50 | + } |
| 51 | + })(); |
| 52 | + |
| 53 | + const decodeError = decodedResult.startsWith("Error:"); |
| 54 | + const encodeError = encodedResult.startsWith("Error:"); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="page-enter"> |
| 58 | + <PageHeader title="Base64 encode / decode" badge="encoding" description="Decode Base64 strings to plain text or encode plain text to Base64." /> |
| 59 | + |
| 60 | + <Card title="Decode"> |
| 61 | + <label style={labelStyle}>Base64 string</label> |
| 62 | + <textarea |
| 63 | + value={encoded} |
| 64 | + onChange={(e) => setEncoded(e.target.value)} |
| 65 | + rows={3} |
| 66 | + placeholder="Paste a Base64-encoded string…" |
| 67 | + style={{ ...s.monoInput, resize: "vertical", fontSize: "var(--sm)", color: "var(--amber)" }} |
| 68 | + /> |
| 69 | + <div style={{ display: "flex", gap: 8, marginTop: 10, marginBottom: "1.2rem" }}> |
| 70 | + <button onClick={() => setEncoded("")} |
| 71 | + style={{ ...s.presetBtn, padding: "6px 14px", fontSize: "var(--xs)" }}> |
| 72 | + Clear |
| 73 | + </button> |
| 74 | + {decodedResult && !decodeError && ( |
| 75 | + <button onClick={() => { setDecoded(decodedResult); setEncoded(""); }} |
| 76 | + style={{ ...s.presetBtn, padding: "6px 14px", fontSize: "var(--xs)" }}> |
| 77 | + Use as encode input |
| 78 | + </button> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + |
| 82 | + <div style={{ height: 1, background: "var(--border)", marginBottom: "1.2rem" }} /> |
| 83 | + |
| 84 | + <ResultBox label="Decoded output" value={decodedResult} color="var(--green)" error={decodeError} /> |
| 85 | + </Card> |
| 86 | + |
| 87 | + <Card title="Encode"> |
| 88 | + <label style={labelStyle}>Plain text</label> |
| 89 | + <textarea |
| 90 | + value={decoded} |
| 91 | + onChange={(e) => setDecoded(e.target.value)} |
| 92 | + rows={3} |
| 93 | + placeholder="Paste plain text to encode as Base64…" |
| 94 | + style={{ ...s.monoInput, resize: "vertical", fontSize: "var(--sm)", color: "var(--teal)" }} |
| 95 | + /> |
| 96 | + <div style={{ display: "flex", gap: 8, marginTop: 10, marginBottom: "1.2rem" }}> |
| 97 | + <button onClick={() => setDecoded("")} |
| 98 | + style={{ ...s.presetBtn, padding: "6px 14px", fontSize: "var(--xs)" }}> |
| 99 | + Clear |
| 100 | + </button> |
| 101 | + {encodedResult && !encodeError && ( |
| 102 | + <button onClick={() => { setEncoded(encodedResult); setDecoded(""); }} |
| 103 | + style={{ ...s.presetBtn, padding: "6px 14px", fontSize: "var(--xs)" }}> |
| 104 | + Use as decode input |
| 105 | + </button> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + |
| 109 | + <div style={{ height: 1, background: "var(--border)", marginBottom: "1.2rem" }} /> |
| 110 | + |
| 111 | + <ResultBox label="Encoded output" value={encodedResult} color="var(--amber)" error={encodeError} /> |
| 112 | + </Card> |
| 113 | + </div> |
| 114 | + ); |
| 115 | +} |
0 commit comments