Files
archie/ui/src/pages/memory.vue
T

178 lines
6.7 KiB
Vue

<script setup lang="ts">
definePage({
name: "memory",
meta: {
label: "Personal memory",
description: "What Archie remembers for you, and the notes shared with the organisation.",
section: "memory",
},
});
import { Plus } from "@lucide/vue";
import { onMounted, ref } from "vue";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { Input } from "@/components/ui/input";
import { Item, ItemGroup } from "@/components/ui/item";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import { Switch } from "@/components/ui/switch";
import { Textarea } from "@/components/ui/textarea";
import { Timestamp } from "@/components/ui/timestamp";
import { errors } from "@/copy/errors";
import { api, describeError, type MemoryNote } from "@/lib/api";
const mine = ref<MemoryNote[]>([]);
const shared = ref<MemoryNote[]>([]);
const loading = ref(true);
const busy = ref(false);
const error = ref("");
// A reload started from an older action must not overwrite a newer answer, so
// each read carries the revision it belongs to.
let revision = 0;
const load = async (): Promise<void> => {
const mineRevision = ++revision;
loading.value = true;
try {
const read = await api.notes();
if (mineRevision !== revision) return;
// The read returns the caller's own notes and the ones shared with the
// organisation, so the two lists come from one answer.
const all = read?.notes ?? [];
mine.value = all.filter((note) => note.scope === "person");
shared.value = all.filter((note) => note.scope === "shared");
error.value = "";
} catch (cause) {
if (mineRevision === revision) error.value = describeError(cause, errors.common.unknown);
} finally {
if (mineRevision === revision) loading.value = false;
}
};
const change = async (action: () => Promise<void>): Promise<void> => {
busy.value = true;
try {
await action();
await load();
} catch (cause) {
error.value = describeError(cause, errors.common.unknown);
} finally {
busy.value = false;
}
};
const draft = ref("");
const remember = (): void => {
void change(async () => {
await api.rememberNote(draft.value.trim());
draft.value = "";
});
};
const editing = ref<MemoryNote | null>(null);
const editOpen = ref(false);
const editDraft = ref("");
const edit = (note: MemoryNote): void => {
editing.value = note;
editDraft.value = note.text;
editOpen.value = true;
};
const save = (): void => {
const note = editing.value;
if (!note) return;
void change(async () => {
await api.correctNote(note.id, editDraft.value.trim());
editOpen.value = false;
});
};
const setDisabled = (note: MemoryNote, disabled: boolean): void => {
void change(() => api.setNoteDisabled(note.id, disabled));
};
const forgetting = ref<MemoryNote | null>(null);
const forgetOpen = ref(false);
const forget = (): void => {
const note = forgetting.value;
if (!note) return;
void change(async () => {
await api.forgetNote(note.id);
forgetOpen.value = false;
});
};
onMounted(load);
</script>
<template>
<div class="flex flex-col gap-4">
<ReadError v-if="error" :message="error" retry-label="Retry" @retry="load" />
<Section title="What Archie remembers for you">
<template #action>
<form class="flex w-full items-center gap-2 sm:w-auto" @submit.prevent="remember">
<Input v-model="draft" class="min-w-0 flex-1 sm:w-64" maxlength="2000" aria-label="New note" placeholder="Something to remember" />
<Button type="submit" size="sm" :disabled="busy || !draft.trim()" aria-label="Add note"><Plus /></Button>
</form>
</template>
<Empty v-if="!loading && !mine.length && !error">
<EmptyHeader><EmptyTitle>Nothing remembered yet.</EmptyTitle></EmptyHeader>
</Empty>
<ItemGroup v-if="mine.length" aria-label="Your notes">
<Item v-for="note in mine" :key="note.id" as="li" variant="outline" size="xs">
<span class="min-w-0 flex-1 break-words text-label" :class="{ 'text-fg-subtle line-through': note.disabled }">{{ note.text }}</span>
<span class="text-caption text-fg-subtle"><Timestamp :at="note.created_at" /></span>
<Switch :model-value="!note.disabled" :disabled="busy" aria-label="Read this note in conversations" @update:model-value="(value: boolean) => setDisabled(note, !value)" />
<Button variant="ghost" size="sm" :disabled="busy" @click="edit(note)">Edit</Button>
<Button variant="ghost" size="sm" class="text-danger" :disabled="busy" @click="forgetting = note; forgetOpen = true">Forget</Button>
</Item>
</ItemGroup>
</Section>
<Section title="Shared with the organisation">
<Empty v-if="!loading && !shared.length && !error">
<EmptyHeader><EmptyTitle>Nothing shared yet.</EmptyTitle></EmptyHeader>
</Empty>
<ItemGroup v-if="shared.length" aria-label="Shared notes">
<Item v-for="note in shared" :key="note.id" as="li" variant="outline" size="xs">
<span class="min-w-0 flex-1 break-words text-label">{{ note.text }}</span>
<span class="text-caption text-fg-subtle">Version {{ note.version }}</span>
<span class="text-caption text-fg-subtle">Published <Timestamp :at="note.created_at" /></span>
</Item>
</ItemGroup>
</Section>
<Dialog v-model:open="editOpen">
<DialogContent>
<DialogHeader><DialogTitle>Edit note</DialogTitle></DialogHeader>
<Textarea v-model="editDraft" rows="4" aria-label="Note" />
<DialogFooter>
<Button variant="ghost" :disabled="busy" @click="editOpen = false">Cancel</Button>
<Button :disabled="busy || !editDraft.trim()" @click="save">Save</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<AlertDialog v-model:open="forgetOpen">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Forget this note?</AlertDialogTitle>
<AlertDialogDescription>Archie stops using "{{ forgetting?.text }}" in your conversations, and the deletion is kept across a restore.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction :disabled="busy" @click="forget">Forget</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</template>