import React, { useState } from 'react'; const morseCodeMap = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----', ' ': '/' }; // Create a reverse map for decoding const reverseMorseCodeMap = Object.entries(morseCodeMap).reduce((acc, [key, value]) => { acc[value] = key; return acc; }, {}); const MorseCodeConverter = ({ addToMessage }) => { const [input, setInput] = useState(''); const [output, setOutput] = useState(''); const [copied, setCopied] = useState(false); const [toMorse, setToMorse] = useState(true); // true for text to morse, false for morse to text const convertToMorse = (text) => { let morse = ''; for (let char of text.toUpperCase()) { if (morseCodeMap[char]) { morse += morseCodeMap[char] + ' '; } else { morse += '? '; // Represent unknown characters } } setOutput(morse.trim()); }; const convertFromMorse = (morse) => { let text = ''; const morseChars = morse.split(' '); for (let morseChar of morseChars) { if (reverseMorseCodeMap[morseChar]) { text += reverseMorseCodeMap[morseChar]; } else if (morseChar === '/') { text += ' '; } } setOutput(text); }; const handleInputChange = (e) => { const newText = e.target.value; setInput(newText); if (toMorse) { convertToMorse(newText); } else { convertFromMorse(newText); } }; const copyToClipboard = () => { navigator.clipboard.writeText(output); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const swapConversion = () => { const oldInput = input; setInput(output); setOutput(oldInput); setToMorse(!toMorse); }; return (

Morse Code Converter