-
Notifications
You must be signed in to change notification settings - Fork 114
feat(core,plugin-history-sync): allow step actions for non-top activities #642
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
980c183
7696863
42cde0f
398bcf8
8f5a022
ae7c98d
289cd62
a8167c8
57b087f
d951a66
8807c12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| "@stackflow/core": minor | ||
| "@stackflow/plugin-history-sync": minor | ||
| --- | ||
|
|
||
| Allow step actions to target lower activities with targetActivityId | ||
|
|
||
| **Core Changes:** | ||
| - Step actions (stepPush, stepPop, stepReplace) can now target any activity in the stack using `targetActivityId` option | ||
| - Previously only the top activity could be targeted | ||
| - Enables modifying previous activity parameters when popping current activity | ||
|
|
||
| **History Sync Plugin Changes:** | ||
| - Added intelligent synchronization when navigating to modified lower activities | ||
| - Uses `history.back()` for step pop to avoid duplicate entries | ||
| - Uses `replaceState()` for step push/replace (accepts forward history volatility per stack semantics) | ||
| - Handles complex step operation sequences correctly | ||
|
|
||
| **Use Case:** | ||
| ```typescript | ||
| // Pop and modify previous activity in single operation | ||
| actions.pop({ | ||
| onBeforePop: () => { | ||
| actions.stepReplace({ newParams }, { targetActivityId: previousActivityId }); | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| This is backward compatible - existing code works without changes. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -395,6 +395,60 @@ export function historySyncPlugin< | |||||||||||||||||||||||||||||||||||||
| (step) => step.id === targetStep?.id, | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Synchronize history state with core state for lower activity step modifications | ||||||||||||||||||||||||||||||||||||||
| // Only apply this logic for non-active (lower) activities to avoid interfering | ||||||||||||||||||||||||||||||||||||||
| // with normal step navigation on the active activity | ||||||||||||||||||||||||||||||||||||||
| if (nextActivity && !nextActivity.isActive) { | ||||||||||||||||||||||||||||||||||||||
| const coreSteps = nextActivity.steps; | ||||||||||||||||||||||||||||||||||||||
| const coreLastStep = last(coreSteps); | ||||||||||||||||||||||||||||||||||||||
| const historyStep = targetStep; | ||||||||||||||||||||||||||||||||||||||
| const historySteps = targetActivity.steps; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Check if history step exists in core state | ||||||||||||||||||||||||||||||||||||||
| const historyStepExistsInCore = historyStep | ||||||||||||||||||||||||||||||||||||||
| ? coreSteps.some((step) => step.id === historyStep.id) | ||||||||||||||||||||||||||||||||||||||
| : true; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (!historyStepExistsInCore) { | ||||||||||||||||||||||||||||||||||||||
| // History step was removed or replaced | ||||||||||||||||||||||||||||||||||||||
| if (coreSteps.length < historySteps.length) { | ||||||||||||||||||||||||||||||||||||||
| // Step Pop: Step count decreased | ||||||||||||||||||||||||||||||||||||||
| // Use history.back() to skip the removed step entry | ||||||||||||||||||||||||||||||||||||||
| requestHistoryTick(() => { | ||||||||||||||||||||||||||||||||||||||
| silentFlag = true; | ||||||||||||||||||||||||||||||||||||||
| history.back(); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multi-step pop only calls back() once — can leave user on an invalid, still-removed step. If core popped N>1 steps on a lower activity, a single Apply this diff: - if (coreSteps.length < historySteps.length) {
- // Step Pop: Step count decreased
- // Use history.back() to skip the removed step entry
- requestHistoryTick(() => {
- silentFlag = true;
- history.back();
- });
- return;
- } else {
+ if (coreSteps.length < historySteps.length) {
+ // Step Pop: jump back by the exact number of removed steps
+ const popDelta = historySteps.length - coreSteps.length;
+ if (popDelta > 0) {
+ requestHistoryTick(() => {
+ silentFlag = true;
+ history.go(-popDelta);
+ });
+ }
+ return;
+ } else {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| // Step Replace or complex operations: Step count same or increased | ||||||||||||||||||||||||||||||||||||||
| // Use replaceState to show current core state | ||||||||||||||||||||||||||||||||||||||
| const match = activityRoutes.find( | ||||||||||||||||||||||||||||||||||||||
| (r) => r.activityName === nextActivity.name, | ||||||||||||||||||||||||||||||||||||||
| )!; | ||||||||||||||||||||||||||||||||||||||
| const template = makeTemplate(match, options.urlPatternOptions); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| requestHistoryTick(() => { | ||||||||||||||||||||||||||||||||||||||
| silentFlag = true; | ||||||||||||||||||||||||||||||||||||||
| replaceState({ | ||||||||||||||||||||||||||||||||||||||
| history, | ||||||||||||||||||||||||||||||||||||||
| pathname: template.fill(nextActivity.params), | ||||||||||||||||||||||||||||||||||||||
| state: { | ||||||||||||||||||||||||||||||||||||||
| activity: nextActivity, | ||||||||||||||||||||||||||||||||||||||
| step: coreLastStep, | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| useHash: options.useHash, | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // If history step exists in core, proceed with normal navigation logic | ||||||||||||||||||||||||||||||||||||||
| // This handles: | ||||||||||||||||||||||||||||||||||||||
| // - Step exists but is not the last (normal step backward) | ||||||||||||||||||||||||||||||||||||||
| // - Multiple step pushes then navigating to middle steps | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const isBackward = () => currentActivity.id > targetActivityId; | ||||||||||||||||||||||||||||||||||||||
| const isForward = () => currentActivity.id < targetActivityId; | ||||||||||||||||||||||||||||||||||||||
| const isStep = () => currentActivity.id === targetActivityId; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.