WIP V improve UX [snapshot task 2]

This commit is contained in:
Schluffe committed 2026-10-17 14:51:48 +02:00
1 parent 5f8c6a1916
commit a73b3075b6
16 files changed
+238 -232

No files matched your search

+30 -48
View File
@@ -1,95 +1,77 @@
<script setup lang="ts">
import { onActivated, onMounted, ref, shallowRef, watch } from 'vue';
import { onActivated, onMounted, ref, shallowRef } from 'vue';
import { clearIdentityStorage, createIdentity, loadIdentityState, saveIdentityState, type IdentityState } from '../lib/identity.ts';
import type { ConnectionUpdate } from '../lib/connections.ts';
import { asError } from '../lib/validation.ts';
const props = defineProps<{ disabled: boolean; preferredDevice?: string }>();
const emit = defineEmits<{ change: [state: IdentityState, enabled: boolean, expected: string] }>();
const props = defineProps<{ disabled: boolean; paired: string; remember: boolean }>();
const emit = defineEmits<{ update: [change: ConnectionUpdate] }>();
const supported = (() => { try { return isSecureContext && !!crypto.subtle && !!window.indexedDB; } catch { return false; } })();
const state = shallowRef<IdentityState>({ identity: null, contacts: [] });
const enabled = ref(false), expected = ref(''), working = ref(false), loaded = ref(false);
const state = shallowRef<IdentityState>({ identity: null });
const working = ref(false), loaded = ref(false);
const error = ref(''), confirmDelete = ref(false);
watch([state, enabled, expected], () => emit('change', state.value, enabled.value, expected.value));
onMounted(async () => {
if (!supported) return;
try {
state.value = await loadIdentityState(); loaded.value = true;
if (props.preferredDevice && state.value.contacts.some(item => item.key === props.preferredDevice)) expected.value = props.preferredDevice;
}
try { state.value = await loadIdentityState(); loaded.value = true; }
catch (err) { error.value = asError(err).message; }
});
onActivated(async () => {
// Another slot may have changed the shared identity or contact list while
// this workspace was cached. Never interrupt an active session to refresh it.
// Another slot may have replaced the shared identity while this workspace
// was cached. Never interrupt an active session to refresh it.
if (!loaded.value || props.disabled) return;
try {
const next = await loadIdentityState();
if (props.disabled) return;
if (next.identity?.publicKey !== state.value.identity?.publicKey) enabled.value = false;
if (!next.contacts.some(item => item.key === expected.value)) expected.value = '';
if (!expected.value && props.preferredDevice && next.contacts.some(item => item.key === props.preferredDevice)) expected.value = props.preferredDevice;
if (next.revision !== state.value.revision) confirmDelete.value = false;
state.value = next;
} catch (err) { error.value = asError(err).message; }
});
async function store(next: IdentityState) {
state.value = await saveIdentityState(next, state.value.revision);
}
async function run(action: () => Promise<void>) {
if (props.disabled || working.value) return;
working.value = true; error.value = '';
try { await action(); } catch (err) { error.value = asError(err).message; }
finally { working.value = false; }
}
function recover() { void run(async () => { if (!confirmDelete.value) return; await clearIdentityStorage(); state.value = { identity: null, contacts: [] }; loaded.value = true; confirmDelete.value = false; }); }
function create() { void run(async () => { await store({ identity: await createIdentity(), contacts: [] }); enabled.value = true; }); }
function remove(key: string) { void run(async () => { await store({ ...state.value, contacts: state.value.contacts.filter(item => item.key !== key) }); if (expected.value === key) expected.value = ''; }); }
function reset() { void run(async () => { if (!confirmDelete.value) return; await store({ identity: null, contacts: [] }); enabled.value = false; expected.value = ''; confirmDelete.value = false; }); }
async function remember(key: string, name: string) {
if (!state.value.identity) throw new Error('Create a local identity first.');
const next = { ...state.value, contacts: [...state.value.contacts.filter(item => item.key !== key), { key, name }] };
await store(next);
}
async function refreshForConnection() {
function recover() { void run(async () => { if (!confirmDelete.value) return; await clearIdentityStorage(); state.value = { identity: null }; loaded.value = true; confirmDelete.value = false; }); }
// Creating an identity from a connection opts that connection in.
function create() { void run(async () => { state.value = await saveIdentityState({ identity: await createIdentity() }, state.value.revision); emit('update', { rememberDevice: true }); }); }
function reset() { void run(async () => { if (!confirmDelete.value) return; state.value = await saveIdentityState({ identity: null }, state.value.revision); confirmDelete.value = false; }); }
function toggle(event: Event) { if (!props.disabled && event.target instanceof HTMLInputElement) emit('update', { rememberDevice: event.target.checked }); }
function forget() { if (!props.disabled) emit('update', { peerKey: '' }); }
async function refreshIdentity() {
if (working.value) throw new Error('Wait for the device settings operation to finish before connecting.');
if (!supported || !enabled.value) return { identity: null, expected: '' };
if (!supported) return null;
try {
const next = await loadIdentityState();
if (next.identity?.publicKey !== state.value.identity?.publicKey) {
enabled.value = false; expected.value = '';
}
if (!next.contacts.some(contact => contact.key === expected.value)) expected.value = '';
if (next.revision !== state.value.revision) confirmDelete.value = false;
state.value = next; loaded.value = true; error.value = '';
return { identity: enabled.value ? next.identity : null, expected: expected.value };
return next.identity;
} catch (err) {
error.value = asError(err).message;
throw err;
}
}
defineExpose({ remember, refreshForConnection });
defineExpose({ refreshIdentity });
</script>
<template>
<details v-if="supported">
<summary>Remembered devices</summary>
<p class="status">Opt in to a device identity stored in this browser. Temporary connections remain available. Clearing site data loses your identity and contacts. Trusted app code is required: it can use this identity.</p>
<summary>Remembered device</summary>
<p class="status">Optionally remember the device on this connection, so later sessions verify it automatically. This uses a device identity stored in this browser and shared by all connections. Clearing site data loses it. Trusted app code is required: it can use this identity.</p>
<fieldset class="device-settings" :disabled="disabled || working || !loaded">
<button v-if="!state.identity" @click="create">Create device identity</button>
<template v-else>
<label><input id="use-device-identity" v-model="enabled" type="checkbox" /> Use my device identity for this connection</label>
<p class="status">Your public identity is included in connection codes when enabled and can link your sessions.</p>
<label for="expected-device">Expected device</label>
<select id="expected-device" v-model="expected" :disabled="!enabled">
<option value="">New device — compare fingerprints manually</option>
<option v-for="contact in state.contacts" :key="contact.key" :value="contact.key">{{ contact.name }} ({{ contact.key.slice(-12) }})</option>
</select>
<ul aria-label="Remembered devices"><li v-for="contact in state.contacts" :key="contact.key">{{ contact.name }} <button @click="remove(contact.key)" :aria-label="'Forget ' + contact.name">Forget</button></li></ul>
<template v-if="paired">
<p class="status">Paired device <code>{{ paired.slice(-12) }}</code>. Later sessions verify it automatically.</p>
<button @click="forget">Forget device</button>
</template>
<label v-else><input id="remember-device" :checked="remember" type="checkbox" @change="toggle" /> Use my device identity on this connection</label>
<p class="status">When enabled, your public identity is included in this connection’s codes and can link your sessions.</p>
<label><input v-model="confirmDelete" type="checkbox" /> Delete this browser’s device identity</label>
<button :disabled="!confirmDelete" @click="reset">Delete device identity</button>
</template>
<label v-if="state.identity"><input v-model="confirmDelete" type="checkbox" /> Delete this browser’s identity and contacts</label>
<button v-if="state.identity" :disabled="!confirmDelete" @click="reset">Delete identity and contacts</button>
</fieldset>
<div v-if="!loaded && error">
<label><input v-model="confirmDelete" type="checkbox" :disabled="disabled || working" /> Delete unreadable device storage, including identity and contacts</label>
<label><input v-model="confirmDelete" type="checkbox" :disabled="disabled || working" /> Delete unreadable device storage</label>
<button :disabled="disabled || working || !confirmDelete" @click="recover">Clear device storage</button>
</div>
<p v-if="error" role="alert" class="error">{{ error }}</p>