Skip to content

Commit de68dbb

Browse files
Merge main into takeover/pr-24949
2 parents 7a8a916 + 9296c20 commit de68dbb

121 files changed

Lines changed: 5102 additions & 3061 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Architecture and its constraints: `docs/internals/overview.md`. Glossary: `docs/
194194
options, and pass combinators as additional function-builder arguments.
195195
- Inferred types over annotations. `any` is the enemy.
196196
- Default to no comment. Write one only for a constraint, an external quirk, or an invariant the code can't show, in one line where you can and four at most. Don't restate names or signatures, narrate steps, record history, or explain the fix you just made; the PR carries that. `t3code/no-low-value-comments` enforces the length cap and the mechanical cases.
197-
- React effects are a last resort: derive in render, act in handlers, reset with `key`, or use a ref callback or store. The `t3code/react-effect-budget` lint rule holds each file to its entry in `scripts/effect-budget.json`; raise an entry only in the PR that needs the effect, and lower it when you remove one.
197+
- React effects are for synchronizing with something outside React. Derive in render and act in handlers instead of syncing state, but don't hide real lifecycle work in refs or stores to dodge one. The `t3code/react-effect-budget` lint rule holds each file to its count in `scripts/effect-budget.json`; a new effect states its reason with `// oxlint-disable-next-line t3code/react-effect-budget -- <why>`.
198198
- Our users drive agents all day and notice a dropped frame, a lying spinner, and a stale label. No continuously repainting animations; they peg the GPU on high-refresh displays.
199199
- If a rule here fights the task in front of you, say so loudly and get a human sign-off before breaking it.
200200

‎apps/mobile/src/components/ui/AppBar.android.tsx‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import { useCallback, useEffect, useRef, useState } from "react";
2-
import {
3-
BackHandler,
4-
Keyboard,
5-
Pressable,
6-
TextInput,
7-
type TextInputInstance,
8-
View,
9-
} from "react-native";
1+
import { useCallback, useRef, useState } from "react";
2+
import { Keyboard, Pressable, TextInput, type TextInputInstance, View } from "react-native";
3+
import { useHardwareBack } from "../../lib/backHandler";
104
import { useToolbarLayout } from "./useToolbarLayout";
115
import { NativeStackScreenOptions } from "../../native/StackHeader";
126
import { AndroidWorkspaceSidebarButton } from "../../features/layout/workspace-sidebar-toolbar";
@@ -34,14 +28,10 @@ export function AppBar(props: AppBarProps) {
3428
setSearchOpen(false);
3529
Keyboard.dismiss();
3630
}, [onSearchChange]);
37-
useEffect(() => {
38-
if (!searching || search?.mode === "inline") return;
39-
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
40-
closeSearch();
41-
return true;
42-
});
43-
return () => subscription.remove();
44-
}, [closeSearch, searching, search?.mode]);
31+
useHardwareBack(searching && search?.mode !== "inline", () => {
32+
closeSearch();
33+
return true;
34+
});
4535
const menuView = props.menus?.map((menu) => (
4636
<Menu
4737
key={menu.title}

‎apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { EnvironmentId, ThreadId, type PreviewSessionSnapshot } from "@t3tools/c
77
import { normalizePreviewUrl, previewPdfAssetUrl } from "@t3tools/shared/preview";
88
import * as Option from "effect/Option";
99
import { useCallback, useEffect, useRef, useState } from "react";
10-
import { AppState, Platform, TextInput, View } from "react-native";
10+
import { Platform, TextInput, View } from "react-native";
1111
import { useSafeAreaInsets } from "react-native-safe-area-context";
1212

1313
import { ControlPill } from "../../components/ui/ControlPill";
14+
import { useAppForeground } from "../../lib/appState";
1415
import { AppBar } from "../../components/ui/AppBar";
1516

1617
import { useThreadServerBrowserTabs } from "../../state/preview";
@@ -79,7 +80,7 @@ function BrowserPreviewScreen({
7980
const prepared = usePreparedConnection(environmentId);
8081
const httpBaseUrl = Option.isSome(prepared) ? prepared.value.httpBaseUrl : null;
8182
const focused = useIsFocused();
82-
const [foreground, setForeground] = useState(AppState.currentState !== "background");
83+
const foreground = useAppForeground();
8384
const [selectedTabId, setSelectedTabId] = useState(initialTabId);
8485
const [pictureInPicture, setPictureInPicture] = useState(NO_PICTURE_IN_PICTURE);
8586
const [streaming, setStreaming] = useState(false);
@@ -94,12 +95,6 @@ function BrowserPreviewScreen({
9495
setPictureInPicture(NO_PICTURE_IN_PICTURE);
9596
setSelectedTabId(tabId);
9697
}
97-
useEffect(() => {
98-
const subscription = AppState.addEventListener("change", (state) =>
99-
setForeground(state !== "background"),
100-
);
101-
return () => subscription.remove();
102-
}, []);
10398
useEffect(() => {
10499
if (focused && loaded && tabs.length === 0) onClose();
105100
}, [focused, loaded, tabs.length, onClose]);

‎apps/mobile/src/features/browser/ThreadBrowserFloat.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import type { EnvironmentId, PreviewSessionSnapshot, ThreadId } from "@t3tools/c
33
import { previewPdfAssetUrl } from "@t3tools/shared/preview";
44
import * as Option from "effect/Option";
55
import { useCallback, useEffect, useRef, useState } from "react";
6-
import { AppState, Pressable, View } from "react-native";
6+
import { Pressable, View } from "react-native";
77
import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";
88

99
import { SymbolView } from "../../components/ui/AppSymbol";
10+
import { useAppForeground } from "../../lib/appState";
1011
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
1112
import { browserTabTitle } from "./browserTabs";
1213
import { PreviewStreamWebView } from "./PreviewStreamWebView";
@@ -41,13 +42,7 @@ export function ThreadBrowserFloat(props: {
4142
if (opened !== undefined) setTabId(opened.tabId);
4243
}, [props.loaded, props.tabs]);
4344
const focused = useIsFocused();
44-
const [foreground, setForeground] = useState(AppState.currentState !== "background");
45-
useEffect(() => {
46-
const subscription = AppState.addEventListener("change", (state) =>
47-
setForeground(state !== "background"),
48-
);
49-
return () => subscription.remove();
50-
}, []);
45+
const foreground = useAppForeground();
5146
const tab = props.tabs.find((entry) => entry.tabId === tabId) ?? null;
5247
const onOpen = props.onOpen;
5348
const open = useCallback(() => {

‎apps/mobile/src/features/devices/DevicePreviewRouteScreen.tsx‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as Cause from "effect/Cause";
1212
import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
1313
import * as Haptics from "expo-haptics";
1414
import { Accelerometer } from "expo-sensors";
15-
import { AppState, BackHandler, Pressable, StatusBar, View } from "react-native";
15+
import { Pressable, StatusBar, View } from "react-native";
1616
import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";
1717
import { useSafeAreaInsets } from "react-native-safe-area-context";
1818

@@ -27,6 +27,8 @@ import {
2727
findHeaderMenuAction,
2828
} from "../../components/ui/headerMenu.android";
2929
import type { AppBarMenuItem } from "../../components/ui/AppBar.types";
30+
import { useAppForeground } from "../../lib/appState";
31+
import { useHardwareBack } from "../../lib/backHandler";
3032
import { deriveBottomControlInsets } from "../../lib/reserved-regions";
3133
import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics";
3234
import { deviceEnvironment, refreshDeviceHubAccess, useDeviceHubAccess } from "../../state/device";
@@ -88,7 +90,7 @@ export function DevicePreviewScreen({
8890
const handleBounds = deriveBottomControlInsets(useNativeColumnLayoutMetrics());
8991
const { themeVariables } = useAppearancePreferences();
9092
const focused = useIsFocused();
91-
const [foreground, setForeground] = useState(AppState.currentState !== "background");
93+
const foreground = useAppForeground();
9294
const [selectedKey, setSelectedKey] = useState<string | null>(initialDeviceKey ?? null);
9395
const [inputConnected, setInputConnected] = useState(false);
9496
const [streamAttempt, setStreamAttempt] = useState(0);
@@ -107,12 +109,6 @@ export function DevicePreviewScreen({
107109
},
108110
[embedded],
109111
);
110-
useEffect(() => {
111-
const subscription = AppState.addEventListener("change", (state) =>
112-
setForeground(state !== "background"),
113-
);
114-
return () => subscription.remove();
115-
}, []);
116112
useEffect(() => {
117113
if (focused && state.data !== null && previews.length === 0) onClose();
118114
}, [focused, state.data, previews.length, onClose]);
@@ -127,14 +123,10 @@ export function DevicePreviewScreen({
127123
});
128124
return () => subscription.remove();
129125
}, [focused, foreground, embedded]);
130-
useEffect(() => {
131-
if (controlsVisible) return;
132-
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
133-
setControlsVisible(true);
134-
return true;
135-
});
136-
return () => subscription.remove();
137-
}, [controlsVisible]);
126+
useHardwareBack(!controlsVisible, () => {
127+
setControlsVisible(true);
128+
return true;
129+
});
138130

139131
const shutDownDevice = async () => {
140132
if (!preview || shuttingDown) return;

‎apps/mobile/src/features/files/FilesHeader.tsx‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
2-
import { BackHandler, Keyboard, type TextInputInstance, View } from "react-native";
1+
import { useCallback, useRef, useState, type ReactNode } from "react";
2+
import { Keyboard, type TextInputInstance, View } from "react-native";
33

44
import {
55
AndroidHeaderIconButton,
@@ -8,6 +8,7 @@ import {
88
import { AndroidAnchoredMenu } from "../../components/ui/AndroidAnchoredMenu";
99
import { SearchField } from "../../components/ui/SearchField";
1010
import { useToolbarLayout } from "../../components/ui/useToolbarLayout";
11+
import { useHardwareBack } from "../../lib/backHandler";
1112

1213
export function FilesHeader(props: {
1314
readonly projectName: string;
@@ -28,14 +29,10 @@ export function FilesHeader(props: {
2829
Keyboard.dismiss();
2930
}, [onSearchQueryChange]);
3031

31-
useEffect(() => {
32-
if (!searching) return;
33-
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
34-
closeSearch();
35-
return true;
36-
});
37-
return () => subscription.remove();
38-
}, [closeSearch, searching]);
32+
useHardwareBack(searching, () => {
33+
closeSearch();
34+
return true;
35+
});
3936

4037
return (
4138
<View>

‎apps/mobile/src/features/files/WorkspaceFileWebPreview.tsx‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useEffect, useState } from "react";
2-
import { BackHandler, Platform, View } from "react-native";
1+
import { useState } from "react";
2+
import { Platform, View } from "react-native";
33
import { WebView } from "react-native-webview";
44
import { SafeAreaView, useSafeAreaInsets } from "react-native-safe-area-context";
55

@@ -9,6 +9,7 @@ import { FilePreviewLoading } from "./FilePreviewFeedback";
99
import { LoadingStrip } from "../../components/ui/LoadingStrip";
1010
import { PdfPreview } from "../../components/PdfPreview";
1111
import { ControlPill } from "../../components/ui/ControlPill";
12+
import { useHardwareBack } from "../../lib/backHandler";
1213
import { isPdfPreviewUri } from "../../lib/pdfPreview";
1314
import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics";
1415

@@ -27,14 +28,10 @@ function DocumentWebPreview(props: { readonly uri: string }) {
2728
const [loadError, setLoadError] = useState<string | null>(null);
2829
const [linkedPdf, setLinkedPdf] = useState<string | null>(null);
2930

30-
useEffect(() => {
31-
if (linkedPdf === null) return;
32-
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
33-
setLinkedPdf(null);
34-
return true;
35-
});
36-
return () => subscription.remove();
37-
}, [linkedPdf]);
31+
useHardwareBack(linkedPdf !== null, () => {
32+
setLinkedPdf(null);
33+
return true;
34+
});
3835

3936
if (isPdfPreviewUri(props.uri)) {
4037
return (

‎apps/mobile/src/features/terminal/NativeTerminalSurface.tsx‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { TerminalOutputState } from "@t3tools/client-runtime/state/terminal";
2-
import { memo, useCallback, useEffect, useRef, useState } from "react";
2+
import {
3+
memo,
4+
useCallback,
5+
useEffect,
6+
useImperativeHandle,
7+
useRef,
8+
useState,
9+
type Ref,
10+
} from "react";
311
import {
412
Pressable,
513
ScrollView,
@@ -38,7 +46,13 @@ interface TerminalResizeEvent {
3846
readonly rows: number;
3947
}
4048

49+
export interface TerminalSurfaceHandle {
50+
/** The text fallback focuses its input; the native view follows `keyboardFocusRequest`. */
51+
readonly focusKeyboard: () => void;
52+
}
53+
4154
interface TerminalSurfaceProps extends ViewProps {
55+
readonly ref?: Ref<TerminalSurfaceHandle>;
4256
readonly terminalKey: string;
4357
readonly buffer: string | null;
4458
readonly output: TerminalOutputState | null;
@@ -71,9 +85,10 @@ function estimateGridSize(input: {
7185
};
7286
}
7387

74-
const FallbackTerminalSurface = memo(function FallbackTerminalSurface(
75-
props: ReadyTerminalSurfaceProps,
76-
) {
88+
const FallbackTerminalSurface = memo(function FallbackTerminalSurface({
89+
ref,
90+
...props
91+
}: ReadyTerminalSurfaceProps) {
7792
const fontSize = props.fontSize ?? MOBILE_TYPOGRAPHY.label.fontSize;
7893
const inputRef = useRef<TextInputInstance>(null);
7994
const { themeAppearance, themeId } = useAppearancePreferences();
@@ -89,15 +104,14 @@ const FallbackTerminalSurface = memo(function FallbackTerminalSurface(
89104
props.onResize(estimateGridSize({ width, height, fontSize }));
90105
};
91106

92-
useEffect(() => {
93-
if (!props.readOnly && (props.keyboardFocusRequest ?? 0) > 0) {
107+
useImperativeHandle(ref, () => ({
108+
focusKeyboard: () => {
109+
if (props.readOnly) return;
110+
// Blur first so a focused input still raises the keyboard; focus lands after the blur commits.
94111
inputRef.current?.blur();
95-
const focusFrame = requestAnimationFrame(() => inputRef.current?.focus());
96-
return () => cancelAnimationFrame(focusFrame);
97-
}
98-
99-
return undefined;
100-
}, [props.keyboardFocusRequest, props.readOnly]);
112+
requestAnimationFrame(() => inputRef.current?.focus());
113+
},
114+
}));
101115

102116
return (
103117
<View

0 commit comments

Comments
 (0)