Line data Source code
1 : import * as React from 'react';
2 :
3 : import { QueryType, type QueryExecutionState } from '../query/query_execution_state.js';
4 : import { getQueryTarget, QueryHistoryViewer, type QueryEntry } from '../query/query_history_viewer.js';
5 : import { ShellQueryExecutionTracker } from '../shell/query_execution.js';
6 :
7 : /// The id under which the queries of the shell are listed
8 1 : export const SHELL_DATABASE_ID = 'shell';
9 :
10 1 : const SHELL_QUERY_HISTORY_CTX = React.createContext<ShellQueryExecutionTracker | null>(null);
11 :
12 : /// Provide the history of the queries that the shell executes
13 1 : export const ShellQueryHistoryProvider: React.FC<{ children: React.ReactElement }> = props => {
14 1 : const [queryExecutions] = React.useState(() => new ShellQueryExecutionTracker());
15 1 : return <SHELL_QUERY_HISTORY_CTX.Provider value={queryExecutions}>{props.children}</SHELL_QUERY_HISTORY_CTX.Provider>;
16 1 : };
17 :
18 1 : export function useShellQueryHistory(): ShellQueryExecutionTracker {
19 1 : return React.useContext(SHELL_QUERY_HISTORY_CTX)!;
20 1 : }
21 :
22 : /// The queries of the shell, the shell runs them on its embedded database
23 1 : export function shellQueryEntries(queries: readonly QueryExecutionState[]): QueryEntry[] {
24 1 : return queries.map(query => ({
25 1 : connectionId: SHELL_DATABASE_ID,
26 1 : sourceName: query.queryMetadata.queryType == QueryType.INTERNAL_SQLFRAME ? 'SQLFrame' : 'Hyper',
27 1 : target: getQueryTarget(query),
28 1 : queryId: query.queryId,
29 1 : query,
30 1 : }));
31 1 : }
32 :
33 : /// The query history of the shell
34 1 : export function ShellQueryViewer(props: { onClose: () => void }) {
35 1 : const queryExecutions = useShellQueryHistory();
36 1 : const queries = React.useSyncExternalStore(queryExecutions.subscribe, queryExecutions.getSnapshot);
37 1 : const entries = React.useMemo(() => shellQueryEntries(queries), [queries]);
38 1 : return <QueryHistoryViewer entries={entries} onClose={props.onClose} />;
39 1 : }
|