add browser session id to workflow run form (#2686)

This commit is contained in:
Shuchang Zheng
2025-06-11 12:49:01 -07:00
committed by GitHub
parent c1e19d27d3
commit 2d2146948a
3 changed files with 107 additions and 3 deletions

View File

@@ -0,0 +1,23 @@
import { useEffect } from "react";
import { UseFormReturn, FieldValues, WatchObserver } from "react-hook-form";
/**
* Syncs a form field value to localStorage whenever it changes.
* @param form - A react-hook-form object with a .watch method
* @param fieldName - The name of the field to watch
* @param storageKey - The key to write to in localStorage
*/
export function useSyncFormFieldToStorage<T extends FieldValues>(
form: UseFormReturn<T>,
fieldName: keyof T & string,
storageKey: string,
) {
useEffect(() => {
const subscription = form.watch(((value, { name }) => {
if (name === fieldName && typeof value[fieldName] === "string") {
localStorage.setItem(storageKey, value[fieldName] as string);
}
}) as WatchObserver<T>);
return () => subscription.unsubscribe();
}, [form, fieldName, storageKey]);
}