import { Button } from "../../components/ui/Button";
import {
AuthSettingsWriteScope,
isManagedSettingPath,
type ServerSettingsPatch,
} from "@t3tools/contracts";
import { useEnvironmentsWithScope } from "../../state/session";
import { AutoSettleDaysField } from "./components/AutoSettleDaysField";
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
import { useAtomSet, useAtomValue } from "@effect/atom-react";
import { AsyncResult } from "effect/reactivity";
import { View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
import { AppText as Text } from "../../components/ui/AppText";
import { useEnvironments } from "../../state/environments";
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
import { serverEnvironment } from "../../state/server";
import { useAtomCommand } from "../../state/use-atom-command";
import { SettingsSection } from "./components/SettingsSection";
import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
import type { ToolCallVisibility } from "@t3tools/contracts/settings";
import { SettingsScreen } from "./components/SettingsScreen";
import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
import {
managedSettingReason,
resolveMobileSettingsTargets,
uniformMobileSetting,
} from "./settings-scoped-server";
export function SettingsLabsRouteScreen() {
const insets = useSafeAreaInsets();
return (
<SettingsScreen title="Labs">
<ScrollView
contentInsetAdjustmentBehavior="automatic"
showsVerticalScrollIndicator={false}
className="flex-1"
contentContainerClassName="gap-6 px-5 pt-4"
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
>
<ToolCallVisibilitySection />
<BetaSettingsSection />
<LegacySettingsSection />
</ScrollView>
</SettingsScreen>
);
}
const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
/**
* Usage-limit and auto-settle rows. The Threads page owns their writes, so they share its queue
* with the page's other project overrides, and their project override reset.
*/
export function AutoSettleSettingsRows(props: {
readonly write: (patch: ServerSettingsPatch) => void;
readonly pending: boolean;
}) {
const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
const writableEnvironments = useEnvironmentsWithScope(selectedTargets, AuthSettingsWriteScope);
const projectSelected = selectedProjectKey !== null;
const projectMembers = projectSelected
? (selectedProject?.members.map((member) => member.project) ?? [])
: null;
const displayTargets = resolveMobileSettingsTargets(
selectedTargets.filter(supportsSharedSettingsSync),
projectMembers,
);
// The page saves to all its targets, so any read-only one blocks the save.
const canWriteSettings =
displayTargets.length > 0 &&
resolveMobileSettingsTargets(selectedTargets, projectMembers).every((target) =>
writableEnvironments.has(target.environment.environmentId),
);
const reference = displayTargets[0] ?? null;
const referenceSettings = reference?.settings ?? null;
if (reference === null || referenceSettings === null) {
return null;
}
const writeToAll = (
patch: Partial<AutoSettleSettings> & {
autoResumeLimitedThreads?: boolean;
snoozeLimitedThreads?: boolean;
},
) => props.write(patch);
const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
{
environmentId: reference.environment.environmentId,
projectId: reference.projectId,
settings: referenceSettings,
},
displayTargets.map((target) => ({
environmentId: target.environment.environmentId,
projectId: target.projectId,
label: target.environment.label,
settings: target.settings,
})),
);
const disabled = !canWriteSettings || props.pending;
const disabledFor = (...keys: string[]) =>
disabled || managedSettingReason(displayTargets, ...keys) !== undefined;
const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
return (
<View className="gap-6">
{!projectSelected ? (
<SettingsSection title="Usage limits">
<SettingsSwitchRow
icon="clock"
label="Resume after usage limits"
subtitle="Automatically resume threads when their usage limits reset. You can cancel auto-resume in each thread."
value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
disabled={disabledFor("autoResumeLimitedThreads")}
onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
/>
<SettingsSwitchRow
icon="clock"
label="Snooze until limits reset"
subtitle="Snooze threads until their usage limits reset. Enable auto-resume to continue the agent after the reset."
value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
disabled={disabledFor("snoozeLimitedThreads")}
onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
/>
</SettingsSection>
) : null}
<SettingsSection title="Auto-settle">
<SettingsSwitchRow
icon="arrow.triangle.branch"
label="Auto-settle merged threads"
value={referenceSettings.sidebarAutoSettleOnMerge}
disabled={disabledFor("sidebarAutoSettleOnMerge")}
onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
/>
<SettingsSwitchRow
icon="clock"
label="Auto-settle inactive threads"
value={afterDays !== null}
disabled={disabledFor("sidebarAutoSettleAfterDays")}
onValueChange={(value) =>
writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
}
/>
{afterDays !== null ? (
<View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
<View className="w-[22px] android:w-6" />
<Text className="flex-1 text-foreground text-lg android:text-base">Inactive days</Text>
<AutoSettleDaysField
value={afterDays}
disabled={disabledFor("sidebarAutoSettleAfterDays")}
onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
/>
</View>
) : null}
</SettingsSection>
{!props.pending && mismatches.length > 0 ? (
<SettingsSection title="Across environments">
<View className="gap-3 p-4">
<Text className="text-base text-foreground">Auto-settle defaults differ</Text>
<Text className="text-sm text-foreground-muted">
{mismatches.map((mismatch) => mismatch.label).join(", ")}
</Text>
<Button
label="Apply auto-settle defaults"
tone="secondary"
disabled={disabledFor("sidebarAutoSettleOnMerge", "sidebarAutoSettleAfterDays")}
onPress={() => writeToAll(autoSettlePatch)}
/>
</View>
</SettingsSection>
) : null}
</View>
);
}
export function WorkingSectionSettings() {
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
const preferences = useAtomValue(mobilePreferencesAtom);
const workingShelfEnabled =
AsyncResult.isSuccess(preferences) && preferences.value.workingShelfEnabled !== false;
return (
<View className="gap-3">
<SettingsSection title="Sidebar">
<SettingsSwitchRow
icon="bolt.circle"
label="Working section"
value={workingShelfEnabled}
onValueChange={(value) => savePreferences({ workingShelfEnabled: value })}
/>
</SettingsSection>
<Text className="px-2 text-sm text-foreground-muted">
Fold working and monitoring threads into a Working section. They return to the top of the
list when they need you. While this is on, active threads are ordered by time and cannot be
moved.
</Text>
</View>
);
}
export function NewThreadSettingsSection() {
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
const preferences = useAtomValue(mobilePreferencesAtom);
const useLastProject =
AsyncResult.isSuccess(preferences) && preferences.value.useLastProjectForNewThreads !== false;
return (
<SettingsSection title="New threads">
<SettingsSwitchRow
icon="square.and.pencil"
label="Use last project"
subtitle="Start new threads in the last project you used on this device."
value={useLastProject}
onValueChange={(value) => savePreferences({ useLastProjectForNewThreads: value })}
/>
</SettingsSection>
);
}
/** Per machine, since each machine's own setting decides whether it links and answers links. */
function BetaSettingsSection() {
const { environments } = useEnvironments();
const supported = environments.filter(
(environment) =>
environment.connection.phase === "connected" &&
environment.serverConfig?.environment.capabilities.peerLinks === true,
);
const writable = useEnvironmentsWithScope(supported, AuthSettingsWriteScope);
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
label: "linked environments beta update",
reportFailure: true,
});
if (supported.length === 0) return null;
return (
<View className="gap-3">
<SettingsSection title="Beta: Linked environments">
{supported.map((environment) => {
const policy = environment.serverConfig?.managedPolicy;
const managed = isManagedSettingPath(policy, "linkedEnvironmentsEnabled");
return (
<SettingsSwitchRow
key={environment.environmentId}
icon="link"
label={environment.label}
{...(managed ? { subtitle: `Managed by ${policy?.organization}.` } : {})}
value={environment.serverConfig?.settings.linkedEnvironmentsEnabled === true}
disabled={managed || !writable.has(environment.environmentId)}
onValueChange={(value) =>
void updateSettings({
environmentId: environment.environmentId,
input: { patch: { linkedEnvironmentsEnabled: value } },
})
}
/>
);
})}
</SettingsSection>
<Text className="px-2 text-sm text-foreground-muted">
Let agents on a machine work on your other machines, and theirs on it. Turn it on for each
machine. While off, nothing new starts over a link; running tasks finish.
</Text>
</View>
);
}
const TOOL_CALL_VISIBILITY_OPTIONS: ReadonlyArray<{
readonly value: ToolCallVisibility;
readonly label: string;
}> = [
{ value: "showAll", label: "Show all" },
{ value: "hideSuccessful", label: "Hide successful" },
{ value: "hideAll", label: "Hide all" },
{ value: "zen", label: "Zen" },
];
function ToolCallVisibilitySection() {
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
const preferences = useAtomValue(mobilePreferencesAtom);
const selected = AsyncResult.isSuccess(preferences)
? (preferences.value.toolCallVisibility ?? "showAll")
: null;
return (
<View className="gap-3">
<SettingsSection title="Beta: Tool calls in chat">
{TOOL_CALL_VISIBILITY_OPTIONS.map((option, index) => (
<ListChoiceRow
key={option.value}
label={option.label}
selected={selected === option.value}
separated={index > 0}
disabled={selected === null}
onPress={() => savePreferences({ toolCallVisibility: option.value })}
/>
))}
</SettingsSection>
<Text className="px-2 text-sm text-foreground-muted">
Hide the commands, edits, and searches agents run. Hide successful keeps failures. Zen shows
only final answers. Approvals, questions, and errors always show.
</Text>
</View>
);
}
function LegacySettingsSection() {
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
const preferences = useAtomValue(mobilePreferencesAtom);
const planModeEnabled =
AsyncResult.isSuccess(preferences) && preferences.value.planModeEnabled === true;
return (
<View className="gap-3">
<SettingsSection title="Legacy">
<SettingsSwitchRow
icon="hammer"
label="Plan Mode"
value={planModeEnabled}
onValueChange={(value) => savePreferences({ planModeEnabled: value })}
/>
</SettingsSection>
<Text className="px-2 text-sm text-foreground-muted">
Opt into retired interfaces kept for compatibility. Plan Mode restores the Build/Plan
control; otherwise every task runs in Build mode.
</Text>
</View>
);
}