From f4561e41c3b7839cb84ffe29b4b3efbab4276207 Mon Sep 17 00:00:00 2001 From: alekswilc Date: Mon, 21 Apr 2025 20:48:21 +0200 Subject: [PATCH] fix: add required files --- packages/backend/src/http/routes/images.ts | 53 +++++++++++++++++++ .../components/mini/profile/StationStat.tsx | 41 ++++++++++++++ .../src/components/mini/profile/TrainStat.tsx | 43 +++++++++++++++ packages/frontend/src/types/images.ts | 10 ++++ packages/frontend/src/util/chunk.ts | 5 ++ 5 files changed, 152 insertions(+) create mode 100644 packages/backend/src/http/routes/images.ts create mode 100644 packages/frontend/src/components/mini/profile/StationStat.tsx create mode 100644 packages/frontend/src/components/mini/profile/TrainStat.tsx create mode 100644 packages/frontend/src/types/images.ts create mode 100644 packages/frontend/src/util/chunk.ts diff --git a/packages/backend/src/http/routes/images.ts b/packages/backend/src/http/routes/images.ts new file mode 100644 index 0000000..be37689 --- /dev/null +++ b/packages/backend/src/http/routes/images.ts @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2025 Aleksander Wilczyński (aleks@alekswilc.dev) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * See LICENSE for more. + */ + +import { Router } from "express"; +import { SuccessResponseBuilder } from '../responseBuilder.js'; +import { generateUrl } from '../../util/imgproxy.js'; +import { trainsMap, stationsMap } from '../../util/contants.js'; + +export class ImagesRoute { + static load() { + const app = Router(); + + app.get("/", async (req, res) => { + + const trains: Record = {}; + + Object.keys(trainsMap).forEach(x => { + trains[x] = generateUrl(trainsMap[x], "f:png/q:50/rs:auto:512:1024:1"); + }) + + const stations: Record = {}; + + Object.keys(stationsMap).forEach(x => { + stations[x] = generateUrl(stationsMap[x], "f:png/q:50/rs:auto:512:1024:1"); + }) + + res.json( + new SuccessResponseBuilder() + .setCode(200) + .setData({ + trains, + stations + }) + .toJSON(), + ); + }); + + return app; + } +} \ No newline at end of file diff --git a/packages/frontend/src/components/mini/profile/StationStat.tsx b/packages/frontend/src/components/mini/profile/StationStat.tsx new file mode 100644 index 0000000..3c5d85e --- /dev/null +++ b/packages/frontend/src/components/mini/profile/StationStat.tsx @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2025 Aleksander Wilczyński (aleks@alekswilc.dev) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * See LICENSE for more. + */ + +import { useTranslation } from 'react-i18next'; +import { formatTime } from '../../../util/time.ts' + +export const StationStat = ({ stationName, time, image }: { stationName: string, time: number, image: string }) => { + const { t } = useTranslation(); + + stationName = stationName === 'N/A' + ? "Untracked" : stationName + + return
+
+ station icon +
+
+

+ {stationName} +

+

{t('profile.stations.time', { time: formatTime(time) })}

+
+
+} \ No newline at end of file diff --git a/packages/frontend/src/components/mini/profile/TrainStat.tsx b/packages/frontend/src/components/mini/profile/TrainStat.tsx new file mode 100644 index 0000000..2aec39a --- /dev/null +++ b/packages/frontend/src/components/mini/profile/TrainStat.tsx @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2025 Aleksander Wilczyński (aleks@alekswilc.dev) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * See LICENSE for more. + */ + +import { useTranslation } from 'react-i18next' +import { formatTime } from '../../../util/time.ts' + +export const TrainStat = ({ trainName, time, distance, score, image }: { trainName: string, time: number, distance: number, score: number, image: string }) => { + const { t } = useTranslation(); + trainName = trainName === 'N/A' + ? "Untracked" : trainName + + return
+
+ train icon +
+
+

+ {trainName} +

+

{t('profile.trains.time', { time: formatTime(time) })}

+

{t('profile.trains.distance', { distance: Math.floor(distance / 1000) })}m

+

{t('profile.trains.score', { score: score })}

+ +
+
+} \ No newline at end of file diff --git a/packages/frontend/src/types/images.ts b/packages/frontend/src/types/images.ts new file mode 100644 index 0000000..9fa0ad6 --- /dev/null +++ b/packages/frontend/src/types/images.ts @@ -0,0 +1,10 @@ +export interface TImagesResponse { + success: boolean; + data: TImagesData; + code: number; +} + +export interface TImagesData { + trains: Record + stations: Record +} \ No newline at end of file diff --git a/packages/frontend/src/util/chunk.ts b/packages/frontend/src/util/chunk.ts new file mode 100644 index 0000000..4d188a0 --- /dev/null +++ b/packages/frontend/src/util/chunk.ts @@ -0,0 +1,5 @@ +export function* chunk(arr: T[], n: number): Generator { + for (let i = 0; i < arr.length; i += n) { + yield arr.slice(i, i + n); + } +} \ No newline at end of file -- 2.39.5