Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/mobile/src/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DynamicColorIOS, Platform, Pressable, ScrollView, StyleSheet } from "re
import { useResolveClassNames } from "uniwind";

import { AppText as Text } from "./components/AppText";
import { renderCompactBrandTitle } from "./components/CompactBrandTitle";
import { ArchivedThreadsRouteScreen } from "./features/archive/ArchivedThreadsRouteScreen";
import { useAgentNotificationNavigation } from "./features/agent-awareness/notificationNavigation";
import { ClerkSettingsSheetDetentProvider } from "./features/cloud/ClerkSettingsSheetDetent";
Expand Down Expand Up @@ -383,7 +384,8 @@ export const RootStack = createNativeStackNavigator({
...GLASS_HEADER_OPTIONS,
contentStyle: { backgroundColor: "transparent" },
headerBackVisible: false,
title: "Threads",
headerTitle: renderCompactBrandTitle,
title: "T3 Code",
},
}),
Thread: createNativeStackScreen({
Expand Down
60 changes: 60 additions & 0 deletions apps/mobile/src/components/CompactBrandTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { View } from "react-native";

import { AppText as Text } from "./AppText";
import { T3Wordmark } from "./T3Wordmark";
import { useThemeColor } from "../lib/useThemeColor";

/**
* Compact brand lockup sized for native navigation bars.
*/
export function CompactBrandTitle() {
const iconColor = useThemeColor("--color-icon");
const mutedColor = useThemeColor("--color-foreground-muted");
const subtleColor = useThemeColor("--color-subtle");

return (
<View
aria-level={1}
accessibilityLabel="T3 Code"
accessible
role="heading"
style={{ alignItems: "center", flexDirection: "row", gap: 6 }}
>
<T3Wordmark color={iconColor} height={11} />
<Text
style={{
color: mutedColor,
fontFamily: "DMSans-Medium",
fontSize: 14,
letterSpacing: -0.35,
}}
>
Code
</Text>
<View
style={{
backgroundColor: subtleColor,
borderRadius: 999,
paddingHorizontal: 6,
paddingVertical: 2,
}}
>
<Text
style={{
color: mutedColor,
fontFamily: "DMSans-Bold",
fontSize: 9,
letterSpacing: 0.9,
textTransform: "uppercase",
}}
>
Alpha
</Text>
</View>
</View>
);
}

export function renderCompactBrandTitle() {
return <CompactBrandTitle />;
}
5 changes: 4 additions & 1 deletion apps/mobile/src/features/home/HomeRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useNavigation } from "@react-navigation/native";
import { useMemo, useState } from "react";

import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
import { renderCompactBrandTitle } from "../../components/CompactBrandTitle";
import { useProjects, useThreadShells } from "../../state/entities";
import { usePendingNewTasks } from "../../state/use-pending-new-tasks";
import { useWorkspaceState } from "../../state/workspace";
Expand Down Expand Up @@ -86,7 +87,9 @@ export function HomeRouteScreen() {
>
<>
{/* Restore the compact title in case the split branch blanked it. */}
<NativeStackScreenOptions options={{ title: "Threads", headerTitle: "Threads" }} />
<NativeStackScreenOptions
options={{ title: "T3 Code", headerTitle: renderCompactBrandTitle }}
/>
<HomeHeader
environments={environments}
searchQuery={searchQuery}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import type { ReactNode } from "react";
import { Platform, useColorScheme } from "react-native";

import { renderCompactBrandTitle } from "../../components/CompactBrandTitle";
import { nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";

const SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
Expand All @@ -34,10 +35,11 @@ const SIDEBAR_SCREEN_OPTIONS: SidebarScreenOptions = {
headerShadowVisible: false,
headerShown: true,
headerStyle: { backgroundColor: "transparent" },
headerTitle: renderCompactBrandTitle,
headerTitleStyle: { fontSize: 18, fontWeight: "800" },
headerTransparent: true,
scrollEdgeEffects: SCROLL_EDGE_EFFECTS,
title: "Threads",
title: "T3 Code",
unstable_navigationItemStyle: "editor",
};

Expand Down
103 changes: 100 additions & 3 deletions apps/mobile/src/native/StackHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useEffect,
useLayoutEffect,
useMemo,
useRef,
type ReactElement,
type ReactNode,
} from "react";
Expand Down Expand Up @@ -61,20 +62,116 @@ function normalizeScreenOptions(
return normalized as NativeStackNavigationOptions;
}

function optionsSignature(value: unknown, seen = new WeakSet<object>()): string {
if (value === null) return "null";
switch (typeof value) {
case "boolean":
case "number":
case "string":
return JSON.stringify(value);
case "undefined":
return "undefined";
case "function":
// Header factories are frequently recreated inline. Their source is
// stable across equivalent renders, while a reference comparison would
// make navigation.setOptions re-enter the navigator indefinitely.
return `function:${Function.prototype.toString.call(value)}`;
case "symbol":
return `symbol:${String(value)}`;
case "bigint":
return `bigint:${String(value)}`;
case "object": {
const object = value as object;
if (seen.has(object)) return "[circular]";
seen.add(object);
if (Array.isArray(value)) {
return `[${value.map((entry) => optionsSignature(entry, seen)).join(",")}]`;
}
// React refs carry mutable native instances that must not make static
// screen options appear different after every render.
if ("current" in object) return "[ref]";
return `{${Object.keys(value as Record<string, unknown>)
.sort()
.map(
(key) =>
`${JSON.stringify(key)}:${optionsSignature((value as Record<string, unknown>)[key], seen)}`,
)
.join(",")}}`;
}
}
return String(value);
}

function stabilizeOptionFunctions(
value: unknown,
path: string,
latestFunctions: Map<string, (...args: unknown[]) => unknown>,
wrappers: Map<string, (...args: unknown[]) => unknown>,
seen = new WeakSet<object>(),
): unknown {
if (typeof value === "function") {
latestFunctions.set(path, value as (...args: unknown[]) => unknown);
let wrapper = wrappers.get(path);
if (!wrapper) {
wrapper = (...args: unknown[]) => {
return latestFunctions.get(path)?.(...args);
};
wrappers.set(path, wrapper);
}
return wrapper;
}
if (Array.isArray(value)) {
if (seen.has(value)) return value;
seen.add(value);
return value.map((entry, index) =>
stabilizeOptionFunctions(entry, `${path}[${index}]`, latestFunctions, wrappers, seen),
);
}
if (value !== null && typeof value === "object") {
if (seen.has(value) || "current" in value) return value;
seen.add(value);
return Object.fromEntries(
Object.entries(value as Record<string, unknown>).map(([key, entry]) => [
key,
stabilizeOptionFunctions(entry, `${path}.${key}`, latestFunctions, wrappers, seen),
]),
);
}
return value;
}

export function NativeStackScreenOptions(props: {
readonly options?: AppNativeStackNavigationOptions;
readonly listeners?: Record<string, (event: never) => void>;
readonly name?: string;
}) {
const navigation = useNativeStackNavigation();
const lastAppliedOptionsSignatureRef = useRef<string | undefined>(undefined);
const latestOptionFunctionsRef = useRef(new Map<string, (...args: unknown[]) => unknown>());
const optionFunctionWrappersRef = useRef(new Map<string, (...args: unknown[]) => unknown>());
const normalizedOptions = useMemo(() => normalizeScreenOptions(props.options), [props.options]);
const stableOptions = normalizedOptions
? (stabilizeOptionFunctions(
normalizedOptions,
"options",
latestOptionFunctionsRef.current,
optionFunctionWrappersRef.current,
) as NativeStackNavigationOptions)
: undefined;

useLayoutEffect(() => {
if (!navigation || !normalizedOptions) {
if (!navigation || !stableOptions) {
return;
}
const signature = optionsSignature(stableOptions);
// Avoid re-entering navigation state when semantically equal options are
// reapplied every layout (common when callers pass unstable object literals).
if (lastAppliedOptionsSignatureRef.current === signature) {
Comment on lines +166 to +169

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reapply options when function output changes

When a header option is a factory whose output depends on component state, this signature is computed after replacing the factory with a stable wrapper, so closure-only changes all look identical and setOptions is skipped. I checked HomeHeader, which uses unstable_headerToolbarItems to build the iOS search/filter toolbar from hasCustomListOptions, filterMenu, and callbacks; after the user changes sort/filter state, the native toolbar/menu can keep showing the stale icon/checked state because the navigator is never told to re-render those options.

Useful? React with 👍 / 👎.

return;
}
navigation.setOptions(normalizedOptions);
}, [navigation, normalizedOptions]);
lastAppliedOptionsSignatureRef.current = signature;
navigation.setOptions(stableOptions);
}, [navigation, stableOptions]);

useEffect(() => {
if (!navigation || !props.listeners) {
Expand Down
2 changes: 2 additions & 0 deletions patches/@react-navigation%2Fnative-stack@7.17.6.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
diff --git a/lib/module/views/useHeaderConfigProps.js b/lib/module/views/useHeaderConfigProps.js
index 0b75c70b4e0d233ee3b5faaf9cfbc40d4f8ed494..eb174e3fde91a7783f132b3fb16b01175ec19220 100644
--- a/lib/module/views/useHeaderConfigProps.js
+++ b/lib/module/views/useHeaderConfigProps.js
@@ -19,6 +19,12 @@
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading