From b602794beb4e555bb7511c65199bd7f4fd50d994 Mon Sep 17 00:00:00 2001 From: Paul Butler Date: Sat, 8 Feb 2025 14:06:42 -0500 Subject: [PATCH] faster --- app/encoder-decoder-content.tsx | 49 ++++++++++----------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/app/encoder-decoder-content.tsx b/app/encoder-decoder-content.tsx index d68e78b..3e1a568 100644 --- a/app/encoder-decoder-content.tsx +++ b/app/encoder-decoder-content.tsx @@ -14,25 +14,17 @@ export function Base64EncoderDecoderContent() { const router = useRouter() const searchParams = useSearchParams() - // Read input state from URL parameters + // Read mode from URL parameters, other state stored locally const mode = searchParams.get("mode") || "encode" - const inputText = searchParams.get("input") || "" - const selectedEmoji = searchParams.get("emoji") || "😀" - - // Store output state locally + const [inputText, setInputText] = useState("") + const [selectedEmoji, setSelectedEmoji] = useState("😀") const [outputText, setOutputText] = useState("") const [errorText, setErrorText] = useState("") - // Update URL when input state changes - const updateURL = (updates: Record) => { + // Update URL when mode changes + const updateMode = (newMode: string) => { const params = new URLSearchParams(searchParams) - Object.entries(updates).forEach(([key, value]) => { - if (value) { - params.set(key, value) - } else { - params.delete(key) - } - }) + params.set("mode", newMode) router.replace(`?${params.toString()}`) } @@ -50,29 +42,16 @@ export function Base64EncoderDecoderContent() { }, [mode, selectedEmoji, inputText]) const handleModeToggle = (checked: boolean) => { - const newMode = checked ? "encode" : "decode" - const params = { - mode: newMode, - input: "" - } - updateURL(newMode === "decode" ? params : { ...params, emoji: "" }) - } - - const handleEmojiSelect = (emoji: string) => { - updateURL({ emoji }) - } - - const handleInputChange = (value: string) => { - updateURL({ input: value }) + updateMode(checked ? "encode" : "decode") + setInputText("") // Clear input text when mode changes } // Handle initial URL state useEffect(() => { - const params = new URLSearchParams(searchParams) - if (!params.has("mode")) { - updateURL({ mode: "encode" }) + if (!searchParams.has("mode")) { + updateMode("encode") } - }, []) + }, [searchParams, updateMode]) const isEncoding = mode === "encode" @@ -89,13 +68,13 @@ export function Base64EncoderDecoderContent() {