Skip to content

Commit

Permalink
feat(cryptography/morse): replace some states with URL state
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 authored Mar 14, 2024
1 parent e6874a0 commit 9d1856f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/app/cryptography/(ciphers)/morse/_lib/morse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 7 additions & 3 deletions src/app/cryptography/(ciphers)/morse/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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,
Expand All @@ -43,7 +44,10 @@ const ActionButton = ({

export default function Page() {
const [plainText, setPlainText] = useState('')
const [method, setMethod] = useState<Method>('encode')
const [method, setMethod] = useQueryState(
'method',
parseAsStringLiteral(Methods).withDefault('encode')
)

const encodedText = morse(plainText, method)

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

0 comments on commit 9d1856f

Please sign in to comment.