|
1 | 1 | "use client"; |
2 | | -import { FetchDataframe } from "services/chat"; |
3 | 2 | import { ChatResponseItem } from "@/types/chat-types"; |
4 | 3 | import React, { useState, useRef, useEffect } from "react"; |
5 | | -import { FaChevronDown, FaChevronUp } from "react-icons/fa"; |
6 | | -import { toast } from "react-toastify"; |
| 4 | +import { Grid } from "gridjs"; |
| 5 | +import DownloadIcon from "../Icons/DownloadIcon"; |
| 6 | +import SearchIcon from "../Icons/SearchIcon"; |
| 7 | +import { useAppStore } from "@/store"; |
| 8 | +import AppTooltip from "../AppTooltip"; |
| 9 | +import { convertToCSV } from "@/utils/convertToCSV"; |
| 10 | +import "gridjs/dist/theme/mermaid.css"; |
7 | 11 |
|
8 | 12 | interface IProps { |
9 | 13 | chatResponse: ChatResponseItem; |
10 | | - index: number; |
11 | 14 | chatId: string; |
12 | 15 | } |
13 | 16 |
|
14 | | -const StyledRecord = ({ record }: { record: string }) => { |
15 | | - if (!record) return record; |
16 | | - |
17 | | - if (record === "true" || record === "True") { |
18 | | - return ( |
19 | | - <span className="text-green-500"> |
20 | | - <svg |
21 | | - xmlns="http://www.w3.org/2000/svg" |
22 | | - className="h-4 w-4 inline" |
23 | | - fill="none" |
24 | | - viewBox="0 0 24 24" |
25 | | - stroke="currentColor" |
26 | | - > |
27 | | - <path |
28 | | - strokeLinecap="round" |
29 | | - strokeLinejoin="round" |
30 | | - strokeWidth={2} |
31 | | - d="M5 13l4 4L19 7" |
32 | | - /> |
33 | | - </svg> |
34 | | - </span> |
35 | | - ); |
36 | | - } else if (record === "false" || record === "False") { |
37 | | - return ( |
38 | | - <span className="text-red-500"> |
39 | | - <svg |
40 | | - xmlns="http://www.w3.org/2000/svg" |
41 | | - className="h-4 w-4 inline" |
42 | | - fill="none" |
43 | | - viewBox="0 0 24 24" |
44 | | - stroke="currentColor" |
45 | | - > |
46 | | - <path |
47 | | - strokeLinecap="round" |
48 | | - strokeLinejoin="round" |
49 | | - strokeWidth={2} |
50 | | - d="M6 18L18 6M6 6l12 12" |
51 | | - /> |
52 | | - </svg> |
53 | | - </span> |
54 | | - ); |
55 | | - } else if (record.includes("http") || record.includes("www")) { |
56 | | - return ( |
57 | | - <a |
58 | | - href={record} |
59 | | - target="_blank" |
60 | | - rel="noreferrer noopener" |
61 | | - className="text-[#86ade4] hover:underline" |
62 | | - > |
63 | | - {record} |
64 | | - </a> |
65 | | - ); |
66 | | - } else if (record.includes("@") && record.includes(".")) { |
67 | | - return ( |
68 | | - <a href={`mailto:${record}`} className="text-[#86ade4] hover:underline"> |
69 | | - {record} |
70 | | - </a> |
71 | | - ); |
72 | | - } else { |
73 | | - const dateRegex = /^\d{4}-\d{2}-\d{2}$/; |
74 | | - if (dateRegex.test(record)) { |
75 | | - const date = new Date(record); |
76 | | - const options: Intl.DateTimeFormatOptions = { |
77 | | - year: "numeric", |
78 | | - month: "long", |
79 | | - day: "numeric", |
80 | | - hour: "numeric", |
81 | | - minute: "numeric", |
82 | | - }; |
83 | | - return date.toLocaleDateString("en-US", options); |
84 | | - } else { |
85 | | - return record; |
86 | | - } |
87 | | - } |
88 | | -}; |
89 | | - |
90 | | -const ChatDataFrame = ({ chatResponse, index, chatId }: IProps) => { |
91 | | - const [expandedCardId, setExpandedCardId] = useState(null); |
92 | | - const [contentHeight, setContentHeight] = useState("300px"); |
93 | | - const [isDownloading, setIsDownloading] = useState(false); |
| 17 | +const ChatDataFrame = ({ chatResponse, chatId }: IProps) => { |
94 | 18 | const contentRef = useRef(null); |
| 19 | + const [search, setSearch] = useState(false); |
| 20 | + const darkMode = useAppStore((state) => state.darkMode); |
| 21 | + |
| 22 | + const grid = new Grid({ |
| 23 | + columns: chatResponse?.value?.headers, |
| 24 | + data: chatResponse?.value?.rows, |
| 25 | + sort: true, |
| 26 | + search: true, |
| 27 | + pagination: { |
| 28 | + limit: 10, |
| 29 | + summary: false, |
| 30 | + }, |
| 31 | + style: { |
| 32 | + table: { |
| 33 | + "white-space": "nowrap", |
| 34 | + }, |
| 35 | + }, |
| 36 | + height: "600px", |
| 37 | + resizable: true, |
| 38 | + }); |
95 | 39 |
|
96 | 40 | useEffect(() => { |
97 | | - if (expandedCardId === index && contentRef.current) { |
98 | | - setContentHeight(`${contentRef.current.scrollHeight}px`); |
99 | | - } else { |
100 | | - setContentHeight("300px"); |
101 | | - } |
102 | | - }, [expandedCardId, index]); |
| 41 | + grid.render(contentRef.current); |
| 42 | + }); |
| 43 | + |
| 44 | + const handleSearch = () => { |
| 45 | + const gridJsHead = contentRef.current.querySelector(".gridjs-head"); |
103 | 46 |
|
104 | | - const handleToggleExpand = (index) => { |
105 | | - setExpandedCardId((prevId) => (prevId === index ? null : index)); |
| 47 | + if (gridJsHead) { |
| 48 | + gridJsHead.style.display = search ? "none" : "block"; |
| 49 | + setSearch(!search); |
| 50 | + } |
106 | 51 | }; |
107 | 52 |
|
108 | | - const handleDownload = async (id: string) => { |
109 | | - setIsDownloading(true); |
110 | | - await FetchDataframe(id) |
111 | | - .then((response) => { |
112 | | - const fileUrl = response?.data?.data?.download_url; |
113 | | - const link = document.createElement("a"); |
114 | | - link.href = fileUrl; |
115 | | - link.setAttribute("download", `dataframe-${id}`); |
116 | | - document.body.appendChild(link); |
117 | | - link.click(); |
118 | | - document.body.removeChild(link); |
119 | | - }) |
120 | | - .catch((error) => { |
121 | | - toast.error( |
122 | | - error?.response?.data?.message |
123 | | - ? error.response.data.message |
124 | | - : error.message |
125 | | - ); |
126 | | - }) |
127 | | - .finally(() => setIsDownloading(false)); |
| 53 | + const handleDownload = async () => { |
| 54 | + const { headers, rows } = chatResponse.value; |
| 55 | + const csvData = convertToCSV(headers, rows); |
| 56 | + const blob = new Blob([csvData], { type: "text/csv" }); |
| 57 | + const url = window.URL.createObjectURL(blob); |
| 58 | + const a = document.createElement("a"); |
| 59 | + a.setAttribute("href", url); |
| 60 | + a.setAttribute("download", `dataframe-${chatId}.csv`); |
| 61 | + document.body.appendChild(a); |
| 62 | + a.click(); |
| 63 | + document.body.removeChild(a); |
| 64 | + window.URL.revokeObjectURL(url); |
128 | 65 | }; |
129 | 66 |
|
130 | 67 | return ( |
131 | | - <div className="mt-2"> |
132 | | - <div |
133 | | - ref={contentRef} |
134 | | - className={`relative bg-white dark:bg-[#333333] rounded-[20px] px-4 py-2 custom-scroll overflow-hidden overflow-x-auto transition-all`} |
135 | | - style={{ maxHeight: contentHeight, height: "auto" }} |
136 | | - > |
137 | | - <table className="overflow-auto w-full"> |
138 | | - <thead> |
139 | | - <tr className="!border-px !border-gray-400 uppercase p-2"> |
140 | | - {chatResponse?.value?.headers?.map((item, index) => ( |
141 | | - <th |
142 | | - key={index} |
143 | | - className="cursor-pointer border-b p-2 text-center border-solid border-r last:border-r-0 text-xs min-w-[100px]" |
144 | | - > |
145 | | - {item.split("_").join(" ")} |
146 | | - </th> |
147 | | - ))} |
148 | | - </tr> |
149 | | - </thead> |
150 | | - <tbody> |
151 | | - {chatResponse?.value?.rows?.map((rows, index) => ( |
152 | | - <tr className="cursor-pointer" key={index}> |
153 | | - {Object.values(rows).map((item, index) => ( |
154 | | - <td |
155 | | - key={index} |
156 | | - className="text-center border-solid border-r dark:border-white border-[rgba(0,0,0,0.10)] last:border-r-0 pt-3" |
157 | | - > |
158 | | - <StyledRecord record={`${item}`} /> |
159 | | - </td> |
160 | | - ))} |
161 | | - </tr> |
162 | | - ))} |
163 | | - </tbody> |
164 | | - </table> |
165 | | - {chatResponse?.value?.rows?.length > 3 && ( |
166 | | - <div |
167 | | - className={`absolute bottom-0 left-0 right-0 h-20 bg-gradient-to-t from-white dark:from-[#191919] to-transparent pointer-events-none ${ |
168 | | - expandedCardId !== index ? "opacity-100" : "opacity-0" |
169 | | - } transition-all`} |
170 | | - ></div> |
171 | | - )} |
172 | | - |
173 | | - {expandedCardId === index && ( |
174 | | - <div className="text-center mt-6"> |
175 | | - <button |
176 | | - className="cursor-pointer bg-[#191919] h-[24px] text-xs text-white font-bold rounded-full px-3 py-1" |
177 | | - onClick={() => { |
178 | | - handleDownload(chatId); |
179 | | - }} |
180 | | - > |
181 | | - {isDownloading ? "Downloading..." : "Download the full table"} |
182 | | - </button> |
183 | | - </div> |
184 | | - )} |
185 | | - </div> |
186 | | - <div className="text-center mt-2 flex flex-col gap-2 items-center justify-center"> |
187 | | - {chatResponse?.value?.rows?.length > 3 && ( |
188 | | - <button |
189 | | - className="h-6 w-6 flex items-center justify-center text-white bg-[#191919] rounded-full" |
190 | | - onClick={() => handleToggleExpand(index)} |
191 | | - > |
192 | | - {expandedCardId === index ? ( |
193 | | - <FaChevronUp size="1em" /> |
194 | | - ) : ( |
195 | | - <FaChevronDown size="1em" /> |
196 | | - )} |
197 | | - </button> |
198 | | - )} |
| 68 | + <div className="flex flex-col mt-2"> |
| 69 | + <div className="flex justify-end"> |
| 70 | + <div className="cursor-pointer" onClick={handleSearch}> |
| 71 | + <AppTooltip text="Search"> |
| 72 | + <SearchIcon color={darkMode ? "#fff" : "#000"} /> |
| 73 | + </AppTooltip> |
| 74 | + </div> |
| 75 | + <div className="cursor-pointer" onClick={handleDownload}> |
| 76 | + <AppTooltip text="Download"> |
| 77 | + <DownloadIcon color={darkMode ? "#fff" : "#000"} /> |
| 78 | + </AppTooltip> |
| 79 | + </div> |
199 | 80 | </div> |
| 81 | + <div ref={contentRef} className={`grid-container${chatId}`} /> |
200 | 82 | </div> |
201 | 83 | ); |
202 | 84 | }; |
|
0 commit comments