v2: fixed oauth errors, oauth clients native control added
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
"use client"
|
||||
|
||||
import { createColumnHelper } from "@tanstack/react-table";
|
||||
import { Dispatch, SetStateAction, useMemo } from "react";
|
||||
import { type DataTableFeatures } from "./features";
|
||||
import { OAuthClient } from "@/types";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { BanIcon, MoreHorizontal, PencilIcon } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||
import { deleteClient } from "@/actions/clients";
|
||||
|
||||
const columnHelper = createColumnHelper<DataTableFeatures, OAuthClient>();
|
||||
|
||||
async function handleDisable({ id, setClients }: { id: string; setClients: Dispatch<SetStateAction<OAuthClient[] | []>> }) {
|
||||
try {
|
||||
await deleteClient(id);
|
||||
setClients((prev) => prev ? prev.map((c) => c.id === id ? { ...c, is_active: false } : c) : prev);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
export function useColumns({ setClients, setEditingClient }: { setClients: Dispatch<SetStateAction<OAuthClient[] | []>>; setEditingClient: Dispatch<SetStateAction<OAuthClient | null>>; }) {
|
||||
const t = useTranslations("MePage.items.applications.table");
|
||||
|
||||
return useMemo(
|
||||
() =>
|
||||
columnHelper.columns([
|
||||
columnHelper.accessor("name", {
|
||||
header: t("client"),
|
||||
cell: ({ row }) => (
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<h4>{row.getValue("name")}</h4>
|
||||
<h5 className="text-xs text-muted-foreground">
|
||||
{row.original.client_id}
|
||||
</h5>
|
||||
</div>
|
||||
),
|
||||
}),
|
||||
columnHelper.accessor("redirect_uris", {
|
||||
header: t("redirectURIs"),
|
||||
cell: ({ row }) => (
|
||||
<div className="flex flex-row gap-1 flex-wrap">
|
||||
{(row.getValue("redirect_uris") as string[]).map((url) => (
|
||||
<Badge key={url} variant="secondary">
|
||||
{url}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
),
|
||||
}),
|
||||
columnHelper.accessor("scopes", {
|
||||
header: t("scopes"),
|
||||
cell: ({ row }) => (
|
||||
<div className="flex flex-row gap-1 flex-wrap">
|
||||
{(row.getValue("scopes") as string[]).map((scope) => (
|
||||
<Badge key={scope} variant="secondary">
|
||||
{scope}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
),
|
||||
}),
|
||||
columnHelper.accessor("is_active", {
|
||||
header: t("status"),
|
||||
cell: ({ row }) => {
|
||||
return row.getValue("is_active") === true ? t("active") : t("disabled")
|
||||
},
|
||||
}),
|
||||
|
||||
columnHelper.display({
|
||||
id: "actions",
|
||||
cell: ({ row }) => {
|
||||
const client = row.original;
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" className="h-8 w-8 p-0">
|
||||
<MoreHorizontal className="h-4 w-4" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setEditingClient(client)}><PencilIcon />{t("edit")}</DropdownMenuItem>
|
||||
{client.is_active && <DropdownMenuItem onClick={() => handleDisable({ id: client.id, setClients })}><BanIcon />{t("disable")}</DropdownMenuItem>}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
},
|
||||
}),
|
||||
]),
|
||||
[t, setClients, setEditingClient]
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user