Compare commits

...

6 Commits

Author SHA1 Message Date
Loic Coenen
dcaa7d0f7c chore: remove useHooks
Some checks failed
Playwright Tests / test (push) Has been cancelled
2025-11-02 21:19:37 +01:00
Loic Coenen
290db22a50 chore
Some checks failed
Playwright Tests / test (push) Has been cancelled
2025-11-02 21:08:49 +01:00
Loic Coenen
abefd6610a feat: readme
Some checks failed
Playwright Tests / test (push) Has been cancelled
2025-11-02 20:54:59 +01:00
Loic Coenen
77113c83e2 feat: RSS feed and config
Some checks failed
Playwright Tests / test (push) Has been cancelled
2025-11-02 20:36:07 +01:00
Loic Coenen
9e872ea8d9 feat: issues
Some checks failed
Playwright Tests / test (push) Has been cancelled
2025-11-01 16:02:29 +01:00
Loic Coenen
4013ae24b2 feat: issues 2025-11-01 16:01:57 +01:00
32 changed files with 2811 additions and 723 deletions

View File

@@ -1,73 +1,10 @@
# React + TypeScript + Vite
# Trainhour
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
This is live on [http://liveboard.xyz](http://liveboard.xyz). Trainhour features:
Currently, two official plugins are available:
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
## React Compiler
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
## Expanding the ESLint configuration
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
```js
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Remove tseslint.configs.recommended and replace with this
tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
tseslint.configs.stylisticTypeChecked,
// Other configs...
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
```
You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Enable lint rules for React
reactX.configs['recommended-typescript'],
// Enable lint rules for React DOM
reactDom.configs.recommended,
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])
```
- Train schedule 🚆📅
- Punctuality indicators 🚦⏱️
- RSS feed 📡📰
- Configuration, location settings ⚙️📍
- News 📣🗞️
- Weather ☀️☁️

View File

File diff suppressed because one or more lines are too long

View File

@@ -1 +1 @@
:root{font-family:system-ui,Avenir,Helvetica,Arial,sans-serif;line-height:1.5;font-weight:400;color-scheme:light dark;color:#ffffffde;background-color:#242424;font-synthesis:none;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}a{font-weight:500;color:#646cff;text-decoration:inherit}a:hover{color:#535bf2}body{margin:0;display:flex;place-items:center;min-width:320px;min-height:100vh}h1{font-size:3.2em;line-height:1.1}button{border-radius:8px;border:1px solid transparent;padding:.6em 1.2em;font-size:1em;font-weight:500;font-family:inherit;background-color:#1a1a1a;cursor:pointer;transition:border-color .25s}button:hover{border-color:#646cff}button:focus,button:focus-visible{outline:4px auto -webkit-focus-ring-color}@media(prefers-color-scheme:light){:root{color:#213547;background-color:#fff}a:hover{color:#747bff}button{background-color:#f9f9f9}}#root{max-width:1280px;margin:0 auto;padding:2rem;text-align:center}.logo{height:6em;padding:1.5em;will-change:filter;transition:filter .3s}.logo:hover{filter:drop-shadow(0 0 2em #646cffaa)}.logo.react:hover{filter:drop-shadow(0 0 2em #61dafbaa)}@keyframes logo-spin{0%{transform:rotate(0)}to{transform:rotate(360deg)}}@media(prefers-reduced-motion:no-preference){a:nth-of-type(2) .logo{animation:logo-spin infinite 20s linear}}.card{padding:2em}.read-the-docs{color:#888}
:root{font-family:system-ui,Avenir,Helvetica,Arial,sans-serif;line-height:1.5;font-weight:400;color-scheme:light dark;color:#ffffffde;background-color:#242424;font-synthesis:none;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}a{font-weight:500;color:#646cff;text-decoration:inherit}a:hover{color:#535bf2}body{margin:0;display:flex;place-items:center;min-width:320px;min-height:100vh}h1{font-size:3.2em;line-height:1.1}button{border-radius:8px;border:1px solid transparent;padding:.6em 1.2em;font-size:1em;font-weight:500;font-family:inherit;background-color:#1a1a1a;cursor:pointer;transition:border-color .25s}button:hover{border-color:#646cff}button:focus,button:focus-visible{outline:4px auto -webkit-focus-ring-color}@media(prefers-color-scheme:light){:root{color:#213547;background-color:#fff}a:hover{color:#747bff}button{background-color:#f9f9f9}}#root{max-width:1280px;margin:0 auto;padding:2rem;text-align:center}

493
dist/assets/index-Dz5JJxrJ.js vendored Normal file
View File

File diff suppressed because one or more lines are too long

4
dist/index.html vendored
View File

@@ -5,8 +5,8 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>trainhour</title>
<script type="module" crossorigin src="/assets/index-Bvsyeuyp.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-COcDBgFa.css">
<script type="module" crossorigin src="/assets/index-Dz5JJxrJ.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-Db4VgUFq.css">
</head>
<body>
<div id="root"></div>

View File

@@ -10,13 +10,16 @@
"preview": "vite preview"
},
"dependencies": {
"@chakra-ui/react": "^3.28.0",
"@types/lodash": "^4.17.20",
"@uidotdev/usehooks": "^2.4.1",
"axios": "^1.13.1",
"lodash": "^4.17.21",
"lucide-react": "^0.552.0",
"react": "^19.1.1",
"react-dom": "^19.1.1",
"react-icons": "^5.5.0",
"rss-parser": "^3.13.0",
"spinners-react": "^1.0.11",
"styled-components": "^6.1.19"
},

1704
pnpm-lock.yaml generated
View File

File diff suppressed because it is too large Load Diff

View File

@@ -5,38 +5,5 @@
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}
@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}

View File

@@ -1,13 +1,20 @@
import { useReducer } from 'react'
import { useCallback, useEffect, useReducer, useState } from 'react'
import { useLoadTrainSchedule } from './hooks/useLoadTrainSchedule'
import { initialState, reducer } from './state'
import { useLocalStorage } from "@uidotdev/usehooks";
import { actions, initialState, reducer, type Action, type State } from './state'
import './App.css'
import { IoSettingsSharp } from 'react-icons/io5';
import styled from "styled-components";
import { NewsWidget, TrainSchedule, WeatherWidget } from './containers';
import {useNewsApi, useWeatherApi} from './hooks';
import styled from 'styled-components';
import {useGiteaApi, useNewsApi, useRssFeed, useWeatherApi} from './hooks';
import {IssueWidget} from './containers/IssuesWidget';
import type {Station} from './types';
import {RssWidget} from './containers/RssWidget';
const Container = styled.div`
display: flex;
@@ -21,27 +28,257 @@ const Pane = styled.div`
`;
const SettingContainer = styled.div`
display: flex;
flex-direction: column;
gap: 1rem;
padding: 1.5rem;
background: #1e1e2f;
color: #f1f1f1;
border-radius: 12px;
font-family: 'Inter', sans-serif;
`;
const FieldGroup = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
background: #2a2a40;
padding: 0.75rem 1rem;
border-radius: 8px;
`;
const Label = styled.label`
font-size: 0.95rem;
color: #b0b0c0;
`;
const Select = styled.select`
background: #3a3a55;
color: #fff;
border: none;
padding: 0.5rem 0.75rem;
border-radius: 6px;
font-size: 0.95rem;
outline: none;
width: 60%;
cursor: pointer;
&:hover {
background: #4a4a6a;
}
`;
const Input = styled.input`
background: #3a3a55;
color: #fff;
border: none;
padding: 0.4rem 0.6rem;
border-radius: 6px;
width: 60px;
text-align: center;
outline: none;
&:focus {
background: #4a4a6a;
}
`;
const TextArea = styled.textarea`
background: #3a3a55;
color: #fff;
border: none;
border-radius: 8px;
padding: 0.75rem;
width: calc(100% - 2em);
height: 80px;
resize: vertical;
outline: none;
&:focus {
background: #4a4a6a;
}
`;
export const Button = styled.button`
background: #4a4a6a;
color: #fff;
border: none;
padding: 0.6rem 1.2rem;
border-radius: 8px;
font-size: 0.95rem;
font-weight: 500;
cursor: pointer;
transition: all 0.2s ease;
font-family: 'Inter', sans-serif;
&:hover {
background: #5b5b7a;
transform: translateY(-1px);
}
&:active {
background: #3a3a55;
transform: translateY(0);
}
&:disabled {
background: #2a2a40;
color: #999;
cursor: not-allowed;
}
`;
// Credit to https://www.benmvp.com/blog/sync-localstorage-react-usereducer-hook/
const usePersistReducer = () => {
const [savedState, saveState] = useState(
initialState,
)
// wrap `reducer` with a memoized function that
// syncs the `newState` to `localStorage` before
// returning `newState`. memoizing is important!
const reducerLocalStorage = useCallback(
(state: State, action: Action) => {
const newState = reducer(state, action)
saveState(newState)
return newState
},
[saveState],
)
// use wrapped reducer and the saved value from
// `localStorage` as params to `useReducer`.
// this will return `[state, dispatch]`
return useReducer(reducerLocalStorage, savedState)
}
function App() {
const [state, dispatch] = useReducer( reducer, initialState,);
const [state, dispatch] = usePersistReducer();
const [settingOpened, setSettingOpened] = useState(false);
useLoadTrainSchedule(state, dispatch);
useNewsApi({state, dispatch});
useWeatherApi({state, dispatch});
const { reloadTrainSchedule } = useLoadTrainSchedule(state, dispatch);
const { reloadNews } = useNewsApi({state, dispatch});
const { reloadWeather } = useWeatherApi({state, dispatch});
const { reloadIssues } = useGiteaApi({state, dispatch})
const { reloadRssFeed } = useRssFeed({state, dispatch})
const { selectedLocation } = state;
const setSelectedLocation = (location: string) => {
dispatch(actions.setSelectedLocation({ location }))
}
useEffect(() => {
reloadNews();
reloadWeather();
reloadIssues();
reloadTrainSchedule();
reloadRssFeed();
}, [selectedLocation, state.config])
const mainContent = <>
<Pane>
<h2>Next trains in {state.selectedLocation}</h2>
<TrainSchedule {...{ state, dispatch }} />
{state.config.rssFollow.length > 0
? <>
<h2>RSS Feed</h2>
<RssWidget {...{ state, dispatch }} />
</>
: <></>
}
</Pane>
<Pane>
<h2>Weather</h2>
<WeatherWidget {...{ state, dispatch }} />
<h2>Issues</h2>
<IssueWidget {...{ state, dispatch }} />
<h2>News</h2>
<NewsWidget {...{ state, dispatch }} />
</Pane>
</>
const {
trainScheduleShow,
trainDelayCompute,
trainCancelCompute,
rssFollow,
} = state.config;
const setTrainScheduleShow = (value: number) => dispatch(actions.setConfig({ setting: 'trainScheduleShow', value }));
const setTrainDelayCompute = (value: number) => dispatch(actions.setConfig({ setting: 'trainDelayCompute', value }));
const setTrainCancelCompute = (value: number) => dispatch(actions.setConfig({ setting: 'trainCancelCompute', value }));
const setRssFollow = (value: string) => dispatch(actions.setConfig({ setting: 'rssFollow', value }));
const settingContent = (
<Pane>
<SettingContainer>
<FieldGroup>
<Label>Location</Label>
<Select value={selectedLocation} onChange={e => setSelectedLocation(e.target.value)}>
{state.stations?.map((option: Station) => (
<option key={option.name} value={option.name}>{option.name}</option>
))}
</Select>
</FieldGroup>
<FieldGroup>
<Label>Show Schedule</Label>
<Input
type="number"
value={trainScheduleShow}
onChange={e => setTrainScheduleShow(parseInt(e.target.value))}
/>
min
</FieldGroup>
<FieldGroup>
<Label>Compute Delay</Label>
<Input
type="number"
value={trainDelayCompute}
onChange={e => setTrainDelayCompute(parseInt(e.target.value))}
/>
min
</FieldGroup>
<FieldGroup>
<Label>Compute Cancellation</Label>
<Input
type="number"
value={trainCancelCompute}
onChange={e => setTrainCancelCompute(parseInt(e.target.value))}
/>
min
</FieldGroup>
<div>
<Label>RSS Feed</Label>
<TextArea
value={rssFollow}
onChange={e => setRssFollow(e.target.value)}
/>
</div>
<Button onClick={() => setSettingOpened(false)}>Close</Button>
</SettingContainer>
</Pane>
);
return (
<Container>
<Pane>
<h1>Next trains</h1>
<TrainSchedule {...{ state, dispatch }} />
</Pane>
<Pane>
<h1>Weather</h1>
<WeatherWidget {...{ state, dispatch }} />
<h1>News</h1>
<NewsWidget {...{ state, dispatch }} />
</Pane>
</Container>
<>
<Container>
<div onClick={() => setSettingOpened(!settingOpened)}><IoSettingsSharp size={40}/></div>
{settingOpened ? settingContent: mainContent}
</Container>
</>
)
}

View File

@@ -31,8 +31,8 @@ export const InfoStyled = styled.div`
`;
export const DestinationStyled = styled.div`
font-size: 1.2rem;
font-weight: 600;
font-size: 1rem;
font-weight: 400;
`;
export const DelayStyled = styled.div`

View File

@@ -5,7 +5,7 @@ import {mean} from "lodash";
import styled from "styled-components";
import { Clock, AlertTriangle, XCircle } from "lucide-react";
import {accentColor} from "../styles";
import { accentColor } from "../styles";
export const IndicatorStyled = styled.div`
display: flex;
@@ -56,17 +56,17 @@ const formatPercent = (num?: number) => {
};
export const Indicator = ({ state }: { state: State }) => {
const inOneHour = state.departures?.filter(filterTrainHour(60));
const inThreeHour = state.departures?.filter(filterTrainHour(-60 * 3));
const delayDepartures = state.departures?.filter(filterTrainHour(state.config.trainDelayCompute));
const cancelledDepartures = state.departures?.filter(filterTrainHour(0, state.config.trainCancelCompute));
const averageDelayGeneral = mean(inOneHour?.map(d => parseInt(d.delay)));
const averageDelayGeneral = mean(delayDepartures?.map(d => parseInt(d.delay)));
const averageDelayedOnly = mean(
inOneHour?.filter(d => parseInt(d.delay) !== 0)?.map(d => parseInt(d.delay))
delayDepartures?.filter(d => parseInt(d.delay) !== 0)?.map(d => parseInt(d.delay))
);
const cancelled =
(inThreeHour?.filter(d => d?.canceled === '1')?.length || 0) /
(inThreeHour?.length || 1);
(cancelledDepartures?.filter(d => d?.canceled === '1')?.length || 0) /
(cancelledDepartures?.length || 1);
return (
<IndicatorStyled>

26
src/components/Issue.tsx Normal file
View File

@@ -0,0 +1,26 @@
import type { IssueType } from "../types/issues"
import styled from 'styled-components';
import {TitleLink} from "./NewsArticle";
export const IssueHeader = styled.div`
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 0.75rem;
`;
export const Issue = ({ issue }: { issue: IssueType }) => {
return (
<IssueHeader>
<a href={`https://git.boomjacky.art/boomjacky/trainhour/issues/${issue.id}`}>
<TitleLink>
{issue.title}
</TitleLink>
</a>
</IssueHeader>
);
};

View File

@@ -20,7 +20,6 @@ export const TimeStyled = styled.div`
align-items: center;
justify-content: center;
padding-right: 1em;
width: 40vh;
`;
export const NowTime = () => {

View File

@@ -1,6 +1,6 @@
import React from "react";
import styled from "styled-components";
import {accentColor} from "../styles";
import { accentColor } from "../styles";
const Container = styled.div`
@@ -12,8 +12,8 @@ const Container = styled.div`
`;
const Dot = styled.button<{ $active: boolean }>`
width: 12px;
height: 12px;
width: 20px;
height: 20px;
padding: 0;
margin: 0;
border: none;

View File

@@ -1,3 +1,4 @@
export * from './Departure';
export * from './NewsArticle';
export * from './NowTime';
export * from './Issue';

View File

@@ -0,0 +1,39 @@
import type { Dispatch } from "react"
import styled from "styled-components";
import { SpinnerDiamond } from "spinners-react";
import type { Action, State } from "../state"
import { Issue } from "../components";
export const IssueContainer = styled.div`
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: stretch;
background: #0b0c10;
color: #fff;
padding: 1rem 1.5rem;
border-radius: 12px;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
font-family: "Inter", sans-serif;
max-width: 600px;
margin: 1rem auto;
`;
type IssueDispatchProps = {
state: State;
dispatch: Dispatch<Action>;
};
export const IssueWidget = ({ state }: IssueDispatchProps) => {
return state.issuesLoading
? <SpinnerDiamond />
: state.issuesError
? <em>{state.issuesError?.message}</em>
: (
<IssueContainer>
{state.issues?.slice(0,3)?.map(issue => <Issue issue={issue} />)}
</IssueContainer>
);
};

View File

@@ -0,0 +1,50 @@
import type { Dispatch } from "react"
import type { Action, State } from "../state"
import styled from "styled-components";
import {accentColor} from "../styles";
export const NewsContainer = styled.div`
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: stretch;
background: #0b0c10;
color: #fff;
padding: 1rem 1.5rem;
border-radius: 12px;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
font-family: "Inter", sans-serif;
max-width: 600px;
margin: 1rem auto;
`;
export const NewsHeader = styled.div`
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 0.75rem;
`;
export const TitleLink = styled.a`
font-size: 1.25rem;
font-weight: 700;
color: ${accentColor};
text-decoration: none;
margin-right: 1rem;
`;
const RssArticle = (article: {title: string, link: string}) => {
return <NewsContainer>
<NewsHeader>{article.title}</NewsHeader>
<a href={article.link}>Read more</a>
</NewsContainer>
}
export const RssWidget = ({ state }: { state: State, dispatch: Dispatch<Action> }) => {
return <>
{state.rss?.map(rss => <RssArticle {...rss} />)}
</>
}

View File

@@ -11,17 +11,15 @@ import { SpinnerDiamond } from 'spinners-react';
import type { DepartureType } from '../types/liveboard';
import { Indicator } from '../components/Indicators';
const showTrainsMinutes = 120;
export const filterTrainHour = (delay: number) => (departure: DepartureType) => {
const time = new Date(parseInt(departure.time) * 1000);
const now = new Date();
const h = time.getHours();
const m = time.getMinutes();
const nowInMinutes = (now.getHours() * 60) + now.getMinutes();
const minutes = h * 60 + m;
return (nowInMinutes + delay) > minutes;
}
export const filterTrainHour = (delay: number, start: number = 0) => (departure: DepartureType) => {
const time = new Date(parseInt(departure.time) * 1000);
const now = new Date();
const h = time.getHours();
const m = time.getMinutes();
const nowInMinutes = (now.getHours() * 60) + now.getMinutes();
const minutes = h * 60 + m;
return (nowInMinutes + delay) > minutes && (nowInMinutes + start ) < minutes;;
}
type TrainScheduleProps = {
dispatch: Dispatch<Action>,
@@ -30,7 +28,7 @@ type TrainScheduleProps = {
export const TrainSchedule = ({ state } : TrainScheduleProps) => {
const filteredDepartures = state.departures?.filter(filterTrainHour(showTrainsMinutes))
const filteredDepartures = state.departures?.filter(filterTrainHour(state.config.trainScheduleShow))
return <>
<NowTime />

View File

@@ -1,3 +1,5 @@
export * from './useLoadTrainSchedule';
export * from './useNewsApi';
export * from './useWeatherApi';
export * from './useGiteaApi';
export * from './useRssFeed';

27
src/hooks/useGiteaApi.tsx Normal file
View File

@@ -0,0 +1,27 @@
import { type Dispatch } from "react";
import { actions, type Action, type State } from "../state";
import type {IssuesResponse} from "../types/issues";
const apiKey = 'a70bbe6b9f70747278ba3ec7a701b2b279be2efc';
export const useGiteaApi = ({ dispatch }: { state : State, dispatch: Dispatch<Action> }) => {
const reloadIssues = (async () => {
try {
dispatch(actions.loadGiteaIssue({}));
const answer = await fetch(`https://git.boomjacky.art/api/v1/repos/boomjacky/trainhour/issues?state=all`, {
method: "GET",
headers: {
"Authorization": `token ${apiKey}`,
"Accept": "application/json"
}
})
const data = await answer.json() as IssuesResponse;
dispatch(actions.loadGiteaIssueSuccess({ data }));
} catch(error) {
dispatch(actions.loadGiteaIssueError({ error: error as Error }));
}
})
return { reloadIssues }
}

View File

@@ -1,47 +1,48 @@
import { useEffect, type Dispatch } from "react"
import { type Dispatch } from "react"
import { actions, type Action, type State } from "../state"
import { flatten, range } from 'lodash';
import axios from 'axios';
import type { LiveBoard } from "../types/liveboard";
import type {StationsRoot} from "../types/station";
const irailApiUrl = 'https://api.irail.be';
const lookahead = 3;
export const useLoadTrainSchedule = (_: State, dispatch: Dispatch<Action>) => {
useEffect(() => {
(async () => {
try {
dispatch(actions.loadTrainSchedule({}))
const now = new Date();
const pad = (num: number) => num.toString().padStart(2, '0');
export const useLoadTrainSchedule = (state: State, dispatch: Dispatch<Action>) => {
const reloadTrainSchedule = (async () => {
try {
dispatch(actions.loadTrainSchedule({}))
const now = new Date();
const pad = (num: number) => num.toString().padStart(2, '0');
const departures =
flatten(await
Promise.all(range(0, lookahead).map(async hourPadding => {
const departures =
flatten(await
Promise.all(range(0, lookahead).map(async hourPadding => {
const day = pad(now.getDate());
const month = pad(now.getMonth() + 1);
const year = now.getFullYear().toString().slice(-2);
const date = `${day}${month}${year}`
const day = pad(now.getDate());
const month = pad(now.getMonth() + 1);
const year = now.getFullYear().toString().slice(-2);
const date = `${day}${month}${year}`
const hours = pad(now.getHours() + hourPadding) ;
const minutes = pad(now.getMinutes());
const time = `${hours}${minutes}`
const hours = pad(now.getHours() + hourPadding) ;
const minutes = pad(now.getMinutes());
const time = `${hours}${minutes}`
const answer = await axios.get(`${irailApiUrl}/liveboard?station=Nivelles&date=${date}&time=${time}&format=json&lang=en&alerts=true`)
const answer = await axios.get(`${irailApiUrl}/liveboard?station=${state.selectedLocation}&date=${date}&time=${time}&format=json&lang=en&alerts=true`)
const liveboard = answer.data as LiveBoard;
const liveboard = answer.data as LiveBoard;
return liveboard.departures.departure;
})))
return liveboard.departures.departure;
})))
const answer = await axios.get(`${irailApiUrl}/stations?format=json&lang=en&alerts=true`);
const stations = (answer.data as StationsRoot).station;
dispatch(actions.loadTrainScheduleSuccess({ departures }))
} catch(error) {
dispatch(actions.loadTrainScheduleError({ error: error as Error}))
}
})()
}, [])
dispatch(actions.loadTrainScheduleSuccess({ departures, stations }))
} catch(error) {
dispatch(actions.loadTrainScheduleError({ error: error as Error}))
}
})
return { reloadTrainSchedule }
}

View File

@@ -1,4 +1,4 @@
import { useEffect, type Dispatch } from "react"
import { type Dispatch } from "react"
import { actions, type Action, type State } from "../state"
import type {Article} from "../types/article"
@@ -10,16 +10,15 @@ export type UseNewsApiProps = {
}
export const useNewsApi = ({dispatch}: UseNewsApiProps) => {
useEffect(() => {
(async () => {
try {
dispatch(actions.loadNews({}));
const answer = await fetch(newsUrl);
const { results: news } = await answer.json() as { results: Article[] }
dispatch(actions.loadNewsSuccess({ news }));
} catch(error) {
dispatch(actions.loadNewsError({ error: error as Error}));
}
})()
}, [])
const reloadNews = (async () => {
try {
dispatch(actions.loadNews({}));
const answer = await fetch(newsUrl);
const { results: news } = await answer.json() as { results: Article[] }
dispatch(actions.loadNewsSuccess({ news }));
} catch(error) {
dispatch(actions.loadNewsError({ error: error as Error}));
}
})
return { reloadNews }
}

60
src/hooks/useRssFeed.tsx Normal file
View File

@@ -0,0 +1,60 @@
import {flatten} from 'lodash';
import { actions, type Action, type RSS, type State } from '../state';
import type { Dispatch } from 'react';
export async function fetchAndParseRSS(url: string): Promise<RSS[]> {
const response = await fetch(`https://cors-anywhere.com/${url}`);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const xmlText = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, "application/xml");
if (xmlDoc.getElementsByTagName('parsererror').length > 0) {
throw new Error("Failed to parse XML content. Check for valid RSS/XML format.");
}
const safeText = (selector: string, context: Element): string => {
const el = context.querySelector(selector);
return el?.textContent?.trim() ?? 'No Data';
};
const rssItems = xmlDoc.querySelectorAll('channel > item');
if (rssItems.length > 0) {
return Array.from(rssItems).map((item: Element): RSS => ({
title: safeText('title', item),
link: safeText('link', item)
}));
}
const atomEntries = xmlDoc.querySelectorAll('entry');
if (atomEntries.length > 0) {
return Array.from(atomEntries).map((entry: Element): RSS => {
const linkElement = entry.querySelector('link[rel="alternate"]') || entry.querySelector('link');
return {
title: safeText('title', entry),
link: linkElement?.getAttribute('href') ?? safeText('link', entry) // Prefer 'href' attribute
};
});
}
// If neither RSS nor Atom format is detected
return [];
}
export const useRssFeed = ({ state, dispatch}: { state: State, dispatch: Dispatch<Action> }) => {
const reloadRssFeed = (async () => {
const answer = await Promise.all(
state.config.rssFollow.split('/n').map((url: string) => fetchAndParseRSS(url))
);
const feeds: RSS[] = flatten(answer);
dispatch(actions.loadRSSFeedsSuccess({ feeds }))
})
return { reloadRssFeed }
}

View File

@@ -1,25 +1,25 @@
import { useEffect, type Dispatch } from "react"
import { type Dispatch } from "react"
import { actions, type Action, type State } from "../state"
import type {WeatherData} from "../types/weather";
const weatherApiUrl = `https://api.weatherapi.com/v1/current.json?key=176d6e98c8894466aa6205455253010&q=Nivelles&aqi=no`;
export type UseWeatherApiProps = {
dispatch: Dispatch<Action>,
state: State
}
export const useWeatherApi = ({ dispatch}: UseWeatherApiProps) => {
useEffect(() => {
(async () => {
try {
dispatch(actions.loadWeather({}));
const answer = await fetch(weatherApiUrl);
const weather = await answer.json() as WeatherData;
dispatch(actions.loadWeatherSuccess({ weather }));
} catch(error) {
dispatch(actions.loadWeatherError({ error: error as Error }));
}
})()
}, [])
export const useWeatherApi = ({ dispatch, state }: UseWeatherApiProps) => {
const reloadWeather = (async () => {
try {
const weatherApiUrl = `https://api.weatherapi.com/v1/current.json?key=176d6e98c8894466aa6205455253010&q=${state.selectedLocation}&aqi=no`;
dispatch(actions.loadWeather({}));
const answer = await fetch(weatherApiUrl);
const weather = await answer.json() as WeatherData;
dispatch(actions.loadWeatherSuccess({ weather }));
} catch(error) {
dispatch(actions.loadWeatherError({ error: error as Error }));
}
});
return { reloadWeather }
}

View File

@@ -8,6 +8,14 @@ import {
loadNewsError,
loadNewsSuccess,
loadNews,
loadGiteaIssueError,
loadGiteaIssueSuccess,
loadGiteaIssue,
setSelectedLocation,
setConfig,
loadRssFeeds,
loadRssFeedsSuccess,
loadRssFeedsError,
} from './consts'
import {
@@ -20,6 +28,14 @@ import {
type LoadNewsError,
type LoadNewsSuccess,
type LoadNews,
type LoadGiteaIssueError,
type LoadGiteaIssueSuccess,
type LoadGiteaIssue,
type SetSelectedLocation,
type SetConfig,
type LoadRssFeeds,
type LoadRssFeedsSuccess,
type LoadRssFeedsError,
} from './types';
export const actions = {
@@ -59,4 +75,36 @@ export const actions = {
type: loadWeatherError,
...args
} as LoadWeatherError),
loadGiteaIssue: (args: Omit<LoadGiteaIssue, "type">) => ({
type: loadGiteaIssue,
...args
} as LoadGiteaIssue),
loadGiteaIssueSuccess: (args: Omit<LoadGiteaIssueSuccess, "type">) => ({
type: loadGiteaIssueSuccess,
...args
} as LoadGiteaIssueSuccess),
loadGiteaIssueError: (args: Omit<LoadGiteaIssueError, "type">) => ({
type: loadGiteaIssueError,
...args
} as LoadGiteaIssueError),
setSelectedLocation: (args: Omit<SetSelectedLocation, "type">) => ({
type: setSelectedLocation,
...args
} as SetSelectedLocation),
setConfig: (args: Omit<SetConfig, "type">) => ({
type: setConfig,
...args
} as SetConfig),
loadRSSFeeds: (args: Omit<LoadRssFeeds, "type">) => ({
type: loadRssFeeds,
...args
} as LoadRssFeeds),
loadRSSFeedsSuccess: (args: Omit<LoadRssFeedsSuccess, "type">) => ({
type: loadRssFeedsSuccess,
...args
} as LoadRssFeedsSuccess),
loadRSSFeedsError: (args: Omit<LoadRssFeedsError, "type">) => ({
type: loadRssFeedsError,
...args
} as LoadRssFeedsError),
}

View File

@@ -11,3 +11,15 @@ export const loadNewsError: ActionType = 'loadNewsError';
export const loadWeather: ActionType = 'loadWeather';
export const loadWeatherSuccess: ActionType = 'loadWeatherSuccess';
export const loadWeatherError: ActionType = 'loadWeatherError';
export const loadGiteaIssue: ActionType = 'loadGiteaIssue';
export const loadGiteaIssueSuccess: ActionType = 'loadGiteaIssueSuccess';
export const loadGiteaIssueError: ActionType = 'loadGiteaIssueError';
export const setSelectedLocation: ActionType = 'setSelectedLocation';
export const setConfig: ActionType = 'setConfig';
export const loadRssFeeds: ActionType = 'RssFeeds';
export const loadRssFeedsSuccess: ActionType = 'RssFeedsSuccess';
export const loadRssFeedsError: ActionType = 'RssFeedsError';

View File

@@ -1,8 +1,11 @@
export type ActionType = string;
import type {Station} from '../../types';
import type {Article} from '../../types/article';
import type {IssuesResponse} from '../../types/issues';
import type { DepartureType } from '../../types/liveboard';
import type {WeatherData} from '../../types/weather';
import type {RSS} from '../state';
import {
loadTrainSchedule,
loadTrainScheduleError,
@@ -13,6 +16,14 @@ import {
loadWeather,
loadWeatherError,
loadWeatherSuccess,
loadGiteaIssueSuccess,
loadGiteaIssueError,
loadGiteaIssue,
setSelectedLocation,
setConfig,
loadRssFeeds,
loadRssFeedsSuccess,
loadRssFeedsError,
} from './consts'
export type Action = {
@@ -25,7 +36,8 @@ export type LoadTrainSchedule = {
export type LoadTrainScheduleSuccess = {
type: typeof loadTrainScheduleSuccess,
departures: DepartureType[]
departures: DepartureType[],
stations: Station[]
}
export type LoadTrainScheduleError = {
@@ -62,3 +74,41 @@ export type LoadWeatherError = {
error: Error
}
export type LoadGiteaIssue = {
type: typeof loadGiteaIssue,
}
export type LoadGiteaIssueSuccess = {
type: typeof loadGiteaIssueSuccess,
data: IssuesResponse
}
export type LoadGiteaIssueError = {
type: typeof loadGiteaIssueError,
error: Error
}
export type SetSelectedLocation = {
type: typeof setSelectedLocation,
location: string,
}
export type SetConfig = {
type: typeof setConfig,
setting: string,
value: string | number,
}
export type LoadRssFeeds = {
type: typeof loadRssFeeds,
}
export type LoadRssFeedsSuccess = {
type: typeof loadRssFeedsSuccess,
feeds: RSS[]
}
export type LoadRssFeedsError = {
type: typeof loadRssFeedsError,
feeds: RSS[]
}

View File

@@ -10,4 +10,18 @@ export const initialState: State = {
weather: undefined,
weatherError: undefined,
weatherLoading: false,
issues: undefined,
issuesError: undefined,
issuesLoading: false,
stations: undefined,
selectedLocation: 'Nivelles',
config: {
rssFollow: '',
trainCancelCompute: -180,
trainDelayCompute: 60,
trainScheduleShow: 120
},
rss: undefined,
rssLoading: false,
rssError: undefined
};

View File

@@ -17,6 +17,18 @@ import {
type LoadWeatherSuccess,
type LoadNewsError,
type LoadNewsSuccess,
loadGiteaIssueSuccess,
loadGiteaIssue,
type LoadGiteaIssueSuccess,
loadGiteaIssueError,
type LoadGiteaIssueError,
setSelectedLocation,
type SetSelectedLocation,
setConfig,
type LoadRssFeedsSuccess,
type SetConfig,
loadRssFeeds,
loadRssFeedsSuccess,
} from './actions';
@@ -34,6 +46,7 @@ export const reducerInner = (state: State, action: Action): State => {
return {
...state,
departures: (action as LoadTrainScheduleSuccess).departures,
stations: (action as LoadTrainScheduleSuccess).stations,
trainScheduleLoading: false,
}
}
@@ -85,6 +98,61 @@ export const reducerInner = (state: State, action: Action): State => {
weatherError: (action as LoadWeatherError).error,
weatherLoading: false,
}
}
else if(action.type === loadGiteaIssue) {
return {
...state,
issuesError: undefined,
issuesLoading: true,
}
}
else if(action.type === loadGiteaIssueSuccess) {
return {
...state,
issues: (action as LoadGiteaIssueSuccess).data,
issuesLoading: false,
}
}
else if(action.type === loadGiteaIssueError) {
return {
...state,
issuesLoading: false,
issuesError: (action as LoadGiteaIssueError).error,
}
}
else if(action.type === loadRssFeeds) {
return {
...state,
rssLoading: true,
}
}
else if(action.type === loadRssFeedsSuccess) {
return {
...state,
rss: (action as LoadRssFeedsSuccess).feeds,
}
}
else if(action.type === loadGiteaIssueError) {
return {
...state,
issuesLoading: false,
issuesError: (action as LoadGiteaIssueError).error,
}
}
else if(action.type === setSelectedLocation) {
return {
...state,
selectedLocation: (action as SetSelectedLocation).location,
}
}
else if(action.type === setConfig) {
return {
...state,
config: {
...state.config,
[(action as SetConfig).setting]: (action as SetConfig).value
}
}
}
return state;
}

View File

@@ -1,7 +1,21 @@
import type {Station} from "../types";
import type { Article } from "../types/article";
import type {IssuesResponse} from "../types/issues";
import type { DepartureType } from "../types/liveboard";
import type { WeatherData } from "../types/weather";
export type RSS = {
title: string,
link: string
}
export type Config = {
trainScheduleShow: number,
trainDelayCompute: number,
trainCancelCompute: number,
rssFollow: string,
}
export type State = {
trainScheduleLoading: boolean,
departures: DepartureType[] | undefined,
@@ -14,4 +28,17 @@ export type State = {
news: Article[] | undefined,
newsLoading: boolean,
newsError: Error | undefined,
issues: IssuesResponse | undefined,
issuesLoading: boolean,
issuesError: Error | undefined,
stations: undefined | Station[],
selectedLocation: string,
config: Config,
rss: RSS[] | undefined,
rssLoading: boolean,
rssError: Error | undefined,
}

64
src/types/issues.tsx Normal file
View File

@@ -0,0 +1,64 @@
interface User {
id: number;
login: string;
login_name: string;
source_id: number;
full_name: string;
email: string;
avatar_url: string;
html_url: string;
language: string;
is_admin: boolean;
last_login: string;
created: string;
restricted: boolean;
active: boolean;
prohibit_login: boolean;
location: string;
website: string;
description: string;
visibility: string;
followers_count: number;
following_count: number;
starred_repos_count: number;
username: string;
}
interface Repository {
id: number;
name: string;
owner: string;
full_name: string;
}
export interface IssueType {
id: number;
url: string;
html_url: string;
number: number;
user: User;
original_author: string;
original_author_id: number;
title: string;
body: string;
ref: string;
assets: any[];
labels: string[];
milestone: any | null;
assignee: any | null;
assignees: any | null;
state: string;
is_locked: boolean;
comments: number;
created_at: string;
updated_at: string;
closed_at: string | null;
due_date: string | null;
time_estimate: number;
pull_request: any | null;
repository: Repository;
pin_order: number;
}
export type IssuesResponse = IssueType[];

15
src/types/station.tsx Normal file
View File

@@ -0,0 +1,15 @@
export interface StationsRoot {
version: string
timestamp: string
station: Station[]
}
export interface Station {
"@id": string
id: string
name: string
locationX: string
locationY: string
standardname: string
}