From 5a73c224b7f2f6554e276b31ad0e78e368643cb2 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 11 Oct 2020 02:29:55 -0700 Subject: [PATCH] Created share token hook. --- components/metrics/ActiveUsers.js | 7 +++++-- components/metrics/BarChart.js | 2 +- components/metrics/BrowsersTable.js | 3 +-- components/metrics/CountriesTable.js | 3 +-- components/metrics/DevicesTable.js | 3 +-- components/metrics/EventsChart.js | 5 ++++- components/metrics/EventsTable.js | 3 +-- components/metrics/MetricsBar.js | 9 ++++++--- components/metrics/MetricsTable.js | 7 ++++--- components/metrics/OSTable.js | 5 ++--- components/metrics/PagesTable.js | 3 +-- components/metrics/ReferrersTable.js | 7 +++---- components/metrics/WebsiteChart.js | 10 ++++++---- components/metrics/WebsiteHeader.js | 4 ++-- components/pages/RealtimeDashboard.js | 3 ++- components/pages/WebsiteDetails.js | 14 ++++++++------ hooks/useShareToken.js | 25 +++++++++++++++++++++++++ lib/auth.js | 5 +++-- lib/constants.js | 1 + pages/api/realtime/update.js | 3 ++- pages/share/[...id].js | 10 +++++----- redux/actions/app.js | 7 ++++++- 22 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 hooks/useShareToken.js diff --git a/components/metrics/ActiveUsers.js b/components/metrics/ActiveUsers.js index 3b691c10..6dabd3d5 100644 --- a/components/metrics/ActiveUsers.js +++ b/components/metrics/ActiveUsers.js @@ -3,12 +3,15 @@ import { FormattedMessage } from 'react-intl'; import classNames from 'classnames'; import useFetch from 'hooks/useFetch'; import Dot from 'components/common/Dot'; +import { TOKEN_HEADER } from 'lib/constants'; +import useShareToken from 'hooks/useShareToken'; import styles from './ActiveUsers.module.css'; -export default function ActiveUsers({ websiteId, token, className }) { +export default function ActiveUsers({ websiteId, className }) { + const shareToken = useShareToken(); const { data } = useFetch(`/api/website/${websiteId}/active`, { - params: { token }, interval: 60000, + headers: { [TOKEN_HEADER]: shareToken?.token }, }); const count = useMemo(() => { return data?.[0]?.x || 0; diff --git a/components/metrics/BarChart.js b/components/metrics/BarChart.js index 05183e1b..a31a6f40 100644 --- a/components/metrics/BarChart.js +++ b/components/metrics/BarChart.js @@ -65,7 +65,7 @@ export default function BarChart({ } function renderYLabel(label) { - return +label > 1 ? formatLongNumber(label) : label; + return +label > 1000 ? formatLongNumber(label) : label; } function renderTooltip(model) { diff --git a/components/metrics/BrowsersTable.js b/components/metrics/BrowsersTable.js index 3481449c..12c1087b 100644 --- a/components/metrics/BrowsersTable.js +++ b/components/metrics/BrowsersTable.js @@ -3,7 +3,7 @@ import { FormattedMessage } from 'react-intl'; import MetricsTable from './MetricsTable'; import { browserFilter } from 'lib/filters'; -export default function BrowsersTable({ websiteId, token, ...props }) { +export default function BrowsersTable({ websiteId, ...props }) { return ( } websiteId={websiteId} - token={token} dataFilter={browserFilter} /> ); diff --git a/components/metrics/CountriesTable.js b/components/metrics/CountriesTable.js index 5b44bd23..59d17dfb 100644 --- a/components/metrics/CountriesTable.js +++ b/components/metrics/CountriesTable.js @@ -5,7 +5,7 @@ import { FormattedMessage } from 'react-intl'; import useCountryNames from 'hooks/useCountryNames'; import useLocale from 'hooks/useLocale'; -export default function CountriesTable({ websiteId, token, onDataLoad, ...props }) { +export default function CountriesTable({ websiteId, onDataLoad, ...props }) { const [locale] = useLocale(); const countryNames = useCountryNames(locale); @@ -20,7 +20,6 @@ export default function CountriesTable({ websiteId, token, onDataLoad, ...props type="country" metric={} websiteId={websiteId} - token={token} onDataLoad={data => onDataLoad?.(percentFilter(data))} renderLabel={renderLabel} /> diff --git a/components/metrics/DevicesTable.js b/components/metrics/DevicesTable.js index e71355db..52b6b5fc 100644 --- a/components/metrics/DevicesTable.js +++ b/components/metrics/DevicesTable.js @@ -4,7 +4,7 @@ import { deviceFilter } from 'lib/filters'; import { FormattedMessage } from 'react-intl'; import { getDeviceMessage } from 'components/messages'; -export default function DevicesTable({ websiteId, token, ...props }) { +export default function DevicesTable({ websiteId, ...props }) { return ( } websiteId={websiteId} - token={token} dataFilter={deviceFilter} renderLabel={({ x }) => getDeviceMessage(x)} /> diff --git a/components/metrics/EventsChart.js b/components/metrics/EventsChart.js index e401402d..9a5827b2 100644 --- a/components/metrics/EventsChart.js +++ b/components/metrics/EventsChart.js @@ -6,13 +6,15 @@ import useFetch from 'hooks/useFetch'; import useDateRange from 'hooks/useDateRange'; import useTimezone from 'hooks/useTimezone'; import usePageQuery from 'hooks/usePageQuery'; -import { EVENT_COLORS } from 'lib/constants'; +import useShareToken from 'hooks/useShareToken'; +import { EVENT_COLORS, TOKEN_HEADER } from 'lib/constants'; export default function EventsChart({ websiteId, className, token }) { const [dateRange] = useDateRange(websiteId); const { startDate, endDate, unit, modified } = dateRange; const [timezone] = useTimezone(); const { query } = usePageQuery(); + const shareToken = useShareToken(); const { data } = useFetch( `/api/website/${websiteId}/events`, @@ -25,6 +27,7 @@ export default function EventsChart({ websiteId, className, token }) { url: query.url, token, }, + headers: { [TOKEN_HEADER]: shareToken?.token }, }, [modified], ); diff --git a/components/metrics/EventsTable.js b/components/metrics/EventsTable.js index 73df5028..c415a3e9 100644 --- a/components/metrics/EventsTable.js +++ b/components/metrics/EventsTable.js @@ -3,7 +3,7 @@ import { FormattedMessage } from 'react-intl'; import MetricsTable from './MetricsTable'; import Tag from 'components/common/Tag'; -export default function EventsTable({ websiteId, token, ...props }) { +export default function EventsTable({ websiteId, ...props }) { return ( } websiteId={websiteId} - token={token} renderLabel={({ x }) =>