Skip to content

Commit bd06807

Browse files
Display preview table for each loading/completed: either embed Splitgraph, or Seafowl
1 parent adf8369 commit bd06807

3 files changed

Lines changed: 107 additions & 76 deletions

File tree

examples/nextjs-import-airbyte-github-export-seafowl/components/ImportExportStepper/ExportPanel.tsx

Lines changed: 98 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useMemo, useCallback } from "react";
1818
import { StepTitle } from "./StepTitle";
1919
import { StepDescription } from "./StepDescription";
2020
import {
21+
type EmbeddedQueryProps,
2122
SeafowlEmbeddedQuery,
2223
SeafowlStargazersQueryLink,
2324
} from "../RepositoryAnalytics/ImportedRepoMetadata";
@@ -27,6 +28,9 @@ import {
2728
SplitgraphEmbeddedQuery,
2829
} from "../RepositoryAnalytics/ImportedRepoMetadata";
2930
import { makeStargazersTableQuery } from "../RepositoryAnalytics/sql-queries";
31+
import type { ExportTable } from "./stepper-states";
32+
33+
import type { TargetSplitgraphRepo } from "../../types";
3034

3135
export 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
};

examples/nextjs-import-airbyte-github-export-seafowl/components/ImportExportStepper/stepper-states.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ParsedUrlQuery } from "querystring";
33
import { useEffect, useReducer } from "react";
44
export type GitHubRepository = { namespace: string; repository: string };
55

6-
type ExportTable = {
6+
export type ExportTable = {
77
destinationSchema: string;
88
destinationTable: string;
99
taskId: string;

examples/nextjs-import-airbyte-github-export-seafowl/components/RepositoryAnalytics/ImportedRepoMetadata.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ const makeSeafowlQueryHref = (sqlQuery: string) => {
166166
})}`;
167167
};
168168

169-
export const SplitgraphEmbeddedQuery = ({
170-
importedRepository,
171-
makeQuery,
172-
}: {
169+
export interface EmbeddedQueryProps {
173170
importedRepository: SplitgraphRepository;
174171
makeQuery: (repo: SplitgraphRepository) => string;
175172
tableName: string;
176-
}) => {
173+
}
174+
175+
export const SplitgraphEmbeddedQuery = ({
176+
importedRepository,
177+
makeQuery,
178+
}: EmbeddedQueryProps) => {
177179
return (
178180
<iframe
179181
src={makeSplitgraphEmbeddableQueryHref(makeQuery(importedRepository))}
@@ -188,11 +190,7 @@ export const SplitgraphEmbeddedQuery = ({
188190
export const SeafowlEmbeddedQuery = ({
189191
importedRepository,
190192
makeQuery,
191-
}: {
192-
importedRepository: SplitgraphRepository;
193-
makeQuery: (repo: SplitgraphRepository) => string;
194-
tableName: string;
195-
}) => {
193+
}: EmbeddedQueryProps) => {
196194
return (
197195
<iframe
198196
src={makeSeafowlEmbeddableQueryHref(makeQuery(importedRepository))}

0 commit comments

Comments
 (0)