summaryrefslogtreecommitdiff
path: root/lib/email-whitelist/table/whitelist-table-columns.tsx
blob: 2533d8637e50ef46f167ce506500e8a8e6bf7de9 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use client';

/* IMPORT */
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { MoreHorizontal, Trash, Pencil } from 'lucide-react';
import { DataTableColumnHeaderSimple } from '@/components/data-table/data-table-column-simple-header';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { formatDate } from '@/lib/utils';
import { type ColumnDef } from '@tanstack/react-table';
import { type Dispatch, type SetStateAction } from 'react';
import { type DataTableRowAction } from '@/types/table';
import { type EmailWhitelist } from '../service';

// ----------------------------------------------------------------------------------------------------

/* TYPES */
interface GetColumnsProps {
  setRowAction: Dispatch<SetStateAction<DataTableRowAction<EmailWhitelist> | null>>;
}

// ----------------------------------------------------------------------------------------------------

/* FUNCTION FOR GETTING COLUMNS SETTING */
export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<EmailWhitelist>[] {
  return [
    // [1] Checkbox Column
    {
      id: 'select',
      header: ({ table }) => (
        <Checkbox
          checked={
            table.getIsAllPageRowsSelected() ||
            (table.getIsSomePageRowsSelected() && 'indeterminate')
          }
          onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
          aria-label="Select all"
          className="translate-y-0.5"
        />
      ),
      cell: ({ row }) => (
        <Checkbox
          checked={row.getIsSelected()}
          onCheckedChange={(value) => row.toggleSelected(!!value)}
          aria-label="Select row"
          className="translate-y-0.5"
        />
      ),
      enableSorting: false,
      enableHiding: false,
    },

    // [2] ID Column
    {
      accessorKey: 'id',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="ID" />
      ),
      cell: ({ row }) => <div>{row.getValue('id')}</div>,
      enableSorting: false,
      enableHiding: false,
      size: 80,
    },

    // [3] Email/Domain Column
    {
      accessorKey: 'displayValue',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="이메일/도메인" />
      ),
      cell: ({ row }) => {
        const displayValue = row.getValue('displayValue') as string;
        const isEmail = displayValue.includes('@');

        return (
          <div className="flex space-x-2">
            <Badge variant={isEmail ? 'default' : 'outline'} className="font-mono">
              {isEmail ? displayValue : `@${displayValue}`}
            </Badge>
          </div>
        );
      },
      enableSorting: false,
      size: 220,
    },

    // [4] Description Column
    {
      accessorKey: 'description',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="설명" />
      ),
      cell: ({ row }) => {
        const description = row.getValue('description') as string;
        return (
          <div className="truncate" title={description || ''}>
            {description || '-'}
          </div>
        );
      },
      enableSorting: false,
      size: 200,
    },

    // [5] Created At Column
    {
      accessorKey: 'createdAt',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="생성일" />
      ),
      cell: ({ row }) => {
        const date = row.getValue('createdAt') as Date;
        return <div>{formatDate(date, "KR")}</div>;
      },
      enableSorting: false,
      size: 120,
    },

    // [6] Created By Column
    {
      accessorKey: 'createdByName',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="생성자" />
      ),
      cell: ({ row }) => {
        const name = row.getValue('createdByName') as string;
        return <div>{name || '-'}</div>;
      },
      enableSorting: false,
      size: 100,
    },

    // [7] Updated At Column
    {
      accessorKey: 'updatedAt',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="수정일" />
      ),
      cell: ({ row }) => {
        const date = row.getValue('updatedAt') as Date;
        return <div>{formatDate(date, "KR")}</div>;
      },
      enableSorting: false,
      size: 120,
    },

    // [8] Updated By Column
    {
      accessorKey: 'updatedByName',
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="수정자" />
      ),
      cell: ({ row }) => {
        const name = row.getValue('updatedByName') as string;
        return <div>{name || '-'}</div>;
      },
      enableSorting: false,
      size: 100,
    },

    // [9] Actions Column
    {
      id: 'actions',
      cell: ({ row }) => (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              aria-label="Open menu"
              variant="ghost"
              className="flex size-6 p-0 data-[state=open]:bg-muted"
            >
              <MoreHorizontal className="size-4" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-[160px]">
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: 'update' })}
            >
              <Pencil className="mr-2 size-4" />
              수정
            </DropdownMenuItem>
            <DropdownMenuSeparator />
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: 'delete' })}
              className="text-destructive"
            >
              <Trash className="mr-2 size-4" />
              삭제
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      ),
      enableSorting: false,
      enableHiding: false,
      enableResizing: false,
      size: 80,
      minSize: 80,
      maxSize: 80,
    },
  ];
}