@@ -18,6 +18,7 @@ import { useMemo, useCallback } from "react";
1818import { StepTitle } from "./StepTitle" ;
1919import { StepDescription } from "./StepDescription" ;
2020import {
21+ type EmbeddedQueryProps ,
2122 SeafowlEmbeddedQuery ,
2223 SeafowlStargazersQueryLink ,
2324} from "../RepositoryAnalytics/ImportedRepoMetadata" ;
@@ -27,6 +28,9 @@ import {
2728 SplitgraphEmbeddedQuery ,
2829} from "../RepositoryAnalytics/ImportedRepoMetadata" ;
2930import { makeStargazersTableQuery } from "../RepositoryAnalytics/sql-queries" ;
31+ import type { ExportTable } from "./stepper-states" ;
32+
33+ import type { TargetSplitgraphRepo } from "../../types" ;
3034
3135export const ExportPanel = ( ) => {
3236 const [
@@ -182,7 +186,9 @@ export const ExportPanel = () => {
182186 ) }
183187 </ StepDescription >
184188 { exportError && < p className = { styles . error } > { exportError } </ p > }
185- { stepperState === "import_complete" && (
189+ { [ "import_complete" , "awaiting_export" , "export_complete" ] . includes (
190+ stepperState
191+ ) && (
186192 < ExportPreview
187193 handleStartExport = { handleStartExport }
188194 tablesToExport = { tablesToExport }
@@ -214,97 +220,124 @@ const ExportPreview = ({
214220 < button className = { styles . startExportButton } onClick = { handleStartExport } >
215221 Start Export of Tables and Queries from Splitgraph to Seafowl
216222 </ button >
217- { /*
218- <h3>Debugging</h3>
219-
220- <SplitgraphEmbeddedQuery
221- importedRepository={{
222- splitgraphNamespace,
223- splitgraphRepository,
224- }}
225- tableName={"Debugging"}
226- makeQuery={() => "select 1;"}
227- /> */ }
228-
229223 < h3 > Tables to Export</ h3 >
230224 { tablesToExport
231225 . filter ( ( _ ) => true )
232226 . map ( ( exportTable ) => (
233- < ExportTablePreview
227+ < ExportEmbedPreviewTableOrQuery
234228 key = { `export-table-preview-${ exportTable . table } ` }
235- splitgraphNamespace = { splitgraphNamespace }
236- splitgraphRepository = { splitgraphRepository }
237- { ...exportTable }
229+ exportInput = { exportTable }
230+ importedRepository = { { splitgraphNamespace, splitgraphRepository } }
231+ makeQuery = { ( { splitgraphNamespace, splitgraphRepository, table } ) =>
232+ `SELECT * FROM "${ splitgraphNamespace } /${ splitgraphRepository } "."${ table } ";`
233+ }
234+ makeMatchInputToExported = { ( exportTableInput ) => ( exportTable ) => {
235+ return (
236+ exportTable . destinationSchema === exportTableInput . repository &&
237+ exportTable . destinationTable === exportTableInput . table
238+ ) ;
239+ } }
238240 />
239241 ) ) }
240242
241243 < h3 > Queries to Export</ h3 >
242244 { queriesToExport
243245 . filter ( ( _ ) => true )
244246 . map ( ( exportQuery ) => (
245- < ExportQueryPreview
247+ < ExportEmbedPreviewTableOrQuery
246248 key = { `export-query-preview-${ exportQuery . destinationTable } -${ exportQuery . destinationSchema } ` }
247- splitgraphNamespace = { splitgraphNamespace }
248- splitgraphRepository = { splitgraphRepository }
249- { ...exportQuery }
249+ exportInput = { exportQuery }
250+ importedRepository = { { splitgraphNamespace, splitgraphRepository } }
251+ makeQuery = { ( { sourceQuery } ) => sourceQuery }
252+ makeMatchInputToExported = { ( exportQueryInput ) =>
253+ ( exportTable : ExportTable ) => {
254+ return (
255+ exportTable . destinationSchema ===
256+ exportQueryInput . destinationSchema &&
257+ exportTable . destinationTable ===
258+ exportQueryInput . destinationTable
259+ ) ;
260+ } }
250261 />
251262 ) ) }
252263 </ >
253264 ) ;
254265} ;
255266
256- const ExportQueryPreview = ( {
257- splitgraphNamespace,
258- splitgraphRepository,
259- destinationSchema,
260- destinationTable,
261- sourceQuery,
262- } : ExportQueryInput & {
263- splitgraphNamespace : string ;
264- splitgraphRepository : string ;
265- } ) => {
266- return (
267- < >
268- < h4 >
269- < code >
270- { destinationSchema } .{ destinationTable }
271- </ code >
272- </ h4 >
273- < SplitgraphEmbeddedQuery
274- importedRepository = { {
275- splitgraphNamespace,
276- splitgraphRepository,
277- } }
278- tableName = { destinationTable }
279- makeQuery = { ( ) => sourceQuery }
280- />
281- </ >
267+ const useFindMatchingExportTable = (
268+ isMatch : ( candidateTable : ExportTable ) => boolean
269+ ) => {
270+ const [ { exportedTablesLoading, exportedTablesCompleted } ] = useStepper ( ) ;
271+
272+ const matchingCompletedTable = useMemo (
273+ ( ) => Array . from ( exportedTablesCompleted ) . find ( isMatch ) ,
274+ [ exportedTablesCompleted , isMatch ]
275+ ) ;
276+ const matchingLoadingTable = useMemo (
277+ ( ) => Array . from ( exportedTablesLoading ) . find ( isMatch ) ,
278+ [ exportedTablesLoading , isMatch ]
282279 ) ;
280+
281+ const completed = matchingCompletedTable ?? false ;
282+ const loading = matchingLoadingTable ?? false ;
283+ const unstarted = ! completed && ! loading ;
284+
285+ return {
286+ completed,
287+ loading,
288+ unstarted,
289+ } ;
283290} ;
284291
285- const ExportTablePreview = ( {
286- splitgraphNamespace,
287- splitgraphRepository,
288- table,
292+ const ExportEmbedPreviewTableOrQuery = <
293+ ExportInputShape extends ExportQueryInput | ExportTableInput
294+ > ( {
295+ importedRepository,
296+ exportInput,
297+ makeQuery,
298+ makeMatchInputToExported,
289299} : {
290- splitgraphNamespace : string ;
291- splitgraphRepository : string ;
292- } & ExportTableInput ) => {
300+ exportInput : ExportInputShape ;
301+ makeQuery : (
302+ tableOrQueryInput : ExportInputShape & {
303+ splitgraphNamespace : string ;
304+ splitgraphRepository : string ;
305+ }
306+ ) => string ;
307+ makeMatchInputToExported : (
308+ tableOrQueryInput : ExportInputShape
309+ ) => ( exported : ExportTable ) => boolean ;
310+ importedRepository : {
311+ splitgraphNamespace : string ;
312+ splitgraphRepository : string ;
313+ } ;
314+ } ) => {
315+ const embedProps = {
316+ importedRepository,
317+ tableName :
318+ "destinationTable" in exportInput
319+ ? exportInput . destinationTable
320+ : exportInput . table ,
321+ makeQuery : ( ) => makeQuery ( { ...exportInput , ...importedRepository } ) ,
322+ } ;
323+
324+ const { unstarted, loading, completed } = useFindMatchingExportTable (
325+ makeMatchInputToExported ( exportInput )
326+ ) ;
327+
328+ const heading =
329+ "table" in exportInput
330+ ? exportInput . table
331+ : `${ exportInput . destinationSchema } .${ exportInput . destinationTable } ` ;
332+
293333 return (
294334 < >
295335 < h4 >
296- < code > { table } </ code >
336+ < code > { heading } </ code >
297337 </ h4 >
298- < SplitgraphEmbeddedQuery
299- importedRepository = { {
300- splitgraphNamespace,
301- splitgraphRepository,
302- } }
303- tableName = { table }
304- makeQuery = { ( { splitgraphNamespace, splitgraphRepository } ) =>
305- `SELECT * FROM "${ splitgraphNamespace } /${ splitgraphRepository } "."${ table } ";`
306- }
307- />
338+ < pre > { JSON . stringify ( { completed, loading } , null , 2 ) } </ pre >
339+ { ( unstarted || loading ) && < SplitgraphEmbeddedQuery { ...embedProps } /> }
340+ { completed && < SeafowlEmbeddedQuery { ...embedProps } /> }
308341 </ >
309342 ) ;
310343} ;
0 commit comments