Skip to content

Enhance view state management by integrating UiStore for scroll position and selected event persistence #160

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

metheos
Copy link

@metheos metheos commented Jun 15, 2025

addresses httptoolkit/httptoolkit#470 and further adds selected event persistence

@CLAassistant
Copy link

CLAassistant commented Jun 15, 2025

CLA assistant check
All committers have signed the CLA.

…ion and selected event persistence

addresses httptoolkit/httptoolkit#470 and further adds selected event persistence
Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @metheos! At a glance it looks like this is very promising, this is a nice improvement I'd definitely like to include. It looks like something has autoformatted all the UI store code along the way though, which makes this impossible to review and would break git blame later on.

Can you undo that please, so only the actual changes for this PR are included changes?

@metheos
Copy link
Author

metheos commented Jun 20, 2025

Should be good now. Missed that when I was testing. Changes were pretty minimal there!

Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

Thanks @metheos! That's much clearer. I've add a proper review here - I think this will be great, but it needs a little work to tidy up the details and avoid the setTimeout etc workarounds.

By the way, in case you're not aware, HTTP Toolkit Pro is totally free for all contributors! I've just set up an account linked to your git commit email, just click 'Get Pro' then 'Log into existing account' and enter your email to get started. Thanks for getting involved! 👍

setTimeout(() => {
this.hasRestoredInitialState = true;
}, 100);
}, 100);
Copy link
Member

Choose a reason for hiding this comment

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

Would be good to avoid the fixed timeouts here. I think we can do so though - we just need to wait until listBodyRef is ready, right? I think we can convert this into a callback ref, which means we can trigger the DOM interaction (updating the scroll position) immediately after that's rendered.

That will be a bit more responsive (no visible jumping around I hope) and should avoid most of the risk of the race condition you're protecting against with hasRestoredInitialState here as well. Does that make sense?

Copy link
Author

Choose a reason for hiding this comment

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

yes, good idea. I wasn't sure what to look for. I'll get rid of this and set it to be handled by a new callback ref (setListBodyRef)

// If the filtered events changed (e.g., new events loaded), try to restore scroll position
setTimeout(() => {
this.restoreScrollPosition();
}, 50);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I totally understand the goal of this bit. Can you explain when this applies in practice, and how it helps? I haven't noticed issues with the scroll position while filtering.

Copy link
Author

Choose a reason for hiding this comment

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

I put it there as more of a safeguard since I've had issues with wonky scroll position behavior depending on the browser/rendering engine in use. We can take it out if you don't expect that to change, I wasn't sure how consistent the web view experience was across platforms.

// If no URL eventId but we have a persisted selection, restore it
setTimeout(() => {
this.listRef.current?.restoreViewState();
}, 100);
Copy link
Member

Choose a reason for hiding this comment

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

Ditto here - we should restructure this to trigger when listRef is set, directly, instead of guessing how long it'll take.

Copy link
Author

Choose a reason for hiding this comment

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

got it, added new callback (setListRef)

metheos and others added 4 commits June 25, 2025 16:44
I don't think we have a clear case where this is needed - can re-evalate
if we find this does have some specific missing edge cases though.
@pimterry
Copy link
Member

pimterry commented Jul 4, 2025

Nice, thanks @metheos! Yes, those were exactly the changes I was looking for. I've taken that a little further, dropped the setTimeout (we don't need to worry much about browser compat - Electron means we effectively ship our own specific browser version for most uses, and I would expect there won't be many issues anyway), and tried to simplify the logic around the view page controlling the list. Can you give it a quick test, and make sure it still works as you expect? I'm not sure if I've missed some case you were trying to cover. If you're happy though, just let me know and I think it's good to merge now.

@metheos
Copy link
Author

metheos commented Jul 6, 2025

Your commit c300f10 removes the intended restoration of the scroll position. Without that code the list will always scroll to the selected event rather than restoring the actual scroll position.

@pimterry
Copy link
Member

pimterry commented Jul 8, 2025

Without that code the list will always scroll to the selected event rather than restoring the actual scroll position.

Hmm, it's just it seems like that was what it was trying to do before though:

  • View page set shouldRestoreViewStateOnRefSet if uiStore.selectedEventId was set
  • The ref triggered a call to restoreViewState
  • In restoreViewState, your code intentionally did exactly what you describe:
    if (this.props.selectedEvent) {
      this.scrollToEvent(this.props.selectedEvent);
    } else {
      this.restoreScrollPosition();
    }
    

But regardless, I think you are right - restoring the scroll position (never jumping to the automatically persisted selected event) by default is the best choice.

I should have a minute to look at that and fix up my changes tomorrow, or you're welcome to take a look. Sorry this has been delayed here a while, but we should be able to get it live this week!

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.

3 participants