|
1 | 1 | import { useFormFieldContext } from "../hooks/form-field-context"; |
2 | | -import { useFormContext } from "../hooks/form-context"; |
3 | 2 | import { useCustomize } from "../hooks/customization-context"; |
4 | 3 | import type { CSSProperties } from "react"; |
| 4 | +import type { ConfigurablePropSql } from "@pipedream/sdk"; |
| 5 | + |
| 6 | +// Type guard to check if value is a structured SQL object |
| 7 | +const isSqlStructuredValue = (value: unknown): value is { app: string; query: string; params: any[] } => { |
| 8 | + return ( |
| 9 | + typeof value === "object" && |
| 10 | + value !== null && |
| 11 | + "query" in value && |
| 12 | + typeof (value as any).query === "string" |
| 13 | + ); |
| 14 | +}; |
5 | 15 |
|
6 | 16 | export function ControlSql() { |
7 | 17 | const formFieldContext = useFormFieldContext(); |
8 | | - const formContext = useFormContext(); |
9 | 18 | const { |
10 | 19 | id, onChange, prop, value, |
11 | 20 | } = formFieldContext; |
12 | 21 | const { |
13 | 22 | getProps, theme, |
14 | 23 | } = useCustomize(); |
15 | 24 |
|
16 | | - // Find the first app prop to determine which database app to use |
17 | | - const appProp = formContext.configurableProps.find((p: any) => p.type === "app"); |
18 | | - const appName = appProp?.app || "postgresql"; // Default to postgresql |
| 25 | + // Cast prop to SQL prop type (this component is only used for SQL props) |
| 26 | + const sqlProp = prop as ConfigurablePropSql; |
| 27 | + |
| 28 | + // Get the app name from the SQL prop's auth configuration |
| 29 | + const appName = sqlProp.auth?.app || "postgresql"; // Default to postgresql |
19 | 30 |
|
20 | 31 | // Extract the query string from the structured value or use empty string |
21 | | - const queryValue = typeof value === "object" && value && "query" in value |
22 | | - ? (value as any).query |
23 | | - : typeof value === "string" |
24 | | - ? value |
25 | | - : ""; |
| 32 | + let queryValue = ""; |
| 33 | + if (isSqlStructuredValue(value)) { |
| 34 | + queryValue = value.query; |
| 35 | + } else if (typeof value === "string") { |
| 36 | + queryValue = value; |
| 37 | + } |
26 | 38 |
|
27 | 39 | const handleChange = (queryText: string) => { |
28 | 40 | // Transform the simple query string into the structured SQL object |
|
0 commit comments