Skip to content

Commit

Permalink
refactor embed page to single page (#268)
Browse files Browse the repository at this point in the history
* refactor embed page to single page

* starbase does not support pragma list
  • Loading branch information
invisal authored Jan 26, 2025
1 parent 391b7e6 commit ff0f23b
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 305 deletions.
90 changes: 90 additions & 0 deletions src/app/(theme)/embed/[driver]/page-client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"use client";
import { Studio } from "@/components/gui/studio";
import { StudioExtensionManager } from "@/core/extension-manager";
import {
createMySQLExtensions,
createPostgreSQLExtensions,
createSQLiteExtensions,
} from "@/core/standard-extension";
import {
IframeMySQLDriver,
IframePostgresDriver,
IframeSQLiteDriver,
} from "@/drivers/iframe-driver";
import ElectronSavedDocs from "@/drivers/saved-doc/electron-saved-doc";
import DoltExtension from "@/extensions/dolt";
import { useSearchParams } from "next/navigation";
import { useEffect, useMemo } from "react";

export default function EmbedPageClient({
driverName,
}: {
driverName: string;
}) {
const searchParams = useSearchParams();

const driver = useMemo(() => {
return createDatabaseDriver(driverName);
}, [driverName]);

const savedDocDriver = useMemo(() => {
if (window.outerbaseIpc?.docs) {
return new ElectronSavedDocs();
}
}, []);

const extensions = useMemo(() => {
return new StudioExtensionManager(createEmbedExtensions(driverName));
}, [driverName]);

useEffect(() => {
return driver.listen();
}, [driver]);

return (
<Studio
driver={driver}
extensions={extensions}
docDriver={savedDocDriver}
name={searchParams.get("name") || "Unnamed Connection"}
color={searchParams.get("color") || "gray"}
/>
);
}

function createDatabaseDriver(driverName: string) {
if (driverName === "turso") {
return new IframeSQLiteDriver({
supportPragmaList: false,
supportBigInt: true,
});
} else if (driverName === "sqlite") {
return new IframeSQLiteDriver();
} else if (driverName === "starbase") {
return new IframeSQLiteDriver({
supportPragmaList: false,
});
} else if (driverName === "mysql" || driverName === "dolt") {
return new IframeMySQLDriver();
} else if (driverName === "postgres") {
return new IframePostgresDriver();
}

return new IframeSQLiteDriver();
}

function createEmbedExtensions(driverName: string) {
if (driverName === "turso") {
return createSQLiteExtensions();
} else if (driverName === "sqlite" || driverName === "starbase") {
return createSQLiteExtensions();
} else if (driverName === "mysql") {
return createMySQLExtensions();
} else if (driverName === "dolt") {
return [...createMySQLExtensions(), new DoltExtension()];
} else if (driverName === "postgres") {
return createPostgreSQLExtensions();
}

return createSQLiteExtensions();
}
48 changes: 48 additions & 0 deletions src/app/(theme)/embed/[driver]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import ClientOnly from "@/components/client-only";
import ThemeLayout from "../../theme_layout";
import EmbedPageClient from "./page-client";

export interface EmbedPageProps {
searchParams: Promise<{
theme?: string;
disableThemeToggle?: string;
[key: string]: any;
}>;
params: Promise<{
driver: string;
}>;
}

export default async function EmbedPage(props: EmbedPageProps) {
const searchParams = await props.searchParams;
const driver = (await props.params).driver;

let overrideTheme: "dark" | "light" | undefined = undefined;
const disableToggle = searchParams.disableThemeToggle === "1";

if (searchParams.theme) {
overrideTheme = searchParams.theme === "dark" ? "dark" : "light";
}

const overrideThemeVariables: Record<string, string> = {};

for (const key in searchParams) {
if (!key.startsWith("themeVariables[")) {
continue;
}

overrideThemeVariables[key.slice(15, -1)] = searchParams[key];
}

return (
<ThemeLayout
overrideTheme={overrideTheme}
disableToggle={disableToggle}
overrideThemeVariables={overrideThemeVariables}
>
<ClientOnly>
<EmbedPageClient driverName={driver} />
</ClientOnly>
</ThemeLayout>
);
}
41 changes: 0 additions & 41 deletions src/app/(theme)/embed/dolt/page-client.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions src/app/(theme)/embed/dolt/page.tsx

This file was deleted.

44 changes: 0 additions & 44 deletions src/app/(theme)/embed/embed-page.tsx

This file was deleted.

37 changes: 0 additions & 37 deletions src/app/(theme)/embed/mysql/page-client.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions src/app/(theme)/embed/mysql/page.tsx

This file was deleted.

37 changes: 0 additions & 37 deletions src/app/(theme)/embed/postgres/page-client.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions src/app/(theme)/embed/postgres/page.tsx

This file was deleted.

37 changes: 0 additions & 37 deletions src/app/(theme)/embed/sqlite/page-client.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions src/app/(theme)/embed/sqlite/page.tsx

This file was deleted.

Loading

0 comments on commit ff0f23b

Please sign in to comment.