WIP IV improve UX [snapshot task 2]
This commit is contained in:
1 parent
28c90d1d71
commit
58c3e2af86
16 files changed
+779
-36
No files matched your search
@@ -0,0 +1,108 @@
|
||||
<script setup lang="ts">
|
||||
import { onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue';
|
||||
import { clearIdentityStorage, createIdentity, loadIdentityState, saveIdentityState, exportBackup, importBackup, type IdentityState } from '../lib/identity.ts';
|
||||
import { asError } from '../lib/validation.ts';
|
||||
const props = defineProps<{ disabled: boolean }>();
|
||||
const emit = defineEmits<{ change: [state: IdentityState, enabled: boolean, expected: string] }>();
|
||||
const supported = (() => { try { return location.protocol !== 'file:' && 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(''), password = ref(''), confirmReplace = ref(false);
|
||||
const backupUrl = ref('');
|
||||
onBeforeUnmount(() => { if (backupUrl.value) URL.revokeObjectURL(backupUrl.value); });
|
||||
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; }
|
||||
catch (err) { error.value = asError(err).message; }
|
||||
});
|
||||
async function store(next: IdentityState) {
|
||||
state.value = await saveIdentityState(next, state.value.revision);
|
||||
if (backupUrl.value) { URL.revokeObjectURL(backupUrl.value); backupUrl.value = ''; }
|
||||
}
|
||||
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; password.value = ''; }
|
||||
}
|
||||
function recover() { void run(async () => { if (!confirmReplace.value) return; await clearIdentityStorage(); state.value = { identity: null, contacts: [] }; loaded.value = true; confirmReplace.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 (!confirmReplace.value) return; await store({ identity: null, contacts: [] }); enabled.value = false; expected.value = ''; confirmReplace.value = false; }); }
|
||||
function backup() { void run(async () => {
|
||||
const data = await exportBackup(state.value, password.value);
|
||||
if (backupUrl.value) URL.revokeObjectURL(backupUrl.value);
|
||||
backupUrl.value = URL.createObjectURL(new Blob([data], { type: 'application/json' }));
|
||||
}); }
|
||||
function restore(event: Event) {
|
||||
const input = event.target as HTMLInputElement, file = input.files?.[0]; input.value = '';
|
||||
if (!file) return;
|
||||
void run(async () => {
|
||||
if (state.value.identity && !confirmReplace.value) throw new Error('Confirm replacement before restoring a backup.');
|
||||
if (file.size > 100000) throw new Error('Backup is too large.');
|
||||
const next = await importBackup(await file.text(), password.value);
|
||||
await store(next); enabled.value = false; expected.value = ''; confirmReplace.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) {
|
||||
if (backupUrl.value) URL.revokeObjectURL(backupUrl.value);
|
||||
backupUrl.value = ''; confirmReplace.value = false; password.value = '';
|
||||
}
|
||||
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 and export 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 for="backup-password">Backup password (at least 12 characters)</label>
|
||||
<input id="backup-password" v-model="password" type="password" autocomplete="new-password" maxlength="1024" />
|
||||
<button :disabled="!state.identity || password.length < 12" @click="backup">Create encrypted backup</button>
|
||||
<a v-if="backupUrl" :href="backupUrl" download="hellcon-device-backup.json">Save encrypted backup</a>
|
||||
<p class="status">A backup contains your identity and contacts. Keep its password separately. Restoring the same backup on another browser gives that browser the same identity.</p>
|
||||
<label v-if="state.identity"><input v-model="confirmReplace" type="checkbox" /> Replace or delete this browser’s identity and contacts</label>
|
||||
<label for="restore-identity">Restore encrypted backup</label>
|
||||
<input id="restore-identity" type="file" accept="application/json,.json" :disabled="password.length < 12 || (!!state.identity && !confirmReplace)" @change="restore" />
|
||||
<button v-if="state.identity" :disabled="!confirmReplace" @click="reset">Delete identity and contacts</button>
|
||||
</fieldset>
|
||||
<div v-if="!loaded && error">
|
||||
<label><input v-model="confirmReplace" type="checkbox" :disabled="disabled || working" /> Delete unreadable device storage, including identity and contacts</label>
|
||||
<button :disabled="disabled || working || !confirmReplace" @click="recover">Clear device storage</button>
|
||||
</div>
|
||||
<p v-if="error" role="alert" class="error">{{ error }}</p>
|
||||
</details>
|
||||
</template>
|
||||
Reference in new issue
Block a user