6.4 KiB
6.4 KiB
Plan — Part 2/2: user-ui (consume the per-bound flat JSON)
Part 2 of 2. Adapts apps/user-ui to request the precomputed flat JSON by boundId and read
the flat shape. Backend + admin UIs are Part 1
(consts-three-tables-part1-backend-admin.md) and must land first (the client depends on
{ boundId } input, listRegions.effectiveBoundId, and the flat payload).
0. Decisions
- user-ui sends
boundId(persisted client-side) and reads the flat JSON. - Scope:
apps/user-uionly.apps/web-uistays on the old contract (breaks; see §4). - AGENTS.md: no test runs, no migrations, no git; log planned diffs to
change-log.txtbefore editing non-.md/.txtfiles.
1. Flat JSON contract (from Part 1)
common.essentialConsts({ boundId }) returns the union of global + region + bound rows plus
identity/env:
{
"versionNum": "1.1.0", "playStoreUrl": "...", "appStoreUrl": "...", "internalAppVersion": 1,
"products_version_num": 2, "availability_version_num": 2, "slots_version_num": 2,
"popularItems": "[]", "allItemsOrder": "[]", "isFlashDeliveryEnabled": true,
"supportMobile": "...", "supportEmail": "...", "welcomeMessage": "...",
"minRegularOrderValue": 300, "freeDeliveryThreshold": 300, "deliveryCharge": 20,
"flashFreeDeliveryThreshold": 500, "flashDeliveryCharge": 69,
"assetsDomain": "...", "apiCacheKey": "...", "regionId": 1, "boundId": 7,
"webviewHtml": null, "isWebviewClosable": true
}
allItemsOrderis the region's value (identical across bounds of the same region).- The nested
effectiveSettings/regionSettingsblock is gone. EssentialConstsApiType(@backend/trpc/router) already reflects this (derived fromscaffoldEssentialConsts).
2. Region-bound state (client must know its boundId)
Today the client knows only regionId (persisted in regionStore); the resolved bound is
transient. Changes:
2.1 src/store/regionStore.ts
- Add
selectedBoundId: number | null. setRegion(region: RegionSummary, boundId?: number | null)persists both; includeselectedBoundIdinpartialize; extend the no-op guard to compare boundId.
2.2 src/hooks/useRegion.ts
- Expose
boundId(from the store) onUseRegionResult. - On a successful
resolveRegionByLocation, persistsetRegion(resolved.region, resolved.bound.id)(instead of region-only).
2.3 src/lib/region-switch.ts
confirmRegionSwitch(region, queryClient, boundId?)forwards the boundId toregionStore.setRegion(manual picks get the region's effective bound id).
2.4 src/components/RegionSelector.tsx
listRegionsnow returnseffectiveBoundIdper region → pass it throughconfirmRegionSwitchso a manually chosen region still has aboundId.
3. Consuming the flat JSON
3.1 src/hooks/prominent-api-hooks.ts
useGetEssentialConsts- read
boundIdfromuseRegion()(notregionId); trpc.common.essentialConsts.useQuery({ boundId }, { refetchInterval: 60000, enabled: isReady && boundId != null, placeholderData: initialData });- persist under
`essential_consts_b${boundId}`(wasessential_consts_r{regionId}).
- read
- URL builders (rename to the new keys):
useCacheUrl/useStoreWithProducts→products_version_num(wascacheVersion);useAvailabilityCacheUrl→availability_version_num;useSlotsCacheUrl→slots_version_num;- region id in URLs →
data.regionId(viauseDiscoveryRegionId, now reading the flatregionIdwithuseRegion().regionIdfallback).
useRegionalConsts→ thin passthrough of the flat data (delivery values are already top-level). Keep the exported function socart-page,checkout-page,floating-cart-bar, andPaymentAndOrderComponentneed no edits.- Remove the
effectiveSettings/regionSettingsfallback fields fromDiscoveryDataand the now unuseddiscoverySettingshelper.
3.2 Unchanged (verify only)
cart-page.tsx,checkout-page.tsx,floating-cart-bar.tsx,PaymentAndOrderComponent.tsxkeep readingdeliveryCharge/flashDeliveryCharge/freeDeliveryThreshold/flashFreeDeliveryThreshold/minRegularOrderValue(now flat).AddToCartDialog.tsx(isFlashDeliveryEnabled),flash-delivery/_layout.tsx(isFlashDeliveryEnabled),NextOrderGlimpse.tsx(supportMobile),me/complaints/index.tsx(supportMobile/supportEmail),UpdateChecker.tsx(internalAppVersion),HealthTestWrapper.tsx(versionNum/playStoreUrl/appStoreUrl),WebViewWrapper.tsx(webviewHtml/isWebviewClosable) — field names unchanged in the flat JSON.LocationBar.tsx,LocationTestWrapper.tsx,FirstUserWrapper.tsx— region-only; unaffected.
4. Out of scope / consequences
apps/web-ui(src/hooks/prominent-api-hooks.ts) still callscommon.essentialConsts({ regionId })and readscacheVersion/availabilityVersionNum/slotsVersionNum→ will fail input validation and show no data until migrated. Not in this plan.- If the hard break must be avoided, Part 1 can add a
regionIdfallback (server maps togetEffectiveBoundId(regionId)); then web-ui keeps working read-only. Decide before shipping.
5. Verification
- Append planned diffs + timestamp to
change-log.txtfirst. - Typecheck:
npx tsc --noEmit --project apps/user-ui/tsconfig.json. - Smoke (after Part 1 is deployed + migration 0009 applied):
- Pick/auto-resolve a region →
boundIdpersisted infreshyo-region. common.essentialConstscalled with{ boundId }; payload cached underessential_consts_b{boundId}.- Catalog URLs contain
products_version_num/availability_version_num/slots_version_numand the correctregionId. - Cart/checkout show the selected bound's delivery values; switching region updates them.
- Flash gate (
isFlashDeliveryEnabled), update gate (internalAppVersion/versionNum), support links, and webview notice all still work.
- Pick/auto-resolve a region →
6. Checklist
change-log.txt— append planned diffs.src/store/regionStore.ts—selectedBoundId+ persist.src/hooks/useRegion.ts— exposeboundId; persist on resolve.src/lib/region-switch.ts— forward boundId.src/components/RegionSelector.tsx— useeffectiveBoundId.src/hooks/prominent-api-hooks.ts—{ boundId }input, per-bound cache key, renamed version fields, simplifieduseRegionalConsts, dropeffectiveSettingsfallbacks.- Typecheck
apps/user-ui; fix self-introduced errors.