Files
triggerdotdev--trigger.dev/apps/webapp/app/hooks/useChanged.ts
T
Chris Arderne 176fb6daf4 fix(webapp): call hooks directly and unconditionally (#4715)
## Summary

Calls dashboard hooks directly instead of passing them as ordinary
callback values, and subscribes to optional Ariakit stores through an
unconditional hook. This keeps hook ordering stable while preserving the
existing behavior when a provider is absent.
2026-08-20 09:59:39 +01:00

30 lines
887 B
TypeScript

import { useEffect, useRef } from "react";
/** Call a function when the id of the item changes */
export function useChanged<T extends { id: string }>(
item: T | undefined,
action: (item: T | undefined) => void,
sendInitialUndefined = true
) {
const previousItemId = useRef<string | undefined>();
const isInitialRender = useRef(true);
const actionRef = useRef(action);
const itemRef = useRef<T | undefined>();
const itemId = item?.id;
actionRef.current = action;
itemRef.current = item;
useEffect(() => {
const shouldSendInitialUndefined =
isInitialRender.current && itemId === undefined && sendInitialUndefined;
if (previousItemId.current !== itemId || shouldSendInitialUndefined) {
actionRef.current(itemRef.current);
}
previousItemId.current = itemId;
isInitialRender.current = false;
}, [itemId, sendInitialUndefined]);
}