1- import { useCallback , useState , useEffect } from 'react' ;
2- import applyQueryResultChanges from 'libs/ApplyQueryResultChanges' ;
3- import generateSqlFromChanges from 'libs/GenerateSqlFromChanges' ;
4- import generateSqlFromPlan from 'libs/GenerateSqlFromPlan' ;
5- import { useSchema } from 'renderer/contexts/SchemaProvider' ;
6- import { useSqlExecute } from 'renderer/contexts/SqlExecuteProvider' ;
1+ import { useState } from 'react' ;
72import { QueryResult } from 'types/SqlResult' ;
83import styles from './styles.module.scss' ;
94import ExportModal from '../ExportModal' ;
10- import { useDialog } from 'renderer/contexts/DialogProvider' ;
115import Toolbar from 'renderer/components/Toolbar' ;
126import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
137import {
148 faChevronLeft ,
159 faChevronRight ,
1610} from '@fortawesome/free-solid-svg-icons' ;
1711import { useDebounceEffect } from 'hooks/useDebounce' ;
18- import { useEditableResult } from 'renderer/contexts/EditableQueryResultProvider ' ;
12+ import CommitChangeToolbarItem from './CommitChangeToolbarItem ' ;
1913
2014interface QueryResultActionProps {
2115 result : QueryResult ;
@@ -40,13 +34,8 @@ export default function QueryResultAction({
4034 onSearchChange,
4135 time,
4236} : QueryResultActionProps ) {
43- const { showErrorDialog } = useDialog ( ) ;
44- const [ changeCount , setChangeCount ] = useState ( 0 ) ;
4537 const [ showExportModal , setShowExportModal ] = useState ( false ) ;
46- const { clearChange, collector } = useEditableResult ( ) ;
47- const { schema, currentDatabase } = useSchema ( ) ;
4838 const [ search , setSearch ] = useState ( '' ) ;
49- const { runner } = useSqlExecute ( ) ;
5039
5140 const rowStart = page * pageSize ;
5241 const rowEnd = Math . min ( resultAfterFilter . length , rowStart + pageSize ) ;
@@ -59,71 +48,18 @@ export default function QueryResultAction({
5948 1000
6049 ) ;
6150
62- useEffect ( ( ) => {
63- const cb = ( count : number ) => {
64- setChangeCount ( count ) ;
65- } ;
66-
67- collector . registerChange ( cb ) ;
68- return ( ) => collector . unregisterChange ( cb ) ;
69- } , [ collector , setChangeCount ] ) ;
70-
71- const onCommit = useCallback ( ( ) => {
72- if ( schema ) {
73- const currentDatabaseSchema = schema [ currentDatabase || '' ] ;
74-
75- if ( currentDatabaseSchema && result ) {
76- const plans = generateSqlFromChanges (
77- currentDatabaseSchema ,
78- result ,
79- collector . getChanges ( )
80- ) ;
81-
82- const rawSql = plans . map ( ( plan ) => ( {
83- sql : generateSqlFromPlan ( plan ) ,
84- } ) ) ;
85-
86- runner
87- . execute ( rawSql , { insideTransaction : true } )
88- . then ( ( ) => {
89- const changes = collector . getChanges ( ) ;
90-
91- if ( changes . new . length === 0 && changes . remove . length === 0 ) {
92- onResultChange ( ( prev ) => {
93- clearChange ( ) ;
94- return applyQueryResultChanges ( prev , changes . changes ) ;
95- } ) ;
96- } else {
97- onRequestRefetch ( ) ;
98- }
99- } )
100- . catch ( ( e ) => {
101- if ( e . message ) {
102- showErrorDialog ( e . message ) ;
103- }
104- } ) ;
105- }
106- }
107- } , [
108- collector ,
109- schema ,
110- currentDatabase ,
111- clearChange ,
112- onResultChange ,
113- onRequestRefetch ,
114- ] ) ;
115-
11651 return (
11752 < div className = { styles . footer } >
11853 < Toolbar >
11954 < Toolbar . Text > Took { Math . round ( time / 1000 ) } s</ Toolbar . Text >
12055 < Toolbar . Separator />
121- < Toolbar . Item
122- text = "Commit"
123- badge = { changeCount }
124- onClick = { onCommit }
125- disabled = { ! changeCount }
56+
57+ < CommitChangeToolbarItem
58+ onRequestRefetch = { onRequestRefetch }
59+ onResultChange = { onResultChange }
60+ result = { result }
12661 />
62+
12763 < Toolbar . Item text = "Export" onClick = { ( ) => setShowExportModal ( true ) } />
12864
12965 < Toolbar . Separator />
0 commit comments