summaryrefslogtreecommitdiff
path: root/lib/page-visits/table/page-visits-table-columns.tsx
blob: 86f2d9c31d89bfde54ef02387a44d2e4e9e9131c (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"use client"

import * as React from "react"
import { type DataTableRowAction } from "@/types/table"
import { type ColumnDef } from "@tanstack/react-table"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"

import { formatDate } from "@/lib/utils"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { ExtendedPageVisit } from "../validation"
import { Eye, ExternalLink, Clock, User, Ellipsis } from "lucide-react"

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<ExtendedPageVisit> | null>>
}

export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<ExtendedPageVisit>[] {
  return [
    {
      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,
    },
    {
      id: "사용자",
      header: "사용자",
      columns: [
        {
          accessorKey: "userEmail",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="사용자" />
          ),
          cell: ({ row }) => {
            const userEmail = row.getValue("userEmail") as string | null
            const userName = row.original.userName
            
            if (!userEmail) {
              return (
                <div className="flex items-center gap-2">
                  <User className="size-3 text-muted-foreground" />
                  <span className="text-muted-foreground text-xs">익명</span>
                </div>
              )
            }
            
            return (
              <div className="flex flex-col">
                <span className="font-medium text-sm">{userEmail}</span>
                {userName && (
                  <span className="text-xs text-muted-foreground">{userName}</span>
                )}
              </div>
            )
          },
        },
      ],
    },
    {
      id: "페이지 정보",
      header: "페이지 정보",
      columns: [
        {
          accessorKey: "route",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="경로" />
          ),
          cell: ({ row }) => {
            const route = row.getValue("route") as string
            const pageTitle = row.original.pageTitle
            
            return (
              <div className="flex flex-col max-w-[200px]">
                <code className="text-xs bg-muted px-1 py-0.5 rounded font-mono">
                  {route}
                </code>
                {pageTitle && (
                  <span className="text-xs text-muted-foreground mt-1 truncate">
                    {pageTitle}
                  </span>
                )}
              </div>
            )
          },
        },
        {
          accessorKey: "visitedAt",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="방문 시간" />
          ),
          cell: ({ row }) => {
            const date = row.getValue("visitedAt") as Date
            return (
              <Tooltip>
                <TooltipTrigger>
                  <div className="text-sm">
                    {formatDate(date, 'KR')}
                  </div>
                </TooltipTrigger>
                <TooltipContent>
                  {formatDate(date, "KR")}
                </TooltipContent>
              </Tooltip>
            )
          },
        },
        {
          accessorKey: "duration",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="체류 시간" />
          ),
          cell: ({ row }) => {
            const duration = row.getValue("duration") as number | null
            const isLongVisit = row.original.isLongVisit
            
            if (!duration) {
              return <span className="text-muted-foreground">-</span>
            }
            
            const minutes = Math.floor(duration / 60)
            const seconds = duration % 60
            
            let displayText = ""
            if (minutes > 0) {
              displayText = `${minutes}분 ${seconds}초`
            } else {
              displayText = `${seconds}초`
            }
            
            return (
              <div className="flex items-center gap-2">
                {isLongVisit && <Clock className="size-3 text-orange-500" />}
                <span className={isLongVisit ? "font-medium" : ""}>
                  {displayText}
                </span>
              </div>
            )
          },
        },
      ],
    },
    {
      id: "환경 정보",
      header: "환경 정보",
      columns: [
        {
          accessorKey: "deviceType",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="디바이스" />
          ),
          cell: ({ row }) => {
            const deviceType = row.getValue("deviceType") as string
            const variants = {
              desktop: "default",
              mobile: "secondary",
              tablet: "outline",
            } as const
            
            return (
              <Badge variant={variants[deviceType as keyof typeof variants] || "default"} className="text-xs">
                {deviceType}
              </Badge>
            )
          },
        },
        {
          accessorKey: "browserName",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="브라우저" />
          ),
          cell: ({ row }) => {
            const browserName = row.getValue("browserName") as string | null
            const osName = row.original.osName
            
            return (
              <div className="flex flex-col">
                <span className="text-sm">{browserName || "Unknown"}</span>
                {osName && (
                  <span className="text-xs text-muted-foreground">{osName}</span>
                )}
              </div>
            )
          },
        },
        {
          accessorKey: "ipAddress",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="IP" />
          ),
          cell: ({ row }) => (
            <code className="text-xs bg-muted px-2 py-1 rounded">
              {row.getValue("ipAddress")}
            </code>
          ),
        },
      ],
    },
    {
      id: "추가 정보",
      header: "추가 정보",
      columns: [
        {
          accessorKey: "referrer",
          header: ({ column }) => (
            <DataTableColumnHeaderSimple column={column} title="리퍼러" />
          ),
          cell: ({ row }) => {
            const referrer = row.getValue("referrer") as string | null
            
            if (!referrer) {
              return <span className="text-muted-foreground text-xs">직접 접근</span>
            }
            
            try {
              const url = new URL(referrer)
              return (
                <div className="flex items-center gap-1">
                  <ExternalLink className="size-3" />
                  <span className="text-xs truncate max-w-[100px]">
                    {url.hostname}
                  </span>
                </div>
              )
            } catch {
              return (
                <span className="text-xs truncate max-w-[100px]">
                  {referrer}
                </span>
              )
            }
          },
        },
      ],
    },
    // {
    //   id: "actions",
    //   cell: function Cell({ row }) {
    //     const visit = row.original

    //     return (
    //       <DropdownMenu>
    //         <DropdownMenuTrigger asChild>
    //           <Button
    //             aria-label="Open menu"
    //             variant="ghost"
    //             className="flex size-8 p-0 data-[state=open]:bg-muted"
    //           >
    //             <Ellipsis className="size-4" aria-hidden="true" />
    //           </Button>
    //         </DropdownMenuTrigger>
    //         <DropdownMenuContent align="end" className="w-40">
    //           <DropdownMenuItem
    //             onSelect={() => setRowAction({ type: "view", row })}
    //           >
    //             <Eye className="mr-2 size-4" aria-hidden="true" />
    //             상세 보기
    //           </DropdownMenuItem>
    //           {visit.userEmail && (
    //             <DropdownMenuItem
    //               onSelect={() => setRowAction({ type: "viewUserActivity", row })}
    //             >
    //               <User className="mr-2 size-4" aria-hidden="true" />
    //               사용자 활동
    //             </DropdownMenuItem>
    //           )}
    //           {visit.route && (
    //             <DropdownMenuItem
    //               onSelect={() => setRowAction({ type: "viewPageStats", row })}
    //             >
    //               <ExternalLink className="mr-2 size-4" aria-hidden="true" />
    //               페이지 통계
    //             </DropdownMenuItem>
    //           )}
    //         </DropdownMenuContent>
    //       </DropdownMenu>
    //     )
    //   },
    //   enableSorting: false,
    //   enableHiding: false,
    // },
  ]
}