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!

added timezone picker

Blue Mouse5d3aee97cc4a
diff --git a/package.json b/package.json
index 4459469c8..d8d643ede 100644
--- a/package.json
+++ b/package.json
@@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
+ "@blaumaus/react-alert": "^7.0.5",
"@headlessui/react": "^1.5.0",
"@heroicons/react": "^1.0.6",
"@tailwindcss/forms": "^0.5.0",
@@ -23,7 +24,6 @@
"prismjs": "^1.28.0",
"prop-types": "^15.8.1",
"react": "^18.0.0",
- "@blaumaus/react-alert": "^7.0.5",
"react-dom": "^18.0.0",
"react-flagkit": "^2.0.4",
"react-helmet-async": "^1.3.0",
@@ -32,9 +32,12 @@
"react-router-dom": "^5.3.1",
"react-router-hash-link": "^2.4.3",
"react-scripts": "5.0.1",
+ "react-timezone-select": "^1.3.1",
"redux": "^4.2.0",
"redux-saga": "^1.1.3",
+ "spacetime": "^7.1.4",
"swetrix": "^1.3.1",
+ "timezone-soft": "^1.3.1",
"url": "^0.11.0"
},
"scripts": {
diff --git a/public/locales/en.json b/public/locales/en.json
index 71344246e..ea2e6fb68 100644
--- a/public/locales/en.json
+++ b/public/locales/en.json
@@ -195,7 +195,8 @@
"updated": "Your account settings have been updated",
"confTimeout": "An email has already been sent, check your mailbox or try again in a few minutes",
"confSent": "An account confirmation link has been sent to your email",
- "noConfLeft": "Unfortunately, you've ran out of your email confirmation requests.\nPlease make sure you are able to receive e-mails and check your SPAM folder again for messages.\nYou may try to use a different email address or contact our customer support service."
+ "noConfLeft": "Unfortunately, you've ran out of your email confirmation requests.\nPlease make sure you are able to receive e-mails and check your SPAM folder again for messages.\nYou may try to use a different email address or contact our customer support service.",
+ "timezone": "The time zone preference.\nThis will be the time zone in which the data is displayed. It's only used for visualization.\nNo data will be modified based on this setting."
},
"dashboard": {
"newProject": "New project",
diff --git a/src/pages/UserSettings/UserSettings.jsx b/src/pages/UserSettings/UserSettings.jsx
index 453093479..7288f9ffd 100644
--- a/src/pages/UserSettings/UserSettings.jsx
+++ b/src/pages/UserSettings/UserSettings.jsx
@@ -14,10 +14,13 @@ import Input from 'ui/Input'
import Button from 'ui/Button'
import Modal from 'ui/Modal'
import Select from 'ui/Select'
+import TimezonePicker from 'ui/TimezonePicker'
import {
isValidEmail, isValidPassword, MIN_PASSWORD_CHARS,
} from 'utils/validator'
+const DEFAULT_TIMEZONE = 'Etc/GMT'
+
const UserSettings = ({
onDelete, onExport, onSubmit, onEmailConfirm, t,
}) => {
@@ -28,6 +31,7 @@ const UserSettings = ({
password: '',
repeat: '',
})
+ const [selectedTimezone, setSelectedTimezone] = useState(user.timezone || DEFAULT_TIMEZONE)
const [reportFrequency, setReportFrequency] = useState(user.reportFrequency)
const [validated, setValidated] = useState(false)
const [errors, setErrors] = useState({})
@@ -139,6 +143,14 @@ const UserSettings = ({
error={beenSubmitted ? errors.repeat : null}
/>
</div>
+ <div className='grid grid-cols-1 gap-y-6 gap-x-4 lg:grid-cols-2 mt-4'>
+ <div>
+ <TimezonePicker
+ value={selectedTimezone}
+ onChange={setSelectedTimezone}
+ />
+ </div>
+ </div>
<Button className='mt-4' type='submit' primary large>
{t('profileSettings.update')}
</Button>
diff --git a/src/ui/Select.jsx b/src/ui/Select.jsx
index 6ea7a6643..f5f80cfed 100644
--- a/src/ui/Select.jsx
+++ b/src/ui/Select.jsx
@@ -12,7 +12,7 @@ const Select = ({
<Listbox className={className} value={title} onChange={onSelect}>
{({ open }) => (
<>
- <Listbox.Label className='block text-sm font-medium text-gray-700 dark:text-gray-100'>{label}</Listbox.Label>
+ <Listbox.Label className='block text-sm whitespace-pre-line font-medium text-gray-700 dark:text-gray-100'>{label}</Listbox.Label>
<div className='mt-1 relative'>
<Listbox.Button className='relative w-full bg-white border border-gray-300 dark:text-gray-50 dark:border-gray-800 dark:bg-gray-700 rounded-md shadow-sm pl-3 pr-10 py-2 text-left cursor-default focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm'>
<span className='block truncate capitalize'>{title}</span>
@@ -46,7 +46,7 @@ const Select = ({
{({ selected, active }) => (
<>
<span className={cx(selected ? 'font-semibold' : 'font-normal', 'block truncate capitalize')}>
- {item}
+ {labelExtractor ? labelExtractor(item) : item}
</span>
{selected ? (
diff --git a/src/ui/TimezonePicker.jsx b/src/ui/TimezonePicker.jsx
new file mode 100644
index 000000000..913099f8d
--- /dev/null
+++ b/src/ui/TimezonePicker.jsx
@@ -0,0 +1,82 @@
+import React from 'react'
+import spacetime from 'spacetime'
+import soft from 'timezone-soft'
+import { allTimezones } from 'react-timezone-select'
+import { useTranslation } from 'react-i18next'
+import PropTypes from 'prop-types'
+import _find from 'lodash/find'
+
+import Select from './Select'
+
+const options = Object.entries(allTimezones)
+ .reduce((selectOptions, zone) => {
+ const now = spacetime.now(zone[0])
+ const tz = now.timezone()
+ const tzStrings = soft(zone[0])
+
+ let abbr = now.isDST() ? tzStrings[0].daylight?.abbr : tzStrings[0].standard?.abbr
+ let altName = now.isDST() ? tzStrings[0].daylight?.name : tzStrings[0].standard?.name
+
+ const min = tz.current.offset * 60
+ const hr = `${(min / 60) ^ 0}:` + (min % 60 === 0 ? '00' : Math.abs(min % 60))
+ const label = `(GMT${hr.includes('-') ? hr : `+${hr}`}) ${zone[1]}`
+
+ selectOptions.push({
+ value: tz.name,
+ label: label,
+ offset: tz.current.offset,
+ abbrev: abbr,
+ altName: altName,
+ })
+
+ return selectOptions
+ }, [])
+ .sort((a, b) => a.offset - b.offset)
+
+const TimezoneSelect = ({
+ value, onChange,
+}) => {
+ const { t } = useTranslation('common')
+ const labelExtractor = (option) => option?.label
+ const keyExtractor = (option) => option?.value
+
+ const handleChange = (label) => {
+ const tz = _find(options, tz => labelExtractor(tz) === label)
+ const key = keyExtractor(tz)
+ onChange(key)
+ }
+
+ const parseTimezone = (zone) => {
+ if (typeof zone === 'object' && zone.value && zone.label) return zone
+ if (typeof zone === 'string') {
+ return _find(options, tz => tz.value === zone)
+ } else if (zone.value && !zone.label) {
+ return _find(options, tz => tz.value === zone.value)
+ }
+ }
+
+ return (
+ <Select
+ title={labelExtractor(parseTimezone(value))}
+ label={t('profileSettings.timezone')}
+ className='w-full'
+ items={options}
+ labelExtractor={labelExtractor}
+ keyExtractor={keyExtractor}
+ onSelect={handleChange}
+ />
+ )
+}
+
+TimezoneSelect.propTypes = {
+ value: PropTypes.oneOfType([
+ PropTypes.string, PropTypes.object,
+ ]).isRequired,
+ onChange: PropTypes.func,
+}
+
+TimezoneSelect.defaultProps = {
+ onChange: () => { },
+}
+
+export default TimezoneSelect