کلیدهای API
پیشنمایش تمامصفحهجدولِ کلیدها با پیشوندِ ماسکشده، دسترسی و آخرین استفاده؛ ساختِ کلید با نام و دسترسیها، نمایشِ یکبارهٔ کلید با کپی و هشدار، و باطلکردن با تأییدِ دوباره.
۱. نصب کامپوننتها
این بلاک از ۱۳ آیتم دیگ استفاده میکند: alert-dialogbadgebuttoncheckboxcontrol-sizedialogdropdown-menuiconslabelpersiantabletext-fieldtoast
npx digdesign@latest add https://docs.digdesign.ir/r/alert-dialog.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/checkbox.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/dialog.json https://docs.digdesign.ir/r/dropdown-menu.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/table.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.jsonpnpm dlx digdesign@latest add https://docs.digdesign.ir/r/alert-dialog.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/checkbox.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/dialog.json https://docs.digdesign.ir/r/dropdown-menu.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/table.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.jsonyarn dlx digdesign@latest add https://docs.digdesign.ir/r/alert-dialog.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/checkbox.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/dialog.json https://docs.digdesign.ir/r/dropdown-menu.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/table.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.jsonbunx --bun digdesign@latest add https://docs.digdesign.ir/r/alert-dialog.json https://docs.digdesign.ir/r/badge.json https://docs.digdesign.ir/r/button.json https://docs.digdesign.ir/r/checkbox.json https://docs.digdesign.ir/r/control-size.json https://docs.digdesign.ir/r/dialog.json https://docs.digdesign.ir/r/dropdown-menu.json https://docs.digdesign.ir/r/icons.json https://docs.digdesign.ir/r/label.json https://docs.digdesign.ir/r/persian.json https://docs.digdesign.ir/r/table.json https://docs.digdesign.ir/r/text-field.json https://docs.digdesign.ir/r/toast.jsonnpx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework vuepnpm dlx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework vueyarn dlx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework vuebunx --bun digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework vuenpx digdesign@latest add button --framework sveltepnpm dlx digdesign@latest add button --framework svelteyarn dlx digdesign@latest add button --framework sveltebunx --bun digdesign@latest add button --framework sveltenpx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu icons input label persian table text-field toast --framework angularpnpm dlx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu icons input label persian table text-field toast --framework angularyarn dlx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu icons input label persian table text-field toast --framework angularbunx --bun digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu icons input label persian table text-field toast --framework angularnpx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework androidpnpm dlx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework androidyarn dlx digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework androidbunx --bun digdesign@latest add alert-dialog badge button checkbox control-size dialog dropdown-menu label persian table text-field toast --framework android۲. کد صفحه
کد را در صفحهٔ پروژهتان بگذارید (React: app/<route>/page.tsx) و متنها، داده و هندلرها را با محصول خودتان عوض کنید. با سوییچر فریمورک کدِ همین صفحه در فریمورک دیگر را ببینید.
"use client"
import * as React from "react"
import { AlertTriangleIcon, CheckIcon, CopyIcon, MoreHorizontalIcon, PlusIcon } from "@/components/icons"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import { ControlSizeProvider } from "@/components/ui/control-size"
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
import { Label } from "@/components/ui/label"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { TextField } from "@/components/ui/text-field"
import { toast } from "@/components/ui/toast"
import { formatJalali, formatRelativeTime, toFaDigits } from "@/lib/persian"
type Key = { id: string; name: string; prefix: string; scopes: string[]; created: string; used?: string }
const INITIAL: Key[] = [
{ id: "k1", name: "سرورِ فروشگاه", prefix: "dg_live_4f2a", scopes: ["خواندن", "نوشتن"], created: "2026-08-12", used: "2026-10-06T08:40:00" },
{ id: "k2", name: "گزارشِ داخلی", prefix: "dg_live_91cd", scopes: ["خواندن"], created: "2026-09-03", used: "2026-10-01T13:00:00" },
{ id: "k3", name: "تستِ اپ", prefix: "dg_test_7b10", scopes: ["خواندن", "نوشتن"], created: "2026-09-20" },
]
const SCOPES = [
{ id: "read", label: "خواندن", text: "دیدنِ فاکتورها، مشتریان و گزارشها" },
{ id: "write", label: "نوشتن", text: "ساخت و ویرایشِ فاکتور و مشتری" },
{ id: "webhooks", label: "وبهوک", text: "دریافتِ رویدادها روی سرورِ شما" },
]
/**
* کلیدهای API: جدولِ کلیدها با پیشوندِ ماسکشده، دسترسیها و آخرین استفاده؛ ساختِ
* کلید با نام و دسترسیها در دیالوگ و نمایشِ کاملِ کلید فقط یک بار با دکمهٔ کپی و
* هشدار؛ باطلکردن با تأییدِ دوباره.
*/
export default function ApiKeysBlock() {
const [keys, setKeys] = React.useState(INITIAL)
const [creating, setCreating] = React.useState(false)
const [name, setName] = React.useState("")
const [scopes, setScopes] = React.useState<string[]>(["read"])
const [created, setCreated] = React.useState<string | null>(null)
const [revoking, setRevoking] = React.useState<Key | null>(null)
const create = () => {
const secret = `dg_live_${Math.random().toString(36).slice(2, 10)}${Math.random().toString(36).slice(2, 18)}`
setKeys((list) => [
{ id: secret, name, prefix: secret.slice(0, 12), scopes: SCOPES.filter((scope) => scopes.includes(scope.id)).map((scope) => scope.label), created: new Date().toISOString().slice(0, 10) },
...list,
])
setCreated(secret)
}
const closeCreate = (next: boolean) => {
setCreating(next)
if (!next) {
setCreated(null)
setName("")
setScopes(["read"])
}
}
return (
<ControlSizeProvider size="xl" asChild>
<main className="mx-auto flex w-full max-w-5xl flex-col gap-8 p-4 sm:p-6">
<header className="flex flex-wrap items-end justify-between gap-x-4 gap-y-8">
<div className="flex flex-col gap-1">
<h1 className="text-2xl font-bold">کلیدهای API</h1>
<p className="text-muted-foreground">برای اتصالِ سرور و ابزارهای خودتان به حسابیار.</p>
</div>
<Dialog open={creating} onOpenChange={closeCreate}>
<DialogTrigger asChild>
<Button>
<PlusIcon />
کلیدِ تازه
</Button>
</DialogTrigger>
<ControlSizeProvider size="xl" asChild>
<DialogContent>
{created ? (
<>
<DialogHeader>
<DialogTitle>کلید ساخته شد</DialogTitle>
<DialogDescription>این کلید را همین حالا جای امنی ذخیره کنید؛ بعد از بستنِ این پنجره دیگر نشان داده نمیشود.</DialogDescription>
</DialogHeader>
<div className="flex items-center gap-2 rounded-lg border border-border bg-subtle p-3">
<code dir="ltr" className="min-w-0 flex-1 truncate text-sm">
{created}
</code>
<Button
variant="ghost"
size="icon"
aria-label="کپی کلید"
onClick={() => {
void navigator.clipboard?.writeText(created)
toast.success("کلید کپی شد.")
}}
>
<CopyIcon />
</Button>
</div>
<p className="flex items-start gap-2 rounded-lg bg-[color-mix(in_oklab,var(--dig-amber-500)_10%,var(--background))] p-3 text-sm leading-6">
<AlertTriangleIcon aria-hidden className="mt-0.5 size-4 shrink-0 text-warning-subtle-foreground" />
کلید را در کدِ سمتِ مرورگر یا مخزنِ عمومی نگذارید.
</p>
<DialogFooter>
<Button onClick={() => closeCreate(false)}>ذخیره کردم</Button>
</DialogFooter>
</>
) : (
<>
<DialogHeader>
<DialogTitle>کلیدِ تازه</DialogTitle>
<DialogDescription>نامی بگذارید که بعداً بدانید این کلید کجا استفاده شده.</DialogDescription>
</DialogHeader>
<ControlSizeProvider size="xl" className="flex flex-col gap-5">
<TextField label="نام" value={name} onValueChange={setName} placeholder="مثلاً سرورِ فروشگاه" required />
<fieldset className="flex flex-col gap-3">
<legend className="mb-1 text-sm font-medium">دسترسیها</legend>
{SCOPES.map((scope) => (
<div key={scope.id} className="flex items-start gap-3">
<Checkbox
id={`scope-${scope.id}`}
checked={scopes.includes(scope.id)}
onCheckedChange={(checked) => setScopes((list) => (checked ? [...list, scope.id] : list.filter((item) => item !== scope.id)))}
/>
<Label htmlFor={`scope-${scope.id}`} className="flex flex-col items-start gap-0.5">
<span>{scope.label}</span>
<span className="text-xs font-normal text-muted-foreground">{scope.text}</span>
</Label>
</div>
))}
</fieldset>
</ControlSizeProvider>
<DialogFooter>
<Button variant="outline" onClick={() => closeCreate(false)}>
انصراف
</Button>
<Button onClick={create} disabled={!name.trim() || scopes.length === 0}>
ساختِ کلید
</Button>
</DialogFooter>
</>
)}
</DialogContent>
</ControlSizeProvider>
</Dialog>
</header>
<div>
<Table stackOnMobile variant="card">
<TableHeader>
<TableRow>
<TableHead>نام</TableHead>
<TableHead>کلید</TableHead>
<TableHead>دسترسی</TableHead>
<TableHead>آخرین استفاده</TableHead>
<TableHead className="w-12 px-0">
<span className="sr-only">کنشها</span>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{keys.map((key) => (
<TableRow key={key.id}>
<TableCell>
<span className="flex flex-col">
<span className="font-medium">{key.name}</span>
<span className="text-xs text-muted-foreground">ساختهشده {formatJalali(new Date(key.created), "long")}</span>
</span>
</TableCell>
<TableCell>
{/* کلیدِ کامل فقط هنگامِ ساخت دیده میشود؛ اینجا شناسهاش کپی میشود تا در لاگ یا پشتیبانی پیدا شود. */}
<span className="flex items-center gap-1">
<code dir="ltr" className="rounded bg-muted px-2 py-1 text-xs">
{key.prefix}••••••••
</code>
<ControlSizeProvider size="default" asChild>
<Button
variant="ghost"
size="icon"
aria-label={`کپیِ شناسهٔ کلیدِ ${key.name}`}
onClick={() => {
void navigator.clipboard?.writeText(key.prefix)
toast.success("شناسهٔ کلید کپی شد.", { description: "خودِ کلید فقط هنگامِ ساخت نمایش داده میشود." })
}}
>
<CopyIcon />
</Button>
</ControlSizeProvider>
</span>
</TableCell>
<TableCell>
<span className="flex flex-wrap gap-1">
{key.scopes.map((scope) => (
<Badge key={scope} variant="soft">
{scope}
</Badge>
))}
</span>
</TableCell>
<TableCell className="text-sm text-muted-foreground">
{key.used ? (
<span className="flex items-center gap-1.5">
<CheckIcon aria-hidden className="size-3.5 text-success-subtle-foreground" />
{formatRelativeTime(new Date(key.used))}
</span>
) : (
"هنوز استفاده نشده"
)}
</TableCell>
<TableCell className="w-12 px-0 text-center">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon" aria-label={`کنشهای ${key.name}`}>
<MoreHorizontalIcon />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem>تغییرِ نام</DropdownMenuItem>
<DropdownMenuItem variant="destructive" onSelect={() => setRevoking(key)}>
باطل کردن
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
<p className="text-sm text-muted-foreground">
{toFaDigits(keys.length)} کلیدِ فعال · راهنمای استفاده در مستنداتِ API
</p>
<AlertDialog open={revoking !== null} onOpenChange={(next) => !next && setRevoking(null)}>
<ControlSizeProvider size="xl" asChild>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>«{revoking?.name}» باطل شود؟</AlertDialogTitle>
<AlertDialogDescription>هر سرویسی که با این کلید کار میکند فوراً قطع میشود و برگشتپذیر نیست.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>منصرف شدم</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
onClick={() => {
setKeys((list) => list.filter((item) => item.id !== revoking?.id))
toast.success("کلید باطل شد.")
}}
>
باطل کردن
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</ControlSizeProvider>
</AlertDialog>
</main>
</ControlSizeProvider>
)
}
<script setup lang="ts">
import { ref } from "vue"
import AlertDialog from "@/components/ui/AlertDialog.vue"
import AlertDialogAction from "@/components/ui/AlertDialogAction.vue"
import AlertDialogCancel from "@/components/ui/AlertDialogCancel.vue"
import AlertDialogContent from "@/components/ui/AlertDialogContent.vue"
import AlertDialogDescription from "@/components/ui/AlertDialogDescription.vue"
import AlertDialogFooter from "@/components/ui/AlertDialogFooter.vue"
import AlertDialogHeader from "@/components/ui/AlertDialogHeader.vue"
import AlertDialogTitle from "@/components/ui/AlertDialogTitle.vue"
import Badge from "@/components/ui/Badge.vue"
import Button from "@/components/ui/Button.vue"
import Checkbox from "@/components/ui/Checkbox.vue"
import ControlSize from "@/components/ui/ControlSize.vue"
import Dialog from "@/components/ui/Dialog.vue"
import DialogContent from "@/components/ui/DialogContent.vue"
import DialogDescription from "@/components/ui/DialogDescription.vue"
import DialogFooter from "@/components/ui/DialogFooter.vue"
import DialogHeader from "@/components/ui/DialogHeader.vue"
import DialogTitle from "@/components/ui/DialogTitle.vue"
import DialogTrigger from "@/components/ui/DialogTrigger.vue"
import DropdownMenu from "@/components/ui/DropdownMenu.vue"
import DropdownMenuContent from "@/components/ui/DropdownMenuContent.vue"
import DropdownMenuItem from "@/components/ui/DropdownMenuItem.vue"
import DropdownMenuTrigger from "@/components/ui/DropdownMenuTrigger.vue"
import Label from "@/components/ui/Label.vue"
import Table from "@/components/ui/Table.vue"
import TableBody from "@/components/ui/TableBody.vue"
import TableCell from "@/components/ui/TableCell.vue"
import TableHead from "@/components/ui/TableHead.vue"
import TableHeader from "@/components/ui/TableHeader.vue"
import TableRow from "@/components/ui/TableRow.vue"
import TextField from "@/components/ui/TextField.vue"
import { toast } from "@/components/ui/Toast.vue"
import { DigIcon } from "@/lib/icons"
import { formatJalali, formatRelativeTime, toFaDigits } from "@/lib/persian"
type Key = { id: string; name: string; prefix: string; scopes: string[]; created: string; used?: string }
const INITIAL: Key[] = [
{ id: "k1", name: "سرورِ فروشگاه", prefix: "dg_live_4f2a", scopes: ["خواندن", "نوشتن"], created: "2026-08-12", used: "2026-10-06T08:40:00" },
{ id: "k2", name: "گزارشِ داخلی", prefix: "dg_live_91cd", scopes: ["خواندن"], created: "2026-09-03", used: "2026-10-01T13:00:00" },
{ id: "k3", name: "تستِ اپ", prefix: "dg_test_7b10", scopes: ["خواندن", "نوشتن"], created: "2026-09-20" },
]
const SCOPES = [
{ id: "read", label: "خواندن", text: "دیدنِ فاکتورها، مشتریان و گزارشها" },
{ id: "write", label: "نوشتن", text: "ساخت و ویرایشِ فاکتور و مشتری" },
{ id: "webhooks", label: "وبهوک", text: "دریافتِ رویدادها روی سرورِ شما" },
]
/**
* کلیدهای API: جدولِ کلیدها با پیشوندِ ماسکشده، دسترسیها و آخرین استفاده؛ ساختِ
* کلید با نام و دسترسیها در دیالوگ و نمایشِ کاملِ کلید فقط یک بار با دکمهٔ کپی و
* هشدار؛ باطلکردن با تأییدِ دوباره.
*/
const keys = ref<Key[]>(INITIAL)
const creating = ref(false)
const name = ref("")
const scopes = ref<string[]>(["read"])
const created = ref<string | null>(null)
const revoking = ref<Key | null>(null)
function create() {
const secret = `dg_live_${Math.random().toString(36).slice(2, 10)}${Math.random().toString(36).slice(2, 18)}`
keys.value = [
{ id: secret, name: name.value, prefix: secret.slice(0, 12), scopes: SCOPES.filter((scope) => scopes.value.includes(scope.id)).map((scope) => scope.label), created: new Date().toISOString().slice(0, 10) },
...keys.value,
]
created.value = secret
}
function closeCreate(next: boolean) {
creating.value = next
if (!next) {
created.value = null
name.value = ""
scopes.value = ["read"]
}
}
function copySecret() {
void navigator.clipboard?.writeText(created.value ?? "")
toast.success("کلید کپی شد.")
}
function copyPrefix(key: Key) {
void navigator.clipboard?.writeText(key.prefix)
toast.success("شناسهٔ کلید کپی شد.", { description: "خودِ کلید فقط هنگامِ ساخت نمایش داده میشود." })
}
function toggleScope(id: string, checked: boolean | "indeterminate") {
scopes.value = checked ? [...scopes.value, id] : scopes.value.filter((item) => item !== id)
}
function revoke() {
keys.value = keys.value.filter((item) => item.id !== revoking.value?.id)
toast.success("کلید باطل شد.")
}
</script>
<template>
<ControlSize size="xl" as-child>
<main class="mx-auto flex w-full max-w-5xl flex-col gap-8 p-4 sm:p-6">
<header class="flex flex-wrap items-end justify-between gap-x-4 gap-y-8">
<div class="flex flex-col gap-1">
<h1 class="text-2xl font-bold">کلیدهای API</h1>
<p class="text-muted-foreground">برای اتصالِ سرور و ابزارهای خودتان به حسابیار.</p>
</div>
<Dialog :open="creating" @update:open="closeCreate">
<DialogTrigger as-child>
<Button>
<DigIcon name="plus" />
کلیدِ تازه
</Button>
</DialogTrigger>
<ControlSize size="xl" as-child>
<DialogContent>
<template v-if="created">
<DialogHeader>
<DialogTitle>کلید ساخته شد</DialogTitle>
<DialogDescription>این کلید را همین حالا جای امنی ذخیره کنید؛ بعد از بستنِ این پنجره دیگر نشان داده نمیشود.</DialogDescription>
</DialogHeader>
<div class="flex items-center gap-2 rounded-lg border border-border bg-subtle p-3">
<code dir="ltr" class="min-w-0 flex-1 truncate text-sm">{{ created }}</code>
<Button variant="ghost" size="icon" aria-label="کپی کلید" @click="copySecret">
<DigIcon name="copy" />
</Button>
</div>
<p class="flex items-start gap-2 rounded-lg bg-[color-mix(in_oklab,var(--dig-amber-500)_10%,var(--background))] p-3 text-sm leading-6">
<DigIcon name="alert-triangle" aria-hidden="true" class="mt-0.5 size-4 shrink-0 text-warning-subtle-foreground" />
کلید را در کدِ سمتِ مرورگر یا مخزنِ عمومی نگذارید.
</p>
<DialogFooter>
<Button @click="closeCreate(false)">ذخیره کردم</Button>
</DialogFooter>
</template>
<template v-else>
<DialogHeader>
<DialogTitle>کلیدِ تازه</DialogTitle>
<DialogDescription>نامی بگذارید که بعداً بدانید این کلید کجا استفاده شده.</DialogDescription>
</DialogHeader>
<ControlSize size="xl" class="flex flex-col gap-5">
<TextField v-model="name" label="نام" placeholder="مثلاً سرورِ فروشگاه" required />
<fieldset class="flex flex-col gap-3">
<legend class="mb-1 text-sm font-medium">دسترسیها</legend>
<div v-for="scope in SCOPES" :key="scope.id" class="flex items-start gap-3">
<Checkbox :id="`scope-${scope.id}`" :checked="scopes.includes(scope.id)" @update:checked="(checked) => toggleScope(scope.id, checked)" />
<Label :for="`scope-${scope.id}`" class="flex flex-col items-start gap-0.5">
<span>{{ scope.label }}</span>
<span class="text-xs font-normal text-muted-foreground">{{ scope.text }}</span>
</Label>
</div>
</fieldset>
</ControlSize>
<DialogFooter>
<Button variant="outline" @click="closeCreate(false)">انصراف</Button>
<Button :disabled="!name.trim() || scopes.length === 0" @click="create">ساختِ کلید</Button>
</DialogFooter>
</template>
</DialogContent>
</ControlSize>
</Dialog>
</header>
<div>
<Table stack-on-mobile variant="card">
<TableHeader>
<TableRow>
<TableHead>نام</TableHead>
<TableHead>کلید</TableHead>
<TableHead>دسترسی</TableHead>
<TableHead>آخرین استفاده</TableHead>
<TableHead class="w-12 px-0">
<span class="sr-only">کنشها</span>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="key in keys" :key="key.id">
<TableCell>
<span class="flex flex-col">
<span class="font-medium">{{ key.name }}</span>
<span class="text-xs text-muted-foreground">ساختهشده {{ formatJalali(new Date(key.created), "long") }}</span>
</span>
</TableCell>
<TableCell>
<!-- کلیدِ کامل فقط هنگامِ ساخت دیده میشود؛ اینجا شناسهاش کپی میشود تا در لاگ یا پشتیبانی پیدا شود. -->
<span class="flex items-center gap-1">
<code dir="ltr" class="rounded bg-muted px-2 py-1 text-xs">{{ key.prefix }}••••••••</code>
<ControlSize size="default" as-child>
<Button variant="ghost" size="icon" :aria-label="`کپیِ شناسهٔ کلیدِ ${key.name}`" @click="copyPrefix(key)">
<DigIcon name="copy" />
</Button>
</ControlSize>
</span>
</TableCell>
<TableCell>
<span class="flex flex-wrap gap-1">
<Badge v-for="scope in key.scopes" :key="scope" variant="soft">{{ scope }}</Badge>
</span>
</TableCell>
<TableCell class="text-sm text-muted-foreground">
<span v-if="key.used" class="flex items-center gap-1.5">
<DigIcon name="check" aria-hidden="true" class="size-3.5 text-success-subtle-foreground" />
{{ formatRelativeTime(new Date(key.used)) }}
</span>
<template v-else>هنوز استفاده نشده</template>
</TableCell>
<TableCell class="w-12 px-0 text-center">
<DropdownMenu>
<DropdownMenuTrigger as-child>
<Button variant="ghost" size="icon" :aria-label="`کنشهای ${key.name}`">
<DigIcon name="more-horizontal" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>تغییرِ نام</DropdownMenuItem>
<DropdownMenuItem variant="destructive" @select="revoking = key">باطل کردن</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
<p class="text-sm text-muted-foreground">{{ toFaDigits(keys.length) }} کلیدِ فعال · راهنمای استفاده در مستنداتِ API</p>
<AlertDialog :open="revoking !== null" @update:open="(next) => !next && (revoking = null)">
<ControlSize size="xl" as-child>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>«{{ revoking?.name }}» باطل شود؟</AlertDialogTitle>
<AlertDialogDescription>هر سرویسی که با این کلید کار میکند فوراً قطع میشود و برگشتپذیر نیست.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>منصرف شدم</AlertDialogCancel>
<AlertDialogAction variant="destructive" @click="revoke">باطل کردن</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</ControlSize>
</AlertDialog>
</main>
</ControlSize>
</template>
کدِ این بلاک هنوز برای Svelte نوشته نشده است.
این موارد هنوز برای Svelte آماده نیستند: alert-dialogbadgecheckboxcontrol-sizedialogdropdown-menulabelpersiantabletext-fieldtoastآیکون alert-triangleآیکون checkآیکون copyآیکون more-horizontalآیکون plus
کدِ React همین بلاک مرجع است. با MCP دیگ و get_block با framework="svelte" ایجنت آن را با کامپوننتهای Svelte میسازد.
import { Component, ChangeDetectionStrategy, signal } from "@angular/core"
import { DigControlSize } from "@/components/ui/control-size"
import {
DigDialog,
DigDialogTrigger,
DigDialogContent,
DigDialogHeader,
DigDialogTitle,
DigDialogDescription,
DigDialogFooter,
} from "@/components/ui/dialog"
import { DigButton } from "@/components/ui/button"
import { DigIcon } from "@/lib/icons"
import { DigTextField } from "@/components/ui/text-field"
import { DigInput, DigInputField } from "@/components/ui/input"
import { DigCheckbox } from "@/components/ui/checkbox"
import { DigLabel } from "@/components/ui/label"
import { DigTable, DigTableHeader, DigTableRow, DigTableHead, DigTableBody, DigTableCell } from "@/components/ui/table"
import { DigBadge } from "@/components/ui/badge"
import {
DigDropdownMenu,
DigDropdownMenuTrigger,
DigDropdownMenuContent,
DigDropdownMenuItem,
} from "@/components/ui/dropdown-menu"
import {
DigAlertDialog,
DigAlertDialogContent,
DigAlertDialogHeader,
DigAlertDialogTitle,
DigAlertDialogDescription,
DigAlertDialogFooter,
DigAlertDialogCancel,
DigAlertDialogAction,
} from "@/components/ui/alert-dialog"
import { toast } from "@/components/ui/toast"
import { formatJalali, formatRelativeTime, toFaDigits } from "@/lib/persian"
type Key = {
id: string
name: string
prefix: string
scopes: string[]
created: string
used?: string
}
const INITIAL: Key[] = [
{
id: "k1",
name: "سرورِ فروشگاه",
prefix: "dg_live_4f2a",
scopes: ["خواندن", "نوشتن"],
created: "2026-08-12",
used: "2026-10-06T08:40:00",
},
{
id: "k2",
name: "گزارشِ داخلی",
prefix: "dg_live_91cd",
scopes: ["خواندن"],
created: "2026-09-03",
used: "2026-10-01T13:00:00",
},
{
id: "k3",
name: "تستِ اپ",
prefix: "dg_test_7b10",
scopes: ["خواندن", "نوشتن"],
created: "2026-09-20",
},
]
const SCOPES = [
{ id: "read", label: "خواندن", text: "دیدنِ فاکتورها، مشتریان و گزارشها" },
{ id: "write", label: "نوشتن", text: "ساخت و ویرایشِ فاکتور و مشتری" },
{ id: "webhooks", label: "وبهوک", text: "دریافتِ رویدادها روی سرورِ شما" },
]
function copyPrefix(key: Key) {
void navigator.clipboard?.writeText(key.prefix)
toast.success("شناسهٔ کلید کپی شد.", {
description: "خودِ کلید فقط هنگامِ ساخت نمایش داده میشود.",
})
}
@Component({
selector: "dig-block-api-keys",
imports: [
DigControlSize,
DigDialog,
DigDialogTrigger,
DigDialogContent,
DigDialogHeader,
DigDialogTitle,
DigDialogDescription,
DigDialogFooter,
DigButton,
DigIcon,
DigTextField,
DigInput,
DigInputField,
DigCheckbox,
DigLabel,
DigTable,
DigTableHeader,
DigTableRow,
DigTableHead,
DigTableBody,
DigTableCell,
DigBadge,
DigDropdownMenu,
DigDropdownMenuTrigger,
DigDropdownMenuContent,
DigDropdownMenuItem,
DigAlertDialog,
DigAlertDialogContent,
DigAlertDialogHeader,
DigAlertDialogTitle,
DigAlertDialogDescription,
DigAlertDialogFooter,
DigAlertDialogCancel,
DigAlertDialogAction,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<main digControlSize="xl" class="mx-auto flex w-full max-w-5xl flex-col gap-8 p-4 sm:p-6">
<header class="flex flex-wrap items-end justify-between gap-x-4 gap-y-8">
<div class="flex flex-col gap-1">
<h1 class="text-2xl font-bold">کلیدهای API</h1>
<p class="text-muted-foreground">برای اتصالِ سرور و ابزارهای خودتان به حسابیار.</p>
</div>
<ng-container digDialog [open]="creating()" (openChange)="closeCreate($event)"
><button digDialogTrigger digButton type="button">
<svg digIcon="plus"></svg>
کلیدِ تازه
</button>
<div digControlSize="xl" digDialogContent>
@if (created()) {
<div digDialogHeader>
<h2 digDialogTitle>کلید ساخته شد</h2>
<p digDialogDescription>
این کلید را همین حالا جای امنی ذخیره کنید؛ بعد از بستنِ این پنجره دیگر نشان داده نمیشود.
</p>
</div>
<div class="flex items-center gap-2 rounded-lg border border-border bg-subtle p-3">
<code dir="ltr" class="min-w-0 flex-1 truncate text-sm">{{ created() }}</code>
<button
digButton
type="button"
variant="ghost"
size="icon"
aria-label="کپی کلید"
(click)="copySecret()"
>
<svg digIcon="copy"></svg>
</button>
</div>
<p
class="flex items-start gap-2 rounded-lg bg-[color-mix(in_oklab,var(--dig-amber-500)_10%,var(--background))] p-3 text-sm leading-6"
>
<svg
digIcon="alert-triangle"
aria-hidden="true"
class="mt-0.5 size-4 shrink-0 text-warning-subtle-foreground"
></svg>
کلید را در کدِ سمتِ مرورگر یا مخزنِ عمومی نگذارید.
</p>
<div digDialogFooter>
<button digButton type="button" (click)="closeCreate(false)">ذخیره کردم</button>
</div>
} @else {
<div digDialogHeader>
<h2 digDialogTitle>کلیدِ تازه</h2>
<p digDialogDescription>نامی بگذارید که بعداً بدانید این کلید کجا استفاده شده.</p>
</div>
<div digControlSize="xl" class="flex flex-col gap-5">
<div digTextField label="نام" required>
<dig-input
><input digInput [value]="name()" (valueChange)="name.set($event)" placeholder="مثلاً سرورِ فروشگاه"
/></dig-input>
</div>
<fieldset class="flex flex-col gap-3">
<legend class="mb-1 text-sm font-medium">دسترسیها</legend>
@for (scope of SCOPES; track scope.id) {
<div class="flex items-start gap-3">
<button
digCheckbox
[id]="\`scope-\${scope.id}\`"
[checked]="scopes().includes(scope.id)"
(checkedChange)="toggleScope(scope.id, $event)"
></button>
<label digLabel [htmlFor]="\`scope-\${scope.id}\`" class="flex flex-col items-start gap-0.5"
><span>{{ scope.label }}</span>
<span class="text-xs font-normal text-muted-foreground">{{ scope.text }}</span></label
>
</div>
}
</fieldset>
</div>
<div digDialogFooter>
<button digButton type="button" variant="outline" (click)="closeCreate(false)">انصراف</button>
<button digButton type="button" [disabled]="!name().trim() || scopes().length === 0" (click)="create()">
ساختِ کلید
</button>
</div>
}
</div></ng-container
>
</header>
<div>
<dig-table stackOnMobile variant="card"
><thead digTableHeader>
<tr digTableRow>
<th digTableHead>نام</th>
<th digTableHead>کلید</th>
<th digTableHead>دسترسی</th>
<th digTableHead>آخرین استفاده</th>
<th digTableHead class="w-12 px-0">
<span class="sr-only">کنشها</span>
</th>
</tr>
</thead>
<tbody digTableBody>
@for (key of keys(); track key.id) {
<tr digTableRow>
<td digTableCell>
<span class="flex flex-col"
><span class="font-medium">{{ key.name }}</span>
<span class="text-xs text-muted-foreground"
>ساختهشده {{ formatJalali(key.created, "long") }}</span
></span
>
</td>
<td digTableCell>
<!-- کلیدِ کامل فقط هنگامِ ساخت دیده میشود؛ اینجا شناسهاش کپی میشود تا در لاگ یا پشتیبانی پیدا شود. -->
<span class="flex items-center gap-1"
><code dir="ltr" class="rounded bg-muted px-2 py-1 text-xs">{{ key.prefix }}••••••••</code>
<button
digControlSize="default"
digButton
type="button"
variant="ghost"
size="icon"
[attr.aria-label]="\`کپیِ شناسهٔ کلیدِ \${key.name}\`"
(click)="copyPrefix(key)"
>
<svg digIcon="copy"></svg></button
></span>
</td>
<td digTableCell>
<span class="flex flex-wrap gap-1">
@for (scope of key.scopes; track scope) {
<span digBadge variant="soft">{{ scope }}</span>
}
</span>
</td>
<td digTableCell class="text-sm text-muted-foreground">
@if (key.used) {
<span class="flex items-center gap-1.5"
><svg digIcon="check" aria-hidden="true" class="size-3.5 text-success-subtle-foreground"></svg>
{{ formatRelativeTime(key.used) }}</span
>
} @else {
هنوز استفاده نشده
}
</td>
<td digTableCell class="w-12 px-0 text-center">
<ng-container digDropdownMenu
><button
digDropdownMenuTrigger
digButton
type="button"
variant="ghost"
size="icon"
[attr.aria-label]="\`کنشهای \${key.name}\`"
>
<svg digIcon="more-horizontal"></svg>
</button>
<div digDropdownMenuContent>
<div digDropdownMenuItem>تغییرِ نام</div>
<div digDropdownMenuItem variant="destructive" (select)="revoking.set(key)">باطل کردن</div>
</div></ng-container
>
</td>
</tr>
}
</tbody></dig-table
>
</div>
<p class="text-sm text-muted-foreground">
{{ toFaDigits(keys().length) }} کلیدِ فعال · راهنمای استفاده در مستنداتِ API
</p>
<ng-container digAlertDialog [open]="revoking() !== null" (openChange)="!$event && revoking.set(null)"
><div digControlSize="xl">
<div digAlertDialogContent>
<div digAlertDialogHeader>
<h2 digAlertDialogTitle>«{{ revoking()?.name }}» باطل شود؟</h2>
<p digAlertDialogDescription>هر سرویسی که با این کلید کار میکند فوراً قطع میشود و برگشتپذیر نیست.</p>
</div>
<div digAlertDialogFooter>
<button digAlertDialogCancel>منصرف شدم</button>
<button digAlertDialogAction variant="destructive" (click)="revoke()">باطل کردن</button>
</div>
</div>
</div></ng-container
>
</main>
`,
})
export class ApiKeysBlock {
readonly toFaDigits = toFaDigits
readonly formatRelativeTime = formatRelativeTime
readonly formatJalali = formatJalali
readonly copyPrefix = copyPrefix
readonly SCOPES = SCOPES
readonly keys = signal<Key[]>(INITIAL)
readonly creating = signal(false)
readonly name = signal("")
readonly scopes = signal<string[]>(["read"])
readonly created = signal<string | null>(null)
readonly revoking = signal<Key | null>(null)
create() {
const secret = `dg_live_${Math.random().toString(36).slice(2, 10)}${Math.random().toString(36).slice(2, 18)}`
this.keys.set([
{
id: secret,
name: this.name(),
prefix: secret.slice(0, 12),
scopes: SCOPES.filter((scope) => this.scopes().includes(scope.id)).map((scope) => scope.label),
created: new Date().toISOString().slice(0, 10),
},
...this.keys(),
])
this.created.set(secret)
}
closeCreate(next: boolean) {
this.creating.set(next)
if (!next) {
this.created.set(null)
this.name.set("")
this.scopes.set(["read"])
}
}
copySecret() {
void navigator.clipboard?.writeText(this.created() ?? "")
toast.success("کلید کپی شد.")
}
toggleScope(id: string, checked: boolean | "indeterminate") {
this.scopes.set(checked ? [...this.scopes(), id] : this.scopes().filter((item) => item !== id))
}
revoke() {
this.keys.set(this.keys().filter((item) => item.id !== this.revoking()?.id))
toast.success("کلید باطل شد.")
}
}
کدِ این بلاک هنوز برای Android نوشته نشده است.
همهٔ کامپوننتهای لازم برای Android آمادهاند؛ با MCP میتوانید همین صفحه را از روی نسخهٔ React بسازید.
کدِ React همین بلاک مرجع است. با MCP دیگ و get_block با framework="android" ایجنت آن را با کامپوننتهای Android میسازد.