Skip to content
TypeScript

perf(mobile): settings screens compile with the React Compiler (#21119)

Julius Marminge93fbedb50c3f
diff --git a/apps/mobile/src/features/settings/SettingsAboutRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsAboutRouteScreen.tsx
index 912ee495fa9..4bc723eb6e8 100644
--- a/apps/mobile/src/features/settings/SettingsAboutRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsAboutRouteScreen.tsx
@@ -1,7 +1,8 @@
import * as Dialog from "../../components/ui/Dialog";
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
import Constants from "expo-constants";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { settlePromise } from "@t3tools/client-runtime/state/runtime";
+import { useEffect, useRef, useState } from "react";
import { Pressable, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -56,30 +57,30 @@ function AppSettingsSection() {
return () => clearTimeout(timer);
}, [updateState]);
- const checkForUpdate = useCallback(async () => {
+ async function checkForUpdate() {
// `disabled={busy}` only takes effect on the next render, so two taps in the
// same frame would both get through. The ref closes that window.
if (updateInFlight.current) return;
updateInFlight.current = true;
- try {
- await runAppUpdateCheck({
+ // Failures arrive through onFailure; the settled result only matters for unlocking.
+ await settlePromise(() =>
+ runAppUpdateCheck({
applyMode: "immediate",
onFailure: (message) => Dialog.alert("Update failed", message),
onStateChange: setUpdateState,
- });
- } finally {
- updateInFlight.current = false;
- }
- }, []);
+ }),
+ );
+ updateInFlight.current = false;
+ }
- const handleVersionPress = useCallback(() => {
+ function handleVersionPress() {
if (!updateCheckAvailable || updateInFlight.current) return;
const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
hiddenUpdateTapCount.current = tap.nextCount;
if (tap.shouldCheck) {
void checkForUpdate();
}
- }, [checkForUpdate, updateCheckAvailable]);
+ }
const statusLabel =
updateState === "checking"
diff --git a/apps/mobile/src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx
index 5d2cfc00fd6..ea5eb2b8ec6 100644
--- a/apps/mobile/src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx
@@ -7,7 +7,7 @@ import {
type EnvironmentId,
type ServerProvider,
} from "@t3tools/contracts";
-import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
+import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import { AsyncResult } from "effect/reactivity";
import { useEffect, useRef, useState } from "react";
import { View } from "react-native";
@@ -96,15 +96,14 @@ function EnvironmentDetail({ environmentId }: { readonly environmentId: Environm
setPending(label);
setError(null);
setNotice(null);
- try {
- await action();
- } catch (cause) {
+ const result = await settlePromise(action);
+ pendingRef.current = false;
+ setPending(null);
+ if (AsyncResult.isFailure(result)) {
+ const cause = squashAtomCommandFailure(result);
setError(
cause instanceof Error ? cause.message : "The action could not be completed. Try again.",
);
- } finally {
- pendingRef.current = false;
- setPending(null);
}
}
@@ -268,19 +267,15 @@ function EnvironmentDetail({ environmentId }: { readonly environmentId: Environm
const controller = new AbortController();
checkController.current = controller;
const timeout = setTimeout(() => controller.abort(), 20_000);
- try {
- const targetVersion = await findEnvironmentUpdate(
- config.environment.serverVersion,
- controller.signal,
- );
- setRelease({
- fromVersion: config.environment.serverVersion,
- targetVersion,
- });
- } finally {
- clearTimeout(timeout);
- checkController.current = null;
- }
+ const fromVersion = config.environment.serverVersion;
+ const result = await settlePromise(() =>
+ findEnvironmentUpdate(fromVersion, controller.signal),
+ );
+ clearTimeout(timeout);
+ checkController.current = null;
+ // `run` owns the error message, so a failed lookup rethrows to it.
+ if (AsyncResult.isFailure(result)) throw squashAtomCommandFailure(result);
+ setRelease({ fromVersion, targetVersion: result.value });
});
}}
/>
diff --git a/apps/mobile/src/features/settings/SettingsEnvironmentsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsEnvironmentsRouteScreen.tsx
index 13a9a7f24c2..3710c2f1e91 100644
--- a/apps/mobile/src/features/settings/SettingsEnvironmentsRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsEnvironmentsRouteScreen.tsx
@@ -3,7 +3,7 @@ import { useNavigation } from "@react-navigation/native";
import { useAtomValue } from "@effect/atom-react";
import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
import type { EnvironmentId } from "@t3tools/contracts";
-import { useCallback, useRef, useState } from "react";
+import { useRef, useState } from "react";
import { Platform, RefreshControl } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -59,49 +59,40 @@ export function SettingsEnvironmentsRouteScreen() {
if (!relaySession || cloudRefreshPendingRef.current) return;
cloudRefreshPendingRef.current = true;
setIsRefreshingCloud(true);
- try {
- await refreshRelayEnvironments();
- } finally {
- cloudRefreshPendingRef.current = false;
- setIsRefreshingCloud(false);
- }
+ await refreshRelayEnvironments();
+ cloudRefreshPendingRef.current = false;
+ setIsRefreshingCloud(false);
}
- const openEnvironment = useCallback(
- (environmentId: EnvironmentId) => {
- navigation.navigate("SettingsSheet", {
- screen: "SettingsContent",
- params: { screen: "SettingsEnvironmentDetail", params: { environmentId } },
- });
- },
- [navigation],
- );
- const handleUpdateEnvironment = useCallback(
- (
- environmentId: EnvironmentId,
- updates: { readonly label: string; readonly displayUrl: string },
- ) => {
- if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
- const actualEnvironment = environmentSections.localEnvironments.find(
- (environment) => environment.environmentId === environmentId,
- );
- const presentedEnvironment = localEnvironments.find(
- (environment) => environment.environmentId === environmentId,
- );
- return onUpdateEnvironment(environmentId, {
- ...updates,
- displayUrl:
- actualEnvironment && presentedEnvironment
- ? resolveShowcaseEnvironmentUpdateDisplayUrl({
- actualDisplayUrl: actualEnvironment.displayUrl,
- presentedDisplayUrl: presentedEnvironment.displayUrl,
- submittedDisplayUrl: updates.displayUrl,
- })
- : updates.displayUrl,
- });
- },
- [environmentSections.localEnvironments, localEnvironments, onUpdateEnvironment],
- );
+ const openEnvironment = (environmentId: EnvironmentId) => {
+ navigation.navigate("SettingsSheet", {
+ screen: "SettingsContent",
+ params: { screen: "SettingsEnvironmentDetail", params: { environmentId } },
+ });
+ };
+ const handleUpdateEnvironment = (
+ environmentId: EnvironmentId,
+ updates: { readonly label: string; readonly displayUrl: string },
+ ) => {
+ if (!SHOWCASE_ENABLED) return onUpdateEnvironment(environmentId, updates);
+ const actualEnvironment = environmentSections.localEnvironments.find(
+ (environment) => environment.environmentId === environmentId,
+ );
+ const presentedEnvironment = localEnvironments.find(
+ (environment) => environment.environmentId === environmentId,
+ );
+ return onUpdateEnvironment(environmentId, {
+ ...updates,
+ displayUrl:
+ actualEnvironment && presentedEnvironment
+ ? resolveShowcaseEnvironmentUpdateDisplayUrl({
+ actualDisplayUrl: actualEnvironment.displayUrl,
+ presentedDisplayUrl: presentedEnvironment.displayUrl,
+ submittedDisplayUrl: updates.displayUrl,
+ })
+ : updates.displayUrl,
+ });
+ };
return (
<SettingsScreen
diff --git a/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx
index eceefbda07f..fa48dbcd0a9 100644
--- a/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx
@@ -8,7 +8,7 @@ import * as Notifications from "expo-notifications";
import { useNavigation } from "@react-navigation/native";
import * as Effect from "effect/Effect";
import { AsyncResult } from "effect/reactivity";
-import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
+import { useEffect, useRef, useState, useSyncExternalStore } from "react";
import { AppState, Linking, Platform } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -98,10 +98,10 @@ function ConfiguredSettingsNotificationsRouteScreen() {
AsyncResult.isSuccess(preferencesResult) &&
preferencesResult.value.liveActivitiesEnabled !== false;
- const connections = useMemo(() => Object.values(savedConnectionsById), [savedConnectionsById]);
+ const connections = Object.values(savedConnectionsById);
const environmentCount = connections.length;
- const refreshNotifications = useCallback(async () => {
+ const refreshNotifications = async () => {
if (Platform.OS !== "ios" && Platform.OS !== "android") {
setNotificationStatus("unsupported");
return;
@@ -113,7 +113,7 @@ function ConfiguredSettingsNotificationsRouteScreen() {
return;
}
setNotificationStatus(result.value.granted ? "enabled" : "disabled");
- }, []);
+ };
useEffect(() => {
void refreshNotifications();
@@ -146,7 +146,7 @@ function ConfiguredSettingsNotificationsRouteScreen() {
);
}, [isLoaded, isSignedIn, preferencesResult]);
- const requestNotifications = useCallback(async () => {
+ const requestNotifications = async () => {
const result = await settleAsyncResult(() =>
runtime.runPromiseExit(
requestAgentNotificationPermission.pipe(
@@ -199,9 +199,9 @@ function ConfiguredSettingsNotificationsRouteScreen() {
{ text: "Open Settings", onPress: () => void Linking.openSettings() },
],
);
- }, []);
+ };
- const promptSignIn = useCallback(() => {
+ const promptSignIn = () => {
Dialog.alert(
"Sign in to T3 Connect",
"Live Activity updates require T3 Connect so relay can deliver updates to this device.",
@@ -213,9 +213,9 @@ function ConfiguredSettingsNotificationsRouteScreen() {
},
],
);
- }, [navigation]);
+ };
- const linkEnvironments = useCallback(async () => {
+ const linkEnvironments = async () => {
if (!isSignedIn) {
promptSignIn();
return;
@@ -310,111 +310,86 @@ function ConfiguredSettingsNotificationsRouteScreen() {
"This device could not be registered with T3 Connect, so activity updates won't appear yet. They'll start once registration succeeds.",
);
}
- }, [
- connections,
- environmentCount,
- getToken,
- isSignedIn,
- liveActivitiesPreferenceEnabled,
- promptSignIn,
- savePreferences,
- ]);
+ };
- const handleDeviceNotificationsChange = useCallback(
- (enabled: boolean) => {
- if (enabled) {
- if (!isSignedIn) {
- promptSignIn();
- return;
- }
- void requestNotifications();
+ const handleDeviceNotificationsChange = (enabled: boolean) => {
+ if (enabled) {
+ if (!isSignedIn) {
+ promptSignIn();
return;
}
+ void requestNotifications();
+ return;
+ }
- Dialog.alert(
- "Disable notifications",
- "Open system Settings to disable notifications for T3 Code.",
- [
- { text: "Cancel", style: "cancel" },
- { text: "Open Settings", onPress: () => void Linking.openSettings() },
- ],
- );
- },
- [isSignedIn, promptSignIn, requestNotifications],
- );
-
- const handleLiveActivitiesChange = useCallback(
- (enabled: boolean) => {
- if (liveActivityWriteInFlight.current) return;
- if (!enabled) {
- liveActivityWriteInFlight.current = true;
- setLiveActivityStatus("linking");
- void (async () => {
- try {
- let token: string | null = null;
- if (isSignedIn) {
- const tokenResult = await settlePromise(() =>
- getToken(resolveRelayClerkTokenOptions()),
- );
- if (tokenResult._tag === "Failure") {
- setLiveActivityStatus("enabled");
- reportAtomCommandResult(tokenResult, {
- label: "live activity disable token lookup",
- });
- return;
- }
- token = tokenResult.value;
- }
+ Dialog.alert(
+ "Disable notifications",
+ "Open system Settings to disable notifications for T3 Code.",
+ [
+ { text: "Cancel", style: "cancel" },
+ { text: "Open Settings", onPress: () => void Linking.openSettings() },
+ ],
+ );
+ };
- const updateResult = await settleAsyncResult(() =>
- runtime.runPromiseExit(
- setLiveActivityUpdatesEnabled({
- canConfigureEnvironment: (environmentId) =>
- readEnvironmentScope(environmentId, AuthRelayWriteScope),
- enabled: false,
- previousEnabled: liveActivitiesPreferenceEnabled,
- clerkToken: token,
- connections,
- }),
- ),
- );
- if (updateResult._tag === "Failure") {
- setLiveActivityStatus(isSignedIn ? "enabled" : "signed-out");
- reportAtomCommandResult(updateResult, {
- label: "live activity disable",
- });
- return;
- }
- savePreferences({ liveActivitiesEnabled: false });
- refreshManagedRelayEnvironments();
- setLiveActivityStatus("disabled");
- } finally {
- liveActivityWriteInFlight.current = false;
- }
- })();
+ async function disableLiveActivities() {
+ let token: string | null = null;
+ if (isSignedIn) {
+ const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions()));
+ if (tokenResult._tag === "Failure") {
+ setLiveActivityStatus("enabled");
+ reportAtomCommandResult(tokenResult, {
+ label: "live activity disable token lookup",
+ });
return;
}
+ token = tokenResult.value;
+ }
- if (!isSignedIn) {
- promptSignIn();
- return;
- }
+ const updateResult = await settleAsyncResult(() =>
+ runtime.runPromiseExit(
+ setLiveActivityUpdatesEnabled({
+ canConfigureEnvironment: (environmentId) =>
+ readEnvironmentScope(environmentId, AuthRelayWriteScope),
+ enabled: false,
+ previousEnabled: liveActivitiesPreferenceEnabled,
+ clerkToken: token,
+ connections,
+ }),
+ ),
+ );
+ if (updateResult._tag === "Failure") {
+ setLiveActivityStatus(isSignedIn ? "enabled" : "signed-out");
+ reportAtomCommandResult(updateResult, {
+ label: "live activity disable",
+ });
+ return;
+ }
+ savePreferences({ liveActivitiesEnabled: false });
+ refreshManagedRelayEnvironments();
+ setLiveActivityStatus("disabled");
+ }
+ // Both writers settle their own failures, so awaiting them never throws.
+ const handleLiveActivitiesChange = async (enabled: boolean) => {
+ if (liveActivityWriteInFlight.current) return;
+ if (!enabled) {
liveActivityWriteInFlight.current = true;
- void linkEnvironments().finally(() => {
- liveActivityWriteInFlight.current = false;
- });
- },
- [
- connections,
- getToken,
- isSignedIn,
- linkEnvironments,
- liveActivitiesPreferenceEnabled,
- promptSignIn,
- savePreferences,
- ],
- );
+ setLiveActivityStatus("linking");
+ await disableLiveActivities();
+ liveActivityWriteInFlight.current = false;
+ return;
+ }
+
+ if (!isSignedIn) {
+ promptSignIn();
+ return;
+ }
+
+ liveActivityWriteInFlight.current = true;
+ await linkEnvironments();
+ liveActivityWriteInFlight.current = false;
+ };
return (
<SettingsScreen title="Notifications">
@@ -465,13 +440,13 @@ function ConfiguredSettingsNotificationsRouteScreen() {
(liveActivityStatus === "enabled" || liveActivityStatus === "linking") &&
deviceRegistered
}
- onValueChange={handleLiveActivitiesChange}
+ onValueChange={(enabled) => void handleLiveActivitiesChange(enabled)}
/>
{liveActivityStatus === "signed-out" && canClearLiveActivitiesPreference ? (
<SettingsRow
icon="bolt.circle"
label="Turn off Live Activity preference"
- onPress={() => handleLiveActivitiesChange(false)}
+ onPress={() => void handleLiveActivitiesChange(false)}
/>
) : null}
{supportsAndroidLiveUpdateSettings() ? (
diff --git a/apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx
index fd0b99b5cbc..bf5b9ba9cdf 100644
--- a/apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsProjectOverviewRouteScreen.tsx
@@ -75,19 +75,16 @@ function ProjectOverviewContent(props: {
if (!canSave) return;
setIsSaving(true);
void (async () => {
- try {
- const results = await Promise.all(
- props.members.map((member) =>
- updateProject({
- environmentId: member.environmentId,
- input: { projectId: member.id, title: nextName },
- }),
- ),
- );
- if (results.every((result) => result._tag !== "Failure")) setDraftName(null);
- } finally {
- setIsSaving(false);
- }
+ const results = await Promise.all(
+ props.members.map((member) =>
+ updateProject({
+ environmentId: member.environmentId,
+ input: { projectId: member.id, title: nextName },
+ }),
+ ),
+ );
+ setIsSaving(false);
+ if (results.every((result) => result._tag !== "Failure")) setDraftName(null);
})();
};
diff --git a/apps/mobile/src/features/settings/SettingsProviderAccountsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsProviderAccountsRouteScreen.tsx
index 7346914fd90..2095eab9502 100644
--- a/apps/mobile/src/features/settings/SettingsProviderAccountsRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsProviderAccountsRouteScreen.tsx
@@ -127,20 +127,15 @@ function ProviderAccount({
pendingRef.current = true;
setPending(true);
setError(null);
- let succeeded = false;
- try {
- const result = await command();
- if (result._tag === "Success") succeeded = true;
- else if (!isAtomCommandInterrupted(result)) {
- const failure = squashAtomCommandFailure(result);
- setError(failure instanceof Error ? failure.message : "Could not update provider sign-in.");
- }
- } catch {
- setError("Could not update provider sign-in.");
- }
+ const result = await command();
pendingRef.current = false;
setPending(false);
- return succeeded;
+ if (result._tag === "Success") return true;
+ if (!isAtomCommandInterrupted(result)) {
+ const failure = squashAtomCommandFailure(result);
+ setError(failure instanceof Error ? failure.message : "Could not update provider sign-in.");
+ }
+ return false;
}
function send(response: ProviderAuthResponse) {
if (!state?.flowId || !interaction) return Promise.resolve(false);
diff --git a/apps/mobile/src/reactCompilerCoverage.test.ts b/apps/mobile/src/reactCompilerCoverage.test.ts
index b00aed91e0e..98515329f4f 100644
--- a/apps/mobile/src/reactCompilerCoverage.test.ts
+++ b/apps/mobile/src/reactCompilerCoverage.test.ts
@@ -58,14 +58,7 @@ const KNOWN_BAILOUTS: Record<string, string> = {
"src/features/review/ReviewSheet.tsx": "try without catch",
"src/features/review/reviewDiffRendering.tsx": "mutates a frozen value",
"src/features/review/useNativeReviewDiffHighlighting.ts": "conditional inside try",
- "src/features/settings/SettingsAboutRouteScreen.tsx": "try without catch",
"src/features/settings/SettingsAuthRouteScreen.tsx": "ref read during render",
- "src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx":
- "try without catch, try/finally",
- "src/features/settings/SettingsEnvironmentsRouteScreen.tsx": "try without catch",
- "src/features/settings/SettingsNotificationsRouteScreen.tsx": "try without catch",
- "src/features/settings/SettingsProjectOverviewRouteScreen.tsx": "try without catch",
- "src/features/settings/SettingsProviderAccountsRouteScreen.tsx": "conditional inside try",
"src/features/settings/SettingsScheduledTasksRouteScreen.tsx": "unsupported syntax",
"src/features/settings/components/SettingsDragHandle.tsx": "ref read during render",
"src/features/terminal/ThreadTerminalRouteScreen.tsx": "ref read during render",