Skip to content

Commit

Permalink
feat(frontend): display humaized durations on dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed May 5, 2023
1 parent b19d61b commit 1c1f693
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions apps/frontend/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ import { useMutation, useQuery } from "@tanstack/react-query";
import type { RegerateUserSummaryMutationVariables } from "@trackona/generated/graphql/backend/graphql";
import { REGENERATE_USER_SUMMARY } from "@trackona/graphql/backend/mutations";
import { USER_SUMMARY } from "@trackona/graphql/backend/queries";
import {
HumanizeDuration,
HumanizeDurationLanguage,
} from "humanize-duration-ts";
import type { ReactElement } from "react";

const service = new HumanizeDurationLanguage();
const humaizer = new HumanizeDuration(service);

const StatTitle = (props: { text: string }) => {
return <Title order={3}>{props.text}</Title>;
};

const StatNumber = (props: { text: number }) => {
const StatNumber = (props: { text: number; isDuration?: boolean }) => {
return (
<Text fw="bold" style={{ display: "inline" }}>
{props.text}
{props.isDuration
? humaizer.humanize(props.text * 1000 * 60)
: props.text}
</Text>
);
};
Expand Down Expand Up @@ -56,7 +65,6 @@ const Page: NextPageWithLayout = () => {
return (
<Container>
<Stack>
<Title>Your summary</Title>
{userSummary.isLoading ? <Loader /> : null}
{userSummary.isError ? (
<Alert color='yellow' icon={<IconAlertCircle size="1rem" />}>
Expand All @@ -83,8 +91,9 @@ const Page: NextPageWithLayout = () => {
<Text>
You watched{" "}
<StatNumber text={userSummary.data.movies.watched} /> movie(s)
totalling <StatNumber text={userSummary.data.movies.runtime} />{" "}
minute(s).
totalling{" "}
<StatNumber text={userSummary.data.movies.runtime} isDuration />
.
</Text>
</Box>
<Box>
Expand All @@ -95,7 +104,7 @@ const Page: NextPageWithLayout = () => {
show(s) and{" "}
<StatNumber text={userSummary.data.shows.watchedEpisodes} />{" "}
episode(s) totalling{" "}
<StatNumber text={userSummary.data.shows.runtime} /> minute(s).
<StatNumber text={userSummary.data.shows.runtime} isDuration />.
</Text>
</Box>
<Box>
Expand All @@ -112,8 +121,11 @@ const Page: NextPageWithLayout = () => {
You listened to{" "}
<StatNumber text={userSummary.data.audioBooks.played} />{" "}
audiobook(s) totalling{" "}
<StatNumber text={userSummary.data.audioBooks.runtime} />{" "}
minute(s).
<StatNumber
text={userSummary.data.audioBooks.runtime}
isDuration
/>
.
</Text>
</Box>
</SimpleGrid>
Expand Down

0 comments on commit 1c1f693

Please sign in to comment.