Skip to content

Commit f9ee805

Browse files
committed
added hooks for chat chats and updated callers to all use reactquery
1 parent b467bac commit f9ee805

File tree

9 files changed

+401
-335
lines changed

9 files changed

+401
-335
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/components/deploy-modal/components/a2a/a2a.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ export function A2aDeploy({
267267

268268
const needsRepublish = existingAgent && (hasFormChanges || hasWorkflowChanges)
269269

270-
// Notify parent of republish status changes (depends on local form state)
271270
useEffect(() => {
272271
onNeedsRepublishChange?.(!!needsRepublish)
273272
}, [needsRepublish, onNeedsRepublishChange])

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/components/deploy-modal/components/chat/chat.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ export function ChatDeploy({
123123
const [errors, setErrors] = useState<FormErrors>({})
124124
const formRef = useRef<HTMLFormElement>(null)
125125

126-
// React Query mutations for chat operations
127126
const createChatMutation = useCreateChat()
128127
const updateChatMutation = useUpdateChat()
129128
const deleteChatMutation = useDeleteChat()
@@ -238,7 +237,6 @@ export function ChatDeploy({
238237
let chatUrl: string
239238

240239
if (existingChat?.id) {
241-
// Update existing chat
242240
const result = await updateChatMutation.mutateAsync({
243241
chatId: existingChat.id,
244242
workflowId,
@@ -247,7 +245,6 @@ export function ChatDeploy({
247245
})
248246
chatUrl = result.chatUrl
249247
} else {
250-
// Create new chat
251248
const result = await createChatMutation.mutateAsync({
252249
workflowId,
253250
formData,
@@ -257,7 +254,6 @@ export function ChatDeploy({
257254
chatUrl = result.chatUrl
258255
}
259256

260-
// Mutations handle cache invalidation, no need for onChatExistsChange
261257
onDeployed?.()
262258
onVersionActivated?.()
263259

@@ -291,7 +287,6 @@ export function ChatDeploy({
291287

292288
setImageUrl(null)
293289
setHasInitializedForm(false)
294-
// Mutation handles cache invalidation, no need for onChatExistsChange
295290
await onRefetchChat()
296291

297292
onDeploymentComplete?.()

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/components/deploy-modal/components/form/form.tsx

Lines changed: 68 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ import { Skeleton } from '@/components/ui'
1717
import { isDev } from '@/lib/core/config/feature-flags'
1818
import { cn } from '@/lib/core/utils/cn'
1919
import { getBaseUrl, getEmailDomain } from '@/lib/core/utils/urls'
20+
import {
21+
type FieldConfig,
22+
useCreateForm,
23+
useDeleteForm,
24+
useFormByWorkflow,
25+
useUpdateForm,
26+
} from '@/hooks/queries/forms'
2027
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
2128
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2229
import { EmbedCodeGenerator } from './components/embed-code-generator'
2330
import { FormBuilder } from './components/form-builder'
24-
import { useFormDeployment } from './hooks/use-form-deployment'
2531
import { useIdentifierValidation } from './hooks/use-identifier-validation'
2632

2733
const logger = createLogger('FormDeploy')
@@ -34,38 +40,11 @@ interface FormErrors {
3440
general?: string
3541
}
3642

37-
interface FieldConfig {
38-
name: string
39-
type: string
40-
label: string
41-
description?: string
42-
required?: boolean
43-
}
44-
45-
export interface ExistingForm {
46-
id: string
47-
identifier: string
48-
title: string
49-
description?: string
50-
customizations: {
51-
primaryColor?: string
52-
thankYouMessage?: string
53-
logoUrl?: string
54-
fieldConfigs?: FieldConfig[]
55-
}
56-
authType: 'public' | 'password' | 'email'
57-
hasPassword?: boolean
58-
allowedEmails?: string[]
59-
showBranding: boolean
60-
isActive: boolean
61-
}
62-
6343
interface FormDeployProps {
6444
workflowId: string
6545
onDeploymentComplete?: () => void
6646
onValidationChange?: (isValid: boolean) => void
6747
onSubmittingChange?: (isSubmitting: boolean) => void
68-
onExistingFormChange?: (exists: boolean) => void
6948
formSubmitting?: boolean
7049
setFormSubmitting?: (submitting: boolean) => void
7150
onDeployed?: () => Promise<void>
@@ -81,7 +60,6 @@ export function FormDeploy({
8160
onDeploymentComplete,
8261
onValidationChange,
8362
onSubmittingChange,
84-
onExistingFormChange,
8563
formSubmitting,
8664
setFormSubmitting,
8765
onDeployed,
@@ -95,16 +73,19 @@ export function FormDeploy({
9573
const [authType, setAuthType] = useState<'public' | 'password' | 'email'>('public')
9674
const [password, setPassword] = useState('')
9775
const [emailItems, setEmailItems] = useState<TagItem[]>([])
98-
const [existingForm, setExistingForm] = useState<ExistingForm | null>(null)
99-
const [isLoading, setIsLoading] = useState(true)
10076
const [formUrl, setFormUrl] = useState('')
10177
const [inputFields, setInputFields] = useState<{ name: string; type: string }[]>([])
10278
const [showPasswordField, setShowPasswordField] = useState(false)
10379
const [fieldConfigs, setFieldConfigs] = useState<FieldConfig[]>([])
10480
const [errors, setErrors] = useState<FormErrors>({})
10581
const [isIdentifierValid, setIsIdentifierValid] = useState(false)
10682

107-
const { createForm, updateForm, deleteForm, isSubmitting } = useFormDeployment()
83+
const { data: existingForm, isLoading } = useFormByWorkflow(workflowId)
84+
const createFormMutation = useCreateForm()
85+
const updateFormMutation = useUpdateForm()
86+
const deleteFormMutation = useDeleteForm()
87+
88+
const isSubmitting = createFormMutation.isPending || updateFormMutation.isPending
10889

10990
const {
11091
isChecking: isCheckingIdentifier,
@@ -124,73 +105,45 @@ export function FormDeploy({
124105
setErrors((prev) => ({ ...prev, [field]: undefined }))
125106
}
126107

127-
// Fetch existing form deployment
108+
// Populate form fields when existing form data is loaded
128109
useEffect(() => {
129-
async function fetchExistingForm() {
130-
if (!workflowId) return
110+
if (existingForm) {
111+
setIdentifier(existingForm.identifier)
112+
setTitle(existingForm.title)
113+
setDescription(existingForm.description || '')
114+
setThankYouMessage(
115+
existingForm.customizations?.thankYouMessage ||
116+
'Your response has been submitted successfully.'
117+
)
118+
setAuthType(existingForm.authType)
119+
setEmailItems(
120+
(existingForm.allowedEmails || []).map((email) => ({ value: email, isValid: true }))
121+
)
122+
if (existingForm.customizations?.fieldConfigs) {
123+
setFieldConfigs(existingForm.customizations.fieldConfigs)
124+
}
131125

126+
const baseUrl = getBaseUrl()
132127
try {
133-
setIsLoading(true)
134-
const response = await fetch(`/api/workflows/${workflowId}/form/status`)
135-
136-
if (response.ok) {
137-
const data = await response.json()
138-
if (data.isDeployed && data.form) {
139-
const detailResponse = await fetch(`/api/form/manage/${data.form.id}`)
140-
if (detailResponse.ok) {
141-
const formDetail = await detailResponse.json()
142-
const form = formDetail.form as ExistingForm
143-
setExistingForm(form)
144-
onExistingFormChange?.(true)
145-
146-
setIdentifier(form.identifier)
147-
setTitle(form.title)
148-
setDescription(form.description || '')
149-
setThankYouMessage(
150-
form.customizations?.thankYouMessage ||
151-
'Your response has been submitted successfully.'
152-
)
153-
setAuthType(form.authType)
154-
setEmailItems(
155-
(form.allowedEmails || []).map((email) => ({ value: email, isValid: true }))
156-
)
157-
if (form.customizations?.fieldConfigs) {
158-
setFieldConfigs(form.customizations.fieldConfigs)
159-
}
160-
161-
const baseUrl = getBaseUrl()
162-
try {
163-
const url = new URL(baseUrl)
164-
let host = url.host
165-
if (host.startsWith('www.')) host = host.substring(4)
166-
setFormUrl(`${url.protocol}//${host}/form/${form.identifier}`)
167-
} catch {
168-
setFormUrl(
169-
isDev
170-
? `http://localhost:3000/form/${form.identifier}`
171-
: `https://sim.ai/form/${form.identifier}`
172-
)
173-
}
174-
}
175-
} else {
176-
setExistingForm(null)
177-
onExistingFormChange?.(false)
178-
179-
const workflowName =
180-
useWorkflowStore.getState().blocks[Object.keys(useWorkflowStore.getState().blocks)[0]]
181-
?.name || 'Form'
182-
setTitle(`${workflowName} Form`)
183-
}
184-
}
185-
} catch (err) {
186-
logger.error('Error fetching form deployment:', err)
187-
} finally {
188-
setIsLoading(false)
128+
const url = new URL(baseUrl)
129+
let host = url.host
130+
if (host.startsWith('www.')) host = host.substring(4)
131+
setFormUrl(`${url.protocol}//${host}/form/${existingForm.identifier}`)
132+
} catch {
133+
setFormUrl(
134+
isDev
135+
? `http://localhost:3000/form/${existingForm.identifier}`
136+
: `https://sim.ai/form/${existingForm.identifier}`
137+
)
189138
}
139+
} else if (!isLoading) {
140+
// Set default title for new forms
141+
const workflowName =
142+
useWorkflowStore.getState().blocks[Object.keys(useWorkflowStore.getState().blocks)[0]]
143+
?.name || 'Form'
144+
setTitle(`${workflowName} Form`)
190145
}
191-
192-
fetchExistingForm()
193-
}, [workflowId, onExistingFormChange])
146+
}, [existingForm, isLoading])
194147

195148
// Get input fields from start block and initialize field configs
196149
useEffect(() => {
@@ -281,17 +234,21 @@ export function FormDeploy({
281234

282235
try {
283236
if (existingForm) {
284-
await updateForm(existingForm.id, {
285-
identifier,
286-
title,
287-
description,
288-
customizations,
289-
authType,
290-
password: password || undefined,
291-
allowedEmails,
237+
await updateFormMutation.mutateAsync({
238+
formId: existingForm.id,
239+
workflowId,
240+
data: {
241+
identifier,
242+
title,
243+
description,
244+
customizations,
245+
authType,
246+
password: password || undefined,
247+
allowedEmails,
248+
},
292249
})
293250
} else {
294-
const result = await createForm({
251+
const result = await createFormMutation.mutateAsync({
295252
workflowId,
296253
identifier,
297254
title,
@@ -342,8 +299,8 @@ export function FormDeploy({
342299
password,
343300
allowedEmails,
344301
isIdentifierValid,
345-
createForm,
346-
updateForm,
302+
createFormMutation,
303+
updateFormMutation,
347304
onDeployed,
348305
onDeploymentComplete,
349306
]
@@ -353,17 +310,19 @@ export function FormDeploy({
353310
if (!existingForm) return
354311

355312
try {
356-
await deleteForm(existingForm.id)
357-
setExistingForm(null)
358-
onExistingFormChange?.(false)
313+
await deleteFormMutation.mutateAsync({
314+
formId: existingForm.id,
315+
workflowId,
316+
})
317+
// Reset form fields after successful deletion
359318
setIdentifier('')
360319
setTitle('')
361320
setDescription('')
362321
setFormUrl('')
363322
} catch (err) {
364323
logger.error('Error deleting form:', err)
365324
}
366-
}, [existingForm, deleteForm, onExistingFormChange])
325+
}, [existingForm, deleteFormMutation, workflowId])
367326

368327
if (isLoading) {
369328
return (

0 commit comments

Comments
 (0)