Skip to content

Commit

Permalink
auto select schema name based on where create table is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Sep 2, 2024
1 parent ac83d0e commit 6011f1d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
55 changes: 29 additions & 26 deletions src/components/gui/schema-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ interface Props {
onDiscard: () => void;
value: DatabaseTableSchemaChange;
onChange: Dispatch<SetStateAction<DatabaseTableSchemaChange>>;
isCreate?: boolean;
}

export default function SchemaEditor({
value,
onChange,
onSave,
onDiscard,
isCreate,
}: Readonly<Props>) {
const { databaseDriver } = useDatabaseDriver();
const isCreateScript = value.name.old === "";
Expand Down Expand Up @@ -73,7 +71,7 @@ export default function SchemaEditor({
<Button
variant="ghost"
onClick={onSave}
disabled={!hasChange || !value.name?.new}
disabled={!hasChange || !value.name?.new || !value.schemaName}
size={"sm"}
>
<LucideSave className="w-4 h-4 mr-2" />
Expand Down Expand Up @@ -152,29 +150,34 @@ export default function SchemaEditor({
)}
</div>

<div className="flex items-center mx-3 mt-1 mb-2 ml-5 gap-2">
<SchemaNameSelect
readonly={!isCreate}
value={value.schemaName}
onChange={(selectedSchema) => {
onChange({ ...value, schemaName: selectedSchema });
}}
/>
<Input
placeholder="Table Name"
value={value.name.new ?? value.name.old ?? ""}
onChange={(e) => {
onChange({
...value,
name: {
...value.name,
new: e.currentTarget.value,
},
});
}}
className="w-[200px]"
/>
<div className="flex items-center mx-3 mt-3 mb-4 ml-5 gap-2">
<div>
<div className="text-xs font-medium mb-1">Table Name</div>
<Input
placeholder="Table Name"
value={value.name.new ?? value.name.old ?? ""}
onChange={(e) => {
onChange({
...value,
name: {
...value.name,
new: e.currentTarget.value,
},
});
}}
className="w-[200px]"
/>
</div>
<div>
<div className="text-xs font-medium mb-1">Schema</div>
<SchemaNameSelect
readonly={!isCreateScript}
value={value.schemaName}
onChange={(selectedSchema) => {
onChange({ ...value, schemaName: selectedSchema });
}}
/>
</div>
</div>
<Separator />
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/gui/schema-editor/schema-name-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function SchemaNameSelect({
className="w-[200px] justify-between"
disabled={readonly}
>
{value ?? "Select schema"}
{value || "Select schema"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
Expand Down
8 changes: 4 additions & 4 deletions src/components/gui/schema-sidebar-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function flattenSchemaGroup(
export default function SchemaList({ search }: Readonly<SchemaListProps>) {
const { databaseDriver } = useDatabaseDriver();
const [selected, setSelected] = useState("");
const { refresh, schema } = useSchema();
const { refresh, schema, currentSchemaName } = useSchema();

const [collapsed, setCollapsed] = useState(() => {
return new Set<string>();
Expand Down Expand Up @@ -127,7 +127,7 @@ export default function SchemaList({ search }: Readonly<SchemaListProps>) {
onClick: () => {
openTab({
type: "schema",
schemaName: item?.schemaName ?? "",
schemaName: item?.schemaName ?? currentSchemaName,
});
},
},
Expand All @@ -147,13 +147,13 @@ export default function SchemaList({ search }: Readonly<SchemaListProps>) {
{ title: "Refresh", onClick: () => refresh() },
].filter(Boolean) as OpenContextMenuList;
},
[refresh]
[refresh, currentSchemaName]
);

const listViewItems = useMemo(() => {
const r = Object.entries(schema).map(([s, tables]) => {
return {
data: {},
data: { type: "schema", schemaName: s },
icon: LucideDatabase,
name: s,
key: s.toString(),
Expand Down
6 changes: 4 additions & 2 deletions src/components/gui/tabs/schema-editor-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export default function SchemaEditorTab({
tableName,
}: Readonly<SchemaEditorTabProps>) {
const { databaseDriver } = useDatabaseDriver();
const [schema, setSchema] = useState<DatabaseTableSchemaChange>(EMPTY_SCHEMA);
const [schema, setSchema] = useState<DatabaseTableSchemaChange>({
...EMPTY_SCHEMA,
schemaName,
});
const [loading, setLoading] = useState(!!tableName);
const [isSaving, setIsSaving] = useState(false);

Expand Down Expand Up @@ -115,7 +118,6 @@ export default function SchemaEditorTab({
onChange={setSchema}
onSave={onSaveToggle}
onDiscard={onDiscard}
isCreate={!tableName && !schemaName}
/>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/base-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type DatabaseValue<T = unknown> = T | undefined | null;
export type DatabaseSchemas = Record<string, DatabaseSchemaItem[]>;

export interface DatabaseSchemaItem {
type: "table" | "trigger" | "view";
type: "table" | "trigger" | "view" | "schema";
name: string;
schemaName: string;
tableName?: string;
Expand Down

0 comments on commit 6011f1d

Please sign in to comment.