From 696d9c978c5c631216fb0186ee0fdca93c4d7588 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 8 Mar 2023 16:37:43 -0800 Subject: [PATCH] Updated date range handling. Fixed share page. --- .../{helpers => common}/StickyHeader.js | 9 +- components/helpers/CheckVisible.js | 41 ------- components/input/DateFilter.js | 2 +- components/input/RefreshButton.js | 6 +- components/input/SettingsButton.js | 7 +- components/input/UserButton.js | 80 ------------- components/input/UserButton.module.css | 25 ---- components/layout/Footer.js | 4 +- components/layout/Footer.module.css | 8 +- components/messages.js | 2 + components/metrics/PageviewsChart.js | 110 ++++++++---------- components/metrics/WebsiteChart.js | 2 +- .../pages/realtime/RealtimeDashboard.js | 2 +- .../settings/profile/DateRangeSetting.js | 2 +- hooks/useDateRange.js | 54 ++++----- lib/date.js | 2 +- lib/middleware.ts | 4 +- next.config.js | 30 ++++- package.json | 4 +- pages/_app.js | 6 +- pages/api/config.ts | 4 +- pages/api/{collect.ts => send.ts} | 0 pages/login.js | 2 +- pages/settings/index.js | 4 +- store/app.js | 14 ++- store/websites.js | 10 +- tracker/index.js | 4 +- yarn.lock | 8 +- 28 files changed, 166 insertions(+), 280 deletions(-) rename components/{helpers => common}/StickyHeader.js (64%) delete mode 100644 components/helpers/CheckVisible.js delete mode 100644 components/input/UserButton.js delete mode 100644 components/input/UserButton.module.css rename pages/api/{collect.ts => send.ts} (100%) diff --git a/components/helpers/StickyHeader.js b/components/common/StickyHeader.js similarity index 64% rename from components/helpers/StickyHeader.js rename to components/common/StickyHeader.js index 62dcb74a..639709f9 100644 --- a/components/helpers/StickyHeader.js +++ b/components/common/StickyHeader.js @@ -12,17 +12,18 @@ export default function StickyHeader({ }) { const { ref: scrollRef, isSticky } = useSticky({ scrollElement }); const { ref: measureRef, dimensions } = useMeasure(); + const active = enabled && isSticky; return (
{children}
diff --git a/components/helpers/CheckVisible.js b/components/helpers/CheckVisible.js deleted file mode 100644 index a150f725..00000000 --- a/components/helpers/CheckVisible.js +++ /dev/null @@ -1,41 +0,0 @@ -import { useState, useRef, useEffect } from 'react'; - -function isInViewport(element) { - const rect = element.getBoundingClientRect(); - return !( - rect.bottom < 0 || - rect.right < 0 || - rect.left > window.innerWidth || - rect.top > window.innerHeight - ); -} - -export default function CheckVisible({ className, children }) { - const [visible, setVisible] = useState(false); - const ref = useRef(); - - useEffect(() => { - const checkPosition = () => { - if (ref.current) { - const state = isInViewport(ref.current); - if (state !== visible) { - setVisible(state); - } - } - }; - - checkPosition(); - - window.addEventListener('scroll', checkPosition); - - return () => { - window.removeEventListener('scroll', checkPosition); - }; - }, [visible]); - - return ( -
- {typeof children === 'function' ? children(visible) : children} -
- ); -} diff --git a/components/input/DateFilter.js b/components/input/DateFilter.js index a340fe48..b10388e7 100644 --- a/components/input/DateFilter.js +++ b/components/input/DateFilter.js @@ -18,7 +18,7 @@ function DateFilter({ websiteId, value, className }) { const [showPicker, setShowPicker] = useState(false); async function handleDateChange(value) { - if (value === 'all') { + if (value === 'all' && websiteId) { const data = await get(`/websites/${websiteId}`); if (data) { diff --git a/components/input/RefreshButton.js b/components/input/RefreshButton.js index 031e4ee6..a5c8493b 100644 --- a/components/input/RefreshButton.js +++ b/components/input/RefreshButton.js @@ -1,6 +1,6 @@ import { useIntl } from 'react-intl'; import { LoadingButton, Icon, Tooltip } from 'react-basics'; -import { setDateRange } from 'store/websites'; +import { setWebsiteDateRange } from 'store/websites'; import useDateRange from 'hooks/useDateRange'; import Icons from 'components/icons'; import { labels } from 'components/messages'; @@ -12,9 +12,9 @@ function RefreshButton({ websiteId, isLoading }) { function handleClick() { if (!isLoading && dateRange) { if (/^\d+/.test(dateRange.value)) { - setDateRange(websiteId, dateRange.value); + setWebsiteDateRange(websiteId, dateRange.value); } else { - setDateRange(websiteId, dateRange); + setWebsiteDateRange(websiteId, dateRange); } } } diff --git a/components/input/SettingsButton.js b/components/input/SettingsButton.js index 46a01e13..3a6068e3 100644 --- a/components/input/SettingsButton.js +++ b/components/input/SettingsButton.js @@ -18,7 +18,12 @@ export default function SettingsButton() { - + e.stopPropagation()} + >
diff --git a/components/input/UserButton.js b/components/input/UserButton.js deleted file mode 100644 index f6b42d62..00000000 --- a/components/input/UserButton.js +++ /dev/null @@ -1,80 +0,0 @@ -import useConfig from 'hooks/useConfig'; -import useUser from 'hooks/useUser'; -import { AUTH_TOKEN } from 'lib/constants'; -import { removeItem } from 'next-basics'; -import { useRouter } from 'next/router'; -import { useRef, useState } from 'react'; -import { Button, Icon, Item, Menu, Popup, Text } from 'react-basics'; -import { FormattedMessage } from 'react-intl'; -import useDocumentClick from 'hooks/useDocumentClick'; -import Profile from 'assets/profile.svg'; -import styles from './UserButton.module.css'; - -export default function UserButton() { - const [show, setShow] = useState(false); - const ref = useRef(); - const { user } = useUser(); - const router = useRouter(); - const { adminDisabled } = useConfig(); - - const menuOptions = [ - { - label: ( - {user.username} }} - /> - ), - value: 'username', - className: styles.username, - }, - { - label: , - value: 'profile', - hidden: adminDisabled, - divider: true, - }, - { label: , value: 'logout' }, - ]; - - function handleClick() { - setShow(state => !state); - } - - function handleSelect(value) { - if (value === 'logout') { - removeItem(AUTH_TOKEN); - router.push('/login'); - } else if (value === 'profile') { - router.push('/profile'); - } - } - - useDocumentClick(e => { - if (!ref.current?.contains(e.target)) { - setShow(false); - } - }); - - return ( -
- - {show && ( - - - {({ label, value }) => ( - - {label} - - )} - - - )} -
- ); -} diff --git a/components/input/UserButton.module.css b/components/input/UserButton.module.css deleted file mode 100644 index 6c4dfdd0..00000000 --- a/components/input/UserButton.module.css +++ /dev/null @@ -1,25 +0,0 @@ -.button { - position: relative; -} - -.username { - border-bottom: 1px solid var(--base500); -} - -.username:hover { - background: var(--base50); -} - -.icon svg { - height: 16px; - width: 16px; -} - -.menu { - left: -50%; - background: var(--base50); - border: 1px solid var(--base500); - border-radius: 4px; - overflow: hidden; - z-index: 100; -} diff --git a/components/layout/Footer.js b/components/layout/Footer.js index 7d202256..64812b8e 100644 --- a/components/layout/Footer.js +++ b/components/layout/Footer.js @@ -8,7 +8,7 @@ export default function Footer() { return (