-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add experimental workspace parameters page for dynamic params #17841
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
dd83a00
f19178c
0c7f4c1
17a2c85
93359a0
82e0b07
e827b2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type * as TypesGen from "api/typesGenerated"; | ||
import { useEffect, useRef } from "react"; | ||
|
||
import type { PreviewParameter } from "api/typesGenerated"; | ||
|
||
type UseSyncFormParametersProps = { | ||
parameters: readonly PreviewParameter[]; | ||
formValues: readonly TypesGen.WorkspaceBuildParameter[]; | ||
setFieldValue: ( | ||
field: string, | ||
value: TypesGen.WorkspaceBuildParameter[], | ||
) => void; | ||
}; | ||
|
||
export function useSyncFormParameters({ | ||
parameters, | ||
formValues, | ||
setFieldValue, | ||
}: UseSyncFormParametersProps) { | ||
// Form values only needs to be updated when parameters change | ||
// Keep track of form values in a ref to avoid unnecessary updates to rich_parameter_values | ||
const formValuesRef = useRef(formValues); | ||
|
||
useEffect(() => { | ||
formValuesRef.current = formValues; | ||
}, [formValues]); | ||
|
||
useEffect(() => { | ||
if (!parameters) return; | ||
const currentFormValues = formValuesRef.current; | ||
|
||
const newParameterValues = parameters.map((param) => { | ||
return { | ||
name: param.name, | ||
value: param.value.valid ? param.value.value : "", | ||
}; | ||
}); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const isChanged = | ||
currentFormValues.length !== newParameterValues.length || | ||
newParameterValues.some( | ||
(p) => | ||
!currentFormValues.find( | ||
(formValue) => | ||
formValue.name === p.name && formValue.value === p.value, | ||
), | ||
); | ||
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. this is O(n^2), but could pretty easily be rewritten to be O(n) using I guess it was already like this tho 🤷♀️ 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. Good call out, I can improve. |
||
|
||
if (isChanged) { | ||
setFieldValue("rich_parameter_values", newParameterValues); | ||
} | ||
}, [parameters, setFieldValue]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't a very general purpose hook. could we put this in modules/workspaces or something instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hook will eventually be used a many different places, TemplateEmbedPage, WorkspaceSettingsPage, CreateWorkspacePage, UpdateBuildParametersDialog, BuildParametersPopover, WorkspaceParametersForm. Its not general purpose but it also doesn't feel like it should be buried in a subfolder. Maybe we don't currently have a good place for things like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to modules/hooks, modules are the place to gather all coder business logic that is not in pages so seems like the best place to put coder specific non-general purpose hooks