Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

refactor(lab): one-line ref effects in Comments and Schedule (#10805)

Christopher Chedeaub5de1a82f044
diff --git a/packages/lab/src/Comments/CommentComposer.tsx b/packages/lab/src/Comments/CommentComposer.tsx
index 4ac8dd8a6a..c69225bb89 100644
--- a/packages/lab/src/Comments/CommentComposer.tsx
+++ b/packages/lab/src/Comments/CommentComposer.tsx
@@ -236,20 +236,26 @@ export function CommentComposerBase({
: [...hostTriggers, agentCommandTrigger],
[agentCommandTrigger, hostTriggers],
);
+ // The host owns these props, so no handler here sees each change. Each ref
+ // is written at commit: a render React discards must not leave its value.
const latestValueRef = useRef(value);
+ // Syncs the handlers' draft with the committed `value`.
+ useLayoutEffect(() => {
+ latestValueRef.current = value;
+ });
const isMountedRef = useRef(true);
const actionPendingRef = useRef(false);
// The latest callback, read when reporting. An inline callback is new on
// every render; depending on it would re-run the unmount cleanup below on
// each render and leave the composer busy after its upload ends.
const onActionPendingChangeRef = useRef(onActionPendingChange);
- const isSubmittingRef = useRef(isSubmitting);
- // Syncs the handlers' refs with the committed props. The host owns these
- // props, so no handler here sees each change. A render React discards, as
- // a suspended transition does, must not leave its values.
+ // Syncs the reporting callback with the committed prop.
useLayoutEffect(() => {
- latestValueRef.current = value;
onActionPendingChangeRef.current = onActionPendingChange;
+ });
+ const isSubmittingRef = useRef(isSubmitting);
+ // Syncs the busy guard with the committed `isSubmitting`.
+ useLayoutEffect(() => {
isSubmittingRef.current = isSubmitting;
});
const imageInputRef = useRef<HTMLInputElement>(null);
diff --git a/packages/lab/src/Comments/useCommentThreadDismissal.ts b/packages/lab/src/Comments/useCommentThreadDismissal.ts
index 51ace43b63..85db45e199 100644
--- a/packages/lab/src/Comments/useCommentThreadDismissal.ts
+++ b/packages/lab/src/Comments/useCommentThreadDismissal.ts
@@ -324,16 +324,15 @@ export function useCommentThreadDismissal({
// through this ref, as the composer's own listeners do. The ref is written
// at commit: a render React discards, as a suspended transition does, must
// not leave its draft for the listeners.
+ const wouldGuard = () =>
+ draft != null && guardedSignatureRef.current !== draft.signature;
const latestRef = useRef<{
request: () => RequestOutcome;
wouldGuard: () => boolean;
} | null>(null);
+ // Syncs the document listeners' draft with the committed render.
useLayoutEffect(() => {
- latestRef.current = {
- request,
- wouldGuard: () =>
- draft != null && guardedSignatureRef.current !== draft.signature,
- };
+ latestRef.current = {request, wouldGuard};
});
const {shouldDismissOnCloseRequest} = useLayerDismissal({
diff --git a/packages/lab/src/Schedule/TimeGridView.tsx b/packages/lab/src/Schedule/TimeGridView.tsx
index f89e89c0f4..f469ecb35a 100644
--- a/packages/lab/src/Schedule/TimeGridView.tsx
+++ b/packages/lab/src/Schedule/TimeGridView.tsx
@@ -179,20 +179,16 @@ export function TimeGridView({
timezoneID,
],
);
- const positioning = useRef({
+ const latestPositioning = {
currentTime,
days,
minHour: normalizedMinHour,
hourHeight,
- });
+ };
+ const positioning = useRef(latestPositioning);
// Syncs the positioning effect's inputs at commit, before it reads them.
useLayoutEffect(() => {
- positioning.current = {
- currentTime,
- days,
- minHour: normalizedMinHour,
- hourHeight,
- };
+ positioning.current = latestPositioning;
});
const isBlockScrollable = scrollState.block.isScrollable;
// The clock is 0 only in the server snapshot; wait for the client's.