Compare commits

...

7 Commits

18 changed files with 2425 additions and 98 deletions

1792
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -41,7 +41,7 @@
"vitest-browser-svelte": "^2.0.2"
},
"dependencies": {
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2348",
"@owlboard/owlboard-ts": "^3.0.0-dev.20260503t0947",
"@tabler/icons-svelte": "^3.40.0"
}
}

View File

@@ -39,7 +39,7 @@
align-items: center;
justify-content: center;
min-height: 48px;
min-width: 48px;
min-width: 98px;
appearance: none;
background: transparent;
border: none;

View File

@@ -0,0 +1,66 @@
<script lang="ts">
let { message = 'Loading...' } = $props();
</script>
<div class="loading-state">
<div class="track">
<div class="shuttle"></div>
</div>
<p>{message}</p>
</div>
<style>
.loading-state {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 4rem 2rem;
width: 75%;
margin: auto;
}
.track {
width: 160px;
height: 3px;
background-color: var(--color-title);
border-radius: 4px;
position: relative;
overflow: hidden;
}
.shuttle {
position: absolute;
width: 50%;
height: 100%;
border-radius: 4px;
background: linear-gradient(90deg, #1abc9c 0%, #3498db 100%);
animation: data-travel 1.6s infinite ease-in-out;
}
p {
font-family: 'URW Gothic', sans-serif;
letter-spacing: 0.15em;
color: var(--color-title);
animation: pulse 2s infinite ease-in-out;
}
@keyframes data-travel {
0% {
left: -50%;
}
100% {
left: 100%;
}
}
@keyframes pulse {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.4;
}
}
</style>

View File

@@ -0,0 +1,55 @@
<script lang="ts">
import noResult from '$lib/assets/img/no-data.svg';
import Button from '$lib/components/ui/Button.svelte';
let { title = 'No results', message = 'Try checking your search term' } = $props();
</script>
<div class="no-results-state">
<img src={noResult} class="image" height="200" width="200" alt="" role="presentation" />
<h3>{title}</h3>
<p>{message}</p>
<div class="btn-container">
<Button
type="button"
onclick={() => (history.length > 1 ? history.back() : (window.location.href = '/'))}
color="accent"
>
Go back
</Button>
</div>
</div>
<style>
.no-results-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
min-height: 400px;
padding: 5rem;
text-align: center;
box-sizing: border-box;
font-family: 'URW-Gothic', sans-serif;
}
.image {
margin-bottom: 1.95rem;
max-width: 90%;
height: auto;
}
h3 {
margin: 0 0 0.5rem 0;
}
p {
margin: 0;
opacity: 0.8;
}
.btn-container {
margin-top: 20px;
}
</style>

View File

@@ -0,0 +1,10 @@
<script lang="ts">
import { LOCATIONS } from '$lib/locations-object.svelte';
let { code } = $props<{ code: string | null | undefined }>();
const location = $derived(LOCATIONS.getName(code));
const displayName = $derived(location?.n ?? code ?? '');
</script>
{displayName}

View File

@@ -8,6 +8,7 @@
let code = $derived(toc.toUpperCase());
</script>
<!-- SPACE MONO for the font? -->
<div class="toc-container {code}">
{code}
</div>
@@ -20,8 +21,22 @@
justify-content: center;
padding: 2px 8px;
font-weight: 800;
font-size: 1.1rem;
background-color: #333;
color: #fff;
font-family: 'Courier New', Courier, monospace;
}
.AW {
/* Transport for Wales */
background: red;
color: white;
}
.LM {
/* West Midlands Trains */
background: rgb(176, 115, 1);
color: white;
}
.GW {
@@ -42,6 +57,12 @@
color: #ffffff;
}
.GN {
/* Great Northern */
background-color: fuchsia;
color: rgb(229, 229, 229);
}
.SW {
/* South Western Railway */
background-color: #2a3389;

View File

@@ -0,0 +1,341 @@
<script lang="ts">
import type { ApiTrainsTrainByHeadcode } from '@owlboard/owlboard-ts';
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
import { slide } from 'svelte/transition';
import { quintOut } from 'svelte/easing';
import { formatUkTime } from '$lib/utils/time';
import TocStyle from '$lib/components/ui/TocStyle.svelte';
import TiplocConverter from '$lib/components/ui/TiplocConverter.svelte';
let { service }: { service: ApiTrainsTrainByHeadcode.TrainByHeadcodeResponse } = $props();
let isExpanded = $state(false);
let loadingDetails = $state(false);
let loadingDetailsError = $state(false);
let loadingDetailsErrorMsg = $state('');
let details = $state(null);
const toggleExpand = async (rid: string) => {
if (isExpanded) {
isExpanded = false;
return;
}
loadingDetails = true;
try {
const result = await OwlClient.trains.getByRid(service.r);
details = result.data;
isExpanded = true;
} catch (e) {
console.Error('Failed to load train details');
loadingDetailsError = true;
loadingDetailsErrorMsg = e.message;
} finally {
loadingDetails = false;
}
};
let OriginDepartureSummary = $derived(formatUkTime(service.od));
async function loadDetails(rid: string) {
if (details) return;
loadingDetails = true;
const result = await OwlClient.trains.getByRid(service.r);
details = result.data;
loadingDetails = false;
}
const estClass = (act, est) => (!act && est ? 'est' : 'act');
</script>
<div class="train-service">
<button class="summary" onclick={toggleExpand} type="button" aria-expanded={isExpanded}>
{#if loadingDetails}
<div class="loading-state"><div class="loading-spinner"></div></div>
{/if}
<div class="operator-summary">
<TocStyle toc={service.o} />
</div>
<div class="main-text-summary">
<div class="time-summary">
{OriginDepartureSummary}
</div>
<div class="location-summary" class:can-all={service.ct}>
{service.ot}
</div>
<div class="location-summary to-summary" class:can-all={service.ct}>to</div>
<div class="location-summary" class:can-all={service.ct}>
{service.dt}
</div>
<!-- Add arrow icon to signify drop-down -->
<!-- ADD LOADING STATE -->
</div>
</button>
{#if isExpanded && details}
<div class="box-ext" transition:slide={{ duration: 800, easing: quintOut }}>
<!-- Here goes the data formatting! -->
<div class="detail-head">
<!-- Cancellation Section -->
{#if service.ct}
<span class="cancel-reason"> Cancelled throughout </span>
{/if}
{#if details.header.cr}
<span class="cancel-reason">
{details.header.cr}
{#if details.header.cl}
{details.header.cn ? ' near ' : ' at '}
<TiplocConverter code={details.header.cl} />
<!-- CONSIDER WRAPPING IN A COMPONENT THAT CONVERTS TO THE LOCATION NAME RATHER THAN TIPLOC -->
{/if}
</span>
{/if}
<!-- Delay Section -->
{#if details.header.dr}
<span class="delay-reason">
{details.header.dr}
{#if details.header.dl}
{details.header.dn ? ' near ' : ' at '}
{details.header.dl}
{/if}
</span>
{/if}
<div class="pis-detail">
<!-- PIS Data Here -->
</div>
</div>
<div class="schedule-table-container">
<table class="schedule-table">
<thead>
<tr>
<th colspan="2"></th>
<th colspan="2">Arr</th>
<th colspan="2">Dep</th>
</tr>
<tr>
<th>Location</th>
<th>Plat</th>
<th>Sch</th>
<th>Act</th>
<th>Sch</th>
<th>Act</th>
</tr>
</thead>
<tbody>
{#each details.locations as loc}
<tr class:pass-loc={loc.r === 'PASS'} class:can-loc={loc.can}>
<td class="tpl-cell">{loc.t}</td>
<td class="plat-cell">{loc.p}</td>
{#if loc.r == 'PASS'}
<td class="time-cell" colspan="2">Pass</td>
<td class="time-cell">{formatUkTime(loc.wtp)}</td>
<td class="time-cell {estClass(loc.atp, loc.etp)}"
>{formatUkTime(loc.atp || loc.etp || '--')}</td
>
{:else}
<td class="time-cell">{formatUkTime(loc.pta || loc.wta || '--')}</td>
<td class="time-cell {estClass(loc.ata, loc.eta)}"
>{formatUkTime(loc.ata || loc.eta || '--')}</td
>
<td class="time-cell">{formatUkTime(loc.ptd || loc.wtd || '--')}</td>
<td class="time-cell {estClass(loc.atd, loc.etd)}"
>{formatUkTime(loc.atd || loc.etd || '--')}</td
>
{/if}
</tr>
{/each}
</tbody>
</table>
</div>
</div>
{/if}
</div>
<style>
.train-service {
background-color: var(--color-accent);
width: 100%;
border-radius: 4px;
box-shadow: var(--shadow-std);
overflow: hidden;
font-family: 'URW Gothic', sans-serif;
transition: 0.2s all;
filter: brightness(1.1);
}
.summary:hover {
filter: invert();
}
.summary {
position: relative;
display: flex;
align-items: center;
width: 100%;
padding: 0.75rem;
min-height: 48px;
border: none;
background: transparent;
cursor: pointer;
text-align: left;
gap: 0.5rem;
}
.loading-state {
position: absolute;
top: 0;
left: 0;
z-index: 2;
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(0, 0, 0, 0.2);
backdrop-filter: blur(5px);
width: 100%;
height: 100%;
font-family: 'URW Gothic', sans-serif;
font-size: 1rem;
color: var(--color-title);
}
.loading-spinner {
width: 16px;
height: 16px;
border: 2px solid rgba(0, 0, 0, 0.1);
border-top-color: #fff;
border-radius: 50%;
animation: load-spin 0.8s linear infinite;
z-index: 3;
}
@keyframes load-spin {
to {
transform: rotate(360deg);
}
}
.operator-summary {
flex-shrink: 0;
}
.main-text-summary {
display: flex;
flex-grow: 1;
align-items: center;
gap: 0.5rem;
}
.time-summary {
font-size: 1.1rem;
font-weight: 700;
color: var(--color-brand);
}
.location-summary {
text-transform: uppercase;
font-weight: 500;
font-size: 1.1rem;
letter-spacing: 0.02em;
color: var(--color-title);
}
.to-summary {
font-size: 0.8rem;
font-style: oblique;
text-transform: lowercase;
}
.can-all {
color: red;
}
.box-ext {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
}
.detail-head {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
text-align: center;
width: 100%;
}
.cancel-reason,
.delay-reason {
display: block;
padding: 4px 8px;
width: 95%;
}
.cancel-reason {
color: rgb(255, 0, 0);
font-weight: 500;
animation: cancel-pulse 2s ease-in-out infinite;
}
.delay-reason {
color: rgb(255, 119, 0);
font-weight: 500;
animation: cancel-pulse 2s ease-in-out infinite;
}
@keyframes cancel-pulse {
0% {
opacity: 1;
text-shadow: 0 0 0px rgb(255, 0, 0);
}
50% {
opacity: 0.8;
text-shadow: 0 0 5px rgba(255, 0, 0, 0.2);
}
100% {
opacity: 1;
text-shadow: 0 0 0px rgb(255, 0, 0);
}
}
.schedule-table-container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.schedule-table {
width: 95%;
max-width: 375px;
padding-bottom: 1rem;
}
th,
td {
text-align: center;
}
.tpl-cell {
color: yellow;
}
.pass-loc {
color: var(--color-title);
opacity: 0.75;
}
.can-loc {
text-decoration: line-through;
}
.est {
font-style: oblique;
opacity: 0.5;
}
.act {
color: yellow;
}
</style>

View File

@@ -1,35 +1,27 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { goto } from '$app/navigation';
import BaseCard from '$lib/components/ui/cards/BaseCard.svelte';
import Textbox from '$lib/components/ui/Textbox.svelte';
import Button from '$lib/components/ui/Button.svelte';
let headcode = $state('');
function handleSearch(e: SubmitEvent) {
e.preventDefault();
if (!headcode.trim()) return;
function handleSearch(e: SubmitEvent) {
e.preventDefault();
if (!headcode.trim()) return;
const searchParams = new URLSearchParams();
searchParams.append('h', headcode.trim().toUpperCase());
const searchParams = new URLSearchParams();
searchParams.append('h', headcode.trim().toUpperCase());
goto(`/trains?${searchParams.toString()}`)
}
goto(`/trains?${searchParams.toString()}`);
}
</script>
<BaseCard header={'Search Train & PIS'}>
<form onsubmit={handleSearch} class="card-content">
<Textbox
placeholder="Enter Headcode"
bind:value={headcode}
maxLength={4}
/>
<Button
type="submit"
disabled={!headcode}
>Search</Button>
</form>
<form onsubmit={handleSearch} class="card-content">
<Textbox placeholder="Enter Headcode" bind:value={headcode} maxLength={4} />
<Button type="submit" disabled={!headcode}>Search</Button>
</form>
</BaseCard>
<style>
@@ -37,9 +29,9 @@
text-align: center;
width: 90%;
margin: auto;
padding: 10px 0 10px 0;
display: flex;
flex-direction: column;
gap: 0.75rem;
padding: 10px 0 0 0;
display: flex;
flex-direction: column;
gap: 0.75rem;
}
</style>
</style>

View File

@@ -44,7 +44,7 @@
width: 90%;
min-height: 98px;
margin: auto;
padding: 10px 0 10px 0;
padding: 10px 0 0 0;
}
.stations-flex {

View File

@@ -10,13 +10,13 @@ class NearestStationsState {
private initGeoConfig: PositionOptions = {
enableHighAccuracy: false,
timeout: 500,
maximumAge: Infinity,
}
maximumAge: Infinity
};
private geoConfig: PositionOptions = {
enableHighAccuracy: false,
timeout: 20000,
maximumAge: 30000,
maximumAge: 30000
};
constructor() {

View File

@@ -28,6 +28,20 @@ class LocationStore {
return loc.t === query || loc.c === query;
});
}
getName(code: string | number | null | undefined): ApiLocationFilter.LocationFilterObject | null {
try {
if (!code) return null;
const query = String(code).toUpperCase().trim();
const match = this.data.find((loc) => loc.t === query || loc.c === query);
return match ?? null;
} catch (e) {
console.error('Error finding location object: ', e);
return null;
}
}
}
export const LOCATIONS = new LocationStore();

17
src/lib/utils/time.ts Normal file
View File

@@ -0,0 +1,17 @@
/**
* Converts ISO/JSON time to UK-formatted HH:MM string.
* Ensures Europe/London timezone irrespective of browser timezone.
*/
export function formatUkTime(dateStr: string | Date | undefined): string {
if (!dateStr) return '--:--';
const date = typeof dateStr === 'string' ? new Date(dateStr) : dateStr;
if (isNaN(date.getTime())) return '--:--';
return date.toLocaleTimeString('en-GB', {
hour: '2-digit',
minute: '2-digit',
hour12: false,
timeZone: 'Europe/London'
});
}

View File

@@ -7,11 +7,13 @@
</script>
<div class="error-wrapper">
{#if page.status == 20}
<img class="err-img" src={noResult} alt="" role="presentation" width="200" height="200" />
{#if page.status == 404}
<!-- Warning no data image -->
<img class="err-img" src={noResult} alt="" role="presentation" width="200" height="200" />
{:else}
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
{/if}
<!-- STOP Error image -->
<img class="err-img" src={stopErr} alt="" role="presentation" width="150" height="210" />
{/if}
<h2 class="label">{page.status}</h2>
<p class="error-message">

View File

@@ -2,10 +2,11 @@
import { page } from '$app/state';
import { slide, fade } from 'svelte/transition';
import { onMount } from 'svelte';
import { navigating } from '$app/state';
import { LOCATIONS } from '$lib/locations-object.svelte';
import { nearestStationsState } from '$lib/geohash.svelte';
import Loading from '$lib/components/ui/Loading.svelte';
import TimezoneWarning from '$lib/components/ui/TimezoneWarning.svelte';
import '$lib/global.css';
@@ -81,7 +82,12 @@
<main>
<TimezoneWarning />
{@render children()}
{#if navigating && navigating.to}
<Loading />
{:else}
{@render children()}
{/if}
</main>
<nav bind:clientWidth={navWidth}>

View File

@@ -1,4 +1,5 @@
<script lang="ts">
import HeadcodeSearchCard from '$lib/components/ui/cards/HeadcodeSearchCard.svelte';
import PisStartEndCard from '$lib/components/ui/cards/pis/PisStartEndCard.svelte';
import PisCode from '$lib/components/ui/cards/pis/PisCode.svelte';
import Button from '$lib/components/ui/Button.svelte';
@@ -60,6 +61,7 @@
{#if !resultsLoaded}
<div class="card-container">
<HeadcodeSearchCard />
<PisStartEndCard onsearch={handleStartEndSearch} />
<PisCode onsearch={handleCodeSearch} />
</div>

View File

@@ -1,12 +1,32 @@
<script lang="ts">
import NoResults from '$lib/components/ui/NoResults.svelte';
import TrainService from '$lib/components/ui/TrainService.svelte';
let { data } = $props();
</script>
<pre>{JSON.stringify(data.results, null, 1)}</pre>
<h6 style="text-align:center;width=100%;margin:auto;padding-top:1rem;font-size:1rem;">
DateSelector
</h6>
{#if data.results.length === 0}
<NoResults message={'No trains found on this date with this headcode.'} />
{:else}
<div class="result-boxes">
{#each data.results as service (service.r)}
<TrainService {service} />
{/each}
</div>
{/if}
<style>
pre {
background: #1e1e1e;
color: #00ff00;
padding: 1rem;
overflow: auto;
}
.result-boxes {
width: 95%;
margin: auto;
padding-top: 1rem;
padding-bottom: 1rem;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 1rem;
}
</style>

View File

@@ -4,52 +4,59 @@ import type { PageLoad } from './$types';
import { error } from '@sveltejs/kit';
export const load: PageLoad = async ({ url }) => {
const headcode = url.searchParams.get('h');
let dateParam = url.searchParams.get('d');
const toc = url.searchParams.get('t') || "";
const headcode = url.searchParams.get('h');
let dateParam = url.searchParams.get('d');
const toc = url.searchParams.get('t') || '';
const date: string | Date = (dateParam === "" || dateParam === null)
? new Date()
: dateParam;
const date: string | Date = dateParam === '' || dateParam === null ? new Date() : dateParam;
if (!headcode) {
throw error(400, {
message: 'Headcode not provided',
owlCode: 'INVALID_DATA'
});
}
if (!headcode) {
throw error(400, {
message: 'Headcode not provided',
owlCode: 'INVALID_DATA'
});
}
try {
const response = await OwlClient.trains.getByHeadcode(headcode, date, toc);
// Declared outside of the try so that it can be used in both the try and catch blocks
let results: ApiTrainsTrainByHeadcode.TrainByHeadcodeResponse[];
// Shouldn't be needed to cast the type...
const results = (response.data || []);
return {
title: headcode.toUpperCase(),
results: results,
}
} catch (e: unknown) {
if (e instanceof ValidationError) {
throw error(400, {
message: e.message,
owlCode: 'VALIDATION_ERROR',
});
} else if (e instanceof ApiError) {
console.log(e);
throw error(20, {
message: e.message,
owlCode: 'API_ERROR',
});
} else if (e instanceof Error) {
throw error(500, {
message: e.message,
owlCode: 'GEN_ERROR',
})
} else {
throw error(500, {
message: "Unexpected error",
owlCode: "UNKNOWN_ERR",
})
}
}
}
try {
const response = await OwlClient.trains.getByHeadcode(headcode, date, toc);
results = response.data;
return {
title: headcode.toUpperCase(),
results: results
};
} catch (e: unknown) {
if (e instanceof ValidationError) {
throw error(400, {
message: e.message,
owlCode: 'VALIDATION_ERROR'
});
} else if (e instanceof ApiError) {
// Check if NO_RESULTS error, and return empty array if that is the case
if (e.code === 'NOT_FOUND') {
return {
title: headcode.toUpperCase(),
results: []
};
} else {
throw error(e.status, {
message: e.message,
owlCode: 'API_ERROR'
});
}
} else if (e instanceof Error) {
throw error(500, {
message: e.message,
owlCode: 'GEN_ERROR'
});
} else {
throw error(500, {
message: 'Unexpected error',
owlCode: 'UNKNOWN_ERR'
});
}
}
};