2026-02-04 02:28:51 +05:30
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
|
|
|
|
|
import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs";
|
|
|
|
|
import {
|
|
|
|
|
type ColumnDef,
|
|
|
|
|
flexRender,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
useReactTable,
|
|
|
|
|
} from "@tanstack/react-table";
|
|
|
|
|
|
|
|
|
|
import { DashboardSidebar } from "@/components/organisms/DashboardSidebar";
|
|
|
|
|
import { DashboardShell } from "@/components/templates/DashboardShell";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2026-02-04 20:21:33 +05:30
|
|
|
import { getApiBaseUrl } from "@/lib/api-base";
|
2026-02-04 16:15:01 +05:30
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from "@/components/ui/dialog";
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
type Board = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
slug: string;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-04 20:21:33 +05:30
|
|
|
const apiBase = getApiBaseUrl();
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
export default function BoardsPage() {
|
|
|
|
|
const { getToken, isSignedIn } = useAuth();
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const [boards, setBoards] = useState<Board[]>([]);
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-02-04 16:15:01 +05:30
|
|
|
const [deleteTarget, setDeleteTarget] = useState<Board | null>(null);
|
|
|
|
|
const [isDeleting, setIsDeleting] = useState(false);
|
|
|
|
|
const [deleteError, setDeleteError] = useState<string | null>(null);
|
2026-02-04 02:28:51 +05:30
|
|
|
|
|
|
|
|
const sortedBoards = useMemo(
|
|
|
|
|
() => [...boards].sort((a, b) => a.name.localeCompare(b.name)),
|
|
|
|
|
[boards]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const loadBoards = async () => {
|
|
|
|
|
if (!isSignedIn) return;
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
|
|
|
|
const token = await getToken();
|
|
|
|
|
const response = await fetch(`${apiBase}/api/v1/boards`, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: token ? `Bearer ${token}` : "",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Unable to load boards.");
|
|
|
|
|
}
|
|
|
|
|
const data = (await response.json()) as Board[];
|
|
|
|
|
setBoards(data);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err instanceof Error ? err.message : "Something went wrong.");
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
loadBoards();
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [isSignedIn]);
|
|
|
|
|
|
2026-02-04 16:15:01 +05:30
|
|
|
const handleDelete = async () => {
|
|
|
|
|
if (!deleteTarget || !isSignedIn) return;
|
|
|
|
|
setIsDeleting(true);
|
|
|
|
|
setDeleteError(null);
|
|
|
|
|
try {
|
|
|
|
|
const token = await getToken();
|
|
|
|
|
const response = await fetch(`${apiBase}/api/v1/boards/${deleteTarget.id}`, {
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: token ? `Bearer ${token}` : "",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Unable to delete board.");
|
|
|
|
|
}
|
|
|
|
|
setBoards((prev) => prev.filter((board) => board.id !== deleteTarget.id));
|
|
|
|
|
setDeleteTarget(null);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setDeleteError(err instanceof Error ? err.message : "Something went wrong.");
|
|
|
|
|
} finally {
|
|
|
|
|
setIsDeleting(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-04 02:28:51 +05:30
|
|
|
const columns = useMemo<ColumnDef<Board>[]>(
|
|
|
|
|
() => [
|
|
|
|
|
{
|
|
|
|
|
accessorKey: "name",
|
|
|
|
|
header: "Board",
|
|
|
|
|
cell: ({ row }) => (
|
|
|
|
|
<div>
|
2026-02-04 13:03:18 +05:30
|
|
|
<p className="font-medium text-strong">{row.original.name}</p>
|
|
|
|
|
<p className="text-xs text-quiet">{row.original.slug}</p>
|
2026-02-04 02:28:51 +05:30
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "actions",
|
|
|
|
|
header: "",
|
|
|
|
|
cell: ({ row }) => (
|
|
|
|
|
<div
|
2026-02-04 16:15:01 +05:30
|
|
|
className="flex items-center justify-end gap-2"
|
2026-02-04 02:28:51 +05:30
|
|
|
onClick={(event) => event.stopPropagation()}
|
|
|
|
|
>
|
|
|
|
|
<Link
|
|
|
|
|
href={`/boards/${row.original.id}`}
|
2026-02-04 13:03:18 +05:30
|
|
|
className="inline-flex h-8 items-center justify-center rounded-lg border border-[color:var(--border)] px-3 text-xs font-medium text-muted transition hover:border-[color:var(--accent)] hover:text-[color:var(--accent)]"
|
2026-02-04 02:28:51 +05:30
|
|
|
>
|
|
|
|
|
Open
|
|
|
|
|
</Link>
|
2026-02-04 16:15:01 +05:30
|
|
|
<Link
|
|
|
|
|
href={`/boards/${row.original.id}/edit`}
|
|
|
|
|
className="inline-flex h-8 items-center justify-center rounded-lg border border-[color:var(--border)] px-3 text-xs font-medium text-muted transition hover:border-[color:var(--accent)] hover:text-[color:var(--accent)]"
|
|
|
|
|
>
|
|
|
|
|
Edit
|
|
|
|
|
</Link>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => setDeleteTarget(row.original)}
|
|
|
|
|
>
|
|
|
|
|
Delete
|
|
|
|
|
</Button>
|
2026-02-04 02:28:51 +05:30
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const table = useReactTable({
|
|
|
|
|
data: sortedBoards,
|
|
|
|
|
columns,
|
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DashboardShell>
|
|
|
|
|
<SignedOut>
|
2026-02-04 13:03:18 +05:30
|
|
|
<div className="flex h-full flex-col items-center justify-center gap-4 rounded-2xl surface-panel p-10 text-center lg:col-span-2">
|
|
|
|
|
<p className="text-sm text-muted">Sign in to view boards.</p>
|
2026-02-04 02:28:51 +05:30
|
|
|
<SignInButton
|
|
|
|
|
mode="modal"
|
|
|
|
|
forceRedirectUrl="/boards"
|
|
|
|
|
signUpForceRedirectUrl="/boards"
|
|
|
|
|
>
|
2026-02-04 13:03:18 +05:30
|
|
|
<Button>Sign in</Button>
|
2026-02-04 02:28:51 +05:30
|
|
|
</SignInButton>
|
|
|
|
|
</div>
|
|
|
|
|
</SignedOut>
|
|
|
|
|
<SignedIn>
|
|
|
|
|
<DashboardSidebar />
|
2026-02-04 13:03:18 +05:30
|
|
|
<div className="flex h-full flex-col gap-6 rounded-2xl surface-panel p-8">
|
2026-02-04 02:28:51 +05:30
|
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
|
|
|
<div>
|
2026-02-04 13:03:18 +05:30
|
|
|
<h2 className="text-lg font-semibold text-strong">Boards</h2>
|
|
|
|
|
<p className="text-sm text-muted">
|
2026-02-04 02:28:51 +05:30
|
|
|
{sortedBoards.length} board
|
|
|
|
|
{sortedBoards.length === 1 ? "" : "s"} total.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2026-02-04 13:03:18 +05:30
|
|
|
<Button onClick={() => router.push("/boards/new")}>
|
2026-02-04 02:28:51 +05:30
|
|
|
New board
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && (
|
2026-02-04 13:03:18 +05:30
|
|
|
<div className="rounded-lg border border-[color:var(--border)] bg-[color:var(--surface-muted)] p-3 text-xs text-muted">
|
2026-02-04 02:28:51 +05:30
|
|
|
{error}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{sortedBoards.length === 0 && !isLoading ? (
|
2026-02-04 13:03:18 +05:30
|
|
|
<div className="flex flex-1 flex-col items-center justify-center gap-2 rounded-2xl border border-dashed border-[color:var(--border)] bg-[color:var(--surface-muted)] p-6 text-center text-sm text-muted">
|
2026-02-04 02:28:51 +05:30
|
|
|
No boards yet. Create your first board to get started.
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-02-04 13:03:18 +05:30
|
|
|
<div className="overflow-hidden rounded-2xl border border-[color:var(--border)] bg-[color:var(--surface)]">
|
|
|
|
|
<table className="min-w-full divide-y divide-[color:var(--border)] text-sm">
|
|
|
|
|
<thead className="bg-[color:var(--surface-muted)]">
|
2026-02-04 02:28:51 +05:30
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
|
<tr key={headerGroup.id}>
|
|
|
|
|
{headerGroup.headers.map((header) => (
|
|
|
|
|
<th
|
|
|
|
|
key={header.id}
|
2026-02-04 13:03:18 +05:30
|
|
|
className="px-4 py-3 text-left text-[11px] font-semibold uppercase tracking-[0.22em] text-quiet"
|
2026-02-04 02:28:51 +05:30
|
|
|
>
|
|
|
|
|
{header.isPlaceholder
|
|
|
|
|
? null
|
|
|
|
|
: flexRender(
|
|
|
|
|
header.column.columnDef.header,
|
|
|
|
|
header.getContext()
|
|
|
|
|
)}
|
|
|
|
|
</th>
|
|
|
|
|
))}
|
|
|
|
|
</tr>
|
|
|
|
|
))}
|
|
|
|
|
</thead>
|
2026-02-04 13:03:18 +05:30
|
|
|
<tbody className="divide-y divide-[color:var(--border)] bg-[color:var(--surface)]">
|
2026-02-04 02:28:51 +05:30
|
|
|
{table.getRowModel().rows.map((row) => (
|
|
|
|
|
<tr
|
|
|
|
|
key={row.id}
|
2026-02-04 13:03:18 +05:30
|
|
|
className="cursor-pointer transition hover:bg-[color:var(--surface-muted)]"
|
2026-02-04 02:28:51 +05:30
|
|
|
onClick={() => router.push(`/boards/${row.original.id}`)}
|
|
|
|
|
>
|
|
|
|
|
{row.getVisibleCells().map((cell) => (
|
|
|
|
|
<td key={cell.id} className="px-4 py-3 align-top">
|
|
|
|
|
{flexRender(
|
|
|
|
|
cell.column.columnDef.cell,
|
|
|
|
|
cell.getContext()
|
|
|
|
|
)}
|
|
|
|
|
</td>
|
|
|
|
|
))}
|
|
|
|
|
</tr>
|
|
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</SignedIn>
|
2026-02-04 16:15:01 +05:30
|
|
|
|
|
|
|
|
<Dialog
|
|
|
|
|
open={!!deleteTarget}
|
|
|
|
|
onOpenChange={(nextOpen) => {
|
|
|
|
|
if (!nextOpen) {
|
|
|
|
|
setDeleteTarget(null);
|
|
|
|
|
setDeleteError(null);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<DialogContent aria-label="Delete board">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Delete board</DialogTitle>
|
|
|
|
|
<DialogDescription>
|
|
|
|
|
This will remove {deleteTarget?.name}. This action cannot be undone.
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
{deleteError ? (
|
|
|
|
|
<div className="rounded-lg border border-[color:var(--border)] bg-[color:var(--surface-muted)] p-3 text-xs text-muted">
|
|
|
|
|
{deleteError}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" onClick={() => setDeleteTarget(null)}>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleDelete} disabled={isDeleting}>
|
|
|
|
|
{isDeleting ? "Deleting…" : "Delete"}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2026-02-04 02:28:51 +05:30
|
|
|
</DashboardShell>
|
|
|
|
|
);
|
|
|
|
|
}
|