-
Notifications
You must be signed in to change notification settings - Fork 0
feat(#96): remove legacy saved-data persistence bypasses #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,177 +1,11 @@ | ||
| import { get, set } from 'idb-keyval'; | ||
|
|
||
| // Keys for our three stores | ||
| const SAVED_CALCS_KEY = 'fish-calc-saved-calcs'; | ||
| const CUSTOM_YIELDS_KEY = 'fish-calc-custom-yields'; | ||
| const CUSTOM_SPECIES_KEY = 'fish-calc-custom-species'; | ||
|
|
||
| // --- Generic synced store factory --- | ||
|
|
||
| function createSyncedStore(storageKey) { | ||
| const getAll = async () => (await get(storageKey)) || []; | ||
|
|
||
| const add = async (item) => { | ||
| const items = await getAll(); | ||
| const newItem = { | ||
| ...item, | ||
| id: item.id || crypto.randomUUID(), | ||
| syncStatus: 'local', | ||
| updatedAt: new Date().toISOString(), | ||
| createdAt: item.createdAt || new Date().toISOString(), | ||
| }; | ||
| items.push(newItem); | ||
| await set(storageKey, items); | ||
| return newItem; | ||
| }; | ||
|
|
||
| const update = async (id, data) => { | ||
| const items = await getAll(); | ||
| const index = items.findIndex((i) => i.id === id); | ||
| if (index === -1) return null; | ||
| items[index] = { | ||
| ...items[index], | ||
| ...data, | ||
| syncStatus: items[index].syncStatus === 'synced' ? 'local' : items[index].syncStatus, | ||
| updatedAt: new Date().toISOString(), | ||
| }; | ||
| await set(storageKey, items); | ||
| return items[index]; | ||
| }; | ||
|
|
||
| const remove = async (id) => { | ||
| const items = await getAll(); | ||
| const item = items.find((i) => i.id === id); | ||
| if (!item) return; | ||
|
|
||
| if (item.syncStatus === 'synced') { | ||
| item.syncStatus = 'pending-delete'; | ||
| item.updatedAt = new Date().toISOString(); | ||
| await set(storageKey, items); | ||
| } else { | ||
| await set(storageKey, items.filter((i) => i.id !== id)); | ||
| } | ||
| }; | ||
|
|
||
| const markSynced = async (id, serverId, serverRevision) => { | ||
| const items = await getAll(); | ||
| const item = items.find((i) => i.id === id); | ||
| if (item) { | ||
| item.syncStatus = 'synced'; | ||
| if (serverId) item.serverId = serverId; | ||
| if (serverRevision != null) item.serverRevision = serverRevision; | ||
| await set(storageKey, items); | ||
| } | ||
| }; | ||
|
|
||
| const removeSyncedDelete = async (id) => { | ||
| const items = await getAll(); | ||
| await set(storageKey, items.filter((i) => i.id !== id)); | ||
| }; | ||
|
|
||
| return { getAll, add, update, remove, markSynced, removeSyncedDelete }; | ||
| } | ||
|
|
||
| // --- Store instances --- | ||
|
|
||
| const calcsStore = createSyncedStore(SAVED_CALCS_KEY); | ||
| const yieldsStore = createSyncedStore(CUSTOM_YIELDS_KEY); | ||
|
|
||
| // --- Named exports for backward compatibility --- | ||
|
|
||
| export const getSavedCalcs = calcsStore.getAll; | ||
| export const addSavedCalc = calcsStore.add; | ||
| export const deleteSavedCalc = calcsStore.remove; | ||
|
|
||
| export const getCustomYields = yieldsStore.getAll; | ||
| export const addCustomYield = yieldsStore.add; | ||
| export const updateCustomYield = yieldsStore.update; | ||
| export const deleteCustomYield = yieldsStore.remove; | ||
|
|
||
| // --- Sync helpers (typed exports instead of string dispatch) --- | ||
|
|
||
| export const markCalcSynced = calcsStore.markSynced; | ||
| export const markYieldSynced = yieldsStore.markSynced; | ||
| export const removeCalcSyncedDelete = calcsStore.removeSyncedDelete; | ||
| export const removeYieldSyncedDelete = yieldsStore.removeSyncedDelete; | ||
|
|
||
| export async function getAllPendingSync() { | ||
| const [calcs, yields] = await Promise.all([calcsStore.getAll(), yieldsStore.getAll()]); | ||
| return { | ||
| calcs: calcs.filter((c) => c.syncStatus === 'local' || c.syncStatus === 'pending-delete'), | ||
| yields: yields.filter((y) => y.syncStatus === 'local' || y.syncStatus === 'pending-delete'), | ||
| }; | ||
| } | ||
|
|
||
| // --- Custom Species (different data model, no sync) --- | ||
|
|
||
| export async function getCustomSpecies() { | ||
| return (await get(CUSTOM_SPECIES_KEY)) || {}; | ||
| } | ||
|
|
||
| export async function setCustomSpecies(data) { | ||
| await set(CUSTOM_SPECIES_KEY, data); | ||
| } | ||
|
|
||
| // --- Bulk merge operations (keep separate due to different field mappings) --- | ||
|
|
||
| export async function mergeSyncedCalcs(serverCalcs) { | ||
| const local = await calcsStore.getAll(); | ||
| // Stringify all local serverIds for type-safe comparison (server returns ints, | ||
| // local may store them as numbers or strings depending on how they were saved). | ||
| // Also track items that are pending-delete so we never resurrect a tombstone. | ||
| const localServerIds = new Set(local.filter((c) => c.serverId).map((c) => String(c.serverId))); | ||
| const pendingDeleteIds = new Set( | ||
| local.filter((c) => c.syncStatus === 'pending-delete' && c.serverId).map((c) => String(c.serverId)) | ||
| ); | ||
|
|
||
| for (const sc of serverCalcs) { | ||
| const sid = String(sc.id); | ||
| // Skip if already tracked locally (any syncStatus) — especially pending-delete | ||
| // tombstones, which must not be resurrected by a pull from the server. | ||
| if (localServerIds.has(sid) || pendingDeleteIds.has(sid)) continue; | ||
|
|
||
| local.push({ | ||
| ...sc, | ||
| serverId: sc.id, | ||
| id: crypto.randomUUID(), | ||
| syncStatus: 'synced', | ||
| updatedAt: sc.created_at || new Date().toISOString(), | ||
| createdAt: sc.created_at || new Date().toISOString(), | ||
| }); | ||
| } | ||
| await set(SAVED_CALCS_KEY, local); | ||
| } | ||
|
|
||
| export async function mergeSyncedYields(serverYields) { | ||
| const local = await yieldsStore.getAll(); | ||
|
|
||
| for (const sy of serverYields) { | ||
| const existing = local.find((y) => String(y.serverId) === String(sy.id)); | ||
| if (existing?.syncStatus === 'synced') { | ||
| // Refresh fields and revision for synced records so future update/delete | ||
| // sends the correct expected_revision. Leave pending-delete and local-edit | ||
| // records alone — they represent in-flight changes and must not be clobbered. | ||
| Object.assign(existing, { | ||
| species: sy.species, | ||
| product: sy.product, | ||
| yield: sy.yield, | ||
| source: sy.source || 'User Input', | ||
| serverRevision: sy.revision ?? null, | ||
| }); | ||
| } else if (!existing) { | ||
| local.push({ | ||
| species: sy.species, | ||
| product: sy.product, | ||
| yield: sy.yield, | ||
| source: sy.source || 'User Input', | ||
| serverId: sy.id, | ||
| serverRevision: sy.revision ?? null, | ||
| id: crypto.randomUUID(), | ||
| syncStatus: 'synced', | ||
| updatedAt: new Date().toISOString(), | ||
| }); | ||
| } | ||
| // else: pending-delete tombstone or local edit — leave unchanged | ||
| } | ||
| await set(CUSTOM_YIELDS_KEY, local); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
VITE_LIFECYCLE_ENABLED=falseorlocalStorage.lifecycle_overrideis set tofalse, this effect now unconditionally loads the identity-scoped repository, andtriggerSynclikewise runs the coordinator. Those settings were introduced specifically as deployment-time and per-browser emergency rollback controls, so deleting their checks makes an unhealthy lifecycle rollout impossible to disable and contradicts the claimed no-behavior-change cleanup. Preserve the gating until the rollback mechanism is deliberately retired.Useful? React with 👍 / 👎.