1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-03 01:18:43 +02:00

Fix/last viewed project (#2530)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

Fix using projectId on redirect.

Fix: only redirect to project(s) on app load


## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2022-11-28 16:10:40 +02:00 committed by GitHub
parent 1fec43947d
commit 1eb0116e11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 55 deletions

View File

@ -30,7 +30,7 @@ const AUTH_PASSWORD = Cypress.env('AUTH_PASSWORD');
Cypress.Commands.add('login', (user = AUTH_USER, password = AUTH_PASSWORD) => Cypress.Commands.add('login', (user = AUTH_USER, password = AUTH_PASSWORD) =>
cy.session(user, () => { cy.session(user, () => {
cy.visit('/'); cy.visit('/');
cy.wait(1000); cy.wait(1500);
cy.get("[data-testid='LOGIN_EMAIL_ID']").type(user); cy.get("[data-testid='LOGIN_EMAIL_ID']").type(user);
if (AUTH_PASSWORD) { if (AUTH_PASSWORD) {

View File

@ -1,4 +1,4 @@
import { Suspense } from 'react'; import { Suspense, useCallback, useEffect, useState } from 'react';
import { Navigate, Route, Routes } from 'react-router-dom'; import { Navigate, Route, Routes } from 'react-router-dom';
import { ErrorBoundary } from 'react-error-boundary'; import { ErrorBoundary } from 'react-error-boundary';
import { Error } from 'component/layout/Error/Error'; import { Error } from 'component/layout/Error/Error';
@ -17,14 +17,48 @@ import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
import { SplashPageRedirect } from 'component/splash/SplashPageRedirect/SplashPageRedirect'; import { SplashPageRedirect } from 'component/splash/SplashPageRedirect/SplashPageRedirect';
import { useStyles } from './App.styles'; import { useStyles } from './App.styles';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import useProjects from '../hooks/api/getters/useProjects/useProjects';
import { useLastViewedProject } from '../hooks/useLastViewedProject';
const InitialRedirect = () => {
const { lastViewed } = useLastViewedProject();
const { projects, loading } = useProjects();
const [redirectTo, setRedirectTo] = useState<string | undefined>();
const getRedirect = useCallback(() => {
if (projects && lastViewed) {
return `/projects/${lastViewed}`;
}
if (projects && !lastViewed && projects.length === 1) {
return `/projects/${projects[0].id}`;
}
return '/projects';
}, [lastViewed, projects]);
useEffect(() => {
if (!loading) {
setRedirectTo(getRedirect());
}
}, [loading, getRedirect]);
if (loading || !redirectTo) {
return <Loader />;
}
return <Navigate to={redirectTo} />;
};
export const App = () => { export const App = () => {
const { classes: styles } = useStyles();
const { authDetails } = useAuthDetails(); const { authDetails } = useAuthDetails();
const { user } = useAuthUser(); const { user } = useAuthUser();
const { isOss } = useUiConfig();
const hasFetchedAuth = Boolean(authDetails || user); const hasFetchedAuth = Boolean(authDetails || user);
const { classes: styles } = useStyles();
const { isOss } = useUiConfig();
const availableRoutes = isOss() const availableRoutes = isOss()
? routes.filter(route => !route.enterprise) ? routes.filter(route => !route.enterprise)
: routes; : routes;
@ -38,6 +72,7 @@ export const App = () => {
condition={!hasFetchedAuth} condition={!hasFetchedAuth}
show={<Loader />} show={<Loader />}
elseShow={ elseShow={
<>
<div className={styles.container}> <div className={styles.container}>
<ToastRenderer /> <ToastRenderer />
<Routes> <Routes>
@ -61,12 +96,7 @@ export const App = () => {
))} ))}
<Route <Route
path="/" path="/"
element={ element={<InitialRedirect />}
<Navigate
to="/projects"
replace
/>
}
/> />
<Route <Route
path="*" path="*"
@ -76,6 +106,7 @@ export const App = () => {
<FeedbackNPS openUrl="http://feedback.unleash.run" /> <FeedbackNPS openUrl="http://feedback.unleash.run" />
<SplashPageRedirect /> <SplashPageRedirect />
</div> </div>
</>
} }
/> />
</PlausibleProvider> </PlausibleProvider>

View File

@ -1,5 +1,5 @@
import { useContext, useEffect, useMemo, useState } from 'react'; import { useContext, useEffect, useMemo, useState } from 'react';
import { Link, Navigate, useNavigate, useSearchParams } from 'react-router-dom'; import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import { mutate } from 'swr'; import { mutate } from 'swr';
import { getProjectFetcher } from 'hooks/api/getters/useProject/getProjectFetcher'; import { getProjectFetcher } from 'hooks/api/getters/useProject/getProjectFetcher';
import useProjects from 'hooks/api/getters/useProjects/useProjects'; import useProjects from 'hooks/api/getters/useProjects/useProjects';
@ -152,10 +152,6 @@ export const ProjectListNew = () => {
? `${filteredProjects.length} of ${projects.length}` ? `${filteredProjects.length} of ${projects.length}`
: projects.length; : projects.length;
if (projects?.length === 1) {
return <Navigate to={`/projects/${projects[0].id}`} replace />;
}
return ( return (
<div ref={ref}> <div ref={ref}>
<PageContent <PageContent

View File

@ -9,22 +9,15 @@ import Authentication from '../Authentication/Authentication';
import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails'; import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails';
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser'; import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
import { parseRedirectParam } from 'component/user/Login/parseRedirectParam'; import { parseRedirectParam } from 'component/user/Login/parseRedirectParam';
import { useLastViewedProject } from '../../../hooks/useLastViewedProject';
const Login = () => { const Login = () => {
const { classes: styles } = useStyles(); const { classes: styles } = useStyles();
const { authDetails } = useAuthDetails(); const { authDetails } = useAuthDetails();
const { user } = useAuthUser(); const { user } = useAuthUser();
const { lastViewed } = useLastViewedProject();
const query = useQueryParams(); const query = useQueryParams();
const resetPassword = query.get('reset') === 'true'; const resetPassword = query.get('reset') === 'true';
const invited = query.get('invited') === 'true'; const invited = query.get('invited') === 'true';
const redirect = query.get('redirect') || '/';
const getRedirect = () => {
return lastViewed == null ? '/projects' : `/projects/${lastViewed}`;
};
const redirect = query.get('redirect') || getRedirect();
if (user) { if (user) {
return <Navigate to={parseRedirectParam(redirect)} replace />; return <Navigate to={parseRedirectParam(redirect)} replace />;

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { setLocalStorageItem, getLocalStorageItem } from '../utils/storage'; import { getLocalStorageItem, setLocalStorageItem } from '../utils/storage';
import useUiConfig from './api/getters/useUiConfig/useUiConfig'; import useUiConfig from './api/getters/useUiConfig/useUiConfig';
export const useLastViewedProject = () => { export const useLastViewedProject = () => {