|
| 1 | +import { useAtomSet, useAtomValue } from "@effect/atom-react"; |
| 2 | +import { useNavigation } from "@react-navigation/native"; |
| 3 | +import { |
| 4 | + MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, |
| 5 | + MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, |
| 6 | + type SidebarAutoSettleMode, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import { AsyncResult } from "effect/unstable/reactivity"; |
| 9 | +import { Platform, Pressable, ScrollView, View } from "react-native"; |
| 10 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 11 | + |
| 12 | +import { AndroidScreenHeader } from "../../components/AndroidScreenHeader"; |
| 13 | +import { AppText as Text } from "../../components/AppText"; |
| 14 | +import { SymbolView } from "../../components/AppSymbol"; |
| 15 | +import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 16 | +import { NativeStackScreenOptions } from "../../native/StackHeader"; |
| 17 | +import { resolveMobileAutoSettlePreferences } from "../../persistence/mobile-preferences"; |
| 18 | +import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; |
| 19 | +import { SettingsSection } from "./components/SettingsSection"; |
| 20 | + |
| 21 | +const AUTO_SETTLE_OPTIONS: ReadonlyArray<{ |
| 22 | + readonly mode: SidebarAutoSettleMode; |
| 23 | + readonly label: string; |
| 24 | + readonly description: string; |
| 25 | +}> = [ |
| 26 | + { |
| 27 | + mode: "never", |
| 28 | + label: "Never", |
| 29 | + description: "Threads settle only when you choose Settle.", |
| 30 | + }, |
| 31 | + { |
| 32 | + mode: "change-request", |
| 33 | + label: "When a pull request merges or closes", |
| 34 | + description: "A missing or uncertain pull-request timestamp keeps the thread active.", |
| 35 | + }, |
| 36 | + { |
| 37 | + mode: "inactivity", |
| 38 | + label: "After inactivity", |
| 39 | + description: "Running, blocked, and open-pull-request threads stay active.", |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +export function mobileAutoSettleModeLabel(mode: SidebarAutoSettleMode): string { |
| 44 | + if (mode === "change-request") return "PR merged or closed"; |
| 45 | + if (mode === "inactivity") return "After inactivity"; |
| 46 | + return "Never"; |
| 47 | +} |
| 48 | + |
| 49 | +export function SettingsAutoSettleRouteScreen() { |
| 50 | + const navigation = useNavigation(); |
| 51 | + const insets = useSafeAreaInsets(); |
| 52 | + const checkmarkColor = useUniwindTheme()["--color-icon"]; |
| 53 | + const preferencesResult = useAtomValue(mobilePreferencesAtom); |
| 54 | + const savePreferences = useAtomSet(updateMobilePreferencesAtom); |
| 55 | + const preferencesReady = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting; |
| 56 | + const resolved = resolveMobileAutoSettlePreferences( |
| 57 | + AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value : {}, |
| 58 | + ); |
| 59 | + |
| 60 | + const setDays = (days: number) => { |
| 61 | + if (!preferencesReady) return; |
| 62 | + savePreferences({ |
| 63 | + autoSettleAfterDays: Math.min( |
| 64 | + MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, |
| 65 | + Math.max(MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, days), |
| 66 | + ), |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <View collapsable={false} className="flex-1 bg-sheet"> |
| 72 | + {Platform.OS === "android" ? ( |
| 73 | + <> |
| 74 | + <NativeStackScreenOptions options={{ headerShown: false }} /> |
| 75 | + <AndroidScreenHeader title="Thread Settling" onBack={() => navigation.goBack()} /> |
| 76 | + </> |
| 77 | + ) : null} |
| 78 | + <ScrollView |
| 79 | + contentInsetAdjustmentBehavior="automatic" |
| 80 | + showsVerticalScrollIndicator={false} |
| 81 | + className="flex-1" |
| 82 | + contentContainerClassName="gap-4 px-5 pt-4" |
| 83 | + contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }} |
| 84 | + > |
| 85 | + <SettingsSection title="Automatic settling"> |
| 86 | + {AUTO_SETTLE_OPTIONS.map((option, index) => ( |
| 87 | + <Pressable |
| 88 | + key={option.mode} |
| 89 | + accessibilityRole="radio" |
| 90 | + accessibilityState={{ |
| 91 | + checked: resolved.autoSettleMode === option.mode, |
| 92 | + disabled: !preferencesReady, |
| 93 | + }} |
| 94 | + disabled={!preferencesReady} |
| 95 | + onPress={() => savePreferences({ autoSettleMode: option.mode })} |
| 96 | + className={ |
| 97 | + index === 0 |
| 98 | + ? "flex-row items-center gap-4 p-4" |
| 99 | + : "flex-row items-center gap-4 border-t border-border-subtle p-4" |
| 100 | + } |
| 101 | + > |
| 102 | + <View className="min-w-0 flex-1 gap-1"> |
| 103 | + <Text className="text-lg text-foreground">{option.label}</Text> |
| 104 | + <Text className="text-sm leading-normal text-foreground-muted"> |
| 105 | + {option.description} |
| 106 | + </Text> |
| 107 | + </View> |
| 108 | + {resolved.autoSettleMode === option.mode ? ( |
| 109 | + <SymbolView |
| 110 | + name="checkmark" |
| 111 | + size={18} |
| 112 | + tintColor={checkmarkColor} |
| 113 | + type="monochrome" |
| 114 | + weight="semibold" |
| 115 | + /> |
| 116 | + ) : null} |
| 117 | + </Pressable> |
| 118 | + ))} |
| 119 | + </SettingsSection> |
| 120 | + |
| 121 | + {resolved.autoSettleMode === "inactivity" ? ( |
| 122 | + <SettingsSection title="Inactivity window"> |
| 123 | + <View className="flex-row items-center gap-4 p-4"> |
| 124 | + <View className="min-w-0 flex-1 gap-1"> |
| 125 | + <Text className="text-lg text-foreground">Days without activity</Text> |
| 126 | + <Text className="text-sm text-foreground-muted">Between 1 and 90 days</Text> |
| 127 | + </View> |
| 128 | + <Pressable |
| 129 | + accessibilityLabel="Decrease inactivity days" |
| 130 | + accessibilityRole="button" |
| 131 | + disabled={ |
| 132 | + !preferencesReady || |
| 133 | + resolved.autoSettleAfterDays <= MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS |
| 134 | + } |
| 135 | + onPress={() => setDays(resolved.autoSettleAfterDays - 1)} |
| 136 | + className="size-10 items-center justify-center rounded-full bg-fill-secondary disabled:opacity-40" |
| 137 | + > |
| 138 | + <SymbolView |
| 139 | + name="minus" |
| 140 | + size={16} |
| 141 | + tintColor={checkmarkColor} |
| 142 | + type="monochrome" |
| 143 | + weight="semibold" |
| 144 | + /> |
| 145 | + </Pressable> |
| 146 | + <Text className="w-8 text-center text-lg text-foreground"> |
| 147 | + {resolved.autoSettleAfterDays} |
| 148 | + </Text> |
| 149 | + <Pressable |
| 150 | + accessibilityLabel="Increase inactivity days" |
| 151 | + accessibilityRole="button" |
| 152 | + disabled={ |
| 153 | + !preferencesReady || |
| 154 | + resolved.autoSettleAfterDays >= MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS |
| 155 | + } |
| 156 | + onPress={() => setDays(resolved.autoSettleAfterDays + 1)} |
| 157 | + className="size-10 items-center justify-center rounded-full bg-fill-secondary disabled:opacity-40" |
| 158 | + > |
| 159 | + <SymbolView |
| 160 | + name="plus" |
| 161 | + size={16} |
| 162 | + tintColor={checkmarkColor} |
| 163 | + type="monochrome" |
| 164 | + weight="semibold" |
| 165 | + /> |
| 166 | + </Pressable> |
| 167 | + </View> |
| 168 | + </SettingsSection> |
| 169 | + ) : null} |
| 170 | + </ScrollView> |
| 171 | + </View> |
| 172 | + ); |
| 173 | +} |
0 commit comments