Skip to content

Commit

Permalink
Merge pull request #254 from outerbase/refactor-code
Browse files Browse the repository at this point in the history
refactor import paths to use absolute paths and change trigger tab to use extensions trigger
  • Loading branch information
invisal authored Jan 21, 2025
2 parents 3aa3d81 + 3d01c99 commit a30e254
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 188 deletions.
35 changes: 7 additions & 28 deletions src/components/gui/schema-sidebar-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SchemaCreateDialog from "./schema-editor/schema-create";
import { scc } from "@/core/command";
import { useConfig } from "@/context/config-provider";
import { OpenContextMenuList } from "@/core/channel-builtin";
import { triggerEditorExtensionTab } from "@/extensions/trigger-editor";

interface SchemaListProps {
search: string;
Expand Down Expand Up @@ -141,7 +142,7 @@ export default function SchemaList({ search }: Readonly<SchemaListProps>) {
(item?: DatabaseSchemaItem) => {
const selectedName = item?.name;
const isTable = item?.type === "table";
const isTrigger = item?.type === "trigger";
// const isTrigger = item?.type === "trigger";

const createMenuSection = {
title: "Create",
Expand All @@ -154,17 +155,6 @@ export default function SchemaList({ search }: Readonly<SchemaListProps>) {
});
},
},
databaseDriver.getFlags().supportCreateUpdateTrigger
? {
title: "Create Trigger",
onClick: () => {
scc.tabs.openBuiltinTrigger({
schemaName: item?.schemaName ?? currentSchemaName,
tableName: item?.tableSchema?.tableName,
});
},
}
: undefined,
...extensions.getResourceCreateMenu(),
],
};
Expand All @@ -182,18 +172,6 @@ export default function SchemaList({ search }: Readonly<SchemaListProps>) {
},
}
: undefined,
databaseDriver.getFlags().supportCreateUpdateTrigger && isTrigger
? {
title: "Edit Trigger",
onClick: () => {
scc.tabs.openBuiltinTrigger({
schemaName: item?.schemaName ?? currentSchemaName,
name: item.name,
tableName: item?.tableSchema?.tableName,
});
},
}
: undefined,
...extensions.getResourceContextMenu(item, "modification"),
]
: [];
Expand Down Expand Up @@ -282,10 +260,11 @@ export default function SchemaList({ search }: Readonly<SchemaListProps>) {
tableName: item.data.name,
});
} else if (item.data.type === "trigger") {
scc.tabs.openBuiltinTrigger({
schemaName: item.data.schemaName,
name: item.name,
});
triggerEditorExtensionTab.open({
schemaName: item.data.schemaName ?? '',
name: item.name ?? '',
tableName: item.data.tableName ?? ''
})
} else if (item.data.type === "schema") {
if (databaseDriver.getFlags().supportUseStatement) {
databaseDriver
Expand Down
10 changes: 0 additions & 10 deletions src/components/gui/schema-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ export default function SchemaView() {
});
}

if (flags.supportCreateUpdateTrigger) {
items.push({
title: "Create Trigger",
key: "create-trigger",
onClick: () => {
scc.tabs.openBuiltinTrigger({ schemaName: currentSchemaName });
},
});
}

return [...items, ...extensions.getResourceCreateMenu()];
}, [databaseDriver, currentSchemaName, extensions]);

Expand Down
2 changes: 1 addition & 1 deletion src/components/gui/table-combobox/TableColumnCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
CommandInput,
CommandItem,
} from "../../ui/command";
import { cn } from "../../lib/utils";
import type { DatabaseTableSchema } from "@/drivers/base-driver";
import { useDatabaseDriver } from "@/context/driver-provider";
import { cn } from "@/lib/utils";

export default function TableColumnCombobox({
value,
Expand Down
2 changes: 1 addition & 1 deletion src/components/gui/table-optimized/TableHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { type ReactElement } from "react";
import type { OptimizeTableHeaderWithIndexProps } from ".";
import TableHeaderResizeHandler from "./TableHeaderResizeHandler";
import { cn } from "../../lib/utils";
import OptimizeTableState from "./OptimizeTableState";
import { cn } from "@/lib/utils";

export default function TableHeader({
idx,
Expand Down
83 changes: 0 additions & 83 deletions src/components/lib/utils.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/core/builtin-tab/open-trigger-tab.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/core/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import { builtinMassDropTableTab } from "../builtin-tab/open-mass-drop-table";
import { builtinOpenQueryTab } from "../builtin-tab/open-query-tab";
import { builtinOpenSchemaTab } from "../builtin-tab/open-schema-tab";
import { builtinOpenTableTab } from "../builtin-tab/open-table-tab";
import { builtinOpenTriggerTab } from "../builtin-tab/open-trigger-tab";
import { tabCloseChannel } from "../extension-tab";

export const scc = {
tabs: {
openBuiltinQuery: builtinOpenQueryTab.open,
openBuiltinTable: builtinOpenTableTab.open,
openBuiltinSchema: builtinOpenSchemaTab.open,
openBuiltinTrigger: builtinOpenTriggerTab.open,
openBuiltinERD: builtinOpenERDTab.open,
openBuiltinMassDropTable: builtinMassDropTableTab.open,

Expand Down
3 changes: 2 additions & 1 deletion src/core/standard-extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import QueryHistoryConsoleLogExtension from "@/extensions/query-console-log";
import ViewEditorExtension from "@/extensions/view-editor";
import TriggerEditorExtension from '@/extensions/trigger-editor';

export function createStandardExtensions() {
return [new QueryHistoryConsoleLogExtension(), new ViewEditorExtension()];
return [new QueryHistoryConsoleLogExtension(), new TriggerEditorExtension(), new ViewEditorExtension()];
}
52 changes: 52 additions & 0 deletions src/extensions/trigger-editor/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { StudioExtension } from "@/core/extension-base";
import { StudioExtensionContext } from "@/core/extension-manager";
import { createTabExtension } from "@/core/extension-tab";
import TriggerTab from "./trigger-tab";
import { LucideCog } from "lucide-react";

export const triggerEditorExtensionTab = createTabExtension<{
schemaName?: string;
name?: string;
tableName?: string;
}>({
name: 'trigger',
key: (options) => {
return `${options.schemaName}.${options.name}`;
},
generate: (options) => ({
title: options.name || 'New Trigger',
component: (
<TriggerTab schemaName={options.schemaName ?? ''} name={options.name ?? ''} tableName={options.tableName ?? ''}/>
),
icon: LucideCog,
})
})

export default class TriggerEditorExtension extends StudioExtension {
extensionName = "trigger-editor";

init(studio: StudioExtensionContext): void {
studio.registerCreateResourceMenu({
key: "trigger",
title: "Create Trigger",
onClick: () => {
triggerEditorExtensionTab.open({});
},
});

studio.registerResourceContextMenu((resource) => {
if (resource.type !== "trigger") return;
return {
key: "trigger",
title: "Edit Trigger",
onClick: () => {
triggerEditorExtensionTab.open({
schemaName: resource.schemaName,
name: resource.name,
tableName: resource.tableName
});
},
};
}, "modification");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import CodePreview from "@/components/gui/code-preview";
import { Button, buttonVariants } from "@/components/ui/button";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Separator } from "@/components/ui/separator";
import { LucideCode, LucideLoader, LucideSave } from "lucide-react";
import CodePreview from "../code-preview";

interface Props {
onSave: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
TriggerOperation,
TriggerWhen,
} from "@/drivers/base-driver";
import TableCombobox from "../table-combobox/TableCombobox";
import { Input } from "@/components/ui/input";
import {
Select,
Expand All @@ -13,11 +12,12 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import SqlEditor from "../sql-editor";
import { produce } from "immer";
import SchemaNameSelect from "../schema-editor/schema-name-select";
import { useSchema } from "@/context/schema-provider";
import { useMemo } from "react";
import SchemaNameSelect from "@/components/gui/schema-editor/schema-name-select";
import TableCombobox from "@/components/gui/table-combobox/TableCombobox";
import SqlEditor from "@/components/gui/sql-editor";

export interface TriggerEditorProps {
onChange: (value: DatabaseTriggerSchema) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
AlertDialogFooter,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { LucideAlertCircle, LucideLoader, LucideSave, LucideTableProperties } from "lucide-react";
import { LucideAlertCircle, LucideLoader, LucideSave } from "lucide-react";
import { useCallback, useState } from "react";
import CodePreview from "../code-preview";
import { Button } from "@/components/ui/button";
import { useDatabaseDriver } from "@/context/driver-provider";
import TriggerTab from "../tabs/trigger-tab";
import { useTabsContext } from "../windows-tab";
import { useSchema } from "@/context/schema-provider";
import { DatabaseTriggerSchema } from "@/drivers/base-driver";
import CodePreview from "@/components/gui/code-preview";
import { triggerEditorExtensionTab } from ".";

interface Props {
onClose: () => void;
Expand All @@ -22,7 +21,6 @@ interface Props {
}

export function TriggerSaveDialog(props: Props) {
const { replaceCurrentTab } = useTabsContext();
const { refresh: refreshSchema } = useSchema();
const { databaseDriver } = useDatabaseDriver();
const [isExecuting, setIsExecuting] = useState(false);
Expand All @@ -36,26 +34,18 @@ export function TriggerSaveDialog(props: Props) {
)
.then(() => {
refreshSchema();
replaceCurrentTab({
component: (
<TriggerTab
tableName={props.trigger.tableName}
schemaName={props.trigger.schemaName}
name={props.trigger.name ?? ""}
/>
),
key: "trigger-" + props.trigger.name || "",
identifier: "trigger-" + props.trigger.name || "",
title: props.trigger.name || "",
icon: LucideTableProperties,
});
triggerEditorExtensionTab.replace({
schemaName: props.trigger.schemaName,
name: props.trigger.name ?? '',
tableName: props.trigger.tableName
})
props.onClose();
})
.catch((err) => setErrorMessage((err as Error).message))
.finally(() => {
setIsExecuting(false);
});
}, [databaseDriver, props, refreshSchema, replaceCurrentTab])
}, [databaseDriver, props, refreshSchema])

return (
<AlertDialog open onOpenChange={props.onClose}>
Expand Down
Loading

0 comments on commit a30e254

Please sign in to comment.