Refactor share-auth flow into reducer + driver pattern#426
Open
valentinpalkovic wants to merge 1 commit intovalentin/viral-sharing-2from
Open
Refactor share-auth flow into reducer + driver pattern#426valentinpalkovic wants to merge 1 commit intovalentin/viral-sharing-2from
valentinpalkovic wants to merge 1 commit intovalentin/viral-sharing-2from
Conversation
Move share-popover state into a typed reducer and split the OAuth sign-in into pluggable drivers (device-code, authorization-code) behind a single SignInDriver interface. Adds the verifying screen, snapshot- based resume across remounts, and a toggle so we can swap OAuth flows without touching call sites.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Refactors the share popover's authentication flow so the UI state and the OAuth state machine no longer share a single tangled component. The popover now drives a typed reducer for screen transitions, and OAuth itself lives behind a small pluggable driver interface with two implementations: device-code (verification code in a new tab + polled token) and authorization-code (popup with redirect back to the addon).
The driver-level split means the rest of the addon never sees the difference — the popover, manager redirect handler, and Authentication screen all consume the same
SignInDriverinterface and resume from the same snapshot shape across remounts. This makes it cheap to switch flows for experiments or rollback without touching call sites.What changed
shareReducer) owns the popover screen + share-request lifecycle, with a singleSTART_UPLOADaction covering all four ways an upload can begin (initial publish, repeat publish, post-auth, and signed-in auto-skip).SignInDriverinterface with two implementations:useShareAuthhook owns the driver lifecycle (start, resume, cancel, snapshot persistence) and translates driver outcomes into reducer actions.managerno longer instantiates a driver at module load — it's gated by an exported boolean constant that mirrors the configured flow.Toggling the OAuth flow
There is a single switch that controls which OAuth flow the addon uses. To change it, edit the constant in
src/utils/signInDriver.ts:Set it to either:
'device-code'— current default. The addon opens the verification URL in a new browser tab, the user signs in on Chromatic, and the addon polls the token endpoint until a token is issued or the device code expires. No popup, no redirect, no special manager handling.'authorization-code'— the addon opens a popup window pointed at Chromatic's authorization URL, Chromatic redirects back to the addon's manager URL with acode+state, the manager forwards the grant to the popover viapostMessage, and the driver exchanges the code for a token.The same constant also drives
handlesOAuthRedirect, an exported boolean the manager checks before installing its OAuth redirect listener — so flipping the switch automatically wires up (or removes) the redirect handler without any further code changes. Snapshots stored in session storage from a previous flow are dropped on mount when they don't match the active flow, so a switch never strands a user mid-sign-in.Test plan
OAUTH_FLOW = 'device-code': verification screen renders with the digit chips, opens the correct URL in a new tab, polls and completes upload after the user authorizes on Chromatic.OAUTH_FLOW = 'authorization-code': popup opens, redirect lands back on the manager, popover transitions to uploading afterpostMessage.share-canceled.yarn vitest run,yarn lint, andtsc --noEmit— all green.🤖 Generated with Claude Code