Skip to content

Commit 4b42b6e

Browse files
feat: handle deep linking for a logged out user (#3659)
1 parent d5cb801 commit 4b42b6e

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/organizations/apis/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// API
2-
import {getDashboards as apiGetDashboards} from 'src/client'
2+
import {getDashboards as apiGetDashboards, getOrgs} from 'src/client'
33

44
// Types
55
import {Dashboard, Organization} from 'src/types'
66

77
// Utils
88
import {addDashboardDefaults} from 'src/schemas/dashboards'
99

10+
// Metrics
11+
import {event} from 'src/cloud/utils/reporting'
12+
1013
// CRUD APIs for Organizations and Organization resources
1114
// i.e. Organization Members, Buckets, Dashboards etc
1215

@@ -33,3 +36,23 @@ export const getDashboards = async (
3336
throw error
3437
}
3538
}
39+
40+
export const getOrg = async () => {
41+
try {
42+
const resp = await getOrgs({})
43+
44+
if (resp.status !== 200) {
45+
throw new Error(resp.data.message)
46+
}
47+
48+
const {orgs} = resp.data
49+
50+
if (Array.isArray(orgs) && orgs.length) {
51+
return orgs[0]
52+
}
53+
54+
throw new Error('Orgs was not an array')
55+
} catch (error) {
56+
event('api.getOrg.fetch.error', {error})
57+
}
58+
}

src/shared/components/NotFound.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import React, {Component, FC} from 'react'
1616
import {connect} from 'react-redux'
1717
import {getOrg} from 'src/organizations/selectors'
1818

19+
import {getOrg as fetchOrg} from 'src/organizations/apis'
20+
1921
import {withRouter, RouteComponentProps} from 'react-router-dom'
2022

2123
import {AppState, Organization} from 'src/types'
@@ -142,25 +144,44 @@ const NotFoundOld: FC = () => (
142144
)
143145

144146
class NotFound extends Component<Props> {
145-
componentDidMount() {
147+
state = {
148+
isFetchingOrg: false,
149+
}
150+
151+
async componentDidMount() {
146152
if (isFlagEnabled('deepLinking')) {
153+
let org = this.props?.org
154+
155+
if (!org) {
156+
this.setState({isFetchingOrg: true})
157+
org = await fetchOrg()
158+
}
159+
147160
const deepLinkingMap = {
148-
'/me/alerts': `/orgs/${this.props.org.id}/alerting`,
149-
'/me/billing': `/orgs/${this.props.org.id}/billing`,
150-
'/me/dashboards': `/orgs/${this.props.org.id}/dashboards-list`,
151-
'/me/notebooks': `/orgs/${this.props.org.id}/notebooks`,
152-
'/me/tasks': `/orgs/${this.props.org.id}/tasks`,
153-
'/me/usage': `/orgs/${this.props.org.id}/usage`,
161+
'/me/alerts': `/orgs/${org.id}/alerting`,
162+
'/me/billing': `/orgs/${org.id}/billing`,
163+
'/me/dashboards': `/orgs/${org.id}/dashboards-list`,
164+
'/me/notebooks': `/orgs/${org.id}/notebooks`,
165+
'/me/tasks': `/orgs/${org.id}/tasks`,
166+
'/me/usage': `/orgs/${org.id}/usage`,
154167
}
155168

156169
if (deepLinkingMap.hasOwnProperty(this.props.location.pathname)) {
157170
this.props.history.replace(deepLinkingMap[this.props.location.pathname])
158171
return
172+
} else {
173+
this.setState({isFetchingOrg: false})
159174
}
160175
}
161176
}
162177

163178
render() {
179+
if (this.state.isFetchingOrg) {
180+
// don't render anything if this component is actively fetching org id
181+
// this prevents popping in a 404 page then redirecting
182+
return null
183+
}
184+
164185
if (isFlagEnabled('newNotFoundPage')) {
165186
return <NotFoundNew />
166187
}

0 commit comments

Comments
 (0)