feat(ui): org switcher and new org for instance admins

This commit is contained in:
sam committed 2026-10-05 23:18:50 +11:00
1 parent 2c447708e9
commit 0a80f21bcd
5 files changed
+249 -95

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import { Plus } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { api } from "@/lib/api";
import { useIdentitiesStore } from "@/stores/identities";
import { loadOrg, viewOrg, type Org } from "./org";
/** Creates an org with its default workspace and first owner, then opens it. */
const open = ref(false);
const id = ref("");
const name = ref("");
const owner = ref("");
const error = ref("");
const busy = ref(false);
const identities = useIdentitiesStore();
const people = computed(() =>
identities.identities.filter((i) => i.kind !== "system" && i.lifecycle === "active"),
);
function opened(value: boolean): void {
open.value = value;
if (value) identities.watch();
}
async function create(): Promise<void> {
busy.value = true;
try {
const created = await api.orgCreate<{ org: Org }>({
id: id.value.trim(),
name: name.value.trim(),
owner_identity: owner.value,
});
await loadOrg(true);
viewOrg(created.org.id);
open.value = false;
id.value = name.value = owner.value = error.value = "";
} catch (cause) {
error.value = String((cause as Error).message || cause);
} finally {
busy.value = false;
}
}
</script>
<template>
<Dialog :open="open" @update:open="opened">
<DialogTrigger as-child>
<Button size="sm" variant="outline"><Plus data-icon="inline-start" /> New org</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>New org</DialogTitle>
</DialogHeader>
<form id="new-org" class="flex flex-col gap-3" @submit.prevent="create">
<Input v-model="name" placeholder="Name" aria-label="Org name" required />
<Input v-model="id" class="font-mono" placeholder="id" aria-label="Org id" required />
<Select v-model="owner">
<SelectTrigger class="w-full" aria-label="Owner">
<SelectValue placeholder="Owner" />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="person in people" :key="person.id" :value="person.id">
{{ person.display_name }}
</SelectItem>
</SelectContent>
</Select>
<p v-if="error" role="alert" class="text-sm text-danger">{{ error }}</p>
</form>
<DialogFooter>
<Button
type="submit"
form="new-org"
:disabled="!id.trim() || !name.trim() || !owner || busy"
>
Create
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</template>
+45 -10
View File
@@ -1,9 +1,17 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { computed, onMounted, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { cn } from "@/lib/utils";
import { loadOrg, org } from "./org";
import NewOrgDialog from "./NewOrgDialog.vue";
import { homeOrg, instanceAdmin, loadOrg, org, orgs, viewOrg } from "./org";
/**
* The Org area's shared frame: the org it is about, and the tab strip that
@@ -12,17 +20,29 @@ import { loadOrg, org } from "./org";
*
* The org name and id are a context bar rather than the page's heading: each
* section states its own, and /org/access is a moved page that still carries
* its own.
* its own. An instance admin gets a switcher over every org in its place.
*/
const TABS = [
{ path: "/org", label: "Members" },
{ path: "/org/agents", label: "Agents" },
{ path: "/org/workspaces", label: "Workspaces" },
{ path: "/org/access", label: "Access" },
{ path: "/org/tokens", label: "Tokens" },
{ path: "/org/access", label: "Access", home: true },
{ path: "/org/tokens", label: "Tokens", home: true },
];
const route = useRoute();
const router = useRouter();
// Access and Tokens act on the caller's own org server-side, so they are
// hidden while an instance admin views another org rather than editing the
// wrong one.
const away = computed(() => !!org.value && org.value.id !== homeOrg.value);
const tabs = computed(() => TABS.filter((tab) => !(away.value && tab.home)));
watch(away, (isAway) => {
if (isAway && TABS.some((tab) => tab.home && route.path.startsWith(tab.path))) {
void router.replace("/org");
}
});
const error = ref("");
onMounted(async () => {
@@ -40,14 +60,28 @@ const current = (path: string) =>
<template>
<div>
<div class="mb-4 flex flex-wrap items-baseline gap-x-3 gap-y-1">
<div v-if="instanceAdmin" class="mb-4 flex flex-wrap items-center gap-2">
<Select :model-value="org?.id" @update:model-value="(id) => viewOrg(String(id))">
<SelectTrigger class="w-72" aria-label="Org">
<SelectValue placeholder="Org" />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="o in orgs" :key="o.id" :value="o.id">
{{ o.name }}
</SelectItem>
</SelectContent>
</Select>
<code v-if="org" class="text-xs text-fg-subtle">{{ org.id }}</code>
<NewOrgDialog />
</div>
<div v-else class="mb-4 flex flex-wrap items-baseline gap-x-3 gap-y-1">
<p class="text-lg font-semibold tracking-[-0.01em]">{{ org?.name ?? "Org" }}</p>
<code v-if="org" class="text-xs text-fg-subtle">{{ org.id }}</code>
</div>
<nav aria-label="Org" class="mb-6 flex flex-wrap items-center gap-1 border-b border-border">
<RouterLink
v-for="tab in TABS"
v-for="tab in tabs"
:key="tab.path"
:to="tab.path"
:aria-current="current(tab.path) ? 'page' : undefined"
@@ -64,7 +98,8 @@ const current = (path: string) =>
<p v-if="error" role="alert" class="mb-4 text-sm text-danger">{{ error }}</p>
<!-- The sections need the org id, so they wait for the one read. -->
<RouterView v-if="org" />
<!-- The sections need the org id, so they wait for the one read; keying on
it remounts the open section when an instance admin switches org. -->
<RouterView v-if="org" :key="org.id" />
</div>
</template>
+28 -8
View File
@@ -2,7 +2,7 @@ import { ref } from "vue";
import { api } from "@/lib/api";
/** The org the dashboard is scoped to. GET /api/orgs returns only it. */
/** An org on the instance. A non-admin only ever sees their own. */
export interface Org {
id: string;
name: string;
@@ -28,19 +28,34 @@ export interface Member {
role: Role;
}
// The org is read once and shared: every Org tab draws the same header, and
// the shell remounts between tab routes, so a per-mount fetch would refetch
// and flash the header on every switch.
// The org list is read once and shared: every Org tab draws the same header,
// and the shell remounts between tab routes, so a per-mount fetch would
// refetch and flash the header on every switch.
export const orgs = ref<Org[]>([]);
export const instanceAdmin = ref(false);
/** The org the caller acts in; Access and Tokens always read this one. */
export const homeOrg = ref("");
const current = ref<Org | null>(null);
let pending: Promise<void> | null = null;
export function loadOrg(): Promise<void> {
if (current.value) return Promise.resolve();
interface OrgList {
orgs: Org[];
current: string;
instance_admin: boolean;
}
/** loadOrg reads the org list; reload refetches it after an org is created. */
export function loadOrg(reload = false): Promise<void> {
if (current.value && !reload) return Promise.resolve();
if (pending) return pending;
pending = api
.orgs<{ orgs: Org[] }>()
.orgs<OrgList>()
.then((response) => {
current.value = response.orgs[0] ?? null;
orgs.value = response.orgs;
instanceAdmin.value = response.instance_admin;
homeOrg.value = response.current;
const keep = current.value?.id ?? response.current;
current.value = response.orgs.find((o) => o.id === keep) ?? response.orgs[0] ?? null;
})
.finally(() => {
pending = null;
@@ -48,4 +63,9 @@ export function loadOrg(): Promise<void> {
return pending;
}
/** viewOrg points the Org area at another org; only an instance admin lists more than one. */
export function viewOrg(id: string): void {
current.value = orgs.value.find((o) => o.id === id) ?? current.value;
}
export const org = current;