forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryResultHeader.tsx
More file actions
140 lines (123 loc) · 4.18 KB
/
Copy pathQueryResultHeader.tsx
File metadata and controls
140 lines (123 loc) · 4.18 KB
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
import MenuLeftIcon from 'mdi-react/MenuLeftIcon';
import OpenInNewIcon from 'mdi-react/OpenInNewIcon';
import React from 'react';
import Button from '../common/Button';
import ExportButton from '../common/ExportButton';
import HSpacer from '../common/HSpacer';
import IconButton from '../common/IconButton';
import IncompleteDataNotification from '../common/IncompleteDataNotification';
import SecondsTimer from '../common/SecondsTimer';
import { selectStatementId } from '../stores/editor-actions';
import {
useSessionBatch,
useSessionIsRunning,
useSessionRunQueryStartTime,
useSessionSelectedStatementId,
useSessionTableLink,
useStatementDurationMs,
useStatementIncomplete,
useStatementRowCount,
useStatementSequence,
useStatementStatus,
useStatementText,
} from '../stores/editor-store';
import useAppContext from '../utilities/use-app-context';
import styles from './QueryResultHeader.module.css';
function QueryResultHeader() {
const isRunning = useSessionIsRunning();
const runQueryStartTime = useSessionRunQueryStartTime();
const statementId = useSessionSelectedStatementId();
const rowCount = useStatementRowCount(statementId);
const hasRows = rowCount !== undefined && rowCount > 0;
const incomplete = useStatementIncomplete(statementId);
const statementDurationMs = useStatementDurationMs(statementId);
const status = useStatementStatus(statementId);
const batch = useSessionBatch();
const numOfStatements = batch?.statements.length || 0;
const statementText = useStatementText(statementId);
const statementSequence = useStatementSequence(statementId);
const { config } = useAppContext();
const tableLink = useSessionTableLink(statementSequence);
const showLink = Boolean(tableLink);
const isStatementFinished = status === 'finished';
const isStatementRunning = status === 'queued' || status === 'started';
let timerContent = null;
if ((statementId && isStatementRunning) || (!statementId && isRunning)) {
timerContent = (
<div style={{ whiteSpace: 'nowrap' }}>
<SecondsTimer startTime={runQueryStartTime} /> seconds
</div>
);
} else if (statementId && isStatementFinished) {
const serverSec =
statementDurationMs !== undefined ? statementDurationMs / 1000 : 0;
timerContent = (
<div style={{ whiteSpace: 'nowrap' }}>{serverSec} seconds</div>
);
} else if (!statementId && batch?.durationMs !== undefined) {
const serverSec = batch?.durationMs / 1000;
timerContent = <div>{serverSec} seconds</div>;
}
return (
<div className={styles.toolbar}>
{statementId && numOfStatements > 1 ? (
<Button
className={styles.returnToStatementsBtn}
variant="primary-ghost"
onClick={() => {
selectStatementId('');
}}
>
<MenuLeftIcon /> Return to statements
</Button>
) : null}
<HSpacer size={1} grow />
{statementId && statementSequence && numOfStatements > 1 && (
<>
<div className={styles.statementHeaderStatementText}>
{`${statementSequence}. ${statementText}`}
</div>
<HSpacer size={1} grow />
</>
)}
{statementId && isStatementFinished && (
<>
<div style={{ whiteSpace: 'nowrap' }}>{rowCount} rows</div>
<HSpacer />
</>
)}
{statementId && isStatementFinished && showLink && (
<>
<IconButton
disabled={!Boolean(tableLink)}
to={tableLink}
target="_blank"
rel="noopener noreferrer"
tooltip="Open table in new window"
>
<OpenInNewIcon size={16} />
</IconButton>
<HSpacer />
</>
)}
{statementId &&
isStatementFinished &&
config?.allowCsvDownload &&
hasRows && (
<>
<ExportButton statementId={statementId} />
<HSpacer />
</>
)}
{statementId && isStatementFinished && incomplete && (
<>
<IncompleteDataNotification />
<HSpacer />
</>
)}
{timerContent}
<HSpacer size={1} />
</div>
);
}
export default React.memo(QueryResultHeader);