Skip to content

Commit

Permalink
feat: support connecting via search params
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Mar 5, 2024
1 parent 2b4746d commit 14c9d0c
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions src/components/main-connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import { TooltipProvider } from "@/components/ui/tooltip";
import { AutoCompleteProvider } from "@/context/AutoCompleteProvider";
import ContextMenuHandler from "./context-menu-handler";
import InternalPubSub from "@/lib/internal-pubsub";
import { useRouter } from "next/navigation";
import { useRouter, useSearchParams } from "next/navigation";
import { normalizeConnectionEndpoint } from "@/lib/validation";
import { SchemaProvider } from "@/context/SchemaProvider";
import ThemeProvider from "@/context/theme-provider";

export interface ConnectionCredential {
url: string;
token: string;
}

function MainConnection({
credential,
}: {
Expand Down Expand Up @@ -51,15 +56,10 @@ function InvalidSession() {
return <div></div>;
}

export default function MainScreen() {
function MainConnectionContainer({
credential,
}: Readonly<{ credential: ConnectionCredential }>) {
const router = useRouter();
const sessionCredential: { url: string; token: string } = useMemo(() => {
const config = JSON.parse(sessionStorage.getItem("connection") ?? "{}");
return {
url: normalizeConnectionEndpoint(config.url),
token: config.token,
};
}, []);

/**
* We use useLayoutEffect because it executes before
Expand All @@ -70,19 +70,19 @@ export default function MainScreen() {
useLayoutEffect(() => {
console.info("Injecting message into window object");
window.internalPubSub = new InternalPubSub();
}, [sessionCredential, router]);
}, [credential, router]);

useEffect(() => {
if (sessionCredential.url) {
document.title = sessionCredential.url + " - LibSQL Studio";
if (credential.url) {
document.title = credential.url + " - LibSQL Studio";
}
}, [sessionCredential]);
}, [credential]);

return sessionCredential?.url ? (
return credential?.url ? (
<>
<AutoCompleteProvider>
<TooltipProvider>
<MainConnection credential={sessionCredential} />
<MainConnection credential={credential} />
</TooltipProvider>
</AutoCompleteProvider>
<ContextMenuHandler />
Expand All @@ -91,3 +91,26 @@ export default function MainScreen() {
<InvalidSession />
);
}

export default function MainScreen() {
const params = useSearchParams();

const finalCredential = useMemo(() => {
const connectionParams = params.get("c");
if (connectionParams) {
const [port, token] = connectionParams.split(":");
return {
url: "ws://localhost:" + port,
token,
};
}

const config = JSON.parse(sessionStorage.getItem("connection") ?? "{}");
return {
url: normalizeConnectionEndpoint(config.url),
token: config.token as string,
};
}, [params]);

return <MainConnectionContainer credential={finalCredential} />;
}

0 comments on commit 14c9d0c

Please sign in to comment.