fix(mobile): preserve multiple model favorites (#12505)
This commit is contained in:
1 parent
93e04160a0
commit
82cd1d1aab
3 files changed
+63
-19
No files matched your search
@@ -47,7 +47,6 @@ import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
|
||||
import { applyProviderOptionSelection } from "../../lib/providerOptions";
|
||||
import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
|
||||
import { useUniwindTheme } from "../../lib/useUniwindTheme";
|
||||
import type { Preferences } from "../../persistence/mobile-preferences";
|
||||
import {
|
||||
NativeHeaderToolbar,
|
||||
NativeStackScreenOptions,
|
||||
@@ -331,12 +330,14 @@ function ThreadSettingsSessionProvider(
|
||||
(option: ModelOption) => {
|
||||
if (!favoritesLoaded) return;
|
||||
void Haptics.selectionAsync();
|
||||
savePreferences((current: Preferences) => ({
|
||||
modelFavorites: toggleModelFavorite(
|
||||
current.modelFavorites ?? EMPTY_MODEL_FAVORITES,
|
||||
option,
|
||||
),
|
||||
}));
|
||||
savePreferences({
|
||||
transform: (current) => ({
|
||||
modelFavorites: toggleModelFavorite(
|
||||
current.modelFavorites ?? EMPTY_MODEL_FAVORITES,
|
||||
option,
|
||||
),
|
||||
}),
|
||||
});
|
||||
},
|
||||
[favoritesLoaded, savePreferences],
|
||||
);
|
||||
|
||||
@@ -5,6 +5,9 @@ import * as Option from "effect/Option";
|
||||
import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
|
||||
import { vi } from "vite-plus/test";
|
||||
import { ProviderInstanceId } from "@t3tools/contracts";
|
||||
import { RegistryContext, useAtomSet } from "@effect/atom-react";
|
||||
import { createElement } from "react";
|
||||
import { renderToString } from "react-dom/server";
|
||||
|
||||
vi.mock("expo-secure-store", () => ({
|
||||
getItemAsync: vi.fn(),
|
||||
@@ -125,12 +128,16 @@ describe("mobile preferences state", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps both favorites when updates are sent before a render", () =>
|
||||
it.effect("keeps both favorites when the React setter sends updates before a render", () =>
|
||||
Effect.gen(function* () {
|
||||
let persisted: Preferences = { modelFavorites: [] };
|
||||
const state = makePreferencesState({
|
||||
load: Effect.succeed(persisted),
|
||||
savePatch: () => Effect.die(new Error("Favorite updates must use a transform.")),
|
||||
savePatch: (patch) =>
|
||||
Effect.sync(() => {
|
||||
persisted = { ...persisted, ...patch };
|
||||
return persisted;
|
||||
}),
|
||||
update: (transform) =>
|
||||
Effect.sync(() => {
|
||||
persisted = { ...persisted, ...transform(persisted) };
|
||||
@@ -142,13 +149,31 @@ describe("mobile preferences state", () => {
|
||||
const unmountUpdate = registry.mount(state.updatePreferencesAtom);
|
||||
yield* AtomRegistry.getResult(registry, state.preferencesAtom, { suspendOnWaiting: true });
|
||||
|
||||
function useSavePreferences() {
|
||||
return useAtomSet(state.updatePreferencesAtom);
|
||||
}
|
||||
const setters: Array<ReturnType<typeof useSavePreferences>> = [];
|
||||
function CaptureSetter() {
|
||||
setters.push(useSavePreferences());
|
||||
return null;
|
||||
}
|
||||
// Exercise the real React setter, which treats bare functions as updates
|
||||
// to the atom's read value. Direct registry.set calls bypass that behavior.
|
||||
renderToString(
|
||||
createElement(RegistryContext.Provider, { value: registry }, createElement(CaptureSetter)),
|
||||
);
|
||||
const savePreferences = setters[0]!;
|
||||
const provider = ProviderInstanceId.make("codex");
|
||||
registry.set(state.updatePreferencesAtom, (current) => ({
|
||||
modelFavorites: [...(current.modelFavorites ?? []), { provider, model: "astra" }],
|
||||
}));
|
||||
registry.set(state.updatePreferencesAtom, (current) => ({
|
||||
modelFavorites: [...(current.modelFavorites ?? []), { provider, model: "sol" }],
|
||||
}));
|
||||
savePreferences({
|
||||
transform: (current) => ({
|
||||
modelFavorites: [...(current.modelFavorites ?? []), { provider, model: "astra" }],
|
||||
}),
|
||||
});
|
||||
savePreferences({
|
||||
transform: (current) => ({
|
||||
modelFavorites: [...(current.modelFavorites ?? []), { provider, model: "sol" }],
|
||||
}),
|
||||
});
|
||||
yield* AtomRegistry.getResult(registry, state.updatePreferencesAtom, {
|
||||
suspendOnWaiting: true,
|
||||
});
|
||||
@@ -158,6 +183,18 @@ describe("mobile preferences state", () => {
|
||||
{ provider, model: "sol" },
|
||||
]);
|
||||
|
||||
savePreferences({
|
||||
transform: (current) => ({
|
||||
modelFavorites: (current.modelFavorites ?? []).filter(
|
||||
(favorite) => favorite.model !== "astra",
|
||||
),
|
||||
}),
|
||||
});
|
||||
yield* AtomRegistry.getResult(registry, state.updatePreferencesAtom, {
|
||||
suspendOnWaiting: true,
|
||||
});
|
||||
expect(persisted.modelFavorites).toEqual([{ provider, model: "sol" }]);
|
||||
|
||||
unmountUpdate();
|
||||
unmountPreferences();
|
||||
registry.dispose();
|
||||
|
||||
@@ -15,7 +15,11 @@ interface OptimisticPreferences {
|
||||
readonly versions: Partial<Record<keyof Preferences, number>>;
|
||||
}
|
||||
|
||||
type PreferencesUpdate = Partial<Preferences> | ((current: Preferences) => Partial<Preferences>);
|
||||
// A bare function is interpreted by useAtomSet as an update to the command's
|
||||
// AsyncResult, not as a preference transform. Keep transforms inside a payload.
|
||||
type PreferencesUpdate =
|
||||
| Partial<Preferences>
|
||||
| { readonly transform: (current: Preferences) => Partial<Preferences> };
|
||||
|
||||
/**
|
||||
* Owns the device preference blob for the lifetime of the app registry.
|
||||
@@ -62,8 +66,10 @@ export function createMobilePreferencesState(runtime: Atom.AtomRuntime<MobilePre
|
||||
(update: PreferencesUpdate, get) => {
|
||||
const currentPreferences = get(preferencesAtom);
|
||||
const patch =
|
||||
typeof update === "function"
|
||||
? update(AsyncResult.isSuccess(currentPreferences) ? currentPreferences.value : {})
|
||||
"transform" in update
|
||||
? update.transform(
|
||||
AsyncResult.isSuccess(currentPreferences) ? currentPreferences.value : {},
|
||||
)
|
||||
: update;
|
||||
const version = ++nextPatchVersion;
|
||||
const current = get(optimisticPatchAtom);
|
||||
@@ -77,7 +83,7 @@ export function createMobilePreferencesState(runtime: Atom.AtomRuntime<MobilePre
|
||||
});
|
||||
return MobilePreferencesStore.pipe(
|
||||
Effect.flatMap((store) =>
|
||||
typeof update === "function" ? store.update(update) : store.savePatch(patch),
|
||||
"transform" in update ? store.update(update.transform) : store.savePatch(patch),
|
||||
),
|
||||
Effect.tap((saved) =>
|
||||
Effect.sync(() => {
|
||||
|
||||
Reference in new issue
Block a user