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!

(feature) Hotkeys (#393)

Andrii Romasiun4a55e397ad90
diff --git a/app/modals/ViewProjectHotkeys.tsx b/app/modals/ViewProjectHotkeys.tsx
new file mode 100644
index 000000000..aa3e30b11
--- /dev/null
+++ b/app/modals/ViewProjectHotkeys.tsx
@@ -0,0 +1,104 @@
+import React, { useMemo } from 'react'
+import { useTranslation } from 'react-i18next'
+import PropTypes from 'prop-types'
+import { TFunction } from 'i18next'
+import _map from 'lodash/map'
+
+import Modal from 'ui/Modal'
+import { Badge } from 'ui/Badge'
+
+interface IViewProjectHotkeys {
+ onClose: () => void
+ isOpened: boolean
+}
+
+interface IHotkeysList {
+ title: string
+ hotkeys: { [key: string]: string }
+}
+
+const getHotkeys = (t: TFunction) => ({
+ [t('modals.shortcuts.timebuckets')]: {
+ [t('project.thisHour')]: 'H',
+ [t('project.today')]: 'T',
+ [t('project.yesterday')]: 'Y',
+ [t('project.last24h')]: 'D',
+ [t('project.lastXDays', { amount: 7 })]: 'W',
+ [t('project.lastXWeeks', { amount: 4 })]: 'M',
+ [t('project.lastXMonths', { amount: 3 })]: 'Q',
+ [t('project.lastXMonths', { amount: 12 })]: 'L',
+ [t('project.lastXMonths', { amount: 24 })]: 'Z',
+ [t('project.all')]: 'A',
+ [t('project.compare')]: 'C',
+ [t('project.custom')]: 'U',
+ },
+ [t('modals.shortcuts.tabs')]: {
+ [t('dashboard.traffic')]: 'Shift + T',
+ [t('dashboard.performance')]: 'Shift + P',
+ [t('dashboard.sessions')]: 'Shift + S',
+ [t('dashboard.funnels')]: 'Shift + F',
+ [t('dashboard.alerts')]: 'Shift + A',
+ [t('common.settings')]: 'Shift + E',
+ },
+ [t('common.general')]: {
+ [t('modals.forecast.title')]: 'Alt + F',
+ [t('project.search')]: 'Alt + S',
+ [t('project.barChart')]: 'Alt + B',
+ [t('project.lineChart')]: 'Alt + L',
+ [t('project.refreshStats')]: 'R',
+ },
+})
+
+const HotkeysList = ({ title, hotkeys }: IHotkeysList) => (
+ <div className='mt-2 first:mt-0'>
+ <table className='min-w-full divide-y divide-gray-300 dark:divide-gray-700'>
+ <thead>
+ <tr>
+ <th className='text-left font-semibold text-base text-gray-900 dark:text-gray-50 pb-2'>{title}</th>
+ <th />
+ </tr>
+ </thead>
+ <tbody className='divide-y divide-gray-200 dark:divide-gray-800'>
+ {_map(hotkeys, (action, label) => (
+ <tr key={label}>
+ <td className='text-left whitespace-nowrap py-2 pl-4 pr-3 text-sm text-gray-900 dark:text-gray-50 sm:pl-0'>
+ {label}
+ </td>
+ <td className='text-left whitespace-nowrap py-2 pl-4 pr-3 text-sm text-gray-900 dark:text-gray-50 sm:pl-0'>
+ <Badge colour='slate' label={action} />
+ </td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+)
+
+const ViewProjectHotkeys = ({ onClose, isOpened }: IViewProjectHotkeys): JSX.Element => {
+ const { t } = useTranslation('common')
+ const hotkeys = useMemo(() => getHotkeys(t), [t])
+
+ return (
+ <Modal
+ onClose={onClose}
+ closeText={t('common.close')}
+ message={
+ <div className='mt-3 gap-5 columns-1 md:columns-2'>
+ {_map(hotkeys, (_hotkeys, title) => (
+ <HotkeysList title={title} hotkeys={_hotkeys} key={title} />
+ ))}
+ </div>
+ }
+ title={t('modals.shortcuts.title')}
+ isOpened={isOpened}
+ size='large'
+ />
+ )
+}
+
+ViewProjectHotkeys.propTypes = {
+ onClose: PropTypes.func.isRequired,
+ isOpened: PropTypes.bool.isRequired,
+}
+
+export default ViewProjectHotkeys
diff --git a/app/pages/Project/View/ViewProject.helpers.tsx b/app/pages/Project/View/ViewProject.helpers.tsx
index 94c53228d..3259fe9df 100644
--- a/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/app/pages/Project/View/ViewProject.helpers.tsx
@@ -39,6 +39,8 @@ import {
tbsFormatMapper24h,
tbsFormatMapperTooltip,
tbsFormatMapperTooltip24h,
+ PROJECT_TABS,
+ isSelfhosted,
} from 'redux/constants'
import { getTimeFromSeconds, getStringFromTime, sumArrays, nFormatter } from 'utils/generic'
import countries from 'utils/isoCountries'
@@ -1360,6 +1362,24 @@ const convertFilters = (filters: any) => {
)
}
+const SHORTCUTS_TABS_MAP = {
+ T: PROJECT_TABS.traffic,
+ P: PROJECT_TABS.performance,
+ F: PROJECT_TABS.funnels,
+ S: PROJECT_TABS.sessions,
+ A: PROJECT_TABS.alerts,
+}
+
+const _SHORTCUTS_TABS_LISTENERS = 'shift+t, shift+p, shift+s, shift+f, shift+e'
+const SHORTCUTS_TABS_LISTENERS = isSelfhosted ? _SHORTCUTS_TABS_LISTENERS : _SHORTCUTS_TABS_LISTENERS + ', shift+a'
+
+const _SHORTCUTS_GENERAL_LISTENERS = 'alt+s,alt+ß, alt+b,alt+∫, alt+l,alt+¬, r'
+const SHORTCUTS_GENERAL_LISTENERS = isSelfhosted
+ ? _SHORTCUTS_GENERAL_LISTENERS
+ : _SHORTCUTS_GENERAL_LISTENERS + ',alt+f,alt+ƒ'
+
+const SHORTCUTS_TIMEBUCKETS_LISTENERS = 'h, t, y, d, w, m, q, l, z, a, u, c'
+
export {
iconClassName,
getFormatDate,
@@ -1382,4 +1402,8 @@ export {
getSettingsFunnels,
getSettingsSession,
convertFilters,
+ SHORTCUTS_TABS_MAP,
+ SHORTCUTS_TABS_LISTENERS,
+ SHORTCUTS_GENERAL_LISTENERS,
+ SHORTCUTS_TIMEBUCKETS_LISTENERS,
}
diff --git a/app/pages/Project/View/ViewProject.tsx b/app/pages/Project/View/ViewProject.tsx
index f6a36a081..74dd4f191 100644
--- a/app/pages/Project/View/ViewProject.tsx
+++ b/app/pages/Project/View/ViewProject.tsx
@@ -25,6 +25,7 @@ import {
import cx from 'clsx'
import dayjs from 'dayjs'
import { useTranslation } from 'react-i18next'
+import { useHotkeys } from 'react-hotkeys-hook'
import _keys from 'lodash/keys'
import _map from 'lodash/map'
import _includes from 'lodash/includes'
@@ -111,6 +112,7 @@ import LineChart from 'ui/icons/LineChart'
import BarChart from 'ui/icons/BarChart'
import Forecast from 'modals/Forecast'
import NewFunnel from 'modals/NewFunnel'
+import ViewProjectHotkeys from 'modals/ViewProjectHotkeys'
import routes from 'routesPath'
import Header from 'components/Header'
import Footer from 'components/Footer'
@@ -153,6 +155,10 @@ import {
FILTER_CHART_METRICS_MAPPING_FOR_COMPARE,
getSettingsFunnels,
convertFilters,
+ SHORTCUTS_TABS_LISTENERS,
+ SHORTCUTS_TABS_MAP,
+ SHORTCUTS_GENERAL_LISTENERS,
+ SHORTCUTS_TIMEBUCKETS_LISTENERS,
} from './ViewProject.helpers'
import CCRow from './components/CCRow'
import FunnelsList from './components/FunnelsList'
@@ -422,6 +428,8 @@ const ViewProject = ({
return projectTab || PROJECT_TABS.traffic
})
+ const [isHotkeysHelpOpened, setIsHotkeysHelpOpened] = useState(false)
+
// sessions
const [sessionsSkip, setSessionsSkip] = useState<number>(0)
const [canLoadMoreSessions, setCanLoadMoreSessions] = useState<boolean>(false)
@@ -2736,6 +2744,97 @@ const ViewProject = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isActiveCompare, activePeriodCompare, dateRangeCompare])
+ /* KEYBOARD SHORTCUTS */
+ const generalShortcutsActions = {
+ B: () => setChartTypeOnClick(chartTypes.bar),
+ '∫': () => setChartTypeOnClick(chartTypes.bar),
+ L: () => setChartTypeOnClick(chartTypes.line),
+ '¬': () => setChartTypeOnClick(chartTypes.line),
+ S: () => setShowFiltersSearch(true),
+ ß: () => setShowFiltersSearch(true),
+ F: onForecastOpen,
+ ƒ: onForecastOpen,
+ r: refreshStats,
+ }
+
+ const timebucketShortcutsMap = {
+ h: '1h',
+ t: 'today',
+ y: 'yesterday',
+ d: '1d',
+ w: '7d',
+ m: '4w',
+ q: '3M',
+ l: '12M',
+ z: '24M',
+ a: KEY_FOR_ALL_TIME,
+ u: 'custom',
+ c: 'compare',
+ }
+
+ // 'Keyboard shortcuts' help modal
+ useHotkeys('shift+?', () => {
+ setIsHotkeysHelpOpened((val) => !val)
+ })
+
+ // 'Tabs switching' shortcuts
+ useHotkeys(SHORTCUTS_TABS_LISTENERS, ({ key }) => {
+ if (key === 'E') {
+ openSettingsHandler()
+ return
+ }
+
+ // @ts-ignore
+ const tab = SHORTCUTS_TABS_MAP[key]
+
+ if (!tab) {
+ return
+ }
+
+ setProjectTab(tab)
+ setActiveTab(tab)
+ })
+
+ // 'General' shortcuts
+ useHotkeys(SHORTCUTS_GENERAL_LISTENERS, ({ key }) => {
+ // @ts-ignore
+ generalShortcutsActions[key]?.()
+ })
+
+ // 'Timebuckets selection' shortcuts
+ useHotkeys(SHORTCUTS_TIMEBUCKETS_LISTENERS, ({ key }) => {
+ const pairs = tbPeriodPairs(t, undefined, undefined, language)
+ // @ts-ignore
+ const pair = _find(pairs, ({ period }) => period === timebucketShortcutsMap[key])
+
+ if (!pair) {
+ return
+ }
+
+ if (pair.isCustomDate) {
+ // @ts-ignore
+ refCalendar.current?.openCalendar?.()
+ return
+ }
+
+ if (pair.period === 'compare') {
+ if (activeTab === PROJECT_TABS.alerts) {
+ return
+ }
+
+ if (isActiveCompare) {
+ compareDisable()
+ } else {
+ setIsActiveCompare(true)
+ }
+
+ return
+ }
+
+ setDateRange(null)
+ updatePeriod(pair)
+ })
+
const TabsSelector = () => (
<div>
<div className='sm:hidden'>
@@ -3907,6 +4006,7 @@ const ViewProject = ({
activeTB={t(`project.${timeBucket}`)}
tb={timeBucket}
/>
+ <ViewProjectHotkeys isOpened={isHotkeysHelpOpened} onClose={() => setIsHotkeysHelpOpened(false)} />
<NewFunnel
project={project}
projectPassword={projectPassword}
diff --git a/package.json b/package.json
index b49cb3816..7ff6336fd 100644
--- a/package.json
+++ b/package.json
@@ -58,6 +58,7 @@
"react-dom": "^18.2.0",
"react-flagkit": "^2.0.4",
"react-flatpickr": "^3.10.13",
+ "react-hotkeys-hook": "^4.5.0",
"react-i18next": "^13.3.1",
"react-outside-click-handler": "^1.3.0",
"react-qr-code": "^2.0.12",
diff --git a/public/locales/en.json b/public/locales/en.json
index d6b533aca..9a497eafb 100644
--- a/public/locales/en.json
+++ b/public/locales/en.json
@@ -91,7 +91,8 @@
"unlink": "Unlink",
"nothingFound": "Nothing found",
"oneMember": "1 Member",
- "xMembers": "{{number}} Members"
+ "xMembers": "{{number}} Members",
+ "general": "General"
},
"main": {
"ukrSupport": "Support the Ukrainian army to fight Russian invasion.",
@@ -813,6 +814,11 @@
"name": "Funnel name",
"steps": "Funnel steps",
"addStep": "Add one more step"
+ },
+ "shortcuts": {
+ "title": "Keyboard shortcuts",
+ "timebuckets": "Timebuckets selection",
+ "tabs": "Tabs switching"
}
},
"pricing": {