Skip to content
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

feat: Pesist FlowEditor state on route changes #3671

Merged
merged 9 commits into from
Sep 17, 2024

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Sep 14, 2024

What does this PR do?

  • Ensures that there's a single, persistent, instance of FlowEditor which is not repeatedly mounted and unmounted on route change.
  • This is achieved by removing the depency of the useLoadingRoute() hook fairly high in the component hierarchy. This hook was causing all child components (the majority of the application) to re-render once it resolved - this is what was causing all re-render, and the related flash of unstyled content (FOUC), on each route change.

Also..

  • Removes redundant /service-flags and /data routes and components (see comments below about why this was included)

Context

This work has come about as a result of search, but has addressed a fairly long term problem we've been facing with routing.

Search is pretty frustrating as the entire state is reset each time a link / node is clicked. Having delved into this routing / re-rendering issue unsuccessfully in the past, my first thought was to use Zustand to store the state to ensure this is persisted - please see #3672 as an example of what this looks like.

This approach works - but I soon realised that in order for it to be functional it's not just the active tab that needs to be persisted, it's (at least) -

  • Search term
  • Search results
  • Scroll position
  • (and reset all of these when I navigate to a new flow)

The same applies to all tabs, and all state within them - clearly this not a viable or maintainable solution.

Solution

It took a lot of digging and frustration to get here! 😅

It turns out that re-rendering is happening due to the use of the useLoadingRoute() hook. This hook triggers a re-render of all children once it resolves and returns a truthy value. Rather than this high-level approach, I think we need a more granular approach to page-by-page loading, or another general solution (routing loading bar?).

Demo

Issue Before (Staging) After (Pizza)
FOUC on route change
Screen.Recording.2024-09-14.at.11.00.08.mov
Screen.Recording.2024-09-14.at.11.02.37.mov
FlowEditor state lost when modal opened
Screen.Recording.2024-09-14.at.11.04.35.mov
Screen.Recording.2024-09-14.at.11.06.56.mov

Outstanding questions

How should we handle genuine loading screens between pages, where a re-render isn't a problem?

Some first thoughts -

We do need some UI here, because whilst removing the FOUC is a big win both visually and in terms of state management (imho!), I do acknowledge that clicking certain links now feels more sluggish as there's a wait between click and visual result. Prime examples here are creating and loading flows.

Copy link

github-actions bot commented Sep 14, 2024

Removed vultr server and associated DNS entries

Comment on lines +51 to +52
const isLoading = useLoadingRoute();
if (isLoading) return <DelayedLoadingIndicator />;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, the login page flashed on initial load of the application.

import React, { PropsWithChildren } from "react";
import { ErrorBoundary } from "react-error-boundary";

const FlowEditorLayout: React.FC<PropsWithChildren> = ({ children }) => (
<ErrorBoundary FallbackComponent={ErrorFallback}>{children}</ErrorBoundary>
<ErrorBoundary FallbackComponent={ErrorFallback}>
<FlowEditor />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the single FlowEditor instance, attached to the layout, which persists regardless of route.

@DafyddLlyr DafyddLlyr requested a review from a team September 14, 2024 10:12
@DafyddLlyr DafyddLlyr marked this pull request as ready for review September 14, 2024 10:13
Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

Big one! Thanks for spending time with this.

Code is looking good, one note/question about some unexpected pizza behavior though

editor.planx.uk/src/routes/team.tsx Outdated Show resolved Hide resolved
editor.planx.uk/src/routes/team.tsx Outdated Show resolved Hide resolved
Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

Previous issue now fixed and everything else working as expected, thanks !!

@DafyddLlyr DafyddLlyr marked this pull request as draft September 16, 2024 16:01
@DafyddLlyr
Copy link
Contributor Author

Moved to draft, hit a bug here where I can't route to internal portals. Will re-open once fixed.

@DafyddLlyr DafyddLlyr marked this pull request as ready for review September 16, 2024 16:40
@DafyddLlyr
Copy link
Contributor Author

I have an implementation of a loading bar here - #3684

Thoughts welcome!

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