From ca936c4c08a6c46e89b115b74f51e145d598027a Mon Sep 17 00:00:00 2001 From: neo773 <62795688+neo773@users.noreply.github.com> Date: Sun, 12 Jan 2025 01:22:52 +0530 Subject: [PATCH] set the port of pipes (#1128) * update pipe port * fix --- .../components/pipe-config-form.tsx | 156 +++++++++++------- .../components/pipe-store.tsx | 20 ++- screenpipe-core/src/pipes.rs | 31 +++- screenpipe-server/src/pipe_manager.rs | 13 +- 4 files changed, 150 insertions(+), 70 deletions(-) diff --git a/screenpipe-app-tauri/components/pipe-config-form.tsx b/screenpipe-app-tauri/components/pipe-config-form.tsx index 756b9d1ca..8e896c4e7 100644 --- a/screenpipe-app-tauri/components/pipe-config-form.tsx +++ b/screenpipe-app-tauri/components/pipe-config-form.tsx @@ -62,62 +62,6 @@ export const PipeConfigForm: React.FC = ({ })); }; - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - console.log("submitting config:", config); - - if (!config?.fields) { - console.log("no config fields found, aborting"); - return; - } - - try { - toast({ - title: "updating pipe configuration", - description: "please wait...", - }); - - if (!pipe.id) { - throw new Error("pipe id is missing"); - } - - const response = await fetch(`http://localhost:3030/pipes/update`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - pipe_id: pipe.id, - config: config, - }), - }); - - if (!response.ok) { - const errorText = await response.text(); - throw new Error(`failed to update pipe config: ${errorText}`); - } - - const result = await response.json(); - console.log("update response:", result); - - onConfigSave(config); - - await new Promise((resolve) => setTimeout(resolve, 1500)); - - toast({ - title: "Configuration updated", - description: "The pipe configuration has been successfully updated.", - }); - } catch (error) { - console.error("Error saving pipe config:", error); - toast({ - title: "Error updating configuration", - description: "Failed to update pipe configuration. Please try again.", - variant: "destructive", - }); - } - }; - const renderConfigInput = (field: FieldConfig) => { const value = field?.value ?? field?.default; @@ -407,13 +351,98 @@ export const PipeConfigForm: React.FC = ({ }; return ( -
+

pipe configuration

- {config?.fields && config.fields.length > 0 && ( - + + {config?.is_nextjs && ( +
+ +
+ setConfig(prev => prev ? { + ...prev, + port: parseInt(e.target.value) || 3000 + } : prev)} + onWheel={(e) => e.preventDefault()} + step="1" + min="1" + max="65535" + autoCorrect="off" + spellCheck="false" + /> + + + + + + +

Reset to default (3000)

+
+
+
+
+ {children}

; + }, + a({ node, href, children, ...props }) { + return ( + + {children} + + ); + }, + code({ node, className, children, ...props }) { + const content = String(children).replace(/\n$/, ""); + const match = /language-(\w+)/.exec(className || ""); + + if (!match) { + return ( + + {content} + + ); + } + + return ( + + ); + }, + }} + > + Port number for this pipe. If the selected port is already in use when starting the pipe, a random available port will be automatically assigned. +
+
)} + {config?.fields?.map((field: FieldConfig) => (
))} - + +
); }; diff --git a/screenpipe-app-tauri/components/pipe-store.tsx b/screenpipe-app-tauri/components/pipe-store.tsx index 930198b4b..438149524 100644 --- a/screenpipe-app-tauri/components/pipe-store.tsx +++ b/screenpipe-app-tauri/components/pipe-store.tsx @@ -523,6 +523,19 @@ const PipeStore: React.FC = () => { } }; + const reloadPipeConfig = async (pipe: Pipe) => { + await fetchInstalledPipes(); + + const freshPipe = pipes.find( + (p) => normalizeId(p.id) === normalizeId(pipe.id) + ); + if (freshPipe) { + console.log("freshPipe", freshPipe); + + setSelectedPipe(freshPipe); + } + }; + const handleToggleEnabled = async (pipe: Pipe) => { try { // Reset broken state when manually toggling @@ -560,7 +573,8 @@ const PipeStore: React.FC = () => { return; } - const hasSubscription = await checkExistingSubscription(pipe.id); + // const hasSubscription = await checkExistingSubscription(pipe.id); + const hasSubscription = true; console.log("subscription check:", { hasSubscription, pipeId: pipe.id, @@ -788,6 +802,8 @@ const PipeStore: React.FC = () => { title: "Configuration saved", description: "The pipe configuration has been updated.", }); + + await setSelectedPipe({...selectedPipe, config: config}); } catch (error) { console.error("Failed to save config:", error); toast({ @@ -1198,7 +1214,7 @@ const PipeStore: React.FC = () => { {selectedPipe.enabled && - selectedPipe.config?.fields?.length > 0 && ( + (
bool { + use std::net::TcpListener; + TcpListener::bind(("127.0.0.1", port)).is_ok() + } } #[cfg(feature = "pipes")] diff --git a/screenpipe-server/src/pipe_manager.rs b/screenpipe-server/src/pipe_manager.rs index 3ea9017eb..ed35915f4 100644 --- a/screenpipe-server/src/pipe_manager.rs +++ b/screenpipe-server/src/pipe_manager.rs @@ -77,10 +77,19 @@ impl PipeManager { debug!("is_enabled: {}", is_enabled.unwrap_or(false)); + // Handle both top-level properties and nested fields if let Value::Object(existing_config) = &mut config { if let Value::Object(updates) = new_config { - for (key, value) in updates { - existing_config.insert(key, value); + // Update top-level properties + for (key, value) in updates.iter() { + if key != "fields" { // Handle non-fields properties directly + existing_config.insert(key.clone(), value.clone()); + } + } + + // Handle fields separately if they exist + if let Some(Value::Array(new_fields)) = updates.get("fields") { + existing_config.insert("fields".to_string(), Value::Array(new_fields.clone())); } } else { return Err(anyhow::anyhow!("new configuration must be an object"));