Add the connections workspace (sidebar, editable slots, per-slot activity, removal dialog with focus wrapping), remove the downloadable local HTML and unreleased identity backup/restore, and start the 0.0.5 draft changelog. Restore the released 0.0.3/0.0.4 changelog entries. Enable focus emulation in browser checks so background tabs receive focus/blur events. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
6.2 KiB
Vue
98 lines
6.2 KiB
Vue
<script setup lang="ts">
|
||
import { onActivated, onMounted, ref, shallowRef, watch } from 'vue';
|
||
import { clearIdentityStorage, createIdentity, loadIdentityState, saveIdentityState, type IdentityState } from '../lib/identity.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 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 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;
|
||
}
|
||
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.
|
||
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() {
|
||
if (working.value) throw new Error('Wait for the device settings operation to finish before connecting.');
|
||
if (!supported || !enabled.value) return { identity: null, expected: '' };
|
||
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 };
|
||
} catch (err) {
|
||
error.value = asError(err).message;
|
||
throw err;
|
||
}
|
||
}
|
||
defineExpose({ remember, refreshForConnection });
|
||
</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>
|
||
<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>
|
||
<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>
|
||
<button :disabled="disabled || working || !confirmDelete" @click="recover">Clear device storage</button>
|
||
</div>
|
||
<p v-if="error" role="alert" class="error">{{ error }}</p>
|
||
</details>
|
||
</template>
|