daphnis/components/scoreplaylog/colums.tsx
2024-07-01 12:47:02 -04:00

117 lines
3.4 KiB
TypeScript

"use client";
import { ColumnDef } from "@tanstack/react-table";
import { MoreHorizontal } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Badge } from "@/components/ui/badge";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { generateShareToken } from "@/app/(sharing)/[token]/token";
import { chuni_score_playlog } from "@/prisma/schemas/artemis/generated/artemis";
import { chuni_static_music } from "@/prisma/schemas/artemis/generated/artemis";
export const columns: ColumnDef<chuni_score_playlog & chuni_static_music>[] = [
{
accessorKey: "title",
header: "Title",
cell: ({ row }) => <div className="font-medium">{row.original.title}</div>,
},
{
accessorKey: "score",
header: "Score",
cell: ({ row }) => (
<div>
{row.original.score?.toLocaleString()}
{row.original.isNewRecord && <span className="pl-2 ">New!!</span>}{" "}
</div>
),
},
{
accessorKey: "userPlayDate",
header: "Date",
},
{
accessorKey: "FC / AJ",
header: "FC / AJ",
cell: ({ row }) => (
<div>
<div className="font-medium">
<span className="space-x-2 pl-2">
{!row.original.isAllJustice && row.original.isFullCombo && (
<span className="">Full Combo</span>
)}
{row.original.isAllJustice && (
<span className=" ">All Justice</span>
)}
</span>
</div>
</div>
),
},
{
accessorKey: "Attempts",
header: "Attempts",
cell: ({ row }) => <div className="font-medium">{row.original.title}</div>,
},
{
id: "actions",
header: () => <div className="text-left pl-2">More</div>,
cell: ({ row }) => {
const song = row.original;
const [error, setError] = useState<string>("");
const router = useRouter();
const handleGenerateShareToken = async () => {
const { token, error } = await generateShareToken(row.original.id);
if (error) {
setError(error);
} else {
const newTab = window.open(`/${token}/${row.original.id}`, "_blank");
if (newTab) {
newTab.focus();
} else {
// Handle popup blocker or inability to open new tab
router.push(`/${token}/${row.original.id}`);
}
}
};
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem
onClick={() =>
song.title && navigator.clipboard.writeText(song.title)
}
>
Copy song title
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={handleGenerateShareToken}>
Share Song
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
},
},
];