Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/(authenticated)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function AuthenticatedLayout({
<SidebarProvider>
<AppSidebar />
<SidebarInset>
<div className="flex flex-1 flex-col gap-4 p-4">
<div className="flex min-h-0 flex-1 flex-col gap-4 overflow-hidden p-4">
<Suspense fallback={null}>
<AuthGate>{children}</AuthGate>
</Suspense>
Expand Down
2 changes: 1 addition & 1 deletion app/(authenticated)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function HomeContent() {
<div className="flex flex-row gap-4 w-full">
<Card className="flex-1">
<CardHeader>
<CardTitle className="text-2xl">Welcome t</CardTitle>
<CardTitle className="text-2xl">Welcome</CardTitle>
<CardDescription>You are signed in as</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
Expand Down
156 changes: 156 additions & 0 deletions app/(authenticated)/users/_components/users-client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
"use client";

import { useMemo, useState } from "react";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { ArrowDownAZ, ArrowUpZA, Search } from "lucide-react";
import { UsersDataTable } from "./users-data-table";
import { usersColumns } from "./users-columns";
import type { UserRow } from "../profile-role-label";

const USER_SUBSCRIPTION_VIEW_TABS = [
{ value: "all", label: "All Users" },
{ value: "active", label: "Active" },
{ value: "inactive", label: "Inactive" },
] as const;

export type UserSubscriptionViewTab =
(typeof USER_SUBSCRIPTION_VIEW_TABS)[number]["value"];

type SortDir = "asc" | "desc";

export interface RoleFilterOption {
value: string;
label: string;
}

interface UsersClientProps {
users: UserRow[];
roleFilterOptions: RoleFilterOption[];
}

export function UsersClient({ users, roleFilterOptions }: UsersClientProps) {
const [tab, setTab] = useState<UserSubscriptionViewTab>("all");
const [roleFilter, setRoleFilter] = useState<string>("all");
const [nameQuery, setNameQuery] = useState<string>("");
const [sortDir, setSortDir] = useState<SortDir>("asc");

const filtered = useMemo(() => {
const query = nameQuery.trim().toLowerCase();

return users
.filter((u) => {
if (!query) return true;
const name = `${u.firstName} ${u.lastName}`.toLowerCase();
return (
name.includes(query) || u.email.toLowerCase().includes(query)
);
})
.filter((u) => {
if (tab === "all") return true;
if (tab === "active") return u.isActive;
return !u.isActive;
})
.filter((u) => {
if (roleFilter === "all") return true;
return u.role === roleFilter;
})
.sort((a, b) => {
const nameA = `${a.firstName} ${a.lastName}`.toLowerCase();
const nameB = `${b.firstName} ${b.lastName}`.toLowerCase();
return sortDir === "asc"
? nameA.localeCompare(nameB)
: nameB.localeCompare(nameA);
});
}, [users, nameQuery, tab, roleFilter, sortDir]);

return (
<div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
<Tabs
value={tab}
onValueChange={(v) => setTab(v as UserSubscriptionViewTab)}
className="flex min-h-0 w-full min-w-0 flex-1 flex-col gap-2 overflow-hidden"
>
<div className="flex w-full min-w-0 shrink-0 flex-wrap items-center justify-between gap-x-3 gap-y-2">
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-2 sm:gap-3">
<div className="relative p-0.5 min-w-[min(100%,10rem)] max-w-full grow sm:max-w-xs sm:grow-0 sm:basis-56">
<Search className="absolute left-3 top-1/2 z-10 h-4 w-4 -translate-y-1/2 text-muted-foreground pointer-events-none" />
<Input
id="users-search"
placeholder="Search by name or email..."
value={nameQuery}
onChange={(e) => setNameQuery(e.target.value)}
className="w-full min-w-0 pl-9"
/>
</div>

<TabsList className="h-auto min-h-8 max-w-full min-w-0 flex-wrap justify-start border border-border">
{USER_SUBSCRIPTION_VIEW_TABS.map(({ value, label }) => (
<TabsTrigger key={value} value={value}>
{label}
</TabsTrigger>
))}
</TabsList>
</div>

<div className="flex min-w-0 shrink-0 flex-wrap items-center justify-end gap-2">
<Select value={roleFilter} onValueChange={setRoleFilter}>
<SelectTrigger
id="users-role-filter"
className="w-[min(100%,11rem)] min-w-[8.5rem] sm:w-[140px]"
>
<SelectValue placeholder="All Roles" />
</SelectTrigger>
<SelectContent>
{roleFilterOptions.map(({ value, label }) => (
<SelectItem key={value} value={value}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>

<Button
id="users-sort-toggle"
variant="outline"
size="icon"
className="shrink-0"
onClick={() =>
setSortDir((prev) => (prev === "asc" ? "desc" : "asc"))
}
aria-label={
sortDir === "asc" ? "Sort Z to A" : "Sort A to Z"
}
>
{sortDir === "asc" ? (
<ArrowDownAZ className="h-4 w-4" />
) : (
<ArrowUpZA className="h-4 w-4" />
)}
</Button>
</div>
</div>

<TabsContent
value={tab}
className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden focus-visible:outline-none"
>
<UsersDataTable
columns={usersColumns}
data={filtered}
emptyMessage="No users match the current filters."
rowLabel={(n) => `${n} user${n === 1 ? "" : "s"}`}
/>
</TabsContent>
</Tabs>
</div>
);
}
129 changes: 129 additions & 0 deletions app/(authenticated)/users/_components/users-columns.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"use client";

import { ColumnDef } from "@tanstack/react-table";
import { Pencil, Tag } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Avatar, AvatarFallback} from "@/components/ui/avatar";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";

import { profileRoleLabel, type UserRow } from "../profile-role-label";

export const usersColumns: ColumnDef<UserRow>[] = [
{
id: "profile",
header: "User Profile",
meta: {
colWidth: "32%",
tdClassName: "whitespace-normal align-middle",
},
cell: ({ row }) => {
const u = row.original;
const fullName = `${u.firstName} ${u.lastName}`;
return (
<div className="flex min-w-0 max-w-full items-center gap-2 sm:gap-3">
<Avatar>
<AvatarFallback className="bg-muted text-xs font-semibold text-muted-foreground">
{`${u.firstName[0] ?? ""}${u.lastName[0] ?? ""}`.toUpperCase()}
</AvatarFallback>
</Avatar>
<div className="flex min-w-0 flex-1 flex-col">
<span className="truncate font-semibold text-sm">
{fullName}
</span>
<span className="truncate text-xs text-muted-foreground">
{u.email}
</span>
</div>
</div>
);
},
},
{
accessorKey: "role",
header: "Role",
meta: { colWidth: "14%" },
cell: ({ row }) => (
<span className="inline-flex max-w-full min-w-0 items-center rounded-full border border-border px-2 py-0.5 text-xs font-medium capitalize text-foreground">
<span className="truncate">
{profileRoleLabel(row.original.role)}
</span>
</span>
),
},
{
id: "status",
header: "Status",
meta: { colWidth: "14%" },
cell: ({ row }) => {
const active = row.original.isActive;
return (
<Badge variant={active ? "default" : "secondary"} className="gap-1.5">
<span
className={`h-2 w-2 rounded-full ${
active ? "bg-green-500" : "bg-muted-foreground/50"
}`}
/>
{active ? "Active" : "Inactive"}
</Badge>
);
Comment on lines +64 to +73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (
<span className="inline-flex items-center gap-1.5 text-sm">
<span
className={`h-2 w-2 rounded-full ${
active ? "bg-green-500" : "bg-muted-foreground/50"
}`}
/>
<span
className={
active ? "text-foreground" : "text-muted-foreground"
}
>
{active ? "Active" : "Inactive"}
</span>
</span>
);
<Badge variant={active ? "default" : "secondary"} className="gap-1.5">
<span
className={`h-2 w-2 rounded-full ${
active ? "bg-green-500" : "bg-muted-foreground/50"
}`}
/>
{active ? "Active" : "Inactive"}
</Badge>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

},
},
{
id: "lastLoginAt",
header: "Last Login",
meta: { colWidth: "18%" },
cell: ({ row }) => (
<span className="block min-w-0 truncate text-sm text-muted-foreground">
{new Intl.DateTimeFormat("en-CA", {
year: "numeric",
month: "short",
day: "numeric",
}).format(new Date(row.original.lastLoginAt || "Never Logged in" ))}
</span>
),
},
{
id: "actions",
header: () => <div className="text-right">Actions</div>,
meta: {
colWidth: "22%",
thClassName: "text-right",
tdClassName: "text-right",
},
cell: () => (
<div className="flex items-center justify-end gap-0.5">
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon-sm"
aria-label="Edit user"
disabled
>
<Pencil />
</Button>
</TooltipTrigger>
<TooltipContent>Edit (coming soon)</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon-sm"
aria-label="Give discount"
disabled
>
<Tag />
</Button>
</TooltipTrigger>
<TooltipContent>Give coupon (coming soon)</TooltipContent>
</Tooltip>
</div>
),
},
];
Loading
Loading