diff --git a/dashboard/lib/hooks/use-top-browsers.ts b/dashboard/lib/hooks/use-top-browsers.ts deleted file mode 100644 index 9bbd10f..0000000 --- a/dashboard/lib/hooks/use-top-browsers.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { queryPipe } from '../api' -import browsers from '../constants/browsers' -import { TopBrowsers, TopBrowsersData } from '../types/top-browsers' -import useDateFilter from './use-date-filter' -import useQuery from './use-query' - -async function getTopBrowsers( - date_from?: string, - date_to?: string -): Promise { - const { data: queryData } = await queryPipe('top_browsers', { - date_from, - date_to, - limit: 4, - }) - const data = [...queryData] - .sort((a, b) => b.visits - a.visits) - .map(({ browser, visits }) => ({ - browser: browsers[browser] ?? browser, - visits, - })) - - return { data } -} - -export default function useTopBrowsers() { - const { startDate, endDate } = useDateFilter() - return useQuery([startDate, endDate, 'topBrowsers'], getTopBrowsers) -} diff --git a/dashboard/lib/hooks/use-top-devices.ts b/dashboard/lib/hooks/use-top-devices.ts deleted file mode 100644 index b28a6bd..0000000 --- a/dashboard/lib/hooks/use-top-devices.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { queryPipe } from '../api' -import devices from '../constants/devices' -import { TopDevicesData, TopDevices } from '../types/top-devices' -import useDateFilter from './use-date-filter' -import useQuery from './use-query' - -async function getTopDevices( - date_from?: string, - date_to?: string -): Promise { - const { data: queryData } = await queryPipe('top_devices', { - date_from, - date_to, - limit: 4, - }) - const data = [...queryData] - .sort((a, b) => b.visits - a.visits) - .map(({ device, visits }) => ({ - device: devices[device] ?? device, - visits, - })) - - return { data } -} - -export default function useTopDevices() { - const { startDate, endDate } = useDateFilter() - return useQuery([startDate, endDate, 'topDevices'], getTopDevices) -} diff --git a/dashboard/lib/hooks/use-top-locations.ts b/dashboard/lib/hooks/use-top-locations.ts deleted file mode 100644 index 7ddb227..0000000 --- a/dashboard/lib/hooks/use-top-locations.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { queryPipe } from '../api' -import { - TopLocation, - TopLocationsData, - TopLocationsSorting, -} from '../types/top-locations' -import useDateFilter from './use-date-filter' -import useParams from './use-params' -import useQuery from './use-query' - -function getFlagEmoji(countryCode: string) { - const codePoints = countryCode - .toUpperCase() - .split('') - .map(char => 127397 + char.charCodeAt(0)) - return String.fromCodePoint(...codePoints) -} - -async function getTopLocations( - sorting: TopLocationsSorting, - date_from?: string, - date_to?: string -) { - const { data: queryData } = await queryPipe( - 'top_locations', - { limit: 8, date_from, date_to } - ) - const regionNames = new Intl.DisplayNames(['en'], { type: 'region' }) - - const data: TopLocation[] = [...queryData] - .sort((a, b) => b[sorting] - a[sorting]) - .map(({ location, ...rest }) => { - const unknownLocation = '🌎 Unknown' - return { - location: location - ? `${getFlagEmoji(location)} ${regionNames.of(location)}` - : unknownLocation, - shortLocation: location - ? `${getFlagEmoji(location)} ${location}` - : unknownLocation, - ...rest, - } - }) - - const locations = data.map(({ location }) => location) - const labels = data.map(record => record[sorting]) - - return { - data, - locations, - labels, - } -} - -export default function useTopLocations() { - const { startDate, endDate } = useDateFilter() - const [sorting] = useParams({ - key: 'top_locations_sorting', - defaultValue: TopLocationsSorting.Visitors, - values: Object.values(TopLocationsSorting), - }) - return useQuery( - [sorting, startDate, endDate, 'topLocations'], - getTopLocations - ) -} diff --git a/dashboard/lib/hooks/use-top-pages.ts b/dashboard/lib/hooks/use-top-pages.ts deleted file mode 100644 index a2c9790..0000000 --- a/dashboard/lib/hooks/use-top-pages.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { queryPipe } from '../api' -import { TopPagesData, TopPagesSorting } from '../types/top-pages' -import useDateFilter from './use-date-filter' -import useParams from './use-params' -import useQuery from './use-query' - -async function getTopPages( - sorting: TopPagesSorting, - date_from?: string, - date_to?: string -) { - const { data: queryData, meta } = await queryPipe('top_pages', { - limit: 8, - date_from, - date_to, - }) - const data = [...queryData].sort((a, b) => b[sorting] - a[sorting]) - - const columnLabels = { - pathname: 'content', - visits: 'visitors', - hits: 'pageviews', - } - const columns = meta.map(({ name }) => ({ - label: columnLabels[name], - value: name, - })) - const pages = data.map(({ pathname }) => pathname) - const labels = data.map(record => record[sorting]) - - return { - data, - columns, - pages, - labels, - } -} - -export default function useTopPages() { - const { startDate, endDate } = useDateFilter() - const [sorting] = useParams({ - key: 'top_pages_sorting', - defaultValue: TopPagesSorting.Visitors, - values: Object.values(TopPagesSorting), - }) - return useQuery([sorting, startDate, endDate, 'topPages'], getTopPages) -} diff --git a/dashboard/lib/hooks/use-top-sources.ts b/dashboard/lib/hooks/use-top-sources.ts deleted file mode 100644 index b1bd3b3..0000000 --- a/dashboard/lib/hooks/use-top-sources.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { queryPipe } from '../api' -import { TopSource, TopSources } from '../types/top-sources' -import useDateFilter from './use-date-filter' -import useQuery from './use-query' - -async function getTopSources( - date_from?: string, - date_to?: string -): Promise { - const { data: queryData } = await queryPipe('top_sources', { - limit: 8, - date_from, - date_to, - }) - - const data: TopSource[] = [...queryData] - .sort((a, b) => b.visits - a.visits) - .map(({ referrer, visits }) => ({ - referrer: referrer || 'Direct', - href: referrer ? `https://${referrer}` : undefined, - visits, - })) - const refs = data.map(({ referrer }) => referrer) - const visits = data.map(({ visits }) => visits) - - return { - data, - refs, - visits, - } -} - -export default function useTopSources() { - const { startDate, endDate } = useDateFilter() - return useQuery([startDate, endDate, 'topSources'], getTopSources) -}