Files
archie/ui/src/settings/IdentityGrantsCard.vue
T
sam f156150a8b
ci/woodpecker/pr/quality Pipeline failed
Quality / check (pull_request) Canceled after 0s
fix(ui): size page layouts by container width, not viewport (#1892)
2026-11-12 23:05:31 +11:00

83 lines
4.3 KiB
Vue

<script setup lang="ts">
import { Item, ItemGroup } from "@/components/ui/item";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { computed, onMounted } from "vue";
import { Plus, X } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Section } from "@/components/ui/section";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { useOrgStore } from "@/stores/org";
import { useSettingsStore } from "@/stores/settings";
import DraftHint from "./DraftHint.vue";
import KindGate from "./KindGate.vue";
/** The identity-grants document: credential services per identity ID. The
* System entry is the default; another identity's list replaces it. */
interface IdentityGrants {
identities?: Record<string, string[] | null>;
}
const SYSTEM_ID = "00000000-0000-5000-8000-000000000001";
const KIND = "identity-grants";
const store = useSettingsStore();
const org = useOrgStore();
onMounted(() => {
void store.load();
org.watchIdentities();
});
const form = computed(() => store.formFor(KIND));
const grants = computed(() => store.draftValue(KIND) as IdentityGrants | undefined);
const error = computed(() => store.pageErrorFor(KIND));
const addable = computed(() =>
org.activeIdentities.filter((a) => a.kind !== "user" && a.kind !== "system" && !(a.id in (grants.value?.identities ?? {}))),
);
const others = computed(() => Object.entries(grants.value?.identities ?? {}).filter(([id]) => id !== SYSTEM_ID));
// Identity ids are free text, so identities are written as whole maps rather
// than by a dotted per-id path.
function setIdentity(id: string, services: string[]) {
form.value?.api.setFieldValue("identities", (prev: Record<string, string[] | null> | undefined) => ({ ...prev, [id]: services }));
}
function removeIdentity(id: string) {
form.value?.api.setFieldValue("identities", (prev: Record<string, string[] | null> | undefined) => {
const { [id]: _, ...rest } = prev ?? {};
return rest;
});
}
</script>
<template>
<Section title="Credential grants">
<Alert v-if="error" variant="destructive" class="mb-3"><AlertDescription>{{ error }}</AlertDescription></Alert>
<KindGate v-if="!grants" :kind="KIND" />
<ItemGroup v-if="grants">
<Item as="div" role="listitem" variant="outline" size="sm" class="flex-wrap">
<span class="w-40 shrink-0 text-label @max-xl:w-24 @max-xl:truncate">Default</span>
<ToggleGroup class="flex-wrap" type="multiple" variant="outline" size="sm" :model-value="grants.identities?.[SYSTEM_ID] ?? []" aria-label="Default grants" @update:model-value="(v) => setIdentity(SYSTEM_ID, v as string[])">
<ToggleGroupItem v-for="service in store.credentialServices" :key="service" :value="service" class="font-mono text-caption">{{ service }}</ToggleGroupItem>
</ToggleGroup>
<DraftHint :kind="KIND" path="identities" />
</Item>
<Item v-for="[id, services] in others" :key="id" as="div" role="listitem" variant="outline" size="sm">
<span class="w-40 shrink-0 truncate text-label @max-xl:w-24">{{ org.identityName(String(id)) }}</span>
<ToggleGroup class="flex-wrap" type="multiple" variant="outline" size="sm" :model-value="services ?? []" :aria-label="`${org.identityName(String(id))} grants`" @update:model-value="(v) => setIdentity(String(id), v as string[])">
<ToggleGroupItem v-for="service in store.credentialServices" :key="service" :value="service" class="font-mono text-caption">{{ service }}</ToggleGroupItem>
</ToggleGroup>
<Button variant="ghost" size="sm" class="ml-auto" :aria-label="`Remove ${org.identityName(String(id))}`" @click="removeIdentity(String(id))"><X /></Button>
</Item>
<div v-if="addable.length" class="py-2.5">
<Select :model-value="undefined" @update:model-value="(id) => setIdentity(String(id), [])">
<SelectTrigger class="w-56" aria-label="Add identity"><Plus class="size-4" /><SelectValue placeholder="Add identity" /></SelectTrigger>
<SelectContent><SelectGroup>
<SelectItem v-for="actor in addable" :key="actor.id" :value="actor.id">{{ actor.display_name || actor.id }}</SelectItem>
</SelectGroup></SelectContent>
</Select>
</div>
</ItemGroup>
</Section>
</template>