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

Enable react router v7_startTransition #1259 #1264

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/e2e/with_mock_data/systems.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
cy.location('search').should('eq', '');
});

it('should be able to navigate through subsystems while preserving the table states when going back', () => {
it.only('should be able to navigate through subsystems while preserving the table states when going back', () => {

Check failure on line 132 in cypress/e2e/with_mock_data/systems.cy.ts

View workflow job for this annotation

GitHub Actions / Lint & Unit Tests

it.only not permitted
cy.visit('/systems/65328f34a40ff5301575a4e3');

cy.findByText('Smaller laser').should('be.visible');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
"preview:build:dev": "yarn build --watch & yarn preview",
"test": "vitest --coverage",
"lint": "eslint --max-warnings=0 --fix ./src ./cypress && tsc --noEmit && tsc --noEmit -p cypress/tsconfig.json",
"build:e2e": "cross-env VITE_APP_BUILD_STANDALONE=true VITE_APP_INCLUDE_MSW=true GENERATE_SOURCEMAP=false yarn build",
"build:e2e": "cross-env VITE_APP_BUILD_STANDALONE=true VITE_APP_INCLUDE_MSW=true GENERATE_SOURCEMAP=false yarn build --watch",
"build:e2e:api": "cross-env VITE_APP_BUILD_STANDALONE=true VITE_APP_INCLUDE_MSW=false GENERATE_SOURCEMAP=false yarn build",
"e2e:serve": "yarn build:e2e && node ./server/e2e-test-server.js",
"e2e:serve": "yarn build:e2e & node ./server/e2e-test-server.js",
"e2e:serve:api": "yarn build:e2e:api && node ./server/e2e-test-server.js",
"e2e:interactive": "start-server-and-test e2e:serve http://localhost:3000 cy:open",
"e2e:interactive:api": "start-server-and-test e2e:serve:api http://localhost:3000 cy:open",
Expand Down
3 changes: 1 addition & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ export default function App() {
<RouterProvider
router={router}
future={{
// Disabled for now and will be addressed in #1259
v7_startTransition: false,
v7_startTransition: true,
}}
/>
);
Expand Down
63 changes: 59 additions & 4 deletions src/common/preservedTableState.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,29 @@
// Keeps track of the last location state update to occur (for detecting browser changes e.g. back button being clicked)
const lastLocationUpdate = useRef(location);

const searchParamsFromWindow = new URLSearchParams(window.location.search);
const urlParamName = props?.urlParamName || 'state';
const log = (value: unknown) => console.log(value + `[${urlParamName}]`);
const compressedState = props?.storeInUrl
? searchParams.get(urlParamName)
? searchParamsFromWindow.get(urlParamName)
: null;
const unparsedState = decompressState(compressedState);

const [parsedState, setParsedState] = useState<StatePartial>(
getParsedState(unparsedState)
);
log('---------- NEW RENDER ---------');
log('Search params: ' + searchParams.toString());
log('Search params 2: ' + searchParamsFromWindow.toString());
log(window.location.search);
log('Unparsed state ' + unparsedState + ' (render)');
log('Parsed state ' + Object.entries(parsedState) + ' (render)');

// Update the search params only if necessary
useEffect(() => {
log('HERE');
log(window.location.search);
log(location.search);
if (props?.storeInUrl) {
// Get the expected unparsed state in the URL for the current internal state
const parsedStateSearchParams = convertInternalState(parsedState);
Expand All @@ -212,14 +223,48 @@
) {
// Clear search params if state is no longer needed
if (newUnparsedState !== '{}') {
searchParams.set(
log('Updating search params (useEffect)');
log('New state: ' + newUnparsedState);
log('Existing search params: ' + searchParamsFromWindow.toString());
searchParamsFromWindow.set(
urlParamName,
LZString.compressToEncodedURIComponent(newUnparsedState)
);
setSearchParams(searchParams, { replace: false });
log('New search params: ' + searchParamsFromWindow.toString());
setSearchParams(searchParamsFromWindow, { replace: false });

// setSearchParams(
// (prev) => {
// console.log(prev.entries());
// const next = new URLSearchParams(
// Object.fromEntries(prev.entries())
// );
// console.log(next.entries());
// next.set(
// urlParamName,
// LZString.compressToEncodedURIComponent(newUnparsedState)
// );
// console.log(newUnparsedState);
// console.log(next.toString());
// return next;
// },
// { replace: false }
// );
} else {
log('Clearing search params (useEffect)');
searchParams.delete(urlParamName);
setSearchParams(searchParams, { replace: false });

// setSearchParams(
// (prev) => {
// const next = new URLSearchParams(
// Object.fromEntries(prev.entries())
// );
// next.delete(urlParamName);
// return next;
// },
// { replace: false }
// );
}
} else {
// Update the internal state to reflect the browser level change
Expand All @@ -229,17 +274,21 @@
if (lastLocationUpdate.current.pathname !== location.pathname)
firstUpdate.current.p = undefined;

log(
'Updating internal state to reflect browser level change (useEffect)'
);

setParsedState(getParsedState(unparsedState));
}
}

lastLocationUpdate.current = location;
}
}, [

Check warning on line 287 in src/common/preservedTableState.component.tsx

View workflow job for this annotation

GitHub Actions / Lint & Unit Tests

React Hook useEffect has missing dependencies: 'log' and 'searchParams'. Either include them or remove the dependency array
location,
parsedState,
props?.storeInUrl,
searchParams,
searchParamsFromWindow,
setSearchParams,
unparsedState,
urlParamName,
Expand Down Expand Up @@ -540,6 +589,12 @@
updaterOrValue,
prevState.p || defaultState.p
);
log(
'Updating internal params: ' +
newValue.pageIndex +
' ' +
newValue.pageSize
);
return {
...prevState,
p:
Expand All @@ -549,7 +604,7 @@
};
});
},
[defaultState.p, props?.paginationOnly, state.p, updateSearchParams]

Check warning on line 607 in src/common/preservedTableState.component.tsx

View workflow job for this annotation

GitHub Actions / Lint & Unit Tests

React Hook useCallback has a missing dependency: 'log'. Either include it or remove the dependency array
);

return {
Expand Down
Loading