Files
archie/ui/src/org/OrgShell.vue
T

68 lines
2.4 KiB
Vue

<script setup lang="ts">
import { Alert, AlertDescription } from "@/components/ui/alert";
import { storeToRefs } from "pinia";
import { computed, onMounted, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import NewOrgDialog from "./NewOrgDialog.vue";
import { useOrgStore } from "@/stores/org";
/**
* The Organisation area's frame: the org every section below is about, and the
* switcher an instance admin uses to point it elsewhere.
*/
const orgStore = useOrgStore();
const { homeOrg, instanceAdmin, org, orgs } = storeToRefs(orgStore);
const error = ref("");
// Policies and models act on the caller's own org server-side, so an instance
// admin looking at another org is sent back rather than editing the wrong one.
const OWN_ORG_ONLY = ["/org/policies", "/org/models", "/org/connections", "/org/repositories"];
const away = computed(() => !!org.value && org.value.id !== homeOrg.value);
const route = useRoute();
const router = useRouter();
watch([away, () => route.path], ([isAway]) => {
if (isAway && OWN_ORG_ONLY.includes(route.path)) void router.replace("/org");
});
onMounted(async () => {
try {
await orgStore.loadOrg();
} catch (cause) {
error.value = String((cause as Error).message || cause);
}
});
</script>
<template>
<div>
<!-- The org is named once: the switcher for an instance admin, a plain
label for everyone else. -->
<div v-if="instanceAdmin" class="mb-4 flex flex-wrap items-center gap-2">
<Select :model-value="org?.id" @update:model-value="(id) => orgStore.viewOrg(String(id))">
<SelectTrigger class="w-72" aria-label="Organisation">
<SelectValue placeholder="Organisation" />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="o in orgs" :key="o.id" :value="o.id">{{ o.name }}</SelectItem>
</SelectContent>
</Select>
<NewOrgDialog />
</div>
<p v-else-if="org" class="mb-4 text-lg font-semibold tracking-[-0.01em]">{{ org.name }}</p>
<Alert v-if="error" variant="destructive" class="mb-4"><AlertDescription>{{ error }}</AlertDescription></Alert>
<!-- 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>