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!

fix: broken object written in project password cache

Blue Mouse45155206c83b
diff --git a/web/app/pages/Captcha/View/ViewCaptcha.tsx b/web/app/pages/Captcha/View/ViewCaptcha.tsx
index e4535797e..28cd7228a 100644
--- a/web/app/pages/Captcha/View/ViewCaptcha.tsx
+++ b/web/app/pages/Captcha/View/ViewCaptcha.tsx
@@ -118,7 +118,7 @@ const ViewCaptcha = () => {
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') as string) || chartTypes.line)
+ const [chartType, setChartType] = useState(getItem('chartType') || chartTypes.line)
const [mainChart, setMainChart] = useState<any>(null)
const { timezone = DEFAULT_TIMEZONE } = user || {}
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index 7b4219021..908dc0e6d 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -520,7 +520,7 @@ const ViewProject = () => {
),
[preferences.customEvents, id, activeChartMetricsCustomEvents],
)
- const [chartType, setChartType] = useState((getItem('chartType') as string) || chartTypes.line)
+ const [chartType, setChartType] = useState(getItem('chartType') || chartTypes.line)
const [periodPairsCompare, setPeriodPairsCompare] = useState<
{
@@ -528,19 +528,7 @@ const ViewProject = () => {
period: string
}[]
>(tbPeriodPairsCompare(t, undefined, language))
- const [isActiveCompare, setIsActiveCompare] = useState(() => {
- const activeCompare = getItem(LS_IS_ACTIVE_COMPARE_KEY)
-
- if (typeof activeCompare === 'string') {
- return activeCompare === 'true'
- }
-
- if (typeof activeCompare === 'boolean') {
- return activeCompare
- }
-
- return false
- })
+ const [isActiveCompare, setIsActiveCompare] = useState(getItem(LS_IS_ACTIVE_COMPARE_KEY) === 'true')
const [activePeriodCompare, setActivePeriodCompare] = useState(periodPairsCompare[0].period)
const activeDropdownLabelCompare = useMemo(
() => _find(periodPairsCompare, (p) => p.period === activePeriodCompare)?.label,
diff --git a/web/app/pages/Project/View/utils/cache.ts b/web/app/pages/Project/View/utils/cache.ts
index 6238d8425..cb7b4fd68 100644
--- a/web/app/pages/Project/View/utils/cache.ts
+++ b/web/app/pages/Project/View/utils/cache.ts
@@ -1,6 +1,6 @@
import { isBrowser, LS_PROJECTS_PROTECTED_KEY, LS_VIEW_PREFS_SETTING } from '~/lib/constants'
import { type Project } from '~/lib/models/Project'
-import { getItem, removeItem, setItem } from '~/utils/localstorage'
+import { getItemJSON, removeItem, setItem } from '~/utils/localstorage'
import { ProjectPreferences } from '../../../../providers/CurrentProjectProvider'
@@ -11,7 +11,7 @@ const getAllProjectPreferences = (): Record<string, ProjectPreferences> => {
return {}
}
- const storedPrefs = (getItem(LS_VIEW_PREFS_SETTING) as Record<string, ProjectPreferences>) || {}
+ const storedPrefs = getItemJSON(LS_VIEW_PREFS_SETTING) || {}
try {
return filterInvalidPreferences(storedPrefs)
@@ -37,7 +37,7 @@ export const setProjectPreferences = (id: Project['id'], preferences: ProjectPre
)
}
-const getAllProjectPasswords = () => (getItem(LS_PROJECTS_PROTECTED_KEY) as Record<string, string>) || {}
+const getAllProjectPasswords = () => getItemJSON(LS_PROJECTS_PROTECTED_KEY) || {}
export const setProjectPassword = (id: Project['id'], password: string) => {
setItem(
diff --git a/web/app/providers/CurrentProjectProvider.tsx b/web/app/providers/CurrentProjectProvider.tsx
index b7ae29927..cc4353e3f 100644
--- a/web/app/providers/CurrentProjectProvider.tsx
+++ b/web/app/providers/CurrentProjectProvider.tsx
@@ -7,7 +7,7 @@ import { toast } from 'sonner'
import { checkPassword, getInstalledExtensions, getProject } from '~/api'
import { isSelfhosted, LS_PROJECTS_PROTECTED_KEY } from '~/lib/constants'
import { Extension, type Project } from '~/lib/models/Project'
-import { getItem, removeItem } from '~/utils/localstorage'
+import { getItemJSON, removeItem } from '~/utils/localstorage'
import routes from '~/utils/routes'
import { getProjectPreferences, setProjectPreferences } from '../pages/Project/View/utils/cache'
@@ -37,7 +37,7 @@ export const useProjectPassword = (id: string) => {
const [searchParams] = useSearchParams()
const projectPassword = useMemo(
- () => searchParams.get('password') || (getItem(LS_PROJECTS_PROTECTED_KEY) as Record<string, string>)?.[id] || '',
+ () => searchParams.get('password') || getItemJSON(LS_PROJECTS_PROTECTED_KEY)?.[id] || '',
[id, searchParams],
)
diff --git a/web/app/utils/localstorage.ts b/web/app/utils/localstorage.ts
index f62e0c5a8..2d91d309b 100644
--- a/web/app/utils/localstorage.ts
+++ b/web/app/utils/localstorage.ts
@@ -19,7 +19,7 @@ export const setItem = (key: string, value: string) => {
localStorage.setItem(key, value)
}
-export const getItem = (key: string): Record<string, any> | string | null => {
+export const getItem = (key: string): string | null => {
if (currentLocalStorage[key]) {
return currentLocalStorage[key]
}
@@ -28,17 +28,21 @@ export const getItem = (key: string): Record<string, any> | string | null => {
return null
}
- const storedValue: string | null = localStorage.getItem(key)
+ return localStorage.getItem(key)
+}
- if (storedValue === null) {
- return null
- }
+export const getItemJSON = (key: string): Record<string, any> | null => {
+ const value = getItem(key)
- try {
- return JSON.parse(storedValue)
- } catch {
- return storedValue
+ if (typeof value === 'string') {
+ try {
+ return JSON.parse(value)
+ } catch {
+ return null
+ }
}
+
+ return value
}
export const removeItem = (key: string) => {