|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { ipcRenderer, clipboard } from 'electron'; |
| 3 | + |
| 4 | +const Base64 = () => { |
| 5 | + const [input, setInput] = useState('Raw data'); |
| 6 | + const [encode, setEncode] = useState(true); |
| 7 | + const [string, setString] = useState(true); |
| 8 | + const [output, setOutput] = useState(btoa('Raw data')); |
| 9 | + const [opening, setOpening] = useState(false); |
| 10 | + const [copied, setCopied] = useState(false); |
| 11 | + |
| 12 | + const handleChangeInput = (evt: { target: { value: string } }) => |
| 13 | + setInput(evt.target.value); |
| 14 | + |
| 15 | + const handleOpenInput = async () => { |
| 16 | + setOpening(true); |
| 17 | + const content = await ipcRenderer.invoke('open-file', []); |
| 18 | + if (string) { |
| 19 | + setInput(Buffer.from(content).toString()); |
| 20 | + } else { |
| 21 | + setInput(content); |
| 22 | + } |
| 23 | + setOpening(false); |
| 24 | + }; |
| 25 | + |
| 26 | + const handleClipboardInput = () => { |
| 27 | + setInput(clipboard.readText()); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleCopyOutput = () => { |
| 31 | + setCopied(true); |
| 32 | + clipboard.write({ text: output }); |
| 33 | + setTimeout(() => setCopied(false), 500); |
| 34 | + }; |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + try { |
| 38 | + if (encode) { |
| 39 | + setOutput(Buffer.from(input).toString('base64')); |
| 40 | + } else { |
| 41 | + setOutput(Buffer.from(input, 'base64').toString()); |
| 42 | + } |
| 43 | + } catch (e) { |
| 44 | + setOutput(e.message); |
| 45 | + } |
| 46 | + }, [input, encode]); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className="min-h-full flex flex-col"> |
| 50 | + <div className="flex justify-between mb-1"> |
| 51 | + <span className="flex space-x-2"> |
| 52 | + <button type="button" className="btn" onClick={handleClipboardInput}> |
| 53 | + Clipboard |
| 54 | + </button> |
| 55 | + <button |
| 56 | + type="button" |
| 57 | + className="btn" |
| 58 | + onClick={handleOpenInput} |
| 59 | + disabled={opening} |
| 60 | + > |
| 61 | + Open... |
| 62 | + </button> |
| 63 | + <div className="flex space-x-2 items-center"> |
| 64 | + <label htmlFor="string" className="flex items-center space-x-1"> |
| 65 | + <input |
| 66 | + type="radio" |
| 67 | + className="btn" |
| 68 | + name="string" |
| 69 | + id="string" |
| 70 | + checked={string} |
| 71 | + onChange={() => setString(true)} |
| 72 | + /> |
| 73 | + <p>Text</p> |
| 74 | + </label> |
| 75 | + <label htmlFor="raw" className="flex items-center space-x-1"> |
| 76 | + <input |
| 77 | + type="radio" |
| 78 | + className="btn" |
| 79 | + name="string" |
| 80 | + id="raw" |
| 81 | + checked={!string} |
| 82 | + onChange={() => setString(false)} |
| 83 | + /> |
| 84 | + <p>Binary</p> |
| 85 | + </label> |
| 86 | + </div> |
| 87 | + </span> |
| 88 | + <span className="flex space-x-4"> |
| 89 | + <div className="flex space-x-4 items-center"> |
| 90 | + <label htmlFor="encoder" className="flex items-center space-x-1"> |
| 91 | + <input |
| 92 | + type="radio" |
| 93 | + className="btn" |
| 94 | + name="encode" |
| 95 | + id="encoder" |
| 96 | + checked={encode} |
| 97 | + onChange={() => setEncode(true)} |
| 98 | + /> |
| 99 | + <p>Encode</p> |
| 100 | + </label> |
| 101 | + <label htmlFor="decoder" className="flex items-center space-x-1"> |
| 102 | + <input |
| 103 | + type="radio" |
| 104 | + className="btn" |
| 105 | + name="encode" |
| 106 | + id="decoder" |
| 107 | + checked={!encode} |
| 108 | + onChange={() => setEncode(false)} |
| 109 | + /> |
| 110 | + <p>Decode</p> |
| 111 | + </label> |
| 112 | + </div> |
| 113 | + <button |
| 114 | + type="button" |
| 115 | + className="btn" |
| 116 | + onClick={handleCopyOutput} |
| 117 | + disabled={copied} |
| 118 | + > |
| 119 | + {copied ? 'Copied' : 'Copy'} |
| 120 | + </button> |
| 121 | + </span> |
| 122 | + </div> |
| 123 | + <div className="flex min-h-full flex-1"> |
| 124 | + <textarea |
| 125 | + onChange={handleChangeInput} |
| 126 | + className="flex-1 min-h-full bg-white p-4 rounded-md" |
| 127 | + value={input} |
| 128 | + disabled={opening} |
| 129 | + /> |
| 130 | + <div className="mx-1" /> |
| 131 | + <textarea |
| 132 | + className="flex-1 min-h-full bg-gray-100 p-4 rounded-md" |
| 133 | + value={output} |
| 134 | + readOnly |
| 135 | + /> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +export default Base64; |
0 commit comments