Munchable keeps the health part of its state on the device, so a returning user's profile and scan history come off local storage rather than out of a database. Which means the app reads data written by a build of itself that no longer exists, and has to decide what to do with the parts it no longer recognises.
Six stores in the app, and only two of them survive a relaunch: the dietary profile and the history. Both of them override zustand's merge, and both of them throw data away.
The profile drops ids this build does not know
The profile holds three arrays of ids: the conditions you are eating for, the allergies you asked to be warned about, and the things you would rather not buy. All three are re-validated on the way in:
merge: (persisted, current) => {
const p = (persisted ?? {}) as Partial<ProfileState>;
return {
...current,
...p,
conditions: Array.isArray(p.conditions) ? p.conditions.filter(isConditionId) : [],
allergens: Array.isArray(p.allergens) ? p.allergens.filter(isAllergenId) : [],
healthy: Array.isArray(p.healthy) ? p.healthy.filter(isHealthPreferenceId) : [],
};
},
The default merge is otherwise exactly right. A profile saved before allergies existed simply has no allergens key, and the default object spread leaves the empty default in place, so adding a field needs no migration at all. The direction that needed work is the other one: a field that still has a value, holding an id this build has retired or renamed.
The comment above that merge names two failures, and they are failures of different severity.
The mild one is a picker. The settings screens look every stored id up in a map by name to draw its label and icon. An id with no entry is an undefined lookup followed by a property access, which is a crash in a settings screen for exactly the users who have been here longest.
The serious one is silence. A stored allergy id that the current build does not recognise is an id that nothing downstream matches, so no warning row is rendered. Nobody sees an error. They see a result screen that looks complete and is quietly not watching for the thing they asked to be warned about. Our own footer is explicit that allergy warnings only ever add a warning and never promise a product is free from anything, and a dropped id turns that into a promise we did not make on purpose.
Filtering means the id disappears from their profile, which is visible: the condition or allergy is simply not ticked any more, in a screen built to be re-ticked. That is a worse outcome than nothing going wrong and a better one than either alternative.
One array, and empty means off
The third of those arrays is the one I would point at in a design review. It drives an optional filter, and the state for "is the filter on" is the array itself:
/**
* ONE array, and empty means the whole filter is off. A separate boolean
* alongside a list is two sources of truth for one question, and the pair
* can disagree: "on with nothing selected" is a filter that checks nothing
* while the settings screen says it is working.
*/
healthy: HealthPreferenceId[];
Switching it on writes the defaults, switching it off empties it, and the engine reads the array directly. There is no healthyEnabled to fall out of step, and no state where the settings screen claims to be checking something while nothing is being checked.
It matters more than usual here because the persisted copy is the one that can disagree. A boolean and a list stored separately give you four combinations when two are meaningful, and the broken pair survives a relaunch.
The history drops entries it cannot draw
The scan history is health data by any reading, so it lives on the device and is not part of what the server can export. Its merge is shaped the same way, and filters on the fields a row needs to be renderable at all:
const entries = Array.isArray(p.entries)
? p.entries
.filter(
(e): e is HistoryEntry =>
!!e && typeof e.id === 'string' && typeof e.barcode === 'string' && typeof e.at === 'number',
)
.map((e) => ({ ...e, feeling: isFeeling(e.feeling) ? e.feeling : undefined }))
: [];
Note the split in how the two kinds of damage are handled. A row missing its id, barcode or timestamp is dropped: there is no list item to draw without them. A row with an unrecognised feeling is kept and the feeling is cleared, because the rest of the entry is still a scan the user made and still belongs in their log. Throwing the whole row away over an optional field would lose real history to a renamed enum value.
The migration between the two
There is one more thing in there that is worth copying, because it is the awkward case nobody designs for in advance. Before the log existed, the profile store kept the last twelve scans for the home screen's recent list. Those had to become the first entries of the log, exactly once.
The problem is that both stores hydrate asynchronously and neither waits for the other, so the migration cannot simply run at startup. It runs when both are ready, whichever that is:
if (run()) return;
const unsubscribe = useProfile.subscribe((s) => {
if (s.hydrated && run()) unsubscribe();
});
Try immediately, and if the other store is not hydrated yet, subscribe and try again on each change until it is, then unsubscribe. The old field is emptied as part of the same run, which is what stops the import happening twice, and the import only proceeds when the log is still empty, which is what stops it overwriting a log that already has real entries.
The rule the two stores share
Anything persisted on a device is data written by a version of your software you no longer control. The shape you read is not the shape you declared; it is every shape you have ever declared, including the ones you got wrong.
So the rule both stores follow is: validate the parts an id or an enum can rot in, drop what cannot be rendered, keep what can, and never let a stale id turn into a feature silently not running. The invisible failure is the expensive one.
The device-only part of this is not just an implementation detail, which is why it is written into our privacy notice rather than only into a comment. The conditions those filtered ids refer to each have a page at munchable.app/conditions, and the per-ingredient answers they drive are at munchable.app/answers.
Top comments (0)