Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
frontend: move getNinetyDaysAgo to a shared utility
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Aug 1, 2024
1 parent c3b2d16 commit cb8bfa4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
10 changes: 2 additions & 8 deletions frontend/src/routes/_account.sessions.browsers.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@ import { ButtonLink } from "../components/ButtonLink";
import EmptyState from "../components/EmptyState";
import Filter from "../components/Filter";
import { type BackwardPagination, usePages } from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";

import { QUERY } from "./_account.sessions.browsers";

const PAGE_SIZE = 6;
const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE };

const getNintyDaysAgo = (): string => {
const date = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// Round down to the start of the day to avoid rerendering/requerying
date.setHours(0, 0, 0, 0);
return date.toISOString();
};

export const Route = createLazyFileRoute("/_account/sessions/browsers")({
component: BrowserSessions,
});
Expand All @@ -45,7 +39,7 @@ function BrowserSessions(): React.ReactElement {
const { inactive, ...pagination } = Route.useLoaderDeps();

const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined,
lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination,
};

Expand Down
10 changes: 2 additions & 8 deletions frontend/src/routes/_account.sessions.browsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type BackwardPagination,
paginationSchema,
} from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";

const PAGE_SIZE = 6;
const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE };
Expand Down Expand Up @@ -78,13 +79,6 @@ const searchSchema = z.object({

type Search = z.infer<typeof searchSchema>;

const getNintyDaysAgo = (): string => {
const date = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// Round down to the start of the day to avoid rerendering/requerying
date.setHours(0, 0, 0, 0);
return date.toISOString();
};

export const Route = createFileRoute("/_account/sessions/browsers")({
// We paginate backwards, so we need to validate the `last` parameter by default
validateSearch: paginationSchema.catch(DEFAULT_PAGE).and(searchSchema),
Expand All @@ -98,7 +92,7 @@ export const Route = createFileRoute("/_account/sessions/browsers")({
abortController: { signal },
}) {
const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined,
lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination,
};

Expand Down
10 changes: 2 additions & 8 deletions frontend/src/routes/_account.sessions.index.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Filter from "../components/Filter";
import OAuth2Session from "../components/OAuth2Session";
import BrowserSessionsOverview from "../components/UserSessionsOverview/BrowserSessionsOverview";
import { type BackwardPagination, usePages } from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";

import { QUERY, LIST_QUERY } from "./_account.sessions.index";

Expand All @@ -36,13 +37,6 @@ const unknownSessionType = (type: never): never => {
throw new Error(`Unknown session type: ${type}`);
};

const getNintyDaysAgo = (): string => {
const date = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// Round down to the start of the day to avoid rerendering/requerying
date.setHours(0, 0, 0, 0);
return date.toISOString();
};

export const Route = createLazyFileRoute("/_account/sessions/")({
component: Sessions,
});
Expand All @@ -57,7 +51,7 @@ function Sessions(): React.ReactElement {
if (user === null) throw notFound();

const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined,
lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination,
};

Expand Down
10 changes: 2 additions & 8 deletions frontend/src/routes/_account.sessions.index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type Pagination,
paginationSchema,
} from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";

const PAGE_SIZE = 6;
const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE };
Expand Down Expand Up @@ -87,13 +88,6 @@ const searchSchema = z.object({

type Search = z.infer<typeof searchSchema>;

const getNintyDaysAgo = (): string => {
const date = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// Round down to the start of the day to avoid rerendering/requerying
date.setHours(0, 0, 0, 0);
return date.toISOString();
};

export const Route = createFileRoute("/_account/sessions/")({
// We paginate backwards, so we need to validate the `last` parameter by default
validateSearch: paginationSchema.catch(DEFAULT_PAGE).and(searchSchema),
Expand All @@ -107,7 +101,7 @@ export const Route = createFileRoute("/_account/sessions/")({
abortController: { signal },
}) {
const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined,
lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination,
};

Expand Down
21 changes: 21 additions & 0 deletions frontend/src/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/** Compute what the date was 90 days ago, rouding down to the start of the day */
export const getNinetyDaysAgo = (): string => {
const date = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// Round down to the start of the day to avoid rerendering/requerying
date.setHours(0, 0, 0, 0);
return date.toISOString();
};

0 comments on commit cb8bfa4

Please sign in to comment.