Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(web) add a console component that allows running sql in the context of an env #293

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion packages/validators/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ export const defNameSchema = z
{ message: "Definition name is invalid." },
);

export const envNameSchema = z.object({ name: z.string().trim().min(1) });
export const envNameSchema = z.object({
name: z
.string()
.trim()
.min(1)
.refine((val) => !restrictedDefSlugs.includes(slugify(val)), {
message: "You can't use a restricted word as an environment name.",
}),
});
1 change: 1 addition & 0 deletions packages/validators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./validators/teams.js";
export * from "./validators/projects.js";
export * from "./validators/auth.js";
export * from "./validators/tables.js";
export * from "./restricted-slugs.js";
5 changes: 4 additions & 1 deletion packages/validators/src/restricted-slugs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ export const restrictedTeamSlugs = [

export const restrictedProjectSlugs = ["people", "settings"];

export const restrictedDefSlugs = ["settings", "tables"];
export const restrictedDefSlugs = ["settings", "tables", "console"];

export const restrictedEnvSlugs = ["settings"];

export const allRestrictedSlugs = [
...restrictedTeamSlugs,
...restrictedProjectSlugs,
...restrictedDefSlugs,
...restrictedEnvSlugs,
];
22 changes: 22 additions & 0 deletions packages/web/app/[team]/[project]/[env]/console/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Console } from "@/components/console";
import {
environmentBySlug,
projectBySlug,
teamBySlug,
} from "@/lib/api-helpers";

export default async function ConsolePage({
params,
}: {
params: { team: string; project: string; env: string };
}) {
const team = await teamBySlug(params.team);
const project = await projectBySlug(params.project, team.id);
const environment = await environmentBySlug(project.id, params.env);

return (
<main className="flex-1 p-4">
<Console environmentId={environment.id} />
</main>
);
}
28 changes: 26 additions & 2 deletions packages/web/app/[team]/[project]/_components/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
"use client";

import { type schema } from "@tableland/studio-store";
import { Ellipsis, LayoutDashboard, Settings, Table2 } from "lucide-react";
import {
Ellipsis,
LayoutDashboard,
Settings,
Table2,
Terminal,
} from "lucide-react";
import {
useParams,
useRouter,
useSelectedLayoutSegment,
useSelectedLayoutSegments,
} from "next/navigation";
import { skipToken } from "@tanstack/react-query";
import { useState } from "react";
Expand Down Expand Up @@ -33,8 +40,16 @@ export function Sidebar() {
env?: string;
table?: string;
}>();

const router = useRouter();

const selectedLayoutSegment = useSelectedLayoutSegment();
const selectedLayoutSegments = useSelectedLayoutSegments();
const isConsole =
!!envSlug &&
!defSlug &&
selectedLayoutSegments.slice(-1).pop() === "console";

const [newDefOpen, setNewDefOpen] = useState(false);
const [importTableOpen, setImportTableOpen] = useState(false);

Expand Down Expand Up @@ -126,14 +141,23 @@ export function Sidebar() {
icon={LayoutDashboard}
title="Overview"
href={`/${teamQuery.data.slug}/${projectQuery.data.slug}/${linkEnv.slug}`}
selected={!defSlug && !!envSlug && envSlug === env?.slug}
selected={
!defSlug && !!envSlug && !isConsole && envSlug === env?.slug
asutula marked this conversation as resolved.
Show resolved Hide resolved
}
/>
<SidebarLink
icon={Terminal}
title="Console"
href={`/${teamQuery.data.slug}/${projectQuery.data.slug}/${linkEnv.slug}/console`}
selected={isConsole}
/>
Comment on lines +148 to 153
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This SidebarLink should go in the SidebarSection above along with the link for Overview. You can then delete this section.

</SidebarSection>
<SidebarSection>
<div className="flex items-center gap-2">
<h3 className="text-base font-medium tracking-wide text-muted-foreground">
Definitions
</h3>

{!!isAuthorizedQuery.data && (
<DropdownMenu>
<DropdownMenuTrigger className="ml-auto text-muted-foreground hover:text-foreground">
Expand Down
3 changes: 3 additions & 0 deletions packages/web/app/prism.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/web/app/prism.js

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions packages/web/components/code-editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";
import Editor from "react-simple-code-editor";
import { highlight, languages } from "@/app/prism.js";
import "@/app/prism.css";

const hightlightWithLineNumbers = (
input: any,
language: any,
hideLineNumbers: boolean,
): any =>
highlight(input, language)
.split("\n")
.map(
(line: string, i: number) =>
`${
hideLineNumbers
? ""
: `<span class="absolute left-0 text-right w-4 font-thin">${i + 1}</span>`
}${line}`,
)
.join("\n");

export function CodeEditor(props: any): React.JSX.Element {
return (
<Editor
preClassName="language-sql"
value={props.code}
onValueChange={props.onChange}
highlight={(code) =>
hightlightWithLineNumbers(
code,
(languages as any).sql,
props.hideLineNumbers,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left the line numbers feature mostly in place, but hard coded it to hide the line numbers. If we want to show line numbers we will need to include the css the old console was using, or convert it to tailwind and add the classes.

)
}
padding={10}
placeholder="SELECT * FROM YourTable;"
textareaId={props.hideLineNumbers ? "codeViewer" : "codeEditor"}
textareaClassName="ml-8"
className="editor language-sql"
disabled={props.loading}
style={{
fontFamily: '"Fira code", "Fira Mono", monospace',
fontSize: 18,
outline: 0,
}}
/>
);
}
Loading
Loading