Files
archie/ui/src/stores/org.ts
T

163 lines
5.5 KiB
TypeScript

import { defineStore } from "pinia";
import { computed, onScopeDispose, ref } from "vue";
import { api } from "../lib/api.ts";
import type { Org } from "../org/org.ts";
import { useSessionStore } from "./session.ts";
export interface Identity {
id: string;
kind: "system" | "bot" | "service_account" | "user";
display_name: string;
lifecycle: "active" | "suspended" | "retired";
version: number;
}
interface OrgList {
orgs: Org[];
current: string;
instance_admin: boolean;
member_orgs?: string[];
}
const message = (cause: unknown) => String((cause as Error).message || cause);
/** The orgs the caller can see, the one the Org area shows, and the
* identities that can join them. Read once and shared: every Org tab draws the
* same header, so a per-mount fetch would flash it on every switch. */
export const useOrgStore = defineStore("org", () => {
const orgs = ref<Org[]>([]);
const instanceAdmin = ref(false);
/** The org the caller acts in; Access and Tokens always read this one. */
const homeOrg = ref("");
/** The orgs the caller may act in; the server refuses any other. */
const memberOrgs = ref<string[]>([]);
/** The org the Org area shows; an instance admin can point it elsewhere. */
const org = ref<Org | null>(null);
let pending: Promise<void> | null = null;
/** loadOrg reads the org list; reload refetches it after an org is created. */
function loadOrg(reload = false): Promise<void> {
if (org.value && !reload) return Promise.resolve();
if (pending) return pending;
pending = api
.orgs<OrgList>()
.then((response) => {
orgs.value = response.orgs;
instanceAdmin.value = response.instance_admin;
homeOrg.value = response.current;
memberOrgs.value = response.member_orgs ?? [response.current];
const keep = org.value?.id ?? response.current;
org.value = response.orgs.find((o) => o.id === keep) ?? response.orgs[0] ?? null;
})
.finally(() => {
pending = null;
});
return pending;
}
/** actInOrg switches the org the whole dashboard acts in. Everything loaded
* so far belongs to the old one, so the page starts over. */
async function actInOrg(id: string): Promise<void> {
await api.orgSelect(id);
window.location.reload();
}
/** viewOrg points the Org area at another org; only an instance admin lists more than one. */
function viewOrg(id: string): void {
org.value = orgs.value.find((o) => o.id === id) ?? org.value;
}
const identities = ref<Identity[]>([]);
/** True once the first identity read settled, so a list can wait without
* showing its empty state. */
const identitiesLoaded = ref(false);
/** False when the acting org is not the system org: the instance's identity
* list is an instance-only read there, so it is not issued and pages show
* an unavailable state rather than a 404. */
const identitiesAvailable = ref(true);
const identityError = ref("");
const identityBusy = ref("");
const session = useSessionStore();
let stop: (() => void) | undefined;
const activeIdentities = computed(() => identities.value.filter((actor) => actor.lifecycle === "active"));
function identityName(id: string): string {
return identities.value.find((actor) => actor.id === id)?.display_name || (/^[0-9a-f]+(?:-[0-9a-f]+){4}$/i.test(id) ? "Unavailable identity" : id);
}
async function loadIdentities(): Promise<void> {
// The instance-wide identity list belongs to the system org; another
// org's read of it answers 404, so it is skipped there.
await session.loadCapabilities();
if (!session.systemOrg) {
identities.value = [];
identitiesAvailable.value = false;
identitiesLoaded.value = true;
identityError.value = "";
return;
}
try {
identities.value = (await api.identities<{ identities: Identity[] }>()).identities;
identityError.value = "";
} catch (cause) {
identityError.value = message(cause);
} finally {
identitiesLoaded.value = true;
}
}
/** watchIdentities reads the list once and then follows the live stream. */
function watchIdentities(): void {
if (stop) return;
void loadIdentities();
stop = session.subscribe("identities", (data) => {
identities.value = (data as { identities: Identity[] }).identities;
identityError.value = "";
identitiesLoaded.value = true;
});
}
async function mutate(key: string, action: () => Promise<unknown>): Promise<boolean> {
identityBusy.value = key;
try {
await action();
await loadIdentities();
return true;
} catch (cause) {
identityError.value = message(cause);
return false;
} finally {
identityBusy.value = "";
}
}
function createIdentity(display_name: string, kind: Identity["kind"]): Promise<boolean> {
return mutate("create", () => api.identityCreate({ display_name, kind }));
}
function identityCommand(value: Identity, command: string, display_name = ""): Promise<boolean> {
return mutate(value.id, () =>
api.identityCommand(value.id, command, { expected_version: value.version, display_name }),
);
}
onScopeDispose(() => stop?.());
return {
orgs,
instanceAdmin,
homeOrg,
memberOrgs,
actInOrg,
org,
loadOrg,
viewOrg,
identities,
identitiesLoaded,
identitiesAvailable,
activeIdentities,
identityName,
identityError,
identityBusy,
watchIdentities,
createIdentity,
identityCommand,
};
});