|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import { useSelector } from 'react-redux' |
| 3 | +import cx from 'classnames' |
| 4 | +import { useParams } from 'react-router-dom' |
| 5 | +import { isNull } from 'lodash' |
| 6 | +import { KeyboardKeys as keys } from 'uiSrc/constants/keys' |
| 7 | + |
| 8 | +import { LoadingContent } from 'uiSrc/components/base/layout' |
| 9 | +import { |
| 10 | + DEFAULT_TEXT_VIEW_TYPE, |
| 11 | + ProfileQueryType, |
| 12 | + WBQueryType, |
| 13 | +} from 'uiSrc/pages/workbench/constants' |
| 14 | +import { |
| 15 | + ResultsMode, |
| 16 | + ResultsSummary, |
| 17 | + RunQueryMode, |
| 18 | +} from 'uiSrc/slices/interfaces/workbench' |
| 19 | +import { |
| 20 | + getVisualizationsByCommand, |
| 21 | + getWBQueryType, |
| 22 | + isGroupResults, |
| 23 | + isSilentModeWithoutError, |
| 24 | + Maybe, |
| 25 | +} from 'uiSrc/utils' |
| 26 | +import { appPluginsSelector } from 'uiSrc/slices/app/plugins' |
| 27 | +import { |
| 28 | + CommandExecutionResult, |
| 29 | + IPluginVisualization, |
| 30 | +} from 'uiSrc/slices/interfaces' |
| 31 | +import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry' |
| 32 | + |
| 33 | +import QueryCardHeader from 'uiSrc/components/query/query-card/QueryCardHeader/QueryCardHeader' |
| 34 | +import QueryCardCommonResult, { |
| 35 | + CommonErrorResponse, |
| 36 | +} from 'uiSrc/components/query/query-card/QueryCardCommonResult' |
| 37 | +import QueryCardCliResultWrapper from 'uiSrc/components/query/query-card/QueryCardCliResultWrapper' |
| 38 | +import QueryCardCliPlugin from 'uiSrc/components/query/query-card/QueryCardCliPlugin' |
| 39 | +import queryStyles from 'uiSrc/components/query/query-card/styles.module.scss' |
| 40 | + |
| 41 | +export interface Props { |
| 42 | + id: string |
| 43 | + command: string |
| 44 | + isOpen: boolean |
| 45 | + result: Maybe<CommandExecutionResult[]> |
| 46 | + activeMode: RunQueryMode |
| 47 | + mode?: RunQueryMode |
| 48 | + activeResultsMode?: ResultsMode |
| 49 | + resultsMode?: ResultsMode |
| 50 | + emptyCommand?: boolean |
| 51 | + summary?: ResultsSummary |
| 52 | + createdAt?: Date |
| 53 | + loading?: boolean |
| 54 | + clearing?: boolean |
| 55 | + isNotStored?: boolean |
| 56 | + executionTime?: number |
| 57 | + db?: number |
| 58 | + hideFields?: string[] |
| 59 | + onQueryDelete: () => void |
| 60 | + onQueryReRun: () => void |
| 61 | + onQueryOpen: () => void |
| 62 | + onQueryProfile: (type: ProfileQueryType) => void |
| 63 | +} |
| 64 | + |
| 65 | +const getDefaultPlugin = (views: IPluginVisualization[], query: string) => |
| 66 | + getVisualizationsByCommand(query, views).find((view) => view.default) |
| 67 | + ?.uniqId || DEFAULT_TEXT_VIEW_TYPE.id |
| 68 | + |
| 69 | +export const getSummaryText = ( |
| 70 | + summary?: ResultsSummary, |
| 71 | + mode?: ResultsMode, |
| 72 | +) => { |
| 73 | + if (summary) { |
| 74 | + const { total, success, fail } = summary |
| 75 | + const summaryText = `${total} Command(s) - ${success} success` |
| 76 | + if (!isSilentModeWithoutError(mode, summary?.fail)) { |
| 77 | + return `${summaryText}, ${fail} error(s)` |
| 78 | + } |
| 79 | + return summaryText |
| 80 | + } |
| 81 | + return summary |
| 82 | +} |
| 83 | + |
| 84 | +const QueryCard = (props: Props) => { |
| 85 | + const { |
| 86 | + id, |
| 87 | + command = '', |
| 88 | + result, |
| 89 | + activeMode, |
| 90 | + mode, |
| 91 | + activeResultsMode, |
| 92 | + resultsMode, |
| 93 | + summary, |
| 94 | + isOpen, |
| 95 | + createdAt, |
| 96 | + onQueryOpen, |
| 97 | + onQueryDelete, |
| 98 | + onQueryProfile, |
| 99 | + onQueryReRun, |
| 100 | + loading, |
| 101 | + clearing, |
| 102 | + emptyCommand, |
| 103 | + isNotStored, |
| 104 | + executionTime, |
| 105 | + db, |
| 106 | + hideFields, |
| 107 | + } = props |
| 108 | + |
| 109 | + const { visualizations = [] } = useSelector(appPluginsSelector) |
| 110 | + |
| 111 | + const { instanceId = '' } = useParams<{ instanceId: string }>() |
| 112 | + const [isFullScreen, setIsFullScreen] = useState<boolean>(false) |
| 113 | + const [queryType, setQueryType] = useState<WBQueryType>( |
| 114 | + getWBQueryType(command, visualizations), |
| 115 | + ) |
| 116 | + const [viewTypeSelected, setViewTypeSelected] = |
| 117 | + useState<WBQueryType>(queryType) |
| 118 | + const [message, setMessage] = useState<string>('') |
| 119 | + const [selectedViewValue, setSelectedViewValue] = useState<string>( |
| 120 | + getDefaultPlugin(visualizations, command || '') || queryType, |
| 121 | + ) |
| 122 | + |
| 123 | + useEffect(() => { |
| 124 | + window.addEventListener('keydown', handleEscFullScreen) |
| 125 | + return () => { |
| 126 | + window.removeEventListener('keydown', handleEscFullScreen) |
| 127 | + } |
| 128 | + }, [isFullScreen]) |
| 129 | + |
| 130 | + const handleEscFullScreen = (event: KeyboardEvent) => { |
| 131 | + if (event.key === keys.ESCAPE && isFullScreen) { |
| 132 | + toggleFullScreen() |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + const toggleFullScreen = () => { |
| 137 | + setIsFullScreen((isFull) => { |
| 138 | + sendEventTelemetry({ |
| 139 | + event: TelemetryEvent.WORKBENCH_RESULTS_IN_FULL_SCREEN, |
| 140 | + eventData: { |
| 141 | + databaseId: instanceId, |
| 142 | + state: isFull ? 'Close' : 'Open', |
| 143 | + }, |
| 144 | + }) |
| 145 | + |
| 146 | + return !isFull |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + useEffect(() => { |
| 151 | + setQueryType(getWBQueryType(command, visualizations)) |
| 152 | + }, [command]) |
| 153 | + |
| 154 | + useEffect(() => { |
| 155 | + if (visualizations.length) { |
| 156 | + const type = getWBQueryType(command, visualizations) |
| 157 | + setQueryType(type) |
| 158 | + setViewTypeSelected(type) |
| 159 | + setSelectedViewValue( |
| 160 | + getDefaultPlugin(visualizations, command) || queryType, |
| 161 | + ) |
| 162 | + } |
| 163 | + }, [visualizations]) |
| 164 | + |
| 165 | + const toggleOpen = () => { |
| 166 | + if (isFullScreen || isSilentModeWithoutError(resultsMode, summary?.fail)) |
| 167 | + return |
| 168 | + |
| 169 | + onQueryOpen() |
| 170 | + } |
| 171 | + |
| 172 | + const changeViewTypeSelected = (type: WBQueryType, value: string) => { |
| 173 | + setViewTypeSelected(type) |
| 174 | + setSelectedViewValue(value) |
| 175 | + } |
| 176 | + |
| 177 | + const commonError = CommonErrorResponse(id, command, result) |
| 178 | + |
| 179 | + const isSizeLimitExceededResponse = ( |
| 180 | + result: Maybe<CommandExecutionResult[]>, |
| 181 | + ) => { |
| 182 | + const resultObj = result?.[0] |
| 183 | + // response.includes - to be backward compatible with responses which don't include sizeLimitExceeded flag |
| 184 | + return ( |
| 185 | + resultObj?.sizeLimitExceeded === true || |
| 186 | + resultObj?.response?.includes?.('Results have been deleted') |
| 187 | + ) |
| 188 | + } |
| 189 | + |
| 190 | + return ( |
| 191 | + <div |
| 192 | + className={cx(queryStyles.containerWrapper, { |
| 193 | + fullscreen: isFullScreen, |
| 194 | + [queryStyles.isOpen]: isOpen, |
| 195 | + })} |
| 196 | + id={id} |
| 197 | + > |
| 198 | + <div |
| 199 | + className={cx(queryStyles.container)} |
| 200 | + data-testid={`query-card-container-${id}`} |
| 201 | + > |
| 202 | + <QueryCardHeader |
| 203 | + isOpen={isOpen} |
| 204 | + isFullScreen={isFullScreen} |
| 205 | + query={command} |
| 206 | + loading={loading} |
| 207 | + clearing={clearing} |
| 208 | + createdAt={createdAt} |
| 209 | + message={message} |
| 210 | + queryType={queryType} |
| 211 | + selectedValue={selectedViewValue} |
| 212 | + activeMode={activeMode} |
| 213 | + mode={mode} |
| 214 | + resultsMode={resultsMode} |
| 215 | + activeResultsMode={activeResultsMode} |
| 216 | + emptyCommand={emptyCommand} |
| 217 | + summary={summary} |
| 218 | + summaryText={getSummaryText(summary, resultsMode)} |
| 219 | + executionTime={executionTime} |
| 220 | + db={db} |
| 221 | + hideFields={hideFields} |
| 222 | + toggleOpen={toggleOpen} |
| 223 | + toggleFullScreen={toggleFullScreen} |
| 224 | + setSelectedValue={changeViewTypeSelected} |
| 225 | + onQueryDelete={onQueryDelete} |
| 226 | + onQueryReRun={onQueryReRun} |
| 227 | + onQueryProfile={onQueryProfile} |
| 228 | + /> |
| 229 | + {isOpen && ( |
| 230 | + <> |
| 231 | + {React.isValidElement(commonError) && |
| 232 | + (!isGroupResults(resultsMode) || isNull(command)) ? ( |
| 233 | + <QueryCardCommonResult loading={loading} result={commonError} /> |
| 234 | + ) : ( |
| 235 | + <> |
| 236 | + {isSizeLimitExceededResponse(result) ? ( |
| 237 | + <QueryCardCliResultWrapper |
| 238 | + loading={loading} |
| 239 | + query={command} |
| 240 | + resultsMode={resultsMode} |
| 241 | + result={result} |
| 242 | + isNotStored={isNotStored} |
| 243 | + isFullScreen={isFullScreen} |
| 244 | + /> |
| 245 | + ) : ( |
| 246 | + <> |
| 247 | + {isGroupResults(resultsMode) && ( |
| 248 | + <QueryCardCliResultWrapper |
| 249 | + loading={loading} |
| 250 | + query={command} |
| 251 | + db={db} |
| 252 | + resultsMode={resultsMode} |
| 253 | + result={result} |
| 254 | + isNotStored={isNotStored} |
| 255 | + isFullScreen={isFullScreen} |
| 256 | + data-testid="group-mode-card" |
| 257 | + /> |
| 258 | + )} |
| 259 | + {(resultsMode === ResultsMode.Default || !resultsMode) && ( |
| 260 | + <> |
| 261 | + {viewTypeSelected === WBQueryType.Plugin && ( |
| 262 | + <> |
| 263 | + {!loading && result !== undefined ? ( |
| 264 | + <QueryCardCliPlugin |
| 265 | + id={selectedViewValue} |
| 266 | + result={result} |
| 267 | + query={command} |
| 268 | + mode={mode} |
| 269 | + setMessage={setMessage} |
| 270 | + commandId={id} |
| 271 | + /> |
| 272 | + ) : ( |
| 273 | + <div className={queryStyles.loading}> |
| 274 | + <LoadingContent |
| 275 | + lines={5} |
| 276 | + data-testid="loading-content" |
| 277 | + /> |
| 278 | + </div> |
| 279 | + )} |
| 280 | + </> |
| 281 | + )} |
| 282 | + {viewTypeSelected === WBQueryType.Text && ( |
| 283 | + <QueryCardCliResultWrapper |
| 284 | + loading={loading} |
| 285 | + query={command} |
| 286 | + resultsMode={resultsMode} |
| 287 | + result={result} |
| 288 | + isNotStored={isNotStored} |
| 289 | + isFullScreen={isFullScreen} |
| 290 | + /> |
| 291 | + )} |
| 292 | + </> |
| 293 | + )} |
| 294 | + </> |
| 295 | + )} |
| 296 | + </> |
| 297 | + )} |
| 298 | + </> |
| 299 | + )} |
| 300 | + </div> |
| 301 | + </div> |
| 302 | + ) |
| 303 | +} |
| 304 | + |
| 305 | +export default React.memo(QueryCard) |
0 commit comments