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

57 lines
2.2 KiB
Vue

<script setup lang="ts">
import { errors } from "@/copy/errors";
import { describeError } from "@/lib/api";
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 { Button } from "@/components/ui/button";
import { useOrgStore } from "@/stores/org";
/**
* The Organisation area's frame: the org every section below is about. The
* topbar switches the org you act in; an instance admin opens any other org
* from Instance > Organisations.
*/
const orgStore = useOrgStore();
const { homeOrg, org } = 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/audit", "/org/policies", "/org/models", "/org/connections", "/org/repositories", "/org/usage"];
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 = describeError(cause, errors.common.unknown);
}
});
</script>
<template>
<div>
<!-- The topbar names the org you act in. Only an instance admin who
opened another org from Instance > Organisations needs it named here,
with the way back. -->
<div v-if="org && away" class="mb-4 flex flex-wrap items-center gap-2">
<p class="text-title font-semibold tracking-[-0.01em]">{{ org.name }}</p>
<Button variant="ghost" size="sm" @click="orgStore.viewOrg(homeOrg)">Back to your organisation</Button>
</div>
<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>