summaryrefslogtreecommitdiff
path: root/lib/docu-list-rule/code-groups/table/code-groups-table.tsx
blob: 0029ed91274a0935e5353df07d236de70c7afdd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use client";
import * as React from "react";
import { useRouter } from "next/navigation";
import { useDataTable } from "@/hooks/use-data-table";
import { DataTable } from "@/components/data-table/data-table";
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar";
import type {
  DataTableAdvancedFilterField,
  DataTableFilterField,
  DataTableRowAction,
} from "@/types/table"
import { getColumns } from "@/lib/docu-list-rule/code-groups/table/code-groups-table-columns";
import { DeleteCodeGroupsDialog } from "@/lib/docu-list-rule/code-groups/table/delete-code-groups-dialog";
import { CodeGroupsEditSheet } from "@/lib/docu-list-rule/code-groups/table/code-groups-edit-sheet";
import { CodeGroupsTableToolbarActions } from "@/lib/docu-list-rule/code-groups/table/code-groups-table-toolbar";
import { codeGroups } from "@/db/schema/docu-list-rule";

interface CodeGroupsTableProps {
  promises?: Promise<[{ data: typeof codeGroups.$inferSelect[]; pageCount: number }] >;
}

export function CodeGroupsTable({ promises }: CodeGroupsTableProps) {
  const router = useRouter();
  const [rowAction, setRowAction] = React.useState<DataTableRowAction<typeof codeGroups.$inferSelect> | null>(null);

  const [{ data, pageCount }] = promises ? React.use(promises) : [{ data: [], pageCount: 0 }];

  const refreshData = React.useCallback(async () => {
    // 전체 페이지 새로고침 대신 router.refresh() 사용 (성능 개선)
    router.refresh();
  }, [router]);

  // 컬럼 설정 - 외부 파일에서 가져옴
  const columns = React.useMemo(
    () => getColumns({ setRowAction }),
    [setRowAction]
  )

  // 필터 필드 설정
  const filterFields: DataTableFilterField<typeof codeGroups.$inferSelect>[] = [];

  // 고급 필터 필드 설정
  const advancedFilterFields: DataTableAdvancedFilterField<typeof codeGroups.$inferSelect>[] = [
    { id: "groupId", label: "Group ID", type: "text" },
    { id: "description", label: "Description", type: "text" },
    { id: "codeFormat", label: "Code Format", type: "text" },
    {
      id: "controlType", label: "Control Type", type: "select", options: [
        { label: "Textbox", value: "textbox" },
        { label: "Combobox", value: "combobox" },
      ]
    },
    {
      id: "isActive", label: "Status", type: "select", options: [
        { label: "Active", value: "true" },
        { label: "Inactive", value: "false" },
      ]
    },
    { id: "createdAt", label: "Created At", type: "date" },
  ];

  const { table } = useDataTable({
      data,
      columns,
      pageCount,
      filterFields,
      enablePinning: true,
      enableAdvancedFilter: true,
      initialState: {
        columnPinning: { right: ["actions"] },
        expanded: {},
      },
      getRowId: (originalRow) => String(originalRow.id),
      shallow: false,
      clearOnDefault: false,
    })

  // 컴포넌트 마운트 후 그룹핑 설정
  React.useEffect(() => {
    if (data && table.getState().grouping.length === 0) {
      table.setGrouping(["projectCode"])
    }
  }, [table, data])

  // 정렬 시 펼쳐진 상태 유지
  React.useEffect(() => {
    const currentExpanded = table.getState().expanded
    if (Object.keys(currentExpanded).length > 0) {
      // 약간의 지연 후 현재 펼쳐진 상태를 다시 설정
      const timer = setTimeout(() => {
        table.setExpanded(currentExpanded)
      }, 100)
      return () => clearTimeout(timer)
    }
  }, [table.getState().sorting, table])



  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
          shallow={false}
        >
          <CodeGroupsTableToolbarActions table={table} onSuccess={refreshData} />
        </DataTableAdvancedToolbar>
      </DataTable>

      <DeleteCodeGroupsDialog
        open={rowAction?.type === "delete"}
        onOpenChange={() => setRowAction(null)}
        codeGroups={rowAction?.row.original ? [rowAction?.row.original] : []}
        showTrigger={false}
        onSuccess={() => {
          rowAction?.row.toggleSelected(false)
          refreshData()
        }}
      />

      <CodeGroupsEditSheet
        open={rowAction?.type === "update"}
        onOpenChange={() => setRowAction(null)}
        data={rowAction?.row.original ?? null}
        onSuccess={refreshData}
      />
    </>
  );
}