Skip to content
TypeScript

chore(lint): enforce react/refs (#25116)

Julius Marmingebeeb2159e132
diff --git a/apps/mobile/src/components/ui/Menu.tsx b/apps/mobile/src/components/ui/Menu.tsx
index 54fb0bec95f..17757fe84b3 100644
--- a/apps/mobile/src/components/ui/Menu.tsx
+++ b/apps/mobile/src/components/ui/Menu.tsx
@@ -51,6 +51,7 @@ export function Menu(props: MenuProps) {
let children = menuProps.children;
if (props.shouldOpenOnLongPress && isValidElement(children)) {
const child = children as ReactElement<Pick<PressableProps, "onTouchStart" | "onPress">>;
+ /* oxlint-disable react/refs -- Cloning the trigger installs event callbacks; the refs are accessed when those callbacks run. */
children = cloneElement(child, {
onTouchStart: (event) => {
// onPressIn can repeat within one gesture when a finger leaves and re-enters the row.
@@ -74,6 +75,7 @@ export function Menu(props: MenuProps) {
child.props.onPress?.(event);
},
});
+ /* oxlint-enable react/refs */
menuProps.onMenuInteractionStart = () => {
menuPress.current.isPreparing = true;
props.onMenuInteractionStart?.();
diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
index 331d7ec7eb5..23ca0675432 100644
--- a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
+++ b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
@@ -387,6 +387,7 @@ function NativeReviewDiffContent(
return null;
}
+ /* oxlint-disable react/refs -- The native view receives this ref; only event callbacks access its current value. */
return createElement(RawNativeView, {
...nativeProps,
refreshEnabled: props.onPullToRefresh !== undefined,
@@ -399,6 +400,7 @@ function NativeReviewDiffContent(
},
ref: nativeRef,
});
+ /* oxlint-enable react/refs */
}
export function resolveNativeReviewDiffView(): ComponentType<NativeReviewDiffViewProps> | null {
diff --git a/apps/mobile/src/features/home/thread-swipe-actions.tsx b/apps/mobile/src/features/home/thread-swipe-actions.tsx
index e75b29a8805..0ad078f73b5 100644
--- a/apps/mobile/src/features/home/thread-swipe-actions.tsx
+++ b/apps/mobile/src/features/home/thread-swipe-actions.tsx
@@ -506,7 +506,11 @@ export function ThreadSwipeable(props: ThreadSwipeableProps) {
rightThreshold={actionsWidth * 0.42}
simultaneousWith={props.simultaneousWith}
>
- {props.children(close)}
+ {
+ /* oxlint-disable react/refs -- The render callback receives a close/open handler; only invoking that handler accesses the ref. */
+ props.children(close)
+ /* oxlint-enable react/refs */
+ }
</ReanimatedSwipeable>
</View>
</Animated.View>
diff --git a/apps/mobile/src/features/threads/ThreadArrangementSheet.tsx b/apps/mobile/src/features/threads/ThreadArrangementSheet.tsx
index 8c6df5a7715..e77c046cca7 100644
--- a/apps/mobile/src/features/threads/ThreadArrangementSheet.tsx
+++ b/apps/mobile/src/features/threads/ThreadArrangementSheet.tsx
@@ -102,6 +102,7 @@ function DragHandle(props: {
canMoveDown: boolean;
}) {
const latest = useRef(props);
+ /* oxlint-disable react/refs -- The pan gesture captures its callbacks once per `disabled` change; they read the latest handlers when native gesture events fire. */
latest.current = props;
const gesture = useMemo(
() =>
@@ -117,6 +118,7 @@ function DragHandle(props: {
.onFinalize((_, success) => latest.current.onEnd(!success)),
[props.disabled, latest],
);
+ /* oxlint-enable react/refs */
return (
<GestureDetector gesture={gesture}>
<View
@@ -271,8 +273,9 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
const drag = useRef<Drag | null>(null);
const frame = useRef<number | null>(null);
const [preview, setPreview] = useState<Drag | null>(null);
- const translateY = useRef(new Animated.Value(0)).current;
+ const [translateY] = useState(() => new Animated.Value(0));
const latest = useRef({ rows, planners, moveThread });
+ // oxlint-disable-next-line react/refs -- The autoscroll rAF loop started at drag start reads the current rows and planners on every frame.
latest.current = { rows, planners, moveThread };
function stop() {
diff --git a/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx b/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx
index 039f0545722..310e7fa8f69 100644
--- a/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx
+++ b/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx
@@ -3,7 +3,6 @@ import {
AudioModule,
RecordingPresets,
requestRecordingPermissionsAsync,
- type RecorderState,
type RecordingStatus,
} from "expo-audio";
import { File } from "expo-file-system";
@@ -35,7 +34,7 @@ import {
voiceInputBlocksSubmission,
type VoiceInputState,
} from "@t3tools/client-runtime/voice-input";
-import { createLazyVoiceRecorder, type LazyVoiceRecorder } from "./lazyVoiceRecorder";
+import { createLazyVoiceRecorder } from "./lazyVoiceRecorder";
import { normalizeVoiceInputDecibels, VOICE_WAVEFORM_SAMPLE_COUNT } from "./voiceInputMetering";
import { VoiceInputSession } from "./voiceInputSession";
import {
@@ -93,17 +92,18 @@ function useVoiceInputRuntime() {
const keepAwakeId = useId();
const keepAwakeSessionRef = useRef(0);
const elapsedSecondsRef = useRef(0);
- const audioLevelsRef = useRef(Array<number>(VOICE_WAVEFORM_SAMPLE_COUNT).fill(0));
- const audioLevels = useSharedValue(audioLevelsRef.current);
- const sessionRef = useRef<VoiceInputSession | null>(null);
- const recorderRef = useRef<LazyVoiceRecorder<RecorderState> | null>(null);
+ const [initialLevels] = useState(() => Array<number>(VOICE_WAVEFORM_SAMPLE_COUNT).fill(0));
+ const audioLevelsRef = useRef(initialLevels);
+ const audioLevels = useSharedValue(initialLevels);
const audioToken = useRef<ReturnType<typeof acquireAudioSession> | null>(null);
- if (!sessionRef.current || !recorderRef.current) {
+ /* oxlint-disable react/refs -- The initializer captures the audio-session ref for later recording callbacks; it does not read it. */
+ const [{ session, recorder }] = useState(() => {
+ let session: VoiceInputSession | null = null;
const recorder = createLazyVoiceRecorder({
create: () => new AudioModule.AudioRecorder(VOICE_RECORDING_OPTIONS),
onStatus: (status: RecordingStatus) => {
- sessionRef.current?.controller.handleRecorderStatus({
+ session?.controller.handleRecorderStatus({
isFinished: status.isFinished,
hasError: status.hasError || status.mediaServicesDidReset === true,
error: status.error,
@@ -126,8 +126,7 @@ function useVoiceInputRuntime() {
return preferredMicrophoneInput(inputs, order);
},
});
- recorderRef.current = recorder;
- sessionRef.current = new VoiceInputSession({
+ session = new VoiceInputSession({
recorder,
getTranscriber: getLocalVoiceTranscriber,
requestPermission: async () => {
@@ -152,15 +151,15 @@ function useVoiceInputRuntime() {
onStateChange: (nextState) =>
setState({
state: nextState,
- ownerKey: sessionRef.current?.ownerKey ?? null,
- label: sessionRef.current?.label ?? null,
+ ownerKey: session?.ownerKey ?? null,
+ label: session?.label ?? null,
}),
});
- }
+ return { session, recorder };
+ });
+ /* oxlint-enable react/refs */
- const session = sessionRef.current;
const controller = session.controller;
- const recorder = recorderRef.current;
useEffect(() => {
const subscription = AppState.addEventListener("change", (nextState) => {
diff --git a/apps/mobile/src/features/voice-input/useVoiceInputController.ts b/apps/mobile/src/features/voice-input/useVoiceInputController.ts
index 153de8a3342..bd3bdc40a52 100644
--- a/apps/mobile/src/features/voice-input/useVoiceInputController.ts
+++ b/apps/mobile/src/features/voice-input/useVoiceInputController.ts
@@ -24,6 +24,7 @@ export function useVoiceInputController(input: {
const global = useGlobalVoiceInput();
const { setOwnerFocused, session } = global;
const latestInput = useRef(input);
+ // oxlint-disable-next-line react/refs -- Transcription finishes asynchronously and must reach the composer's current selection setter, not the one captured at start.
latestInput.current = input;
const mounted = useRef(true);
useEffect(() => {
diff --git a/apps/mobile/src/native/T3ComposerEditor.ios.tsx b/apps/mobile/src/native/T3ComposerEditor.ios.tsx
index 751cb546f5c..49e75ac7652 100644
--- a/apps/mobile/src/native/T3ComposerEditor.ios.tsx
+++ b/apps/mobile/src/native/T3ComposerEditor.ios.tsx
@@ -139,7 +139,19 @@ export function ComposerEditor({
const [, forceNativeEventRender] = useState(0);
// Start history empty so restored drafts or recycled views receive a non-echo controlled payload.
const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
- const confirmedTokensRef = useRef(collectComposerInlineTokens(props.value));
+ // Tokens advance from the previous committed value; render-phase state keeps
+ // an abandoned render from leaking a trailing token into the next one.
+ const [confirmedTokens, setConfirmedTokens] = useState(() => ({
+ value: props.value,
+ tokens: collectComposerInlineTokens(props.value),
+ }));
+ let tokens = confirmedTokens.tokens;
+ if (confirmedTokens.value !== props.value) {
+ tokens = collectComposerInlineTokens(props.value, {
+ preserveTrailingFrom: confirmedTokens.tokens,
+ });
+ setConfirmedTokens({ value: props.value, tokens });
+ }
const bodyText = useScaledTextRole("body");
const theme = useUniwindTheme();
const fontFamily = FONT_FAMILIES.regular;
@@ -158,10 +170,6 @@ export function ComposerEditor({
const skillLabels = useMemo(() => composerSkillLabels(skills), [skills]);
const chipTextColor = createNativeComposerTheme(theme).text;
const tokensJson = useMemo(() => {
- const tokens = collectComposerInlineTokens(props.value, {
- preserveTrailingFrom: confirmedTokensRef.current,
- });
- confirmedTokensRef.current = tokens;
return JSON.stringify(
composerContextEditorTokens(props.value, tokens, skillLabels).map((token) => {
const record =
@@ -194,11 +202,12 @@ export function ComposerEditor({
};
}),
);
- }, [props.value, props.context, skillLabels, theme, chipTextColor, confirmedTokensRef]);
+ }, [props.value, tokens, props.context, skillLabels, theme, chipTextColor]);
// Every render resolves against the snapshot history, so a render whose
// (value, selection) lags the acknowledged native state is stamped behind
// the native revision and rejected by the editor instead of re-applying a
// stale caret or stale text mid-typing.
+ /* oxlint-disable react/refs -- Native controlled edits must reconcile against the last acknowledged native event before sending the next payload. */
const controlledEventCount = resolveComposerControlledEventCount(
props.value,
selection ?? null,
@@ -211,6 +220,7 @@ export function ComposerEditor({
mostRecentEventCount,
nativeEventSnapshotsRef.current,
);
+ /* oxlint-enable react/refs */
const isNativeEcho =
controlledEventCount === mostRecentEventCount && acknowledgesLatestNativeEvent;
const controlledDocumentJson = JSON.stringify({
diff --git a/apps/mobile/src/native/T3ComposerEditor.native.tsx b/apps/mobile/src/native/T3ComposerEditor.native.tsx
index b518ea2c831..f2921471901 100644
--- a/apps/mobile/src/native/T3ComposerEditor.native.tsx
+++ b/apps/mobile/src/native/T3ComposerEditor.native.tsx
@@ -137,8 +137,19 @@ export function ComposerEditor({
const [, forceNativeEventRender] = useState(0);
// Start history empty so restored drafts or recycled views receive a non-echo controlled payload.
const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
- const [initialConfirmedTokens] = useState(() => collectComposerInlineTokens(props.value));
- const confirmedTokensRef = useRef(initialConfirmedTokens);
+ // Tokens advance from the previous committed value; render-phase state keeps
+ // an abandoned render from leaking a trailing token into the next one.
+ const [confirmedTokens, setConfirmedTokens] = useState(() => ({
+ value: props.value,
+ tokens: collectComposerInlineTokens(props.value),
+ }));
+ let tokens = confirmedTokens.tokens;
+ if (confirmedTokens.value !== props.value) {
+ tokens = collectComposerInlineTokens(props.value, {
+ preserveTrailingFrom: confirmedTokens.tokens,
+ });
+ setConfirmedTokens({ value: props.value, tokens });
+ }
const theme = useUniwindTheme();
const handlePaste = useNativePaste((uris) => onPasteImages?.(uris));
@@ -156,10 +167,6 @@ export function ComposerEditor({
const skillLabels = useMemo(() => composerSkillLabels(skills), [skills]);
const chipTextColor = createNativeComposerTheme(theme).text;
const tokensJson = useMemo(() => {
- const tokens = collectComposerInlineTokens(props.value, {
- preserveTrailingFrom: confirmedTokensRef.current,
- });
- confirmedTokensRef.current = tokens;
return JSON.stringify(
composerContextEditorTokens(props.value, tokens, skillLabels).map((token) => {
const record =
@@ -192,11 +199,12 @@ export function ComposerEditor({
};
}),
);
- }, [props.value, props.context, skillLabels, theme, chipTextColor, confirmedTokensRef]);
+ }, [props.value, tokens, props.context, skillLabels, theme, chipTextColor]);
// Every render resolves against the snapshot history, so a render whose
// (value, selection) lags the acknowledged native state is stamped behind
// the native revision and rejected by the editor instead of re-applying a
// stale caret or stale text mid-typing.
+ /* oxlint-disable react/refs -- Native controlled edits must reconcile against the last acknowledged native event before sending the next payload. */
const controlledEventCount = resolveComposerControlledEventCount(
props.value,
selection ?? null,
@@ -209,6 +217,7 @@ export function ComposerEditor({
mostRecentEventCount,
nativeEventSnapshotsRef.current,
);
+ /* oxlint-enable react/refs */
const isNativeEcho =
controlledEventCount === mostRecentEventCount && acknowledgesLatestNativeEvent;
const controlledDocumentJson = JSON.stringify({
diff --git a/vite.config.ts b/vite.config.ts
index 1352189136e..131f6b4ce60 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -228,6 +228,7 @@ export default defineConfig({
"react/jsx-no-constructed-context-values": "off",
"react/memo-dependencies": "error",
"react/purity": "error",
+ "react/refs": "error",
"react/immutability": "error",
"react/no-array-index-key": "error",
"react/no-unstable-nested-components": "error",