From 9d1856ff580bac09e4f19821dc8b3db8a561bba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Felipe=20Gon=C3=A7alves?= Date: Thu, 14 Mar 2024 17:11:15 +0000 Subject: [PATCH] feat(cryptography/morse): replace some states with URL state --- src/app/cryptography/(ciphers)/morse/_lib/morse.ts | 3 ++- src/app/cryptography/(ciphers)/morse/page.tsx | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/app/cryptography/(ciphers)/morse/_lib/morse.ts b/src/app/cryptography/(ciphers)/morse/_lib/morse.ts index 8bed227..17af69c 100644 --- a/src/app/cryptography/(ciphers)/morse/_lib/morse.ts +++ b/src/app/cryptography/(ciphers)/morse/_lib/morse.ts @@ -98,7 +98,8 @@ function decrypt(text: string) { return decodedChars.join('') } -export type Method = 'encode' | 'decode' +export const Methods = ['encode', 'decode'] as const +export type Method = (typeof Methods)[number] export function morse(text: string, method: Method = 'encode') { return method === 'encode' ? encrypt(text) : decrypt(text) } diff --git a/src/app/cryptography/(ciphers)/morse/page.tsx b/src/app/cryptography/(ciphers)/morse/page.tsx index 0d69fbe..c32865c 100644 --- a/src/app/cryptography/(ciphers)/morse/page.tsx +++ b/src/app/cryptography/(ciphers)/morse/page.tsx @@ -2,6 +2,7 @@ import { useState } from 'react' import { Download, LucideIcon } from 'lucide-react' +import { parseAsStringLiteral, useQueryState } from 'nuqs' import { downloadText } from '~/shared/lib/download-text' @@ -17,7 +18,7 @@ import { } from '~/shared/components/select' import { CopyButton } from '~/shared/components/copy-button' -import { Method, morse } from './_lib/morse' +import { Method, Methods, morse } from './_lib/morse' const ActionButton = ({ title, @@ -43,7 +44,10 @@ const ActionButton = ({ export default function Page() { const [plainText, setPlainText] = useState('') - const [method, setMethod] = useState('encode') + const [method, setMethod] = useQueryState( + 'method', + parseAsStringLiteral(Methods).withDefault('encode') + ) const encodedText = morse(plainText, method) @@ -73,7 +77,7 @@ export default function Page() {