import type { Component } from 'solid-js' import type { DivProps } from '../../ui/utils-ui' import { filterAndMap, lastWriteWins } from '@wovin/core/query' import { Logger } from 'besonders-logger' import { createEffect, createMemo, createSignal, Show, splitProps, untrack } from 'solid-js' import { getMnemonicFromIDB, initializeCryptoKeypairs } from '../../data/agent/AgentCrypto' import { boundInput, useAgent } from '../../data/agent/AgentState' import { getApplogDB } from '../../data/ApplogDB' import { copyToClipboard } from '../../data/block-ui-helpers-paste' import { Iconify } from '../mini-components' const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.DEBUG) // eslint-disable-line unused-imports/no-unused-vars const persist = ['username', 'devicename', 'agentcode'] // will sync to localStorage const buttonStyle = { // '--sl-spacing-small': '6px', } export const AgentEditor: Component void }> = (allProps) => { const [props, restProps] = splitProps(allProps, ['initialSetup', 'onSave']) const agent = useAgent() const originalAg = agent.expectedAgentHash const [dirty, setDirty] = createSignal(false) const dirtyOrInitial = createMemo(() => props.initialSetup || dirty()) createEffect(() => { DEBUG(`Updating dirty flag from`, untrack(() => dirty()), 'to', !agent.isAgentStringMatching.get(), { dbLogs: filterAndMap(lastWriteWins(getApplogDB()), { en: agent.ag, at: 'agent/appAgent' }, 'vl'), }) setDirty(!agent.isAgentStringMatching.get()) }) const onChange = (_newVal) => { DEBUG(`Changed, setting dirty`, { before: dirty() }) setDirty(true) } const onSave = async () => { // agent.ensureAgentAtoms() // recycle the old mnemonic and write the new agent situation to idb and atoms const { ag, expectedAgentHash } = agent DEBUG({ ag, expectedAgentHash, originalAg }) const existingMnemonicToRecycle = await getMnemonicFromIDB(agent, originalAg) if (!existingMnemonicToRecycle) throw ERROR('saving a new agent string requires a mnemonic', { ag, expectedAgentHash, originalAg }) const cleanedMnemString = existingMnemonicToRecycle.replaceAll(' ', ',').replaceAll(',,,', ',').replaceAll(',,', ',') const cleanedMnemArray = cleanedMnemString.split(',') DEBUG({ cleanedMnemArray }) const expectedAgentHashBeforeInitialize = expectedAgentHash initializeCryptoKeypairs(agent, cleanedMnemArray, true) // recycle the previous mnemonic and skip the warning props.onSave?.() if (props.initialSetup) { WARN({ expectedAgentHashBeforeInitialize, expectedAgentHash }, '//TODO consider cleaning up IDB if this was an initial setup save') } setDirty(false) } const saveButtonLabel = () => props.initialSetup && !dirty() ? 'Continue as \'demouser\'' : 'Save' return (
{boundInput('username', persist, {}, onChange /* , !!props.initialSetup */)} . {agent.shortDID} @ { agent.app // } . { boundInput('agentcode', persist, {}, onChange) // } . { boundInput('devicename', persist, {}, onChange) // }
{/* ? might not be needed anymore */} {saveButtonLabel()} { /* */ } { copyToClipboard(agent.agentString) }} > Copy { copyToClipboard(agent.did) }} > Copy DID
) }