Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 51 additions & 24 deletions components/cipher/CipherLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useState, useEffect } from 'react'
import { useState, useEffect, useRef } from 'react'
import type { CipherDefinition } from '../../lib/cipher/registry'
import { useCipherWorker } from '../../lib/hooks/useCipherWorker'
import StepAnimator from './StepAnimator'
Expand Down Expand Up @@ -45,6 +45,7 @@ const isValidHistoryArray = (data: unknown): data is HistoryEntry[] => {

export default function CipherLayout({ cipher }: CipherLayoutProps) {
const { runCipher, loading, error: workerError } = useCipherWorker()
const abortControllerRef = useRef<AbortController | null>(null)

const [input, setInput] = useState(cipher.defaultInput)
const [key, setKey] = useState(cipher.defaultKey)
Expand All @@ -65,6 +66,9 @@ export default function CipherLayout({ cipher }: CipherLayoutProps) {

// Reset inputs when cipher changes
useEffect(() => {
if (abortControllerRef.current) {
abortControllerRef.current.abort()
}
setInput(cipher.defaultInput)
setKey(cipher.defaultKey)
setResult(null)
Expand Down Expand Up @@ -97,14 +101,27 @@ export default function CipherLayout({ cipher }: CipherLayoutProps) {
if (opt.id === 'bobSecret') setBobSecret(opt.default)
})
}

return () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort()
}
}
}, [cipher])

const handleRun = async () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort()
}
const controller = new AbortController()
abortControllerRef.current = controller

setError(null)
try {
// Gather options
const options: any = {
instrument: true, // Always request instrumented steps for visualizer
signal: controller.signal,
}

if (cipher.id === 'des' || cipher.id === '3des' || cipher.id === 'aes') {
Expand All @@ -125,35 +142,45 @@ export default function CipherLayout({ cipher }: CipherLayoutProps) {
const currentAction = cipher.id === 'dh' ? 'encrypt' : action

const res = await runCipher(currentAction, cipher.id, input, key, options)
setResult(res)
setCurrentStep(0)

if (res?.output !== undefined) {
const entry: HistoryEntry = {
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
input,
key,
action: currentAction,
output: String(res.output),
timestamp: new Date().toLocaleString(),
}

if (!controller.signal.aborted) {
setResult(res)
setCurrentStep(0)

if (res?.output !== undefined) {
const entry: HistoryEntry = {
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
input,
key,
action: currentAction,
output: String(res.output),
timestamp: new Date().toLocaleString(),
}

setHistory((prev) => {
const next = [entry, ...prev].slice(0, 5)
if (typeof window !== 'undefined') {
try {
window.localStorage.setItem(getHistoryStorageKey(cipher.id), JSON.stringify(next))
} catch (e) {
// Silently fail if localStorage is unavailable (quota exceeded, disabled, private mode, etc.)
console.warn('Failed to save history:', e)
setHistory((prev) => {
const next = [entry, ...prev].slice(0, 5)
if (typeof window !== 'undefined') {
try {
window.localStorage.setItem(getHistoryStorageKey(cipher.id), JSON.stringify(next))
} catch (e) {
// Silently fail if localStorage is unavailable (quota exceeded, disabled, private mode, etc.)
console.warn('Failed to save history:', e)
}
}
}
return next
})
return next
})
}
}
} catch (err: any) {
if (err.name === 'AbortError') {
return
}
setError(err.message || 'An error occurred during calculation.')
setResult(null)
} finally {
if (abortControllerRef.current === controller) {
abortControllerRef.current = null
}
}
}

Expand Down
Loading