Skip to content

fix(pos-app): load default partner API key on first install#377

Merged
ignaciosantise merged 1 commit intomainfrom
feat/pos-default-partner-key
Feb 5, 2026
Merged

fix(pos-app): load default partner API key on first install#377
ignaciosantise merged 1 commit intomainfrom
feat/pos-default-partner-key

Conversation

@ignaciosantise
Copy link
Copy Markdown
Collaborator

Problem

On first app install, the default partner API key was not preloaded. Users had to manually navigate to Settings, clear the merchant ID, and save to trigger the default values. Additionally, after resetting to defaults, the Partner API Key field appeared empty until navigating away and back.

Root Causes

  1. Race condition: clearStaleSecureStorage() in _layout.tsx was running after onRehydrateStorage had already saved the default API key, deleting it immediately
  2. State mismatch: After clearMerchantId saved a new API key, the useMerchantFlow hook's local state wasn't refreshed because the useEffect dependency was stable

Solution

  • Moved clearStaleSecureStorage() to run at the start of onRehydrateStorage (before defaults are set)
  • Added isMerchantApiKeySet as a dependency to the API key loading effect to trigger state refresh after changes

Changes

  • store/useSettingsStore.ts: Call clearStaleSecureStorage() at start of hydration
  • app/_layout.tsx: Remove the now-redundant clearStaleSecureStorage() call
  • hooks/use-merchant-flow.ts: Add dependency to refresh API key state on store updates

Testing

  • Fresh install: merchant ID and partner API key should both load from env defaults
  • Payment creation should work immediately without manual settings adjustment
  • Settings reset: Partner API Key field should show masked value immediately

Fix race condition where clearStaleSecureStorage() was deleting the default
partner API key immediately after it was saved during store hydration.

Also ensure the merchant flow hook refreshes the local API key state when
the store updates via clearMerchantId().

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 5, 2026 15:42
@vercel
Copy link
Copy Markdown

vercel bot commented Feb 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-native-examples Ready Ready Preview, Comment Feb 5, 2026 3:44pm

Request Review

@claude
Copy link
Copy Markdown

claude bot commented Feb 5, 2026

Claude finished @ignaciosantise's task in 1m 3s —— View job


Code Review

Tasks:

  • Read modified files for full context
  • Analyze race condition fix
  • Analyze state management fix
  • Check for issues
  • Provide feedback

✅ No issues found

Analysis Summary:

Race condition fix (lines 303-305 in useSettingsStore.ts):

  • clearStaleSecureStorage() now runs at start of onRehydrateStorage before defaults are set
  • Prevents deletion of freshly-set default API key on first install
  • Correctly clears stale secure storage only when app_has_launched flag is false

State refresh fix (line 78 in use-merchant-flow.ts):

  • Added isMerchantApiKeySet to useEffect dependency array
  • Ensures storedMerchantApiKey local state refreshes after clearMerchantId updates the store
  • Previously, stable getMerchantApiKey reference prevented re-run after store changes

Code removal (_layout.tsx lines 71-74 removed):

  • Redundant clearStaleSecureStorage() call removed since it now runs during hydration
  • Prevents duplicate execution and potential timing issues

All changes correctly address the reported issues. Logic is sound, no edge cases identified.


Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes two critical issues that prevented the default partner API key from being properly loaded on first app install: a race condition where stale storage clearing happened after default values were set, and a state synchronization issue where the UI didn't refresh after resetting credentials.

Changes:

  • Fixed race condition by moving clearStaleSecureStorage() to execute at the start of store hydration, before default values are written
  • Added isMerchantApiKeySet as a dependency to trigger API key state refresh in the merchant flow hook
  • Removed redundant clearStaleSecureStorage() call from _layout.tsx

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
dapps/pos-app/store/useSettingsStore.ts Moved clearStaleSecureStorage() to run at the start of onRehydrateStorage callback to execute before default values are set
dapps/pos-app/hooks/use-merchant-flow.ts Added isMerchantApiKeySet as a useEffect dependency to refresh local API key state when store updates
dapps/pos-app/app/_layout.tsx Removed duplicate clearStaleSecureStorage() call that was causing the race condition

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ignaciosantise ignaciosantise merged commit 53347ed into main Feb 5, 2026
15 checks passed
@ignaciosantise ignaciosantise deleted the feat/pos-default-partner-key branch February 5, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants