|
1 | | -import { useContext, useEffect } from "react"; |
2 | | -import { DBConnection, Tab } from "../../data/models"; |
3 | | -import { selectDBConnection } from "../../redux/dbConnectionSlice"; |
4 | | -import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
5 | | -import toast from "react-hot-toast"; |
6 | | -import InfiniteScroll from "react-infinite-scroll-component"; |
7 | | -import dateformat from "dateformat"; |
8 | | -import { |
9 | | - getDBQueryLogs, |
10 | | - reset, |
11 | | - selectDBQueryLogs, |
12 | | - selectDBQueryLogsNext, |
13 | | -} from "../../redux/dbHistorySlice"; |
14 | | -import TabContext from "../layouts/tabcontext"; |
| 1 | +import { useContext, useEffect } from 'react' |
| 2 | +import { DBConnection, Tab } from '../../data/models' |
| 3 | +import { selectDBConnection } from '../../redux/dbConnectionSlice' |
| 4 | +import { useAppDispatch, useAppSelector } from '../../redux/hooks' |
| 5 | +import toast from 'react-hot-toast' |
| 6 | +import InfiniteScroll from 'react-infinite-scroll-component' |
| 7 | +import dateformat from 'dateformat' |
| 8 | +import { getDBQueryLogs, reset, selectDBQueryLogs, selectDBQueryLogsNext } from '../../redux/dbHistorySlice' |
| 9 | +import TabContext from '../layouts/tabcontext' |
15 | 10 |
|
16 | | -type DBHistoryPropType = {}; |
17 | 11 |
|
18 | | -const DBHistoryFragment = ({}: DBHistoryPropType) => { |
19 | | - const dispatch = useAppDispatch(); |
| 12 | +type DBHistoryPropType = { |
| 13 | +} |
20 | 14 |
|
21 | | - const currentTab: Tab = useContext(TabContext)!; |
| 15 | +const DBHistoryFragment = ({ }: DBHistoryPropType) => { |
22 | 16 |
|
23 | | - const dbConnection: DBConnection | undefined = |
24 | | - useAppSelector(selectDBConnection); |
25 | | - const dbQueryLogs = useAppSelector(selectDBQueryLogs); |
26 | | - const dbQueryLogsNext = useAppSelector(selectDBQueryLogsNext); |
| 17 | + const dispatch = useAppDispatch() |
27 | 18 |
|
28 | | - useEffect(() => { |
29 | | - if (dbConnection) { |
30 | | - (async () => { |
31 | | - dispatch(reset()); |
32 | | - })(); |
33 | | - fetchDBQueryLogs(); |
34 | | - } |
35 | | - }, [dispatch, dbConnection]); |
| 19 | + const currentTab: Tab = useContext(TabContext)! |
36 | 20 |
|
37 | | - const fetchDBQueryLogs = async () => { |
38 | | - const result = await dispatch( |
39 | | - getDBQueryLogs({ dbConnId: dbConnection!.id }) |
40 | | - ).unwrap(); |
41 | | - if (!result.success) { |
42 | | - toast.error(result.error!); |
43 | | - } |
44 | | - }; |
| 21 | + const dbConnection: DBConnection | undefined = useAppSelector(selectDBConnection) |
| 22 | + const dbQueryLogs = useAppSelector(selectDBQueryLogs) |
| 23 | + const dbQueryLogsNext = useAppSelector(selectDBQueryLogsNext) |
45 | 24 |
|
46 | | - function refreshHandler() { |
| 25 | + useEffect(() => { |
| 26 | + if (dbConnection) { |
| 27 | + (async () => { |
| 28 | + dispatch(reset()) |
| 29 | + })() |
| 30 | + fetchDBQueryLogs() |
| 31 | + } |
| 32 | + }, [dispatch, dbConnection]) |
| 33 | + |
| 34 | + const fetchDBQueryLogs = async () => { |
| 35 | + const result = await dispatch(getDBQueryLogs({ dbConnId: dbConnection!.id })).unwrap() |
| 36 | + if (!result.success) { |
| 37 | + toast.error(result.error!) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + function refreshHandler() { |
47 | 42 | dispatch(reset()); |
48 | 43 | fetchDBQueryLogs(); |
49 | | - } |
| 44 | + } |
50 | 45 |
|
51 | | - return ( |
52 | | - <div className={currentTab.isActive ? "db-tab-active" : "db-tab"}> |
53 | | - {dbConnection && ( |
54 | | - <> |
55 | | - <div className="is-flex is-justify-content-space-between "> |
56 | | - <h1>Showing History in {dbConnection.name}</h1> |
57 | | - <button className="button is-flex" onClick={refreshHandler}> |
58 | | - <span className="icon is-small"> |
59 | | - <i className="fas fa-sync" /> |
60 | | - </span> |
61 | | - <span>Refresh</span> |
62 | | - </button> |
63 | | - </div> |
64 | | - <br /> |
65 | | - <InfiniteScroll |
66 | | - dataLength={dbQueryLogs.length} |
67 | | - next={fetchDBQueryLogs} |
68 | | - hasMore={dbQueryLogsNext !== -1} |
69 | | - loader={<p style={{ textAlign: "center" }}>Loading...</p>} |
70 | | - endMessage={ |
71 | | - <p style={{ textAlign: "center" }}> |
72 | | - <b>You have seen it all!</b> |
73 | | - </p> |
74 | | - } |
75 | | - scrollableTarget="maincontent" |
76 | | - > |
77 | | - <table className="table is-bordered is-striped is-narrow is-hoverable is-fullwidth"> |
78 | | - <tbody> |
79 | | - {dbQueryLogs.map((log) => ( |
80 | | - <tr key={log.id}> |
81 | | - <td> |
82 | | - <code>{log.query}</code> |
83 | | - </td> |
84 | | - <td |
85 | | - style={{ |
86 | | - fontSize: "14px", |
87 | | - width: "120px", |
88 | | - }} |
89 | | - > |
90 | | - {dateformat( |
91 | | - log.createdAt, |
92 | | - "mmm dd, yyyy HH:MM:ss" |
93 | | - )} |
94 | | - </td> |
95 | | - </tr> |
96 | | - ))} |
97 | | - </tbody> |
98 | | - </table> |
99 | | - </InfiniteScroll> |
100 | | - </> |
101 | | - )} |
102 | | - </div> |
103 | | - ); |
104 | | -}; |
| 46 | + return ( |
| 47 | + <div className={currentTab.isActive ? "db-tab-active" : "db-tab"}> |
| 48 | + {dbConnection && |
| 49 | + <> |
| 50 | + <div className="is-flex is-justify-content-space-between "> |
| 51 | + <h1>Showing History in {dbConnection.name}</h1> |
| 52 | + <button className="button is-flex" onClick={refreshHandler}> |
| 53 | + <span className="icon is-small"> |
| 54 | + <i className="fas fa-sync" /> |
| 55 | + </span> |
| 56 | + <span>Refresh</span> |
| 57 | + </button> |
| 58 | + </div> |
| 59 | + <br /> |
| 60 | + <InfiniteScroll |
| 61 | + dataLength={dbQueryLogs.length} |
| 62 | + next={fetchDBQueryLogs} |
| 63 | + hasMore={dbQueryLogsNext !== -1} |
| 64 | + loader={ |
| 65 | + <p style={{ textAlign: 'center' }}> |
| 66 | + Loading... |
| 67 | + </p> |
| 68 | + } |
| 69 | + endMessage={ |
| 70 | + <p style={{ textAlign: 'center' }}> |
| 71 | + <b>You have seen it all!</b> |
| 72 | + </p> |
| 73 | + } |
| 74 | + scrollableTarget="maincontent" |
| 75 | + > |
| 76 | + <table className={"table is-bordered is-striped is-narrow is-hoverable is-fullwidth"}> |
| 77 | + <tbody> |
| 78 | + {dbQueryLogs.map((log) => ( |
| 79 | + <tr key={log.id}> |
| 80 | + <td> |
| 81 | + <code>{log.query}</code> |
| 82 | + </td> |
| 83 | + <td style={{ fontSize: '14px', width: '120px' }}> |
| 84 | + {dateformat(log.createdAt, "mmm dd, yyyy HH:MM:ss")} |
| 85 | + </td> |
| 86 | + </tr> |
| 87 | + ) |
| 88 | + )} |
| 89 | + </tbody> |
| 90 | + </table> |
| 91 | + </InfiniteScroll> |
| 92 | + </> |
| 93 | + } |
| 94 | + </div> |
| 95 | + ) |
| 96 | +} |
105 | 97 |
|
106 | | -export default DBHistoryFragment; |
| 98 | +export default DBHistoryFragment |
0 commit comments