Files
archie/ui/src/settings/IdentityGrantsCard.vue
T

76 lines
3.7 KiB
Vue

<script setup lang="ts">
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";
/** The identity-grants document: the default credential services,
* and per identity a list that replaces the default. */
interface IdentityGrants {
root: string[] | null;
identities?: Record<string, string[]>;
}
const KIND = "identity-grants";
const store = useSettingsStore();
const org = useOrgStore();
onMounted(() => {
void store.load();
org.watchIdentities();
});
const grants = computed(() => store.drafts[KIND]?.value 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 ?? {}))),
);
function setRoot(services: string[]) {
if (grants.value) grants.value.root = services;
}
function setIdentity(id: string, services: string[]) {
if (grants.value) grants.value.identities = { ...grants.value.identities, [id]: services };
}
function removeIdentity(id: string) {
if (!grants.value?.identities) return;
const { [id]: _, ...rest } = grants.value.identities;
grants.value.identities = rest;
}
</script>
<template>
<Section title="Credential grants">
<p v-if="error" role="alert" class="mb-3 text-sm text-danger">{{ error }}</p>
<div v-if="grants" class="divide-y divide-border">
<div class="flex flex-wrap items-center gap-3 py-2.5">
<span class="w-40 shrink-0 text-sm">Default</span>
<ToggleGroup type="multiple" variant="outline" size="sm" :model-value="grants.root ?? []" aria-label="Default grants" @update:model-value="(v) => setRoot(v as string[])">
<ToggleGroupItem v-for="service in store.credentialServices" :key="service" :value="service" class="font-mono text-xs">{{ service }}</ToggleGroupItem>
</ToggleGroup>
<DraftHint :kind="KIND" path="root" />
</div>
<div v-for="(services, id) in grants.identities" :key="id" class="flex flex-wrap items-center gap-3 py-2.5">
<span class="w-40 shrink-0 truncate text-sm">{{ org.identityName(String(id)) }}</span>
<ToggleGroup 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-xs">{{ service }}</ToggleGroupItem>
</ToggleGroup>
<Button variant="ghost" size="sm" class="ml-auto" :aria-label="`Remove ${org.identityName(String(id))}`" @click="removeIdentity(String(id))"><X /></Button>
</div>
<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>
</div>
</Section>
</template>