From 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Tue, 25 Mar 2025 15:55:45 +0900 Subject: initial commit --- lib/tasks/table/tasks-table-floating-bar.tsx | 354 +++++++++++++++++++++++++++ 1 file changed, 354 insertions(+) create mode 100644 lib/tasks/table/tasks-table-floating-bar.tsx (limited to 'lib/tasks/table/tasks-table-floating-bar.tsx') diff --git a/lib/tasks/table/tasks-table-floating-bar.tsx b/lib/tasks/table/tasks-table-floating-bar.tsx new file mode 100644 index 00000000..6d367f81 --- /dev/null +++ b/lib/tasks/table/tasks-table-floating-bar.tsx @@ -0,0 +1,354 @@ +"use client" + +import * as React from "react" +import { tasks, type Task } from "@/db/schema/tasks" +import { SelectTrigger } from "@radix-ui/react-select" +import { type Table } from "@tanstack/react-table" +import { + ArrowUp, + CheckCircle2, + Download, + Loader, + Trash2, + X, +} from "lucide-react" +import { toast } from "sonner" + +import { exportTableToExcel } from "@/lib/export" +import { Button } from "@/components/ui/button" +import { Portal } from "@/components/ui/portal" +import { + Select, + SelectContent, + SelectGroup, + SelectItem, +} from "@/components/ui/select" +import { Separator } from "@/components/ui/separator" +import { + Tooltip, + TooltipContent, + TooltipTrigger, +} from "@/components/ui/tooltip" +import { Kbd } from "@/components/kbd" + +import { removeTasks, modifiTasks } from "@/lib//tasks/service" +import { DeleteTasksDialog } from "./delete-tasks-dialog" +import { ActionConfirmDialog } from "@/components/ui/action-dialog" + +interface TasksTableFloatingBarProps { + table: Table +} + + +export function TasksTableFloatingBar({ table }: TasksTableFloatingBarProps) { + const rows = table.getFilteredSelectedRowModel().rows + + const [isPending, startTransition] = React.useTransition() + const [action, setAction] = React.useState< + "update-status" | "update-priority" | "export" | "delete" + >() + const [popoverOpen, setPopoverOpen] = React.useState(false) + + // Clear selection on Escape key press + React.useEffect(() => { + function handleKeyDown(event: KeyboardEvent) { + if (event.key === "Escape") { + table.toggleAllRowsSelected(false) + } + } + + window.addEventListener("keydown", handleKeyDown) + return () => window.removeEventListener("keydown", handleKeyDown) + }, [table]) + + + + // 공용 confirm dialog state + const [confirmDialogOpen, setConfirmDialogOpen] = React.useState(false) + const [confirmProps, setConfirmProps] = React.useState<{ + title: string + description?: string + onConfirm: () => Promise | void + }>({ + title: "", + description: "", + onConfirm: () => { }, + }) + + // 1) "삭제" Confirm 열기 + function handleDeleteConfirm() { + setAction("delete") + setConfirmProps({ + title: `Delete ${rows.length} user${rows.length > 1 ? "s" : ""}?`, + description: "This action cannot be undone.", + onConfirm: async () => { + startTransition(async () => { + const { error } = await removeTasks({ + ids: rows.map((row) => row.original.id), + }) + if (error) { + toast.error(error) + return + } + toast.success("Users deleted") + table.toggleAllRowsSelected(false) + setConfirmDialogOpen(false) + }) + }, + }) + setConfirmDialogOpen(true) + } + + // 2) + function handleSelectStatus(newStatus: Task["status"]) { + setAction("update-status") + + setConfirmProps({ + title: `Update ${rows.length} task${rows.length > 1 ? "s" : ""} with status: ${newStatus}?`, + description: "This action will override their current status.", + onConfirm: async () => { + startTransition(async () => { + const { error } = await modifiTasks({ + ids: rows.map((row) => row.original.id), + status: newStatus, + }) + if (error) { + toast.error(error) + return + } + toast.success("Tasks updated") + setConfirmDialogOpen(false) + }) + }, + }) + setConfirmDialogOpen(true) + } + + // 3) + function handleSelectPriority(newPriority: Task["priority"]) { + setAction("update-priority") + + setConfirmProps({ + title: `Update ${rows.length} task${rows.length > 1 ? "s" : ""} with priority: ${newPriority}?`, + description: "This action will override their current priority.", + onConfirm: async () => { + startTransition(async () => { + const { error } = await modifiTasks({ + ids: rows.map((row) => row.original.id), + priority: newPriority, + }) + if (error) { + toast.error(error) + return + } + toast.success("Tasks updated") + setConfirmDialogOpen(false) + }) + }, + }) + setConfirmDialogOpen(true) + } + + return ( + +
+
+
+
+ + {rows.length} selected + + + + + + + +

Clear selection

+ + Esc + +
+
+
+ +
+ + + + + + + +

Export tasks

+
+
+ + + + + +

Delete tasks

+
+
+
+
+
+
+ + + {/* 공용 Confirm Dialog */} + +
+ ) +} -- cgit v1.2.3