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!

period fix

kruzhambus1bc3a25b4a20
diff --git a/src/pages/Captcha/View/ViewCaptcha.tsx b/src/pages/Captcha/View/ViewCaptcha.tsx
index 372ead833..30a135501 100644
--- a/src/pages/Captcha/View/ViewCaptcha.tsx
+++ b/src/pages/Captcha/View/ViewCaptcha.tsx
@@ -102,8 +102,8 @@ const ViewProject = ({
const [isPanelsDataEmpty, setIsPanelsDataEmpty] = useState<boolean>(false)
const [isPaidFeatureOpened, setIsPaidFeatureOpened] = useState<boolean>(false)
const [analyticsLoading, setAnalyticsLoading] = useState<boolean>(true)
- const [period, setPeriod] = useState<string>(projectViewPrefs[id]?.period || periodPairs[3].period)
- const [timeBucket, setTimebucket] = useState<string>(projectViewPrefs[id]?.timeBucket || periodPairs[3].tbs[1])
+ const [period, setPeriod] = useState<string>(projectViewPrefs ? projectViewPrefs[id]?.period || periodPairs[3].period : periodPairs[3].period)
+ const [timeBucket, setTimebucket] = useState<string>(projectViewPrefs ? projectViewPrefs[id]?.timeBucket || periodPairs[3].tbs[1] : periodPairs[3].tbs[1])
const activePeriod: {
period: string,
label: string,
@@ -130,13 +130,13 @@ const ViewProject = ({
const isLoading = authenticated ? _isLoading : false
const tnMapping = typeNameMapping(t)
const refCalendar = useRef(null)
- const localStorageDateRange = projectViewPrefs[id]?.rangeDate
+ const localStorageDateRange = projectViewPrefs ? projectViewPrefs[id]?.rangeDate : null
const [dateRange, setDateRange] = useState<Date[] | null>(localStorageDateRange ? [new Date(localStorageDateRange[0]), new Date(localStorageDateRange[1])] : null)
const timeFormat = useMemo(() => user.timeFormat || TimeFormat['12-hour'], [user])
const [ref, size] = useSize() as any
const rotateXAxias = useMemo(() => (size.width > 0 && size.width < 500), [size])
- const [chartType, setChartType] = useState(getItem('chartType') || chartTypes.line)
+ const [chartType, setChartType] = useState<string>(getItem('chartType') as string || chartTypes.line)
const { name } = project as IProject
@@ -570,7 +570,7 @@ const ViewProject = ({
// @ts-ignore
const url = new URL(window.location)
const { searchParams } = url
- const intialPeriod = searchParams.get('period')
+ const intialPeriod = projectViewPrefs ? searchParams.get('period') || projectViewPrefs[id]?.period : searchParams.get('period') || '7d'
if (!_includes(validPeriods, intialPeriod) || (id !== SWETRIX_PID && !isPaidTierUsed && _includes(paidPeriods, intialPeriod))) {
return
}
diff --git a/src/pages/Project/View/ViewProject.tsx b/src/pages/Project/View/ViewProject.tsx
index a713a05f0..5f431ae59 100644
--- a/src/pages/Project/View/ViewProject.tsx
+++ b/src/pages/Project/View/ViewProject.tsx
@@ -90,7 +90,13 @@ const ViewProject = ({
cache: any,
cachePerf: any,
setProjectCache: (pid: string, data: any, key: string) => void,
- projectViewPrefs: any,
+ projectViewPrefs: {
+ [key: string]: {
+ period: string,
+ timeBucket: string,
+ rangeDate?: Date[],
+ },
+ } | null,
setProjectViewPrefs: (pid: string, period: string, timeBucket: string, rangeDate?: Date[]) => void,
setPublicProject: (project: Partial<IProject | ISharedProject>) => void,
setLiveStatsForProject: (id: string, count: number) => void,
@@ -145,8 +151,8 @@ const ViewProject = ({
const [isPaidFeatureOpened, setIsPaidFeatureOpened] = useState<boolean>(false)
const [isForecastOpened, setIsForecastOpened] = useState<boolean>(false)
const [analyticsLoading, setAnalyticsLoading] = useState<boolean>(true)
- const [period, setPeriod] = useState<string>(projectViewPrefs[id]?.period || periodPairs[3].period)
- const [timeBucket, setTimebucket] = useState<string>(projectViewPrefs[id]?.timeBucket || periodPairs[3].tbs[1])
+ const [period, setPeriod] = useState<string>(projectViewPrefs ? projectViewPrefs[id]?.period || periodPairs[3].period : periodPairs[3].period)
+ const [timeBucket, setTimebucket] = useState<string>(projectViewPrefs ? projectViewPrefs[id]?.timeBucket || periodPairs[3].tbs[1] : periodPairs[3].tbs[1])
const activePeriod = useMemo(() => _find(periodPairs, p => p.period === period), [period, periodPairs])
const [chartData, setChartData] = useState<any>({})
const [mainChart, setMainChart] = useState<any>(null)
@@ -172,7 +178,7 @@ const ViewProject = ({
const isLoading = authenticated ? _isLoading : false
const tnMapping = typeNameMapping(t)
const refCalendar = useRef(null)
- const localStorageDateRange = projectViewPrefs[id]?.rangeDate
+ const localStorageDateRange = projectViewPrefs ? projectViewPrefs[id]?.rangeDate : null
const [dateRange, setDateRange] = useState<null | Date[]>(localStorageDateRange ? [new Date(localStorageDateRange[0]), new Date(localStorageDateRange[1])] : null)
const [activeTab, setActiveTab] = useState<string>(() => {
// @ts-ignore
@@ -197,7 +203,7 @@ const ViewProject = ({
const timeFormat = useMemo(() => user.timeFormat || TimeFormat['12-hour'], [user])
const [ref, size] = useSize() as any
const rotateXAxias = useMemo(() => (size.width > 0 && size.width < 500), [size])
- const [chartType, setChartType] = useState<string>(getItem('chartType') || chartTypes.line)
+ const [chartType, setChartType] = useState<string>(getItem('chartType') as string || chartTypes.line)
const tabs: {
id: string
@@ -1216,7 +1222,7 @@ const ViewProject = ({
// @ts-ignore
const url = new URL(window.location)
const { searchParams } = url
- const intialPeriod = searchParams.get('period') || '7d'
+ const intialPeriod = projectViewPrefs ? searchParams.get('period') || projectViewPrefs[id]?.period : searchParams.get('period') || '7d'
const tab = searchParams.get('tab')
if (tab === PROJECT_TABS.performance) {
diff --git a/src/redux/reducers/ui/cache.ts b/src/redux/reducers/ui/cache.ts
index 5e713645d..3deefd577 100644
--- a/src/redux/reducers/ui/cache.ts
+++ b/src/redux/reducers/ui/cache.ts
@@ -1,19 +1,18 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
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,
} from 'redux/constants'
export const getInitialViewPrefs = (LS_VIEW: string) => {
if (typeof window !== 'undefined' && window.localStorage) {
- const storedPrefs = window.localStorage.getItem(LS_VIEW)
- if (typeof storedPrefs === 'string') {
- try {
- return JSON.parse(storedPrefs)
- } catch (e) {
- window.localStorage.removeItem(LS_VIEW)
- }
+ const storedPrefs = getItem(LS_VIEW)
+ try {
+ return storedPrefs
+ } catch (e) {
+ removeItem(LS_VIEW)
}
}
@@ -21,18 +20,24 @@ export const getInitialViewPrefs = (LS_VIEW: string) => {
}
interface IInitialState {
- analytics: any
- analyticsPerf: any
- captchaAnalytics: any
- captchaProjectsViewPrefs: any
- projectViewPrefs: any
+ analytics: any
+ analyticsPerf: any
+ captchaAnalytics: any
+ captchaProjectsViewPrefs: any
+ projectViewPrefs: {
+ [key: string]: {
+ period: string
+ timeBucket: string
+ rangeDate?: Date[]
+ },
+ } | null
}
const initialState: IInitialState = {
analytics: {},
analyticsPerf: {},
captchaAnalytics: {},
- captchaProjectsViewPrefs: getInitialViewPrefs(LS_CAPTCHA_VIEW_PREFS_SETTING),
+ captchaProjectsViewPrefs: getInitialViewPrefs(LS_CAPTCHA_VIEW_PREFS_SETTING) || {},
projectViewPrefs: getInitialViewPrefs(LS_VIEW_PREFS_SETTING),
}
@@ -107,7 +112,19 @@ const cacheSlice = createSlice({
} : {
period, timeBucket,
}
- window.localStorage.setItem(LS_VIEW_PREFS_SETTING, JSON.stringify(viewPrefs))
+ const storedPrefs = getItem(LS_VIEW_PREFS_SETTING) as {
+ [key: string]: {
+ period: string
+ timeBucket: string
+ rangeDate?: Date[]
+ },
+ }
+ if (typeof storedPrefs === 'object' && storedPrefs !== null) {
+ storedPrefs[pid] = viewPrefs
+ setItem(LS_VIEW_PREFS_SETTING, JSON.stringify(storedPrefs))
+ } else {
+ setItem(LS_VIEW_PREFS_SETTING, JSON.stringify({ [pid]: viewPrefs }))
+ }
state.projectViewPrefs = {
...state.projectViewPrefs,
[pid]: viewPrefs,
@@ -122,7 +139,19 @@ const cacheSlice = createSlice({
} : {
period, timeBucket,
}
- window.localStorage.setItem(LS_CAPTCHA_VIEW_PREFS_SETTING, JSON.stringify(viewPrefs))
+ const storedPrefs = getItem(LS_CAPTCHA_VIEW_PREFS_SETTING) as {
+ [key: string]: {
+ period: string
+ timeBucket: string
+ rangeDate?: Date[]
+ },
+ }
+ if (typeof storedPrefs === 'object' && storedPrefs !== null) {
+ storedPrefs[pid] = viewPrefs
+ setItem(LS_CAPTCHA_VIEW_PREFS_SETTING, JSON.stringify(storedPrefs))
+ } else {
+ setItem(LS_CAPTCHA_VIEW_PREFS_SETTING, JSON.stringify({ [pid]: viewPrefs }))
+ }
state.captchaProjectsViewPrefs = {
...state.captchaProjectsViewPrefs,
[pid]: viewPrefs,
diff --git a/src/redux/reducers/ui/projects.ts b/src/redux/reducers/ui/projects.ts
index c555366e3..93fdc8e56 100644
--- a/src/redux/reducers/ui/projects.ts
+++ b/src/redux/reducers/ui/projects.ts
@@ -44,7 +44,7 @@ const initialState: IInitialState = {
dashboardPaginationPage: 1,
dashboardPaginationPageShared: 1,
dashboardPaginationPageCaptcha: 1,
- dashboardTabs: getItem('dashboardTabs') || tabForOwnedProject,
+ dashboardTabs: getItem('dashboardTabs') as string || tabForOwnedProject,
projectTab: PROJECT_TABS.traffic,
alerts: [],
subscribers: [],
diff --git a/src/utils/localstorage.ts b/src/utils/localstorage.ts
index 8fd108c42..e65785d3a 100644
--- a/src/utils/localstorage.ts
+++ b/src/utils/localstorage.ts
@@ -15,7 +15,7 @@ export const setItem: setItemType = (key, value) => {
localStorage.setItem(key, value)
}
-type getItemType = (key: string) => string | null
+type getItemType = (key: string) => string | null | {}
export const getItem: getItemType = (key) => {
if (currentLocalStorage[key]) {