From cb8bfa466b0862b22bb42949cebacd674cdeef5f Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Wed, 31 Jul 2024 12:43:11 +0200 Subject: [PATCH] frontend: move `getNinetyDaysAgo` to a shared utility --- .../_account.sessions.browsers.lazy.tsx | 10 ++------- .../src/routes/_account.sessions.browsers.tsx | 10 ++------- .../routes/_account.sessions.index.lazy.tsx | 10 ++------- .../src/routes/_account.sessions.index.tsx | 10 ++------- frontend/src/utils/dates.ts | 21 +++++++++++++++++++ 5 files changed, 29 insertions(+), 32 deletions(-) create mode 100644 frontend/src/utils/dates.ts diff --git a/frontend/src/routes/_account.sessions.browsers.lazy.tsx b/frontend/src/routes/_account.sessions.browsers.lazy.tsx index a552db4d..b0708485 100644 --- a/frontend/src/routes/_account.sessions.browsers.lazy.tsx +++ b/frontend/src/routes/_account.sessions.browsers.lazy.tsx @@ -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, }); @@ -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, }; diff --git a/frontend/src/routes/_account.sessions.browsers.tsx b/frontend/src/routes/_account.sessions.browsers.tsx index bc870dd1..02c58f3c 100644 --- a/frontend/src/routes/_account.sessions.browsers.tsx +++ b/frontend/src/routes/_account.sessions.browsers.tsx @@ -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 }; @@ -78,13 +79,6 @@ const searchSchema = z.object({ type Search = z.infer; -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), @@ -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, }; diff --git a/frontend/src/routes/_account.sessions.index.lazy.tsx b/frontend/src/routes/_account.sessions.index.lazy.tsx index 95a52b6e..770cb0f1 100644 --- a/frontend/src/routes/_account.sessions.index.lazy.tsx +++ b/frontend/src/routes/_account.sessions.index.lazy.tsx @@ -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"; @@ -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, }); @@ -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, }; diff --git a/frontend/src/routes/_account.sessions.index.tsx b/frontend/src/routes/_account.sessions.index.tsx index 6defa444..3a6da94a 100644 --- a/frontend/src/routes/_account.sessions.index.tsx +++ b/frontend/src/routes/_account.sessions.index.tsx @@ -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 }; @@ -87,13 +88,6 @@ const searchSchema = z.object({ type Search = z.infer; -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), @@ -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, }; diff --git a/frontend/src/utils/dates.ts b/frontend/src/utils/dates.ts new file mode 100644 index 00000000..84ac2b45 --- /dev/null +++ b/frontend/src/utils/dates.ts @@ -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(); +};