diff --git a/src-tauri/sqlx-data.json b/src-tauri/sqlx-data.json index bc571861..0f6a7439 100644 --- a/src-tauri/sqlx-data.json +++ b/src-tauri/sqlx-data.json @@ -378,15 +378,15 @@ }, "query": "\n SELECT\n id,\n model,\n workspace_id,\n created_at,\n updated_at,\n name,\n url,\n method,\n body,\n body_type,\n authentication AS \"authentication!: Json>\",\n authentication_type,\n sort_priority,\n headers AS \"headers!: sqlx::types::Json>\"\n FROM http_requests\n WHERE workspace_id = ?\n " }, - "80bc37d283b67a70919c7b03a106fe563829741fb2c2fbd34ae4d8f581ecb697": { + "6f12b56113b09966b472431b6cb95c354bea51b4dfb22a96517655c0fca0ab05": { "describe": { "columns": [], "nullable": [], "parameters": { - "Right": 2 + "Right": 3 } }, - "query": "\n UPDATE environments\n SET (data, updated_at) = (?, CURRENT_TIMESTAMP)\n WHERE id = ?;\n " + "query": "\n UPDATE environments\n SET (name, data, updated_at) = (?, ?, CURRENT_TIMESTAMP)\n WHERE id = ?;\n " }, "84be2b954870ab181738656ecd4d03fca2ff21012947014c79626abfce8e999b": { "describe": { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 8f6e1f60..30774b12 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -425,10 +425,14 @@ async fn update_environment( ) -> Result { let pool = &*db_instance.lock().await; - let updated_environment = - models::update_environment(environment.id.as_str(), environment.data.0, pool) - .await - .expect("Failed to update request"); + let updated_environment = models::update_environment( + environment.id.as_str(), + environment.name.as_str(), + environment.data.0, + pool, + ) + .await + .expect("Failed to update request"); emit_and_return(&window, "updated_model", updated_environment) } diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index 01741bd1..e725fea8 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -254,6 +254,7 @@ pub async fn create_environment( pub async fn update_environment( id: &str, + name: &str, data: HashMap, pool: &Pool, ) -> Result { @@ -262,9 +263,10 @@ pub async fn update_environment( sqlx::query!( r#" UPDATE environments - SET (data, updated_at) = (?, CURRENT_TIMESTAMP) + SET (name, data, updated_at) = (?, ?, CURRENT_TIMESTAMP) WHERE id = ?; "#, + name, json_data, id, ) diff --git a/src-web/components/EnvironmentActionsDropdown.tsx b/src-web/components/EnvironmentActionsDropdown.tsx new file mode 100644 index 00000000..8fc0ee35 --- /dev/null +++ b/src-web/components/EnvironmentActionsDropdown.tsx @@ -0,0 +1,129 @@ +import classnames from 'classnames'; +import { memo, useMemo } from 'react'; +import { Button } from './core/Button'; +import type { DropdownItem } from './core/Dropdown'; +import { Dropdown } from './core/Dropdown'; +import { Icon } from './core/Icon'; +import { InlineCode } from './core/InlineCode'; +import { useEnvironments } from '../hooks/useEnvironments'; +import { useActiveEnvironment } from '../hooks/useActiveEnvironment'; +import { useUpdateEnvironment } from '../hooks/useUpdateEnvironment'; +import { useCreateEnvironment } from '../hooks/useCreateEnvironment'; +import { usePrompt } from '../hooks/usePrompt'; +import { useDialog } from './DialogContext'; +import { EnvironmentEditDialog } from './EnvironmentEditDialog'; + +type Props = { + className?: string; +}; + +export const EnvironmentActionsDropdown = memo(function EnvironmentActionsDropdown({ + className, +}: Props) { + const environments = useEnvironments(); + const [activeEnvironment, setActiveEnvironment] = useActiveEnvironment(); + const updateEnvironment = useUpdateEnvironment(activeEnvironment?.id ?? null); + const createEnvironment = useCreateEnvironment(); + const prompt = usePrompt(); + const dialog = useDialog(); + + const items: DropdownItem[] = useMemo(() => { + const environmentItems = environments.map( + (e) => ({ + key: e.id, + label: e.name, + onSelect: async () => { + setActiveEnvironment(e); + }, + }), + [], + ); + const activeEnvironmentItems: DropdownItem[] = + environments.length <= 1 + ? [] + : [ + ...environmentItems, + { + type: 'separator', + label: activeEnvironment?.name, + }, + ]; + + return [ + ...activeEnvironmentItems, + { + key: 'edit', + label: 'Edit', + leftSlot: , + onSelect: async () => { + dialog.show({ + title: 'Environments', + render: () => , + }); + }, + }, + { + key: 'rename', + label: 'Rename', + leftSlot: , + onSelect: async () => { + const name = await prompt({ + title: 'Rename Environment', + description: ( + <> + Enter a new name for {activeEnvironment?.name} + + ), + name: 'name', + label: 'Name', + defaultValue: activeEnvironment?.name, + }); + updateEnvironment.mutate({ name }); + }, + }, + // { + // key: 'delete', + // label: 'Delete', + // leftSlot: , + // onSelect: deleteEnv.mutate, + // variant: 'danger', + // }, + { type: 'separator' }, + { + key: 'create-environment', + label: 'Create Environment', + leftSlot: , + onSelect: async () => { + const name = await prompt({ + name: 'name', + label: 'Name', + defaultValue: '', + description: 'Enter a name for the new environment', + title: 'Create Environment', + }); + createEnvironment.mutate({ name }); + }, + }, + ]; + }, [ + environments, + activeEnvironment?.name, + // deleteEnvironment.mutate, + dialog, + prompt, + updateEnvironment, + createEnvironment, + ]); + + return ( + + + + ); +}); diff --git a/src-web/components/EnvironmentEditDialog.tsx b/src-web/components/EnvironmentEditDialog.tsx index 50261475..254277f2 100644 --- a/src-web/components/EnvironmentEditDialog.tsx +++ b/src-web/components/EnvironmentEditDialog.tsx @@ -1,4 +1,3 @@ -import { useState } from 'react'; import { useCreateEnvironment } from '../hooks/useCreateEnvironment'; import { useEnvironments } from '../hooks/useEnvironments'; import { usePrompt } from '../hooks/usePrompt'; @@ -7,19 +6,24 @@ import type { Environment } from '../lib/models'; import { Button } from './core/Button'; import { Editor } from './core/Editor'; import classnames from 'classnames'; +import { useActiveEnvironment } from '../hooks/useActiveEnvironment'; -export const EnvironmentEditDialog = function () { +export const EnvironmentEditDialog = function() { const prompt = usePrompt(); const environments = useEnvironments(); const createEnvironment = useCreateEnvironment(); - const [activeEnvironment, setActiveEnvironment] = useState(null); + const [activeEnvironment, setActiveEnvironment] = useActiveEnvironment(); return (
-