Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

improvments logic with server

kruzhambusc4e16890fab6
diff --git a/src/pages/Project/View/Panels.tsx b/src/pages/Project/View/Panels.tsx
index 893f8ef8e..a79bab866 100644
--- a/src/pages/Project/View/Panels.tsx
+++ b/src/pages/Project/View/Panels.tsx
@@ -775,12 +775,12 @@ const Panel = ({
>
<SankeyChart
disableLegend
- pid={pid}
- period={period}
- timeBucket={timeBucket}
- from={from}
- to={to}
- timezone={timezone}
+ pid={pid || ''}
+ period={period || ''}
+ timeBucket={timeBucket || ''}
+ from={from || ''}
+ to={to || ''}
+ timezone={timezone || ''}
/>
<Modal
onClose={() => setModal(false)}
@@ -798,12 +798,12 @@ const Panel = ({
message={(
<div className='h-96 dark:text-gray-800'>
<SankeyChart
- pid={pid}
- period={period}
- timeBucket={timeBucket}
- from={from}
- to={to}
- timezone={timezone}
+ pid={pid || ''}
+ period={period || ''}
+ timeBucket={timeBucket || ''}
+ from={from || ''}
+ to={to || ''}
+ timezone={timezone || ''}
isReversed={isReversedUserFlow}
/>
</div>
diff --git a/src/pages/Project/View/ViewProject.tsx b/src/pages/Project/View/ViewProject.tsx
index aa8801d62..0324c2ac4 100644
--- a/src/pages/Project/View/ViewProject.tsx
+++ b/src/pages/Project/View/ViewProject.tsx
@@ -1892,7 +1892,7 @@ const ViewProject = ({
name={panelName}
data={panelsData.data[type]}
customTabs={customTabs}
- period={activePeriod?.period}
+ period={period}
pid={id}
timeBucket={timeBucket}
from={dateRange ? getFormatDate(dateRange[0]) : null}
diff --git a/src/pages/Project/View/components/SankeyChart.tsx b/src/pages/Project/View/components/SankeyChart.tsx
index 9a7dda77b..da833f079 100644
--- a/src/pages/Project/View/components/SankeyChart.tsx
+++ b/src/pages/Project/View/components/SankeyChart.tsx
@@ -1,12 +1,14 @@
-import React from 'react'
+import React, { useEffect, useState } from 'react'
import { ResponsiveSankey } from '@nivo/sankey'
import { connect } from 'react-redux'
import { StateType, AppDispatch } from 'redux/store'
import UIActions from 'redux/reducers/ui'
+import { errorsActions } from 'redux/reducers/errors'
import { IUserFlow } from 'redux/models/IUserFlow'
import _isEmpty from 'lodash/isEmpty'
import { getUserFlowCacheKey } from 'redux/constants'
import { getUserFlow } from 'api'
+import Loader from 'ui/Loader'
const dataTest = {
nodes: [
@@ -104,10 +106,15 @@ const mapDispatchToProps = (dispatch: AppDispatch) => ({
period,
}))
},
+ generateError: (message: string) => {
+ dispatch(errorsActions.genericError({
+ message,
+ }))
+ },
})
const SankeyChart = ({
- disableLegend, pid, period, timeBucket, from, to, timezone, userFlowAscendingCache, userFlowDescendingCache, isReversed,
+ disableLegend, pid, period, timeBucket, from, to, timezone, userFlowAscendingCache, userFlowDescendingCache, isReversed, setUserFlowAscending, setUserFlowDescending, generateError,
}: {
disableLegend?: boolean
pid: string
@@ -123,15 +130,56 @@ const SankeyChart = ({
from: string
to: string
isReversed?: boolean
+ setUserFlowAscending: (data: IUserFlow, id: string, pd: string) => void
+ setUserFlowDescending: (data: IUserFlow, id: string, pd: string) => void
+ generateError: (message: string) => void
}) => {
- const userFlowAscending = userFlowAscendingCache[getUserFlowCacheKey(pid, period)]
- const userFlowDescending = userFlowDescendingCache[getUserFlowCacheKey(pid, period)]
+ const key = getUserFlowCacheKey(pid, period)
+ const userFlowAscending = userFlowAscendingCache[key]
+ const userFlowDescending = userFlowDescendingCache[key]
+ const [isLoading, setIsLoading] = useState<boolean>(true)
+
+ const fetchUserFlow = async () => {
+ setIsLoading(true)
+ await getUserFlow(pid, timeBucket, period, from, to, timezone)
+ .then((res: {
+ ascending: IUserFlow
+ descending: IUserFlow
+ }) => {
+ const { ascending, descending } = res
+
+ setUserFlowAscending(ascending, pid, period)
+ setUserFlowDescending(descending, pid, period)
+ })
+ .catch((err: Error) => {
+ generateError(err.message)
+ })
+ .finally(() => {
+ setIsLoading(false)
+ })
+ }
+
+ useEffect(() => {
+ if (_isEmpty(userFlowAscending) && _isEmpty(userFlowDescending)) {
+ fetchUserFlow()
+ } else if (isLoading) {
+ setIsLoading(false)
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [period, timeBucket, from, to, timezone, pid])
+
+ if (isLoading) {
+ return <Loader />
+ }
+
+ if (_isEmpty(userFlowAscending) && _isEmpty(userFlowDescending)) {
+ return null
+ }
return (
<ResponsiveSankey
- data={isReversed
- ? !_isEmpty(userFlowDescending) ? userFlowDescending : defaultData
- : !_isEmpty(userFlowAscending) ? userFlowAscending : defaultData}
+ // @ts-ignore
+ data={isReversed ? userFlowDescending : userFlowAscending}
margin={{
top: 0, right: disableLegend ? 0 : 120, bottom: 0, left: 20,
}}
diff --git a/src/redux/reducers/ui/cache.ts b/src/redux/reducers/ui/cache.ts
index a6b04cb03..f33f98b39 100644
--- a/src/redux/reducers/ui/cache.ts
+++ b/src/redux/reducers/ui/cache.ts
@@ -3,7 +3,7 @@ import _filter from 'lodash/filter'
import _isEmpty from 'lodash/isEmpty'
import { getItem, removeItem, setItem } from 'utils/localstorage'
import {
- getProjectCacheKey, LS_VIEW_PREFS_SETTING, LS_CAPTCHA_VIEW_PREFS_SETTING, getProjectCaptchaCacheKey,
+ getProjectCacheKey, LS_VIEW_PREFS_SETTING, LS_CAPTCHA_VIEW_PREFS_SETTING, getProjectCaptchaCacheKey, getUserFlowCacheKey,
} from 'redux/constants'
import { IUserFlow } from 'redux/models/IUserFlow'
@@ -211,7 +211,7 @@ const cacheSlice = createSlice({
}
},
setUserFlowAscending(state, { payload }: PayloadAction<{ pid: string, period: string, data: IUserFlow }>) {
- const key = payload.period + payload.pid
+ const key = getUserFlowCacheKey(payload.pid, payload.period)
state.userFlowAscending = {
...state.userFlowAscending,
@@ -219,7 +219,7 @@ const cacheSlice = createSlice({
}
},
setUserFlowDescending(state, { payload }: PayloadAction<{ pid: string, period: string, data: IUserFlow }>) {
- const key = payload.period + payload.pid
+ const key = getUserFlowCacheKey(payload.pid, payload.period)
state.userFlowDescending = {
...state.userFlowDescending,