-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert everything to PaginatedTableCards
- Loading branch information
1 parent
2eab93a
commit 3652ecc
Showing
7 changed files
with
207 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useFetcher } from "@remix-run/react"; | ||
|
||
import type { LoaderFunctionArgs } from "@remix-run/cloudflare"; | ||
import { json } from "@remix-run/cloudflare"; | ||
|
||
import { paramsFromUrl } from "~/lib/utils"; | ||
import PaginatedTableCard from "~/components/PaginatedTableCard"; | ||
|
||
export async function loader({ context, request }: LoaderFunctionArgs) { | ||
const { analyticsEngine } = context; | ||
|
||
const { interval, site, page = 1 } = paramsFromUrl(request.url); | ||
const tz = context.requestTimezone as string; | ||
|
||
return json({ | ||
countsByProperty: await analyticsEngine.getCountByBrowser( | ||
site, | ||
interval, | ||
tz, | ||
Number(page), | ||
), | ||
page: Number(page), | ||
}); | ||
} | ||
|
||
export const BrowserCard = ({ | ||
siteId, | ||
interval, | ||
}: { | ||
siteId: string; | ||
interval: string; | ||
}) => { | ||
return ( | ||
<PaginatedTableCard | ||
siteId={siteId} | ||
interval={interval} | ||
columnHeaders={["Browser", "Visitors"]} | ||
dataFetcher={useFetcher<typeof loader>()} | ||
loaderUrl="/resources/browser" | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useFetcher } from "@remix-run/react"; | ||
|
||
import type { LoaderFunctionArgs } from "@remix-run/cloudflare"; | ||
import { json } from "@remix-run/cloudflare"; | ||
|
||
import { paramsFromUrl } from "~/lib/utils"; | ||
import PaginatedTableCard from "~/components/PaginatedTableCard"; | ||
|
||
function convertCountryCodesToNames( | ||
countByCountry: [string, number][], | ||
): [string, number][] { | ||
const regionNames = new Intl.DisplayNames(["en"], { type: "region" }); | ||
return countByCountry.map((countByBrowserRow) => { | ||
let countryName; | ||
try { | ||
// throws an exception if country code isn't valid | ||
// use try/catch to be defensive and not explode if an invalid | ||
// country code gets insrted into Analytics Engine | ||
countryName = regionNames.of(countByBrowserRow[0])!; // "United States" | ||
} catch (err) { | ||
countryName = "(unknown)"; | ||
} | ||
const count = countByBrowserRow[1]; | ||
return [countryName, count]; | ||
}); | ||
} | ||
|
||
export async function loader({ context, request }: LoaderFunctionArgs) { | ||
const { analyticsEngine } = context; | ||
|
||
const { interval, site, page = 1 } = paramsFromUrl(request.url); | ||
const tz = context.requestTimezone as string; | ||
|
||
const countByCountry = await analyticsEngine.getCountByCountry( | ||
site, | ||
interval, | ||
tz, | ||
Number(page), | ||
); | ||
|
||
// normalize country codes to country names | ||
// NOTE: this must be done ONLY on server otherwise hydration mismatches | ||
// can occur because Intl.DisplayNames produces different results | ||
// in different browsers (see ) | ||
const normalizedCountByCountry = convertCountryCodesToNames(countByCountry); | ||
|
||
return json({ | ||
countsByProperty: normalizedCountByCountry, | ||
page: Number(page), | ||
}); | ||
} | ||
|
||
export const CountryCard = ({ | ||
siteId, | ||
interval, | ||
}: { | ||
siteId: string; | ||
interval: string; | ||
}) => { | ||
return ( | ||
<PaginatedTableCard | ||
siteId={siteId} | ||
interval={interval} | ||
columnHeaders={["Browser", "Visitors"]} | ||
dataFetcher={useFetcher<typeof loader>()} | ||
loaderUrl="/resources/country" | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useFetcher } from "@remix-run/react"; | ||
|
||
import type { LoaderFunctionArgs } from "@remix-run/cloudflare"; | ||
import { json } from "@remix-run/cloudflare"; | ||
|
||
import { paramsFromUrl } from "~/lib/utils"; | ||
import PaginatedTableCard from "~/components/PaginatedTableCard"; | ||
|
||
export async function loader({ context, request }: LoaderFunctionArgs) { | ||
const { analyticsEngine } = context; | ||
|
||
const { interval, site, page = 1 } = paramsFromUrl(request.url); | ||
const tz = context.requestTimezone as string; | ||
|
||
return json({ | ||
countsByProperty: await analyticsEngine.getCountByDevice( | ||
site, | ||
interval, | ||
tz, | ||
Number(page), | ||
), | ||
page: Number(page), | ||
}); | ||
} | ||
|
||
export const DeviceCard = ({ | ||
siteId, | ||
interval, | ||
}: { | ||
siteId: string; | ||
interval: string; | ||
}) => { | ||
return ( | ||
<PaginatedTableCard | ||
siteId={siteId} | ||
interval={interval} | ||
columnHeaders={["Path", "Visitors"]} | ||
dataFetcher={useFetcher<typeof loader>()} | ||
loaderUrl="/resources/device" | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters