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

Fixed "Back" button redirection #8277

Merged
merged 10 commits into from
Aug 19, 2024
Merged
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
4 changes: 4 additions & 0 deletions changelog.d/20240813_115132_klakhov_fix_go_back.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- Go back button behavior on analytics page
(<https://github.com/cvat-ai/cvat/pull/8277>)
2 changes: 1 addition & 1 deletion cvat-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-ui",
"version": "1.64.5",
"version": "1.64.6",
"description": "CVAT single-page application",
"main": "src/index.tsx",
"scripts": {
Expand Down
17 changes: 17 additions & 0 deletions cvat-ui/src/actions/navigation-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import { ActionUnion, createAction } from 'utils/redux';

export enum NavigationActionTypes {
CHANGE_LOCATION = 'CHANGE_LOCATION',
}

export const navigationActions = {
changeLocation: (from: string, to: string) => createAction(
NavigationActionTypes.CHANGE_LOCATION, { from, to },
),
};

export type NavigationActions = ActionUnion<typeof navigationActions>;
6 changes: 5 additions & 1 deletion cvat-ui/src/components/cvat-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ interface CVATAppProps {
initInvitations: () => void;
initRequests: () => void;
loadServerAPISchema: () => void;
onChangeLocation: (from: string, to: string) => void;
userInitialized: boolean;
userFetching: boolean;
organizationFetching: boolean;
Expand Down Expand Up @@ -141,7 +142,7 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP

public componentDidMount(): void {
const core = getCore();
const { history, location } = this.props;
const { history, location, onChangeLocation } = this.props;
const {
HEALTH_CHECK_RETRIES, HEALTH_CHECK_PERIOD, HEALTH_CHECK_REQUEST_TIMEOUT,
SERVER_UNAVAILABLE_COMPONENT, RESET_NOTIFICATIONS_PATHS,
Expand Down Expand Up @@ -173,6 +174,9 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
history.listen((newLocation) => {
customWaViewHit(newLocation.pathname, newLocation.search, newLocation.hash);
const { location: prevLocation } = this.props;

onChangeLocation(prevLocation.pathname, newLocation.pathname);

const shouldResetNotifications = RESET_NOTIFICATIONS_PATHS.from.some(
(pathname) => prevLocation.pathname === pathname,
);
Expand Down
5 changes: 5 additions & 0 deletions cvat-ui/src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ const LOCAL_STORAGE_LAST_FRAME_MEMORY_LIMIT = 20;

const REQUEST_SUCCESS_NOTIFICATION_DURATION = 5; // seconds

const BLACKLISTED_GO_BACK_PATHS = [
/\/auth.+/,
];

export default {
UNDEFINED_ATTRIBUTE_VALUE,
NO_BREAK_SPACE,
Expand Down Expand Up @@ -174,4 +178,5 @@ export default {
LOCAL_STORAGE_SEEN_GUIDES_MEMORY_LIMIT,
LOCAL_STORAGE_LAST_FRAME_MEMORY_LIMIT,
REQUEST_SUCCESS_NOTIFICATION_DURATION,
BLACKLISTED_GO_BACK_PATHS,
};
3 changes: 3 additions & 0 deletions cvat-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { resetErrors, resetMessages } from 'actions/notification-actions';
import { getInvitationsAsync } from 'actions/invitations-actions';
import { getRequestsAsync } from 'actions/requests-async-actions';
import { getServerAPISchemaAsync } from 'actions/server-actions';
import { navigationActions } from 'actions/navigation-actions';
import { CombinedState, NotificationsState, PluginsState } from './reducers';

createCVATStore(createRootReducer);
Expand Down Expand Up @@ -73,6 +74,7 @@ interface DispatchToProps {
initInvitations: () => void;
initRequests: () => void;
loadServerAPISchema: () => void;
onChangeLocation: (from: string, to: string) => void;
}

function mapStateToProps(state: CombinedState): StateToProps {
Expand Down Expand Up @@ -124,6 +126,7 @@ function mapDispatchToProps(dispatch: any): DispatchToProps {
initInvitations: (): void => dispatch(getInvitationsAsync({ page: 1 }, true)),
initRequests: (): void => dispatch(getRequestsAsync({ page: 1 })),
loadServerAPISchema: (): void => dispatch(getServerAPISchemaAsync()),
onChangeLocation: (from: string, to: string): void => dispatch(navigationActions.changeLocation(from, to)),
};
}

Expand Down
5 changes: 5 additions & 0 deletions cvat-ui/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,10 @@ export interface RequestsState {
query: RequestsQuery;
}

export interface NavigationState {
prevLocation: string | null;
}

export interface CombinedState {
auth: AuthState;
projects: ProjectsState;
Expand All @@ -989,6 +993,7 @@ export interface CombinedState {
webhooks: WebhooksState;
requests: RequestsState;
serverAPI: ServerAPIState;
navigation: NavigationState;
}

export interface Indexable {
Expand Down
43 changes: 43 additions & 0 deletions cvat-ui/src/reducers/navigation-reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import { NavigationActions, NavigationActionTypes } from 'actions/navigation-actions';
import config from 'config';
import { NavigationState } from '.';

const defaultState: NavigationState = {
prevLocation: null,
};

export default function (
state: NavigationState = defaultState,
action:NavigationActions,
): NavigationState {
switch (action.type) {
case NavigationActionTypes.CHANGE_LOCATION: {
const { BLACKLISTED_GO_BACK_PATHS } = config;
const { from, to } = action.payload;

if (from === to) {
return state;
}

for (const path of BLACKLISTED_GO_BACK_PATHS) {
if (path.test(from)) {
return {
...state,
prevLocation: null,
};
}
}

return {
...state,
prevLocation: from,
};
}
default:
return state;
}
}
2 changes: 2 additions & 0 deletions cvat-ui/src/reducers/root-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import webhooksReducer from './webhooks-reducer';
import invitationsReducer from './invitations-reducer';
import requestsReducer from './requests-reducer';
import serverAPIReducer from './server-api-reducer';
import navigationReducer from './navigation-reducer';

export default function createRootReducer(): Reducer {
return combineReducers({
Expand All @@ -51,5 +52,6 @@ export default function createRootReducer(): Reducer {
invitations: invitationsReducer,
requests: requestsReducer,
serverAPI: serverAPIReducer,
navigation: navigationReducer,
});
}
11 changes: 4 additions & 7 deletions cvat-ui/src/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2021-2022 Intel Corporation
// Copyright (C) 2023 CVAT.ai Corporation
// Copyright (C) 2023-2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -64,13 +64,10 @@ export function usePlugins(

export function useGoBack(): () => void {
const history = useHistory();
const prevLocation = useSelector((state: CombinedState) => state.navigation.prevLocation) ?? '/tasks';
const goBack = useCallback(() => {
if (history.action !== 'POP') {
history.goBack();
} else {
history.push('/');
}
}, []);
history.push(prevLocation);
}, [prevLocation]);

return goBack;
}
Expand Down
Loading