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!

some improvements

Blue Mouse433c157d21e7
diff --git a/package.json b/package.json
index d08db32cf..2523fd79e 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,7 @@
"debug": "^4.3.4",
"dom-to-image": "^2.6.0",
"file-saver": "^2.0.5",
+ "flatpickr": "^4.6.13",
"i18n-iso-countries": "^7.4.0",
"i18next": "^21.6.16",
"i18next-browser-languagedetector": "^6.1.4",
@@ -26,6 +27,7 @@
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-flagkit": "^2.0.4",
+ "react-flatpickr": "^3.10.12",
"react-helmet-async": "^1.3.0",
"react-i18next": "^11.16.7",
"react-redux": "^8.0.1",
diff --git a/public/locales/en.json b/public/locales/en.json
index 016a9468d..8e76f3067 100644
--- a/public/locales/en.json
+++ b/public/locales/en.json
@@ -295,6 +295,7 @@
"lastXDays": "Last {{amount}} days",
"lastXWeeks": "Last {{amount}} weeks",
"lastXMonths": "Last {{amount}} months",
+ "custom": "Custom date",
"hour": "Hour",
"day": "Day",
"week": "Week",
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index da32234a1..729f7447b 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -35,12 +35,13 @@ import PropTypes from 'prop-types'
import Title from 'components/Title'
import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
- tbPeriodPairs, tbsFormatMapper, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE,
+ tbPeriodPairs, tbsFormatMapper, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, timeBucketToDays,
} from 'redux/constants'
import Button from 'ui/Button'
import Loader from 'ui/Loader'
import Dropdown from 'ui/Dropdown'
import Checkbox from 'ui/Checkbox'
+import FlatPicker from 'ui/Flatpicker'
import {
Panel, Overview, CustomEvents,
} from './Panels'
@@ -49,7 +50,6 @@ import {
getProjectData, getProject, getOverallStats, getLiveVisitors,
} from 'api'
import './styles.css'
-import FlatPicker from 'ui/Flatpicker'
countries.registerLocale(countriesEn)
countries.registerLocale(countriesDe)
@@ -65,7 +65,7 @@ const getColumns = (chart, showTotal, t) => {
return [
['x', ..._map(chart.x, el => dayjs(el).toDate())],
[t('project.unique'), ...chart.uniques],
- [t('project.total'), ...chart.visits ],
+ [t('project.total'), ...chart.visits],
]
}
@@ -285,12 +285,6 @@ const ViewProject = ({
const refCalendar = useRef(null)
const localstorageRangeDate = projectViewPrefs[id]?.rangeDate
const [rangeDate, setRangeDate] = useState(localstorageRangeDate ? [new Date(localstorageRangeDate[0]), new Date(localstorageRangeDate[1])] : null)
- const timeBucketToDays = [
- { lt: 7, tb: ['hour','day'] }, // 7 days
- { lt: 28, tb: ['day','week'] }, // 4 weeks
- { lt: 366, tb: ['week','month']}, // 12 months
- { lt: 732, tb: ['month'] }, // 24 months
- ]
const { name } = project
@@ -415,27 +409,29 @@ const ViewProject = ({
}, [isLoading, showTotal, chartData, mainChart, t])
useEffect(() => {
- if (period !== 'Custom') {
+ if (period !== 'custom') {
loadAnalytics()
- } else if (timeBucket !== 'Custom') {
+ } else if (timeBucket !== 'custom') {
loadAnalytics(true)
}
}, [project, period, timeBucket, periodPairs]) // eslint-disable-line
useEffect(() => {
- if(rangeDate) {
+ if (rangeDate) {
const days = Math.ceil(Math.abs(rangeDate[1].getTime() - rangeDate[0].getTime()) / (1000 * 3600 * 24))
+ console.log(timeBucketToDays)
+
for (let index in timeBucketToDays) {
if (timeBucketToDays[index].lt >= days) {
setTimebucket(timeBucketToDays[index].tb[0])
setPeriodPairs(tbPeriodPairs(t, timeBucketToDays[index].tb, rangeDate))
- setPeriod('Custom')
- setProjectViewPrefs(id, 'Custom', timeBucketToDays[index].tb[0], rangeDate)
+ setPeriod('custom')
+ setProjectViewPrefs(id, 'custom', timeBucketToDays[index].tb[0], rangeDate)
break
}
}
}
- }, [rangeDate]) // eslint-disable-line
+ }, [rangeDate, t]) // eslint-disable-line
useEffect(() => {
const updateLiveVisitors = async () => {
@@ -496,7 +492,7 @@ const ViewProject = ({
setTimebucket(tb)
}
- if (newPeriod.period !== 'Custom') {
+ if (newPeriod.period !== 'custom') {
setProjectViewPrefs(id, newPeriod.period, tb)
setPeriod(newPeriod.period)
}
@@ -572,7 +568,7 @@ const ViewProject = ({
labelExtractor={(pair) => pair.label}
keyExtractor={(pair) => pair.label}
onSelect={(pair) => {
- if (pair.isCustomDate) {;
+ if (pair.isCustomDate) {
setTimeout(() => {
refCalendar.current.openCalendar()
}, 100)
diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.js
index 89fa7d495..0b5eef076 100644
--- a/src/redux/actions/ui/index.js
+++ b/src/redux/actions/ui/index.js
@@ -84,7 +84,7 @@ const deleteProjectCache = (pid, period, timeBucket) => ({
const setProjectViewPrefs = (pid, period, timeBucket, rangeDate) => ({
type: types.SET_PROJECT_VIEW_PREFS,
payload: {
- pid, period, timeBucket, rangeDate
+ pid, period, timeBucket, rangeDate,
},
})
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index 41dfb4fb7..5bb66213b 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -39,13 +39,20 @@ export const tbPeriodPairs = (t, tbs, dates) => [{
tbs: ['month'],
access: 'paid',
}, {
- label: dates ? `${dates[0].toLocaleDateString()}-${dates[1].toLocaleDateString()}` : "Custom date",
+ label: dates ? `${dates[0].toLocaleDateString()}-${dates[1].toLocaleDateString()}` : t('project.custom'),
isCustomDate: true,
- period: 'Custom',
- tbs: tbs || ['Custom'],
+ period: 'custom',
+ tbs: tbs || ['custom'],
access: 'paid',
}]
+export const timeBucketToDays = [
+ { lt: 7, tb: ['hour','day'] }, // 7 days
+ { lt: 28, tb: ['day','week'] }, // 4 weeks
+ { lt: 366, tb: ['week','month']}, // 12 months
+ { lt: 732, tb: ['month'] }, // 24 months
+]
+
export const tbsFormatMapper = {
'hour': '%d %B %H:%M',
'day': '%d %B',
diff --git a/src/redux/reducers/ui/cache.js b/src/redux/reducers/ui/cache.js
index 247e9d800..65d0fd9bf 100644
--- a/src/redux/reducers/ui/cache.js
+++ b/src/redux/reducers/ui/cache.js
@@ -34,6 +34,12 @@ const cacheReducer = (state = getInitialState(), { type, payload }) => {
switch (type) {
case types.SET_PROJECT_CACHE: {
const { pid, period, timeBucket, data } = payload
+
+ // temporarily do not store custom date ranges in cache
+ if (period === 'custom') {
+ return state
+ }
+
const key = getProjectCacheKey(period, timeBucket)
return {
@@ -83,7 +89,7 @@ const cacheReducer = (state = getInitialState(), { type, payload }) => {
projectViewPrefs: {
...state.projectViewPrefs,
[pid]: rangeDate ? {
- period, timeBucket, rangeDate
+ period, timeBucket, rangeDate,
} : {
period, timeBucket,
},
diff --git a/src/redux/sagas/ui/set_project_view_prefs.js b/src/redux/sagas/ui/set_project_view_prefs.js
index 4a1bb0a64..235a94a45 100644
--- a/src/redux/sagas/ui/set_project_view_prefs.js
+++ b/src/redux/sagas/ui/set_project_view_prefs.js
@@ -6,7 +6,7 @@ export default function setProjectViewPrefs({ payload: { pid, period, timeBucket
const viewPrefs = {
...getInitialViewPrefs(),
[pid]: rangeDate ? {
- period, timeBucket, rangeDate
+ period, timeBucket, rangeDate,
} : {
period, timeBucket,
},
diff --git a/src/ui/Dropdown.jsx b/src/ui/Dropdown.jsx
index ab6725825..63d2fcc19 100644
--- a/src/ui/Dropdown.jsx
+++ b/src/ui/Dropdown.jsx
@@ -35,7 +35,7 @@ const Dropdown = ({
leaveFrom='transform opacity-100 scale-100'
leaveTo='transform opacity-0 scale-95'
>
- <Menu.Items static className='z-10 origin-top-right absolute right-0 mt-2 min-w-max rounded-md shadow-lg bg-white dark:bg-gray-700 ring-1 ring-black ring-opacity-5 focus:outline-none'>
+ <Menu.Items static className='z-10 origin-top-right absolute right-0 mt-2 w-40 min-w-max rounded-md shadow-lg bg-white dark:bg-gray-700 ring-1 ring-black ring-opacity-5 focus:outline-none'>
<div className='py-1'>
{_map(items, item => (
<Menu.Item key={keyExtractor ? keyExtractor(item) : item}>
diff --git a/src/ui/Flatpicker.css b/src/ui/Flatpicker.css
index c218db9fb..187222d7b 100644
--- a/src/ui/Flatpicker.css
+++ b/src/ui/Flatpicker.css
@@ -1,17 +1,17 @@
-.flatpickr-calendar.static {
+.flatpicker-custom .flatpickr-calendar.static {
position: absolute !important;
}
-.inline {
+.flatpicker-custom .inline {
display: block !important;
}
-.flatpickr-calendar.inline {
+.flatpicker-custom .flatpickr-calendar.inline {
z-index: 9999;
position: absolute;
}
-.flatpickr-calendar.static.open {
+.flatpicker-custom .flatpickr-calendar.static.open {
left: -310px;
top: 30px;
-}
\ No newline at end of file
+}
diff --git a/src/ui/Flatpicker.jsx b/src/ui/Flatpicker.jsx
index 5fcac0f17..e9e0e86d4 100644
--- a/src/ui/Flatpicker.jsx
+++ b/src/ui/Flatpicker.jsx
@@ -1,12 +1,14 @@
-import React from "react"
-import Flatpickr from "react-flatpickr"
+import React from 'react'
+import Flatpickr from 'react-flatpickr'
+import _size from 'lodash/size'
+
+import { getItem } from 'utils/localstorage'
import './Flatpicker.css'
-import { getItem } from "utils/localstorage"
if (getItem('colour-theme') === 'light') {
- import("flatpickr/dist/themes/light.css")
+ import('flatpickr/dist/themes/light.css')
} else {
- import("flatpickr/dist/themes/dark.css")
+ import('flatpickr/dist/themes/dark.css')
}
class FlatPicker extends React.Component {
@@ -17,7 +19,7 @@ class FlatPicker extends React.Component {
}
setCustomDate(dates) {
- if (dates.length === 2) {
+ if (_size(dates) === 2) {
this.props.onChange(dates)
this.close()
}
@@ -42,28 +44,28 @@ class FlatPicker extends React.Component {
render() {
return (
- <div className="h-0">
- <Flatpickr
- id="calendar"
- value={this.props.value}
- options={{
- mode: 'range',
- maxDate: 'today',
- minDate: this.removeMonths(new Date(), 24),
- showMonths: 1,
- static: true,
- animate: true,
- altInput: true,
- position: "auto right",
- altInputClass: 'hidden'
- }}
- ref={this.calendar}
- className="invisible"
- onChange={this.setCustomDate}
- />
+ <div className='h-0 flatpicker-custom'>
+ <Flatpickr
+ id='calendar'
+ value={this.props.value}
+ options={{
+ mode: 'range',
+ maxDate: 'today',
+ minDate: this.removeMonths(new Date(), 24),
+ showMonths: 1,
+ static: true,
+ animate: true,
+ altInput: true,
+ position: 'auto right',
+ altInputClass: 'hidden'
+ }}
+ ref={this.calendar}
+ className='invisible'
+ onChange={this.setCustomDate}
+ />
</div>
)
}
}
-export default FlatPicker
\ No newline at end of file
+export default FlatPicker