Files
Docwell/Web-App/frontend/src/components/CalendarAccounts.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

228 lines
8.5 KiB
Vue

<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
<script setup lang="ts">
import type { CalendarAccount, CalendarAccountInput, LabelColor } from "@docwell/api-client";
import { ApiError } from "@docwell/api-client";
import { DwButton, DwCallout } from "@docwell/ui";
import { onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import { useApi } from "../api";
import { useFormErrors } from "../composables/useFormErrors";
import FormField from "./FormField.vue";
// The signed-in person's calendars (ADR-16): iCalendar feeds, CalDAV and
// Google accounts whose events show on their journal pages. Only they see them.
const api = useApi();
const route = useRoute();
const router = useRouter();
const { t, locale } = useI18n();
const errors = useFormErrors();
const accounts = ref<CalendarAccount[]>([]);
// The server may run without calendars; the section then stays hidden.
const available = ref(true);
const busy = ref(false);
const adding = ref(false);
// Whether the server has a Google OAuth app, and the outcome of a Google
// sign-in the browser just came back from.
const google = ref(false);
const connected = ref(false);
const colors: LabelColor[] = ["blue", "green", "orange", "purple", "red", "teal", "yellow", "pink", "gray"];
const blank = (): Required<Pick<CalendarAccountInput, "name" | "kind" | "url" | "username" | "password">> => ({
name: "",
kind: "ics",
url: "",
username: "",
password: "",
});
const form = ref(blank());
onMounted(async () => {
const q = route.query;
if (q.calendar === "connected") connected.value = true;
if (typeof q.calendarError === "string") errors.message.value = q.calendarError;
if (q.calendar || q.calendarError) {
const rest = { ...q };
delete rest.calendar;
delete rest.calendarError;
void router.replace({ query: rest });
}
try {
const [list, integrations] = await Promise.all([
api.calendars.list(),
api.integrations().catch(() => ({ google: false, microsoft: false, readwise: false })),
]);
accounts.value = list;
google.value = integrations.google;
} catch (e) {
if (e instanceof ApiError && e.status === 404) available.value = false;
else errors.show(e);
}
});
// Google sign-in happens on Google's site; it comes back to this page.
const connectGoogle = () =>
run(async () => {
const { url } = await api.calendars.googleConnect();
window.location.assign(url);
});
async function run(action: () => Promise<unknown>) {
busy.value = true;
errors.reset();
try {
await action();
accounts.value = await api.calendars.list();
} catch (e) {
errors.show(e);
} finally {
busy.value = false;
}
}
const add = () =>
run(async () => {
const f = form.value;
await api.calendars.create({
name: f.name.trim(),
kind: f.kind,
url: f.url.trim(),
username: f.username.trim() || undefined,
password: f.password || undefined,
});
form.value = blank();
adding.value = false;
});
const toggle = (a: CalendarAccount) => run(() => api.calendars.update(a.id, { enabled: !a.enabled }));
const recolor = (a: CalendarAccount, color: LabelColor) => run(() => api.calendars.update(a.id, { color }));
const refresh = (a: CalendarAccount) => run(() => api.calendars.refresh(a.id));
const remove = (a: CalendarAccount) => {
if (confirm(t("calendars.confirmRemove", { name: a.name }))) void run(() => api.calendars.remove(a.id));
};
const when = (iso: string) =>
iso
? new Intl.DateTimeFormat(locale.value, { dateStyle: "medium", timeStyle: "short" }).format(
new Date(iso.replace(" ", "T")),
)
: "";
</script>
<template>
<section v-if="available" class="space-y-4 dw-card" data-testid="calendars-section">
<div>
<h2 class="text-ui-lg">{{ $t("calendars.title") }}</h2>
<p class="mt-1 text-ui-md text-ink-muted">{{ $t("calendars.hint") }}</p>
</div>
<DwCallout v-if="errors.message.value" tone="danger" size="ui">{{ errors.message.value }}</DwCallout>
<DwCallout v-else-if="connected" tone="success" size="ui" data-testid="calendar-connected">
{{ $t("calendars.googleConnected") }}
</DwCallout>
<ul v-if="accounts.length" class="divide-y divide-line rounded-md border border-line" data-testid="calendar-list">
<li v-for="a in accounts" :key="a.id" class="flex flex-wrap items-center gap-x-3 gap-y-2 px-3 py-2">
<select
:value="a.color"
class="dw-input w-24"
:aria-label="$t('calendars.color')"
:disabled="busy"
@change="recolor(a, ($event.target as HTMLSelectElement).value as LabelColor)"
>
<option v-for="c in colors" :key="c" :value="c">{{ $t(`editor.colors.${c}`) }}</option>
</select>
<span class="min-w-0 flex-1">
<span class="flex items-center gap-2 font-medium">
<span
class="size-2 rounded-full"
:data-color="a.color"
:style="{ background: 'var(--dw-c, var(--color-accent))' }"
aria-hidden="true"
/>
<span class="truncate">{{ a.name }}</span>
<span class="text-ui-xs font-normal text-ink-muted uppercase">{{
a.kind === "caldav" ? "CalDAV" : a.kind === "google" ? "Google" : "iCal"
}}</span>
</span>
<span class="block truncate text-ui-sm text-ink-muted" :title="a.url">{{
a.kind === "google" ? a.username : a.url
}}</span>
<span v-if="a.lastError" class="block text-ui-sm text-danger" data-testid="calendar-error">{{
a.lastError
}}</span>
<span v-else-if="a.lastSync" class="block text-ui-sm text-ink-muted">
{{ $t("calendars.synced", { date: when(a.lastSync), n: a.events }) }}
</span>
</span>
<label class="flex items-center gap-1.5 text-ui-sm">
<input type="checkbox" :checked="a.enabled" :disabled="busy" @change="toggle(a)" />
{{ $t("calendars.show") }}
</label>
<DwButton size="sm" variant="ghost" icon="restore" :disabled="busy" @click="refresh(a)">
{{ $t("calendars.refresh") }}
</DwButton>
<DwButton size="sm" variant="danger" :disabled="busy" @click="remove(a)">{{ $t("calendars.remove") }}</DwButton>
</li>
</ul>
<p v-else-if="!adding" class="text-ui-sm text-ink-muted">{{ $t("calendars.none") }}</p>
<form v-if="adding" class="space-y-3" data-testid="calendar-form" @submit.prevent="add">
<div class="flex flex-wrap gap-2">
<label class="flex flex-col gap-1.5">
<span class="text-ui-sm font-semibold">{{ $t("calendars.kind") }}</span>
<select v-model="form.kind" class="dw-input">
<option value="ics">{{ $t("calendars.kinds.ics") }}</option>
<option value="caldav">{{ $t("calendars.kinds.caldav") }}</option>
</select>
</label>
<FormField
v-model="form.name"
:label="$t('calendars.name')"
class="min-w-48 flex-1"
required
:error="errors.fields.value.name"
/>
</div>
<FormField
v-model="form.url"
:label="$t('calendars.url')"
required
:hint="$t(form.kind === 'ics' ? 'calendars.icsHint' : 'calendars.caldavHint')"
:error="errors.fields.value.url"
/>
<div class="flex flex-wrap gap-2">
<FormField
v-model="form.username"
:label="$t('calendars.username')"
class="min-w-48 flex-1"
autocomplete="off"
/>
<FormField
v-model="form.password"
:label="$t('calendars.password')"
type="password"
class="min-w-48 flex-1"
autocomplete="new-password"
/>
</div>
<div class="flex gap-2">
<DwButton
type="submit"
variant="primary"
:disabled="busy || !form.name.trim() || !form.url.trim()"
data-testid="calendar-add"
>
{{ $t(busy ? "calendars.checking" : "calendars.add") }}
</DwButton>
<DwButton variant="ghost" :disabled="busy" @click="adding = false">{{ $t("calendars.cancel") }}</DwButton>
</div>
</form>
<div v-else class="flex flex-wrap gap-2">
<DwButton icon="plus" data-testid="calendar-new" @click="adding = true">{{ $t("calendars.new") }}</DwButton>
<DwButton v-if="google" icon="link" :disabled="busy" data-testid="calendar-google" @click="connectGoogle">
{{ $t("calendars.connectGoogle") }}
</DwButton>
</div>
</section>
</template>