Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
import React, { createContext, useContext } from 'react'
import { useStoredInterval } from './interval-picker'
import { useSiteContext } from '../../site-context'
import { useDashboardStateContext } from '../../dashboard-state-context'
import { Interval } from './intervals'
type GraphIntervalContextValue = {
selectedInterval: Interval
onIntervalClick: (interval: Interval) => void
availableIntervals: Interval[]
}
const GraphIntervalContext = createContext<GraphIntervalContextValue | null>(
null
)
export function GraphIntervalProvider({
children
}: {
children: React.ReactNode
}) {
const site = useSiteContext()
const { dashboardState } = useDashboardStateContext()
const { selectedInterval, onIntervalClick, availableIntervals } =
useStoredInterval({
domain: site.domain,
siteTimezoneOffset: site.offset,
siteStatsBegin: site.statsBegin,
to: dashboardState.to,
from: dashboardState.from,
period: dashboardState.period,
comparison: dashboardState.comparison,
compare_to: dashboardState.compare_to,
compare_from: dashboardState.compare_from
})
return (
<GraphIntervalContext.Provider
value={{ selectedInterval, onIntervalClick, availableIntervals }}
>
{children}
</GraphIntervalContext.Provider>
)
}
export const useGraphIntervalContext = (): GraphIntervalContextValue =>
useContext(GraphIntervalContext)!