- Notifications
You must be signed in to change notification settings - Fork929
feat: setup connection to dynamic parameters websocket#17393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
7b61485
f5d4a1c
0fc4289
d109874
7c13eb7
4290284
74084fb
05adc15
b2c662a
2da7d99
98dfee2
d1ada89
e04ce2f
a6f480d
2613100
1e66a71
9a9201e
1777ca9
440fedc
4f056af
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1009,10 +1009,29 @@ class ApiMethods { | ||
return response.data; | ||
}; | ||
templateVersionDynamicParameters = ( | ||
versionId: string, | ||
{ | ||
onMessage, | ||
onError, | ||
}: { | ||
onMessage: (response: TypesGen.DynamicParametersResponse) => void; | ||
onError: (error: Error) => void; | ||
}, | ||
): WebSocket => { | ||
const socket = createWebSocket( | ||
`/api/v2/templateversions/${versionId}/dynamic-parameters`, | ||
); | ||
socket.addEventListener("message", (event) => | ||
onMessage(JSON.parse(event.data) as TypesGen.DynamicParametersResponse), | ||
); | ||
socket.addEventListener("error", () => { | ||
onError?.(new Error("Connection for dynamic parameters failed.")); | ||
socket.close(); | ||
}); | ||
jaaydenh marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return socket; | ||
}; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -49,7 +49,7 @@ const CreateWorkspacePageExperimental: FC = () => { | ||
const [currentResponse, setCurrentResponse] = | ||
useState<DynamicParametersResponse | null>(null); | ||
const [wsResponseId, setWSResponseId] = useState<number>(-1); | ||
constws = useRef<WebSocket | null>(null); | ||
const customVersionId = searchParams.get("version") ?? undefined; | ||
const defaultName = searchParams.get("name"); | ||
@@ -81,58 +81,54 @@ const CreateWorkspacePageExperimental: FC = () => { | ||
const realizedVersionId = | ||
customVersionId ?? templateQuery.data?.active_version_id; | ||
const onMessage = useCallback((response: DynamicParametersResponse) => { | ||
setCurrentResponse((prev) => { | ||
if (prev?.id === response.id) { | ||
return prev; | ||
} | ||
return response; | ||
}); | ||
}, []); | ||
// Initialize the WebSocket connection when there is a valid template version ID | ||
useEffect(() => { | ||
if (!realizedVersionId) { | ||
return; | ||
} | ||
if (ws.current) { | ||
ws.current.close(); | ||
} | ||
const socket = API.templateVersionDynamicParameters(realizedVersionId, { | ||
onMessage, | ||
onError: (error) => { | ||
console.error("Failed to parse dynamic parameters webSocket message:", error); | ||
}, | ||
}); | ||
ws.current = socket; | ||
return () => { | ||
if (ws.current) { | ||
ws.current.close(); | ||
} | ||
}; | ||
}, [realizedVersionId, onMessage]); | ||
const sendMessage = (formValues: Record<string, string>) => { | ||
setWSResponseId((prevId) => { | ||
const request: DynamicParametersRequest = { | ||
id: prevId + 1, | ||
inputs: formValues, | ||
}; | ||
if (ws.current && ws.current.readyState === WebSocket.OPEN) { | ||
ws.current.send(JSON.stringify(request)); | ||
return prevId + 1; | ||
} | ||
return prevId; | ||
}); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. one last thought, I noticed that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. onMessage was wrapped because its used as a dependency for a useEffect. Sure I can wrap sendMessage as well. | ||
const organizationId = templateQuery.data?.organization_id; | ||
@@ -143,7 +139,7 @@ const CreateWorkspacePageExperimental: FC = () => { | ||
isLoadingExternalAuth, | ||
} = useExternalAuth(realizedVersionId); | ||
const isLoadingFormData = ws.current?.readyState !== WebSocket.OPEN || | ||
templateQuery.isLoading || permissionsQuery.isLoading; | ||
const loadFormDataError = templateQuery.error ?? permissionsQuery.error; | ||