Skip to content

Commit 9d1856f

Browse files
authored
feat(cryptography/morse): replace some states with URL state
1 parent e6874a0 commit 9d1856f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/app/cryptography/(ciphers)/morse/_lib/morse.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ function decrypt(text: string) {
9898
return decodedChars.join('')
9999
}
100100

101-
export type Method = 'encode' | 'decode'
101+
export const Methods = ['encode', 'decode'] as const
102+
export type Method = (typeof Methods)[number]
102103
export function morse(text: string, method: Method = 'encode') {
103104
return method === 'encode' ? encrypt(text) : decrypt(text)
104105
}

src/app/cryptography/(ciphers)/morse/page.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useState } from 'react'
44
import { Download, LucideIcon } from 'lucide-react'
5+
import { parseAsStringLiteral, useQueryState } from 'nuqs'
56

67
import { downloadText } from '~/shared/lib/download-text'
78

@@ -17,7 +18,7 @@ import {
1718
} from '~/shared/components/select'
1819
import { CopyButton } from '~/shared/components/copy-button'
1920

20-
import { Method, morse } from './_lib/morse'
21+
import { Method, Methods, morse } from './_lib/morse'
2122

2223
const ActionButton = ({
2324
title,
@@ -43,7 +44,10 @@ const ActionButton = ({
4344

4445
export default function Page() {
4546
const [plainText, setPlainText] = useState('')
46-
const [method, setMethod] = useState<Method>('encode')
47+
const [method, setMethod] = useQueryState(
48+
'method',
49+
parseAsStringLiteral(Methods).withDefault('encode')
50+
)
4751

4852
const encodedText = morse(plainText, method)
4953

@@ -73,7 +77,7 @@ export default function Page() {
7377
<div className="flex justify-center gap-2 flex-wrap">
7478
<Select
7579
onValueChange={value => setMethod(value as Method)}
76-
defaultValue="encode"
80+
defaultValue={method}
7781
>
7882
<SelectTrigger className="w-fit space-x-3">
7983
<SelectValue />

0 commit comments

Comments
 (0)