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!

use ViewProjectContext for captcha

Blue Mouse6eea6d889642
diff --git a/web/app/pages/Captcha/View/ViewCaptcha.tsx b/web/app/pages/Captcha/View/ViewCaptcha.tsx
index 75583027c..8618ebb0a 100644
--- a/web/app/pages/Captcha/View/ViewCaptcha.tsx
+++ b/web/app/pages/Captcha/View/ViewCaptcha.tsx
@@ -37,6 +37,7 @@ import {
OS_LOGO_MAP_DARK,
isBrowser,
ThemeType,
+ DEFAULT_TIMEZONE,
} from '~/lib/constants'
import { CaptchaProject } from '~/lib/models/Project'
import Loader from '~/ui/Loader'
@@ -69,6 +70,7 @@ import Filters from './components/Filters'
import TBPeriodSelector from './components/TBPeriodSelector'
import UIActions from '~/lib/reducers/ui'
import { DownloadIcon, RotateCw, SettingsIcon } from 'lucide-react'
+import { ViewProjectContext } from '~/pages/Project/View/ViewProject'
const PageLoader = () => (
<div className='min-h-min-footer bg-gray-50 dark:bg-slate-900'>
@@ -147,6 +149,8 @@ const ViewCaptcha = ({ ssrTheme }: ViewCaptchaProps) => {
const _theme = isBrowser ? theme : ssrTheme
+ const { timezone = DEFAULT_TIMEZONE } = user || {}
+
useEffect(() => {
if (!project) {
// TODO: Probably should display something like "Loading..."
@@ -602,245 +606,283 @@ const ViewCaptcha = ({ ssrTheme }: ViewCaptchaProps) => {
return (
<ClientOnly fallback={<PageLoader />}>
{() => (
- <>
- <EventsRunningOutBanner />
- <div ref={ref} className='bg-gray-50 dark:bg-slate-900'>
- <div
- className='mx-auto min-h-min-footer w-full max-w-[1584px] px-2 py-6 sm:px-4 lg:px-8'
- ref={dashboardRef}
- >
- <div className='mt-2 flex flex-col items-center justify-between lg:flex-row lg:items-start'>
- <h2 className='break-words break-all text-xl font-bold text-gray-900 dark:text-gray-50'>
- {project.name}
- </h2>
- <div className='mx-auto mt-3 flex w-full max-w-[420px] flex-wrap items-center justify-between sm:mx-0 sm:w-auto sm:max-w-none lg:mt-0'>
- <button
- type='button'
- title={t('project.refreshStats')}
- onClick={refreshStats}
- className={cx(
- 'relative mr-3 rounded-md bg-gray-50 p-2 text-sm font-medium hover:bg-white hover:shadow-sm focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:bg-slate-900 dark:hover:bg-slate-800 focus:dark:border-gray-200 focus:dark:ring-gray-200',
- {
- 'cursor-not-allowed opacity-50': authLoading || dataLoading,
- },
- )}
- >
- <RotateCw className='h-5 w-5 text-gray-700 dark:text-gray-50' />
- </button>
- <Dropdown
- header={t('project.exportData')}
- items={exportTypes}
- title={[<DownloadIcon key='download-icon' className='h-5 w-5' strokeWidth={1.5} />]}
- labelExtractor={(item) => item.label}
- keyExtractor={(item) => item.label}
- onSelect={(item) => item.onClick()}
- className={cx('mr-3', { hidden: isPanelsDataEmpty || analyticsLoading })}
- chevron='mini'
- buttonClassName='!p-2 rounded-md hover:bg-white hover:shadow-sm dark:hover:bg-slate-800 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 focus:dark:ring-gray-200 focus:dark:border-gray-200'
- headless
- />
- <div
- className={cx('space-x-2 border-gray-200 dark:border-gray-600 sm:mr-3 lg:border-x lg:px-3', {
- // TODO: Fix a crash when user selects 'bar' chart and refreshes the page:
- // Uncaught TypeError: can't access property "create", point5 is undefined
- hidden: isPanelsDataEmpty || analyticsLoading || checkIfAllMetricsAreDisabled || true,
- })}
- >
+ <ViewProjectContext.Provider
+ value={{
+ // States
+ projectId: project?.id,
+ projectPassword: '',
+ timezone,
+ dateRange,
+ isLoading: authLoading,
+ timeBucket,
+ period,
+ activePeriod,
+ periodPairs,
+ timeFormat,
+ size,
+ allowedToManage: project.role === 'admin' || project.role === 'owner',
+ dataLoading,
+ activeTab: 'traffic',
+ filters,
+
+ // Functions
+ setDateRange,
+ updatePeriod,
+ updateTimebucket,
+ setPeriodPairs,
+
+ // Refs
+ refCalendar,
+ }}
+ >
+ <>
+ <EventsRunningOutBanner />
+ <div ref={ref} className='bg-gray-50 dark:bg-slate-900'>
+ <div
+ className='mx-auto min-h-min-footer w-full max-w-[1584px] px-2 py-6 sm:px-4 lg:px-8'
+ ref={dashboardRef}
+ >
+ <div className='mt-2 flex flex-col items-center justify-between lg:flex-row lg:items-start'>
+ <h2 className='break-words break-all text-xl font-bold text-gray-900 dark:text-gray-50'>
+ {project.name}
+ </h2>
+ <div className='mx-auto mt-3 flex w-full max-w-[420px] flex-wrap items-center justify-between sm:mx-0 sm:w-auto sm:max-w-none lg:mt-0'>
<button
type='button'
- title={t('project.barChart')}
- onClick={() => setChartTypeOnClick(chartTypes.bar)}
+ title={t('project.refreshStats')}
+ onClick={refreshStats}
className={cx(
- 'relative rounded-md fill-gray-700 p-2 text-sm font-medium focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:fill-gray-50 focus:dark:border-gray-200 focus:dark:ring-gray-200',
+ 'relative mr-3 rounded-md bg-gray-50 p-2 text-sm font-medium hover:bg-white hover:shadow-sm focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:bg-slate-900 dark:hover:bg-slate-800 focus:dark:border-gray-200 focus:dark:ring-gray-200',
{
- 'bg-white stroke-white shadow-sm dark:bg-slate-800 dark:stroke-slate-800':
- chartType === chartTypes.bar,
- 'bg-gray-50 stroke-gray-50 dark:bg-slate-900 dark:stroke-slate-900 [&_svg]:hover:fill-gray-500 [&_svg]:hover:dark:fill-gray-200':
- chartType !== chartTypes.bar,
+ 'cursor-not-allowed opacity-50': authLoading || dataLoading,
},
)}
>
- <BarChart className='h-5 w-5 [&_path]:stroke-[3.5%]' />
+ <RotateCw className='h-5 w-5 text-gray-700 dark:text-gray-50' />
</button>
- <button
- type='button'
- title={t('project.lineChart')}
- onClick={() => setChartTypeOnClick(chartTypes.line)}
- className={cx(
- 'relative rounded-md fill-gray-700 p-2 text-sm font-medium focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:fill-gray-50 focus:dark:border-gray-200 focus:dark:ring-gray-200',
- {
- 'bg-white stroke-white shadow-sm dark:bg-slate-800 dark:stroke-slate-800':
- chartType === chartTypes.line,
- 'bg-gray-50 stroke-gray-50 dark:bg-slate-900 dark:stroke-slate-900 [&_svg]:hover:fill-gray-500 [&_svg]:hover:dark:fill-gray-200':
- chartType !== chartTypes.line,
- },
- )}
- >
- <LineChart className='h-5 w-5 [&_path]:stroke-[3.5%]' />
- </button>
- </div>
- {!isPanelsDataEmpty && (
<Dropdown
- items={chartMetrics}
- title={t('project.metricVis')}
- className={cx({ hidden: isPanelsDataEmpty || analyticsLoading })}
- labelExtractor={(pair) => {
- const { label, active } = pair
-
- return (
- <Checkbox
- className={cx('px-4 py-2', { hidden: isPanelsDataEmpty || analyticsLoading })}
- label={label}
- checked={active}
- onChange={() => {}}
- />
- )
- }}
- keyExtractor={(pair) => pair.id}
- onSelect={({ id: pairID }) => {
- switchActiveChartMetric(pairID)
- }}
- buttonClassName='!px-3'
- selectItemClassName='group text-gray-700 dark:text-gray-50 dark:border-gray-800 dark:bg-slate-800 block text-sm cursor-pointer hover:bg-gray-200 dark:hover:bg-slate-700'
+ header={t('project.exportData')}
+ items={exportTypes}
+ title={[<DownloadIcon key='download-icon' className='h-5 w-5' strokeWidth={1.5} />]}
+ labelExtractor={(item) => item.label}
+ keyExtractor={(item) => item.label}
+ onSelect={(item) => item.onClick()}
+ className={cx('mr-3', { hidden: isPanelsDataEmpty || analyticsLoading })}
chevron='mini'
+ buttonClassName='!p-2 rounded-md hover:bg-white hover:shadow-sm dark:hover:bg-slate-800 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 focus:dark:ring-gray-200 focus:dark:border-gray-200'
headless
/>
- )}
- <TBPeriodSelector
- activePeriod={activePeriod}
- updateTimebucket={updateTimebucket}
- timeBucket={timeBucket}
- items={_filter(periodPairs, (item) => !_includes(['all', '1h'], item.period))}
- title={activePeriod?.label}
- onSelect={(pair) => {
- if (pair.isCustomDate) {
- setTimeout(() => {
- // @ts-ignore
- refCalendar.current.openCalendar()
- }, 100)
- } else {
- setPeriodPairs(captchaTbPeriodPairs(t, undefined, undefined, language))
- setDateRange(null)
- updatePeriod(pair)
- }
- }}
- />
- {(project.role === 'admin' || project.role === 'owner') && (
- <button
- type='button'
- onClick={openSettingsHandler}
- className='flex px-3 text-sm font-medium text-gray-700 hover:text-gray-600 dark:text-gray-50 dark:hover:text-gray-200'
+ <div
+ className={cx('space-x-2 border-gray-200 dark:border-gray-600 sm:mr-3 lg:border-x lg:px-3', {
+ // TODO: Fix a crash when user selects 'bar' chart and refreshes the page:
+ // Uncaught TypeError: can't access property "create", point5 is undefined
+ hidden: isPanelsDataEmpty || analyticsLoading || checkIfAllMetricsAreDisabled || true,
+ })}
>
- <>
- <SettingsIcon className='mr-1 h-5 w-5' strokeWidth={1.5} />
- {t('common.settings')}
- </>
- </button>
- )}
- <FlatPicker
- ref={refCalendar}
- onChange={(date) => setDateRange(date)}
- value={dateRange || []}
- maxDateMonths={MAX_MONTHS_IN_PAST}
- />
- </div>
- </div>
- {analyticsLoading && <Loader />}
- {isPanelsDataEmpty && <NoEvents filters={filters} resetFilters={resetFilters} />}
- <div className={cx('pt-4', { hidden: isPanelsDataEmpty || analyticsLoading })}>
- <div
- className={cx('h-80', {
- hidden: checkIfAllMetricsAreDisabled,
- })}
- >
- <div className='h-80 [&_svg]:!overflow-visible' id='captchaChart' />
- </div>
- <Filters
- filters={filters}
- onRemoveFilter={filterHandler}
- onChangeExclusive={onChangeExclusive}
- tnMapping={tnMapping}
- resetFilters={resetFilters}
- />
- {dataLoading && (
- <div className='static mt-4 !bg-transparent' id='loader'>
- <div className='loader-head dark:!bg-slate-800'>
- <div className='first dark:!bg-slate-600' />
- <div className='second dark:!bg-slate-600' />
+ <button
+ type='button'
+ title={t('project.barChart')}
+ onClick={() => setChartTypeOnClick(chartTypes.bar)}
+ className={cx(
+ 'relative rounded-md fill-gray-700 p-2 text-sm font-medium focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:fill-gray-50 focus:dark:border-gray-200 focus:dark:ring-gray-200',
+ {
+ 'bg-white stroke-white shadow-sm dark:bg-slate-800 dark:stroke-slate-800':
+ chartType === chartTypes.bar,
+ 'bg-gray-50 stroke-gray-50 dark:bg-slate-900 dark:stroke-slate-900 [&_svg]:hover:fill-gray-500 [&_svg]:hover:dark:fill-gray-200':
+ chartType !== chartTypes.bar,
+ },
+ )}
+ >
+ <BarChart className='h-5 w-5 [&_path]:stroke-[3.5%]' />
+ </button>
+ <button
+ type='button'
+ title={t('project.lineChart')}
+ onClick={() => setChartTypeOnClick(chartTypes.line)}
+ className={cx(
+ 'relative rounded-md fill-gray-700 p-2 text-sm font-medium focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:fill-gray-50 focus:dark:border-gray-200 focus:dark:ring-gray-200',
+ {
+ 'bg-white stroke-white shadow-sm dark:bg-slate-800 dark:stroke-slate-800':
+ chartType === chartTypes.line,
+ 'bg-gray-50 stroke-gray-50 dark:bg-slate-900 dark:stroke-slate-900 [&_svg]:hover:fill-gray-500 [&_svg]:hover:dark:fill-gray-200':
+ chartType !== chartTypes.line,
+ },
+ )}
+ >
+ <LineChart className='h-5 w-5 [&_path]:stroke-[3.5%]' />
+ </button>
</div>
- </div>
- )}
- <div className='mt-5 grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-3'>
- {!_isEmpty(panelsData.types) &&
- _map(PANELS_ORDER, (type: keyof typeof tnMapping) => {
- const panelName = tnMapping[type]
- const panelIcon = panelIconMapping[type]
-
- if (type === 'cc') {
- const rowMapper = (entry: any) => {
- const { name: entryName, cc } = entry
-
- if (cc) {
- return <CCRow cc={cc} name={entryName} language={language} />
- }
+ {!isPanelsDataEmpty && (
+ <Dropdown
+ items={chartMetrics}
+ title={t('project.metricVis')}
+ className={cx({ hidden: isPanelsDataEmpty || analyticsLoading })}
+ labelExtractor={(pair) => {
+ const { label, active } = pair
- return <CCRow cc={entryName} language={language} />
+ return (
+ <Checkbox
+ className={cx('px-4 py-2', { hidden: isPanelsDataEmpty || analyticsLoading })}
+ label={label}
+ checked={active}
+ onChange={() => {}}
+ />
+ )
+ }}
+ keyExtractor={(pair) => pair.id}
+ onSelect={({ id: pairID }) => {
+ switchActiveChartMetric(pairID)
+ }}
+ buttonClassName='!px-3'
+ selectItemClassName='group text-gray-700 dark:text-gray-50 dark:border-gray-800 dark:bg-slate-800 block text-sm cursor-pointer hover:bg-gray-200 dark:hover:bg-slate-700'
+ chevron='mini'
+ headless
+ />
+ )}
+ <TBPeriodSelector
+ activePeriod={activePeriod}
+ updateTimebucket={updateTimebucket}
+ timeBucket={timeBucket}
+ items={_filter(periodPairs, (item) => !_includes(['all', '1h'], item.period))}
+ title={activePeriod?.label}
+ onSelect={(pair) => {
+ if (pair.isCustomDate) {
+ setTimeout(() => {
+ // @ts-ignore
+ refCalendar.current.openCalendar()
+ }, 100)
+ } else {
+ setPeriodPairs(captchaTbPeriodPairs(t, undefined, undefined, language))
+ setDateRange(null)
+ updatePeriod(pair)
}
+ }}
+ />
+ {(project.role === 'admin' || project.role === 'owner') && (
+ <button
+ type='button'
+ onClick={openSettingsHandler}
+ className='flex px-3 text-sm font-medium text-gray-700 hover:text-gray-600 dark:text-gray-50 dark:hover:text-gray-200'
+ >
+ <>
+ <SettingsIcon className='mr-1 h-5 w-5' strokeWidth={1.5} />
+ {t('common.settings')}
+ </>
+ </button>
+ )}
+ <FlatPicker
+ ref={refCalendar}
+ onChange={(date) => setDateRange(date)}
+ value={dateRange || []}
+ maxDateMonths={MAX_MONTHS_IN_PAST}
+ />
+ </div>
+ </div>
+ {analyticsLoading && <Loader />}
+ {isPanelsDataEmpty && <NoEvents filters={filters} resetFilters={resetFilters} />}
+ <div className={cx('pt-4', { hidden: isPanelsDataEmpty || analyticsLoading })}>
+ <div
+ className={cx('h-80', {
+ hidden: checkIfAllMetricsAreDisabled,
+ })}
+ >
+ <div className='h-80 [&_svg]:!overflow-visible' id='captchaChart' />
+ </div>
+ <Filters
+ filters={filters}
+ onRemoveFilter={filterHandler}
+ onChangeExclusive={onChangeExclusive}
+ tnMapping={tnMapping}
+ resetFilters={resetFilters}
+ />
+ {dataLoading && (
+ <div className='static mt-4 !bg-transparent' id='loader'>
+ <div className='loader-head dark:!bg-slate-800'>
+ <div className='first dark:!bg-slate-600' />
+ <div className='second dark:!bg-slate-600' />
+ </div>
+ </div>
+ )}
+ <div className='mt-5 grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-3'>
+ {!_isEmpty(panelsData.types) &&
+ _map(PANELS_ORDER, (type: keyof typeof tnMapping) => {
+ const panelName = tnMapping[type]
+ const panelIcon = panelIconMapping[type]
- return (
- <Panel
- key={type}
- icon={panelIcon}
- id={type}
- onFilter={filterHandler}
- name={panelName}
- data={panelsData.data[type]}
- rowMapper={rowMapper}
- />
- )
- }
+ if (type === 'cc') {
+ const rowMapper = (entry: any) => {
+ const { name: entryName, cc } = entry
- if (type === 'dv') {
- return (
- <Panel
- key={type}
- icon={panelIcon}
- id={type}
- onFilter={filterHandler}
- name={panelName}
- data={panelsData.data[type]}
- rowMapper={(entry: { name: keyof typeof deviceIconMapping }) => {
- const { name: entryName } = entry
+ if (cc) {
+ return <CCRow cc={cc} name={entryName} language={language} />
+ }
+
+ return <CCRow cc={entryName} language={language} />
+ }
+
+ return (
+ <Panel
+ key={type}
+ icon={panelIcon}
+ id={type}
+ onFilter={filterHandler}
+ name={panelName}
+ data={panelsData.data[type]}
+ rowMapper={rowMapper}
+ />
+ )
+ }
- const icon = deviceIconMapping[entryName]
+ if (type === 'dv') {
+ return (
+ <Panel
+ key={type}
+ icon={panelIcon}
+ id={type}
+ onFilter={filterHandler}
+ name={panelName}
+ data={panelsData.data[type]}
+ rowMapper={(entry: { name: keyof typeof deviceIconMapping }) => {
+ const { name: entryName } = entry
+
+ const icon = deviceIconMapping[entryName]
+
+ if (!icon) {
+ return entryName
+ }
+
+ return (
+ <>
+ {icon}
+ &nbsp;
+ {entryName}
+ </>
+ )
+ }}
+ capitalize
+ />
+ )
+ }
- if (!icon) {
- return entryName
- }
+ if (type === 'br') {
+ const rowMapper = (entry: any) => {
+ const { name: entryName } = entry
+ // @ts-ignore
+ const logoUrl = BROWSER_LOGO_MAP[entryName]
+ if (!logoUrl) {
return (
<>
- {icon}
+ <GlobeAltIcon className='h-5 w-5' />
&nbsp;
{entryName}
</>
)
- }}
- capitalize
- />
- )
- }
+ }
- if (type === 'br') {
- const rowMapper = (entry: any) => {
- const { name: entryName } = entry
- // @ts-ignore
- const logoUrl = BROWSER_LOGO_MAP[entryName]
-
- if (!logoUrl) {
return (
<>
- <GlobeAltIcon className='h-5 w-5' />
+ <img src={logoUrl} className='h-5 w-5' alt='' />
&nbsp;
{entryName}
</>
@@ -848,56 +890,60 @@ const ViewCaptcha = ({ ssrTheme }: ViewCaptchaProps) => {
}
return (
- <>
- <img src={logoUrl} className='h-5 w-5' alt='' />
- &nbsp;
- {entryName}
- </>
+ <Panel
+ key={type}
+ icon={panelIcon}
+ id={type}
+ onFilter={filterHandler}
+ name={panelName}
+ data={panelsData.data[type]}
+ rowMapper={rowMapper}
+ />
)
}
- return (
- <Panel
- key={type}
- icon={panelIcon}
- id={type}
- onFilter={filterHandler}
- name={panelName}
- data={panelsData.data[type]}
- rowMapper={rowMapper}
- />
- )
- }
+ if (type === 'os') {
+ const rowMapper = (entry: any) => {
+ const { name: entryName } = entry
+ // @ts-ignore
+ const logoPathLight = OS_LOGO_MAP[entryName]
+ // @ts-ignore
+ const logoPathDark = OS_LOGO_MAP_DARK[entryName]
+
+ let logoPath = _theme === 'dark' ? logoPathDark : logoPathLight
+ logoPath ||= logoPathLight
- if (type === 'os') {
- const rowMapper = (entry: any) => {
- const { name: entryName } = entry
- // @ts-ignore
- const logoPathLight = OS_LOGO_MAP[entryName]
- // @ts-ignore
- const logoPathDark = OS_LOGO_MAP_DARK[entryName]
+ if (!logoPath) {
+ return (
+ <>
+ <GlobeAltIcon className='h-5 w-5' />
+ &nbsp;
+ {entryName}
+ </>
+ )
+ }
- let logoPath = _theme === 'dark' ? logoPathDark : logoPathLight
- logoPath ||= logoPathLight
+ const logoUrl = `/${logoPath}`
- if (!logoPath) {
return (
<>
- <GlobeAltIcon className='h-5 w-5' />
+ <img src={logoUrl} className='h-5 w-5 dark:fill-gray-50' alt='' />
&nbsp;
{entryName}
</>
)
}
- const logoUrl = `/${logoPath}`
-
return (
- <>
- <img src={logoUrl} className='h-5 w-5 dark:fill-gray-50' alt='' />
- &nbsp;
- {entryName}
- </>
+ <Panel
+ key={type}
+ icon={panelIcon}
+ id={type}
+ onFilter={filterHandler}
+ name={panelName}
+ data={panelsData.data[type]}
+ rowMapper={rowMapper}
+ />
)
}
@@ -909,27 +955,15 @@ const ViewCaptcha = ({ ssrTheme }: ViewCaptchaProps) => {
onFilter={filterHandler}
name={panelName}
data={panelsData.data[type]}
- rowMapper={rowMapper}
/>
)
- }
-
- return (
- <Panel
- key={type}
- icon={panelIcon}
- id={type}
- onFilter={filterHandler}
- name={panelName}
- data={panelsData.data[type]}
- />
- )
- })}
+ })}
+ </div>
</div>
</div>
</div>
- </div>
- </>
+ </>
+ </ViewProjectContext.Provider>
)}
</ClientOnly>
)
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index 90afabd8b..1b60ca504 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -242,7 +242,7 @@ interface ViewProjectContextType {
refCalendar: React.MutableRefObject<any>
}
-const ViewProjectContext = createContext<ViewProjectContextType | undefined>(undefined)
+export const ViewProjectContext = createContext<ViewProjectContextType | undefined>(undefined)
export const useViewProjectContext = () => {
const context = useContext(ViewProjectContext)