@@ -3,6 +3,7 @@ import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
33import {
44 modelPreferencesMigrationPatch ,
55 resolveModelPreferences ,
6+ supportsServerModelPreferences ,
67} from "@t3tools/shared/serverSettings" ;
78import type {
89 EnvironmentId ,
@@ -14,7 +15,7 @@ import type {
1415 ServerSettings ,
1516} from "@t3tools/contracts" ;
1617import type { LegendListRenderItemProps } from "@legendapp/list/react-native" ;
17- import { RegistryContext , useAtomValue } from "@effect/atom-react" ;
18+ import { RegistryContext , useAtomSet , useAtomValue } from "@effect/atom-react" ;
1819import { AnimatedLegendList } from "@legendapp/list/reanimated" ;
1920import {
2021 getProviderOptionCurrentLabel ,
@@ -74,7 +75,7 @@ import {
7475import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass" ;
7576import { ChatGptSharingStatus } from "./ChatGptSharingStatus" ;
7677import { environmentServerConfigsAtom , serverEnvironment } from "../../state/server" ;
77- import { mobilePreferencesAtom } from "../../state/preferences" ;
78+ import { mobilePreferencesAtom , updateMobilePreferencesAtom } from "../../state/preferences" ;
7879import { useAtomCommand } from "../../state/use-atom-command" ;
7980import { useNewTaskFlow } from "./new-task-flow-provider" ;
8081import { MaterialScreenContent } from "../../components/MaterialScreenContent" ;
@@ -315,6 +316,7 @@ type ThreadSettingsSessionValue = {
315316 readonly toggleHidden : ( option : ModelOption ) => void ;
316317 readonly pendingHiddenKeys : ReadonlySet < string > ;
317318 readonly manageHidden : boolean ;
319+ readonly canManageHidden : boolean ;
318320 readonly setManageHidden : ( value : boolean ) => void ;
319321 readonly runtimeMode : RuntimeMode ;
320322 readonly runtimeModeChoices : ReturnType < typeof runtimeModeChoicesForSupportedModes > ;
@@ -350,7 +352,13 @@ function ThreadSettingsSessionProvider(
350352 const configs = useAtomValue ( environmentServerConfigsAtom ) ;
351353 const config = props . environmentId ? configs . get ( props . environmentId ) : undefined ;
352354 const settings = config ?. settings ;
353- const [ manageHidden , setManageHidden ] = useState ( false ) ;
355+ const serverModelPreferences = supportsServerModelPreferences (
356+ settings ,
357+ config ?. environment . capabilities ,
358+ ) ;
359+ const saveLocalPreferences = useAtomSet ( updateMobilePreferencesAtom ) ;
360+ const [ manageHiddenEnabled , setManageHidden ] = useState ( false ) ;
361+ const manageHidden = serverModelPreferences && manageHiddenEnabled ;
354362 const pendingHidden = useRef ( new Set < string > ( ) ) ;
355363 const pendingFavorites = useRef (
356364 new Map < string , { favorite : boolean ; settings : ServerSettings ; pending : boolean } > ( ) ,
@@ -381,10 +389,23 @@ function ThreadSettingsSessionProvider(
381389 } ) . favorites ;
382390 const saveSettings = useAtomCommand ( serverEnvironment . updateSettings , "model favorites update" ) ;
383391 useEffect ( ( ) => {
384- if ( ! props . environmentId || ! settings || ! AsyncResult . isSuccess ( preferences ) ) return ;
392+ if (
393+ ! props . environmentId ||
394+ ! settings ||
395+ ! serverModelPreferences ||
396+ ! AsyncResult . isSuccess ( preferences )
397+ )
398+ return ;
385399 const patch = modelPreferencesMigrationPatch ( settings , { favorites : legacyFavorites } ) ;
386400 if ( patch ) void saveSettings ( { environmentId : props . environmentId , input : { patch } } ) ;
387- } , [ legacyFavorites , preferences , props . environmentId , saveSettings , settings ] ) ;
401+ } , [
402+ legacyFavorites ,
403+ preferences ,
404+ props . environmentId ,
405+ saveSettings ,
406+ settings ,
407+ serverModelPreferences ,
408+ ] ) ;
388409 const favoriteKeys = useMemo (
389410 ( ) =>
390411 new Set (
@@ -395,10 +416,32 @@ function ThreadSettingsSessionProvider(
395416 const toggleFavorite = useCallback (
396417 async ( option : ModelOption ) => {
397418 if ( ! favoritesLoaded || ! props . environmentId ) return ;
398- const currentSettings = registry
399- . get ( environmentServerConfigsAtom )
400- . get ( props . environmentId ) ?. settings ;
419+ const currentConfig = registry . get ( environmentServerConfigsAtom ) . get ( props . environmentId ) ;
420+ const currentSettings = currentConfig ?. settings ;
401421 if ( ! currentSettings ) return ;
422+ if (
423+ ! supportsServerModelPreferences ( currentSettings , currentConfig ?. environment . capabilities )
424+ ) {
425+ void Haptics . selectionAsync ( ) ;
426+ saveLocalPreferences ( {
427+ transform : ( current ) => {
428+ const favorites = current . modelFavorites ?? [ ] ;
429+ const provider = option . selection . instanceId ;
430+ const model = option . selection . model ;
431+ const exists = favorites . some (
432+ ( favorite ) => favorite . provider === provider && favorite . model === model ,
433+ ) ;
434+ return {
435+ modelFavorites : exists
436+ ? favorites . filter (
437+ ( favorite ) => favorite . provider !== provider || favorite . model !== model ,
438+ )
439+ : [ ...favorites , { provider, model } ] ,
440+ } ;
441+ } ,
442+ } ) ;
443+ return ;
444+ }
402445 const favorites = resolveModelPreferences ( currentSettings , {
403446 favorites : legacyFavorites ,
404447 } ) . favorites ;
@@ -459,12 +502,20 @@ function ThreadSettingsSessionProvider(
459502 }
460503 }
461504 } ,
462- [ favoritesLoaded , legacyFavorites , props . environmentId , registry , saveSettings ] ,
505+ [
506+ favoritesLoaded ,
507+ legacyFavorites ,
508+ props . environmentId ,
509+ registry ,
510+ saveLocalPreferences ,
511+ saveSettings ,
512+ ] ,
463513 ) ;
464514 const toggleHidden = useCallback (
465515 ( option : ModelOption ) => {
466516 if (
467517 ! settings ||
518+ ! serverModelPreferences ||
468519 ! props . environmentId ||
469520 ! option . canHide ||
470521 pendingHidden . current . has ( option . key )
@@ -491,7 +542,7 @@ function ThreadSettingsSessionProvider(
491542 setPendingHiddenKeys ( new Set ( pendingHidden . current ) ) ;
492543 } ) ;
493544 } ,
494- [ props . environmentId , saveSettings , settings ] ,
545+ [ props . environmentId , saveSettings , settings , serverModelPreferences ] ,
495546 ) ;
496547 const [ showLegacyToggle , setShowLegacyToggle ] = useState ( false ) ;
497548 const [ providerFilter , setProviderFilter ] = useState < string | null > ( null ) ;
@@ -611,6 +662,7 @@ function ThreadSettingsSessionProvider(
611662 providerInstanceId : props . providerInstanceId ,
612663 providerGroups,
613664 manageHidden,
665+ canManageHidden : serverModelPreferences ,
614666 setManageHidden,
615667 toggleHidden,
616668 pendingHiddenKeys,
@@ -660,6 +712,7 @@ function ThreadSettingsSessionProvider(
660712 props . onUpdateRuntimeMode ,
661713 providerGroups ,
662714 manageHidden ,
715+ serverModelPreferences ,
663716 toggleHidden ,
664717 pendingHiddenKeys ,
665718 runtimeModeChoices ,
@@ -958,14 +1011,18 @@ function ThreadSettingsOptionsItem(props: {
9581011 </ Animated . View >
9591012 </ Animated . View >
9601013
961- < Text className = "px-5 pb-2 pt-7 text-sm font-t3-medium text-foreground-muted" > Catalog</ Text >
1014+ { ( session . canManageHidden || ( Platform . OS !== "ios" && session . hasLegacyModels ) ) && (
1015+ < Text className = "px-5 pb-2 pt-7 text-sm font-t3-medium text-foreground-muted" > Catalog</ Text >
1016+ ) }
9621017 < View className = "mx-4 overflow-hidden rounded-2xl bg-grouped-card" >
963- < SwitchRow
964- label = "Manage hidden models"
965- value = { session . manageHidden }
966- onValueChange = { session . setManageHidden }
967- isLast = { Platform . OS === "ios" || ! session . hasLegacyModels }
968- />
1018+ { session . canManageHidden && (
1019+ < SwitchRow
1020+ label = "Manage hidden models"
1021+ value = { session . manageHidden }
1022+ onValueChange = { session . setManageHidden }
1023+ isLast = { Platform . OS === "ios" || ! session . hasLegacyModels }
1024+ />
1025+ ) }
9691026 { Platform . OS !== "ios" && session . hasLegacyModels ? (
9701027 < SwitchRow
9711028 isLast
0 commit comments