Files
Docwell/Web-App/frontend/src/views/settings/AccountView.vue
T
tajniak81andClaude Opus 5.5 6984c7d162 Connect Google Calendar, import OneNote notebooks, sync Readwise, and package the clipper
Phase F follow-ups (ADR-16):

- Calendars: events are now stored in calendar_events by a
  calendars.sync job (every 15 minutes per account, 90 days back and
  400 ahead), so journals open at once and keep showing events while a
  source is down. Google accounts connect with OAuth (calendar.readonly)
  through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access.
- OneNote: whole notebooks import through Microsoft Graph (Notes.Read,
  DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with
  section groups, sections, subpages, pictures and attached files. The
  Microsoft sign-in lives in memory for an hour and is never stored.
- Readwise: an hourly sync with the person's sealed access token; new
  books get a page under "Readwise", new highlights are appended once.
  New readwise_syncs collection.
- internal/oauth: signed states and one callback route per provider,
  routed by purpose. directory gains ImportConverted and CallerOf.
- Phone app: "Today's journal" on the home screen and calendar events
  on journal pages.
- Web clipper: `npm run package` builds Chrome/Edge, Firefox and source
  zips; store/ holds listing texts, permission notes and a privacy
  policy.
- OpenAPI, generated clients, translations, compose and .env examples,
  and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:59:34 +01:00

341 lines
14 KiB
Vue

<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
<script setup lang="ts">
import type { ApiToken, NewApiToken, TotpSetup, TotpStatus } from "@docwell/api-client";
import { DwButton, DwCallout } from "@docwell/ui";
import { onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useApi } from "../../api";
import { useEditorPrefs } from "../../stores/preferences";
import { localeNames, storedLocale, storeLocale, supportedLocales, type Locale } from "../../i18n";
import CalendarAccounts from "../../components/CalendarAccounts.vue";
import ReadwiseSync from "../../components/ReadwiseSync.vue";
import FormField from "../../components/FormField.vue";
import PageShell from "../../components/PageShell.vue";
import { useFormErrors } from "../../composables/useFormErrors";
import { usePlatform, useRuntimeConfig } from "../../platform";
import { useAuthStore } from "../../stores/auth";
// The signed-in person's own settings: the app's language on this device,
// two-step sign-in with an authenticator app, and personal API tokens.
const api = useApi();
const auth = useAuthStore();
const platform = usePlatform();
const config = useRuntimeConfig();
const { locale } = useI18n();
const errors = useFormErrors();
const busy = ref(false);
const copied = ref("");
const language = ref<Locale | "">(storedLocale());
const prefs = useEditorPrefs();
const prefSwitches = ["newPageFullWidth", "dateTitles", "showProperties", "showBacklinks", "codeLineNumbers"] as const;
function pickLanguage() {
locale.value = storeLocale(language.value);
}
const totp = ref<TotpStatus | null>(null);
const setup = ref<TotpSetup | null>(null);
const code = ref("");
const recovery = ref<string[]>([]);
const disabling = ref(false);
const tokens = ref<ApiToken[]>([]);
const tokenName = ref("");
const tokenScope = ref<"read" | "write">("read");
const tokenDays = ref(90);
const created = ref<NewApiToken | null>(null);
onMounted(async () => {
try {
[totp.value, tokens.value] = await Promise.all([api.me.totp(), api.me.tokens()]);
} catch (e) {
errors.show(e);
}
});
async function run(action: () => Promise<unknown>) {
busy.value = true;
errors.reset();
try {
await action();
} catch (e) {
errors.show(e);
} finally {
busy.value = false;
}
}
const startTotp = () =>
run(async () => {
setup.value = await api.me.setupTotp();
code.value = "";
});
const enableTotp = () =>
run(async () => {
recovery.value = (await api.me.enableTotp(code.value.trim())).recoveryCodes;
setup.value = null;
code.value = "";
totp.value = await api.me.totp();
await auth.reload();
});
const disableTotp = () =>
run(async () => {
await api.me.disableTotp(code.value.trim());
disabling.value = false;
code.value = "";
recovery.value = [];
totp.value = await api.me.totp();
await auth.reload();
});
const createToken = () =>
run(async () => {
created.value = await api.me.createToken({
name: tokenName.value.trim(),
scope: tokenScope.value,
expiresDays: tokenDays.value,
});
tokenName.value = "";
tokens.value = await api.me.tokens();
});
const revokeToken = (t: ApiToken) =>
run(async () => {
await api.me.revokeToken(t.id);
if (created.value?.id === t.id) created.value = null;
tokens.value = await api.me.tokens();
});
async function copy(which: string, text: string) {
await navigator.clipboard.writeText(text);
copied.value = which;
setTimeout(() => (copied.value = ""), 2000);
}
const when = (s: string) =>
s ? new Date(s.replace(" ", "T")).toLocaleDateString(locale.value, { dateStyle: "medium" }) : "";
</script>
<template>
<PageShell :title="$t('account.title')" :subtitle="auth.user?.email">
<DwCallout v-if="errors.message.value" tone="danger" size="ui">{{ errors.message.value }}</DwCallout>
<!-- The desktop app: which server this account is on, and the others. -->
<section v-if="platform.servers" class="dw-card flex flex-wrap items-center gap-3" data-testid="servers-section">
<div class="min-w-0 flex-1">
<h2 class="text-ui-lg">{{ $t("servers.title") }}</h2>
<p class="mt-1 truncate text-ui-sm text-ink-muted">{{ $t("servers.signedInTo", { url: config.apiUrl }) }}</p>
</div>
<RouterLink :to="{ name: 'servers' }" class="dw-link">{{ $t("servers.manage") }}</RouterLink>
</section>
<!-- The app's language, kept per device like the theme. -->
<section class="dw-card flex flex-wrap items-center gap-3" data-testid="language-section">
<label for="language" class="min-w-0 flex-1">
<h2 class="text-ui-lg">{{ $t("account.language.title") }}</h2>
<p class="mt-1 text-ui-md text-ink-muted">{{ $t("account.language.hint") }}</p>
</label>
<select id="language" v-model="language" class="dw-input" data-testid="language" @change="pickLanguage">
<option value="">{{ $t("account.language.system") }}</option>
<option v-for="l in supportedLocales" :key="l" :value="l" :lang="l">{{ localeNames[l] }}</option>
</select>
</section>
<!-- Two-step sign-in -->
<!-- How pages read and start out, kept per device like the language. -->
<section class="space-y-3 dw-card" data-testid="editor-section">
<div>
<h2 class="text-ui-lg">{{ $t("account.editor.title") }}</h2>
<p class="mt-1 text-ui-md text-ink-muted">{{ $t("account.editor.hint") }}</p>
</div>
<label class="flex flex-wrap items-center gap-3 text-ui-md">
<span class="min-w-0 flex-1">{{ $t("account.editor.textSize") }}</span>
<select v-model="prefs.textSize" class="dw-input" data-testid="text-size">
<option value="sm">{{ $t("account.editor.sizes.sm") }}</option>
<option value="md">{{ $t("account.editor.sizes.md") }}</option>
<option value="lg">{{ $t("account.editor.sizes.lg") }}</option>
</select>
</label>
<label class="flex flex-wrap items-center gap-3 text-ui-md">
<span class="min-w-0 flex-1">{{ $t("account.editor.newPageMode") }}</span>
<select v-model="prefs.newPageMode" class="dw-input" data-testid="new-page-mode">
<option value="">{{ $t("account.editor.modes.workspace") }}</option>
<option value="page">{{ $t("account.editor.modes.page") }}</option>
<option value="edgeless">{{ $t("account.editor.modes.edgeless") }}</option>
</select>
</label>
<label v-for="k in prefSwitches" :key="k" class="flex items-center gap-3 text-ui-md">
<input v-model="prefs[k]" type="checkbox" class="accent-primary" :data-testid="`pref-${k}`" />
{{ $t(`account.editor.${k}`) }}
</label>
<div class="flex flex-wrap items-center gap-3 text-ui-md">
<span class="min-w-0 flex-1">
{{ $t("account.editor.edgelessStyles") }}
<span class="block text-ui-sm text-ink-muted">{{ $t("account.editor.edgelessStylesHint") }}</span>
</span>
<DwButton
size="sm"
:disabled="!prefs.edgelessStyles"
data-testid="reset-edgeless-styles"
@click="prefs.edgelessStyles = null"
>
{{ $t("account.editor.resetStyles") }}
</DwButton>
</div>
</section>
<section v-if="totp" class="space-y-4 dw-card" data-testid="totp-section">
<div>
<h2 class="text-ui-lg">{{ $t("account.totp.title") }}</h2>
<p class="mt-1 text-ui-md text-ink-muted">{{ $t("account.totp.hint") }}</p>
</div>
<DwCallout v-if="recovery.length" tone="warning" size="ui" data-testid="recovery-codes">
<p class="font-medium">{{ $t("account.totp.recoveryTitle") }}</p>
<p class="mt-1">{{ $t("account.totp.recoveryHint") }}</p>
<ul class="mt-2 grid grid-cols-2 gap-x-6 gap-y-1 font-mono text-ui-md">
<li v-for="c in recovery" :key="c">{{ c }}</li>
</ul>
<DwButton size="sm" icon="link" class="mt-3" @click="copy('recovery', recovery.join('\n'))">
{{ copied === "recovery" ? $t("workspace.copied") : $t("account.totp.copyCodes") }}
</DwButton>
</DwCallout>
<template v-if="totp.enabled">
<p class="text-ui-md" data-testid="totp-on">
{{ $t("account.totp.on", { n: totp.recoveryCodesLeft }, totp.recoveryCodesLeft) }}
</p>
<form v-if="disabling" class="space-y-3" @submit.prevent="disableTotp">
<FormField
v-model="code"
:label="$t('account.totp.codeOrRecovery')"
autocomplete="one-time-code"
required
:error="errors.fields.value.code"
/>
<div class="flex gap-2">
<DwButton type="submit" variant="danger" :disabled="busy || !code.trim()">{{
$t("account.totp.turnOff")
}}</DwButton>
<DwButton variant="ghost" @click="disabling = false">{{ $t("comments.cancel") }}</DwButton>
</div>
</form>
<DwButton v-else variant="secondary" @click="disabling = true">{{ $t("account.totp.turnOff") }}</DwButton>
</template>
<form v-else-if="setup" class="space-y-3" @submit.prevent="enableTotp">
<p class="text-ui-md">{{ $t("account.totp.scan") }}</p>
<img
v-if="setup.qr"
:src="setup.qr"
:alt="$t('account.totp.qrAlt')"
class="size-48 rounded-sm bg-white p-2"
data-testid="totp-qr"
/>
<p class="text-ui-sm text-ink-muted">
{{ $t("account.totp.manual") }}
<code class="break-all font-mono text-ink">{{ setup.secret }}</code>
</p>
<FormField
v-model="code"
:label="$t('account.totp.firstCode')"
autocomplete="one-time-code"
required
:error="errors.fields.value.code"
/>
<div class="flex gap-2">
<DwButton
type="submit"
variant="primary"
:disabled="busy || code.trim().length < 6"
data-testid="totp-enable"
>{{ $t("account.totp.turnOn") }}</DwButton
>
<DwButton variant="ghost" @click="setup = null">{{ $t("comments.cancel") }}</DwButton>
</div>
</form>
<DwButton v-else variant="primary" :disabled="busy" data-testid="totp-start" @click="startTotp">{{
$t("account.totp.setUp")
}}</DwButton>
</section>
<CalendarAccounts />
<ReadwiseSync />
<!-- API tokens -->
<section class="space-y-4 dw-card" data-testid="tokens-section">
<div>
<h2 class="text-ui-lg">{{ $t("account.tokens.title") }}</h2>
<p class="mt-1 text-ui-md text-ink-muted">{{ $t("account.tokens.hint") }}</p>
</div>
<DwCallout v-if="created" tone="success" size="ui" data-testid="new-token">
<p class="font-medium">{{ $t("account.tokens.createdTitle", { name: created.name }) }}</p>
<p class="mt-1">{{ $t("account.tokens.createdHint") }}</p>
<span class="mt-2 flex gap-2">
<input
:value="created.token"
readonly
class="dw-input min-w-0 flex-1 font-mono text-ui-sm"
:aria-label="$t('account.tokens.token')"
@focus="($event.target as HTMLInputElement).select()"
/>
<DwButton size="sm" icon="link" @click="copy('token', created.token)">
{{ copied === "token" ? $t("workspace.copied") : $t("workspace.copyLink") }}
</DwButton>
</span>
</DwCallout>
<form class="flex flex-wrap items-end gap-2" @submit.prevent="createToken">
<FormField
v-model="tokenName"
:label="$t('account.tokens.name')"
class="min-w-48 flex-1"
required
:error="errors.fields.value.name"
/>
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">{{ $t("account.tokens.scope") }}</span>
<select v-model="tokenScope" class="dw-input">
<option value="read">{{ $t("account.tokens.read") }}</option>
<option value="write">{{ $t("account.tokens.write") }}</option>
</select>
</label>
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">{{ $t("account.tokens.expires") }}</span>
<select v-model.number="tokenDays" class="dw-input">
<option :value="30">{{ $t("account.tokens.days", { n: 30 }) }}</option>
<option :value="90">{{ $t("account.tokens.days", { n: 90 }) }}</option>
<option :value="365">{{ $t("account.tokens.days", { n: 365 }) }}</option>
<option :value="0">{{ $t("account.tokens.never") }}</option>
</select>
</label>
<DwButton type="submit" variant="primary" :disabled="busy || !tokenName.trim()" data-testid="token-create">{{
$t("account.tokens.create")
}}</DwButton>
</form>
<ul v-if="tokens.length" class="divide-y divide-line rounded-md border border-line" data-testid="token-list">
<li v-for="t in tokens" :key="t.id" class="flex flex-wrap items-center gap-x-3 gap-y-1 px-3 py-2">
<span class="min-w-0 flex-1">
<span class="block font-medium">{{ t.name }}</span>
<span class="block text-ui-sm text-ink-muted">
<code class="font-mono">{{ t.prefix }}…</code> · {{ $t(`account.tokens.${t.scope}`) }} ·
{{ t.lastUsed ? $t("account.tokens.lastUsed", { date: when(t.lastUsed) }) : $t("account.tokens.unused") }}
<template v-if="t.expires"> · {{ $t("account.tokens.expiresOn", { date: when(t.expires) }) }}</template>
</span>
</span>
<DwButton size="sm" variant="danger" :disabled="busy" @click="revokeToken(t)">{{
$t("account.tokens.revoke")
}}</DwButton>
</li>
</ul>
<p v-else class="text-ui-sm text-ink-muted">{{ $t("account.tokens.none") }}</p>
</section>
<p class="text-ui-md">
<RouterLink to="/settings/organization" class="dw-link">{{ $t("account.organization") }}</RouterLink>
</p>
</PageShell>
</template>