1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-09 04:22:45 +03:00

frontend: move getNinetyDaysAgo to a shared utility

This commit is contained in:
Quentin Gliech
2024-07-31 12:43:11 +02:00
parent c3b2d163a9
commit cb8bfa466b
5 changed files with 29 additions and 32 deletions

View File

@@ -23,19 +23,13 @@ import { ButtonLink } from "../components/ButtonLink";
import EmptyState from "../components/EmptyState"; import EmptyState from "../components/EmptyState";
import Filter from "../components/Filter"; import Filter from "../components/Filter";
import { type BackwardPagination, usePages } from "../pagination"; import { type BackwardPagination, usePages } from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";
import { QUERY } from "./_account.sessions.browsers"; import { QUERY } from "./_account.sessions.browsers";
const PAGE_SIZE = 6; const PAGE_SIZE = 6;
const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE }; 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")({ export const Route = createLazyFileRoute("/_account/sessions/browsers")({
component: BrowserSessions, component: BrowserSessions,
}); });
@@ -45,7 +39,7 @@ function BrowserSessions(): React.ReactElement {
const { inactive, ...pagination } = Route.useLoaderDeps(); const { inactive, ...pagination } = Route.useLoaderDeps();
const variables = { const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined, lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination, ...pagination,
}; };

View File

@@ -21,6 +21,7 @@ import {
type BackwardPagination, type BackwardPagination,
paginationSchema, paginationSchema,
} from "../pagination"; } from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";
const PAGE_SIZE = 6; const PAGE_SIZE = 6;
const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE }; const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE };
@@ -78,13 +79,6 @@ const searchSchema = z.object({
type Search = z.infer<typeof searchSchema>; 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")({ export const Route = createFileRoute("/_account/sessions/browsers")({
// We paginate backwards, so we need to validate the `last` parameter by default // We paginate backwards, so we need to validate the `last` parameter by default
validateSearch: paginationSchema.catch(DEFAULT_PAGE).and(searchSchema), validateSearch: paginationSchema.catch(DEFAULT_PAGE).and(searchSchema),
@@ -98,7 +92,7 @@ export const Route = createFileRoute("/_account/sessions/browsers")({
abortController: { signal }, abortController: { signal },
}) { }) {
const variables = { const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined, lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination, ...pagination,
}; };

View File

@@ -25,6 +25,7 @@ import Filter from "../components/Filter";
import OAuth2Session from "../components/OAuth2Session"; import OAuth2Session from "../components/OAuth2Session";
import BrowserSessionsOverview from "../components/UserSessionsOverview/BrowserSessionsOverview"; import BrowserSessionsOverview from "../components/UserSessionsOverview/BrowserSessionsOverview";
import { type BackwardPagination, usePages } from "../pagination"; import { type BackwardPagination, usePages } from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";
import { QUERY, LIST_QUERY } from "./_account.sessions.index"; import { QUERY, LIST_QUERY } from "./_account.sessions.index";
@@ -36,13 +37,6 @@ const unknownSessionType = (type: never): never => {
throw new Error(`Unknown session type: ${type}`); 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/")({ export const Route = createLazyFileRoute("/_account/sessions/")({
component: Sessions, component: Sessions,
}); });
@@ -57,7 +51,7 @@ function Sessions(): React.ReactElement {
if (user === null) throw notFound(); if (user === null) throw notFound();
const variables = { const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined, lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination, ...pagination,
}; };

View File

@@ -21,6 +21,7 @@ import {
type Pagination, type Pagination,
paginationSchema, paginationSchema,
} from "../pagination"; } from "../pagination";
import { getNinetyDaysAgo } from "../utils/dates";
const PAGE_SIZE = 6; const PAGE_SIZE = 6;
const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE }; const DEFAULT_PAGE: BackwardPagination = { last: PAGE_SIZE };
@@ -87,13 +88,6 @@ const searchSchema = z.object({
type Search = z.infer<typeof searchSchema>; 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/")({ export const Route = createFileRoute("/_account/sessions/")({
// We paginate backwards, so we need to validate the `last` parameter by default // We paginate backwards, so we need to validate the `last` parameter by default
validateSearch: paginationSchema.catch(DEFAULT_PAGE).and(searchSchema), validateSearch: paginationSchema.catch(DEFAULT_PAGE).and(searchSchema),
@@ -107,7 +101,7 @@ export const Route = createFileRoute("/_account/sessions/")({
abortController: { signal }, abortController: { signal },
}) { }) {
const variables = { const variables = {
lastActive: inactive ? { before: getNintyDaysAgo() } : undefined, lastActive: inactive ? { before: getNinetyDaysAgo() } : undefined,
...pagination, ...pagination,
}; };

View File

@@ -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();
};