Add preferences and privacy policy
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
import BaseCard from '$lib/components/ui/cards/BaseCard.svelte';
|
||||
import Button from '$lib/components/ui/form-elements/Button.svelte';
|
||||
|
||||
import { prefs } from '$lib/preferences.svelte';
|
||||
|
||||
import { fade } from 'svelte/transition';
|
||||
import { flip } from 'svelte/animate';
|
||||
|
||||
@@ -12,7 +14,9 @@
|
||||
|
||||
<BaseCard header={'Nearby Stations'}>
|
||||
<div class="card-content">
|
||||
{#if nearestStationsState.error && nearestStationsState.list.length === 0}
|
||||
{#if !prefs.current.NearToMe}
|
||||
<Button onclick={() => prefs.toggleLocation(true)}>Enable Location</Button>
|
||||
{:else if nearestStationsState.error && nearestStationsState.list.length === 0}
|
||||
<p class="msg">{nearestStationsState.error}</p>
|
||||
{:else if nearestStationsState.loading && nearestStationsState.list.length === 0}
|
||||
<p class="msg">Locating stations...</p>
|
||||
|
||||
112
src/lib/components/ui/form-elements/Toggle.svelte
Normal file
112
src/lib/components/ui/form-elements/Toggle.svelte
Normal file
@@ -0,0 +1,112 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
checked: boolean;
|
||||
id: string;
|
||||
label: string;
|
||||
disabled?: boolean;
|
||||
onchange?: (checked: boolean) => void;
|
||||
}
|
||||
|
||||
let { checked = $bindable(false), id, label, disabled = false, onchange }: Props = $props();
|
||||
|
||||
function handleInput(e: Event) {
|
||||
const target = e.target as HTMLInputElement;
|
||||
checked = target.checked;
|
||||
onchange?.(checked);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="toggle-wrapper" class:is-disabled={disabled}>
|
||||
<!-- 1. Interactive Switch Mechanism (Sits Left) -->
|
||||
<div class="toggle-control">
|
||||
<input type="checkbox" {id} {disabled} {checked} onchange={handleInput} class="toggle-input" />
|
||||
<label for={id} class="toggle-track">
|
||||
<span class="toggle-thumb"></span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- 2. Visible Text Label (Sits Right) -->
|
||||
<label for={id} class="toggle-label-text">
|
||||
{label}
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.toggle-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
gap: 1.75rem;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.toggle-control {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.toggle-input {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.toggle-track {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
width: 3.75rem;
|
||||
height: 2rem;
|
||||
background-color: rgb(93, 45, 45);
|
||||
border-radius: 1rem;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.2s ease;
|
||||
}
|
||||
|
||||
.toggle-thumb {
|
||||
position: absolute;
|
||||
top: 0.25rem;
|
||||
left: 0.25rem;
|
||||
width: 1.5rem;
|
||||
height: 1.5rem;
|
||||
background-color: var(--color-accent);
|
||||
border-radius: 50%;
|
||||
transition: transform 0.2s ease;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
||||
filter: brightness(0.95);
|
||||
}
|
||||
|
||||
.toggle-input:checked + .toggle-track {
|
||||
background-color: rgb(11, 70, 11);
|
||||
}
|
||||
|
||||
.toggle-input:checked + .toggle-track .toggle-thumb {
|
||||
transform: translateX(1.75rem);
|
||||
}
|
||||
|
||||
.toggle-input:focus-visible + .toggle-track {
|
||||
outline: 2px solid #3b82f6;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.toggle-label-text {
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
color: var(--color-title);
|
||||
}
|
||||
|
||||
.is-disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.is-disabled .toggle-track,
|
||||
.is-disabled .toggle-label-text {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
</style>
|
||||
@@ -1,12 +1,15 @@
|
||||
import { OwlClient, ValidationError, ApiError } from './owlClient';
|
||||
import type { ApiStationsNearestStations } from '@owlboard/owlboard-ts';
|
||||
import { prefs } from './preferences.svelte';
|
||||
|
||||
class NearestStationsState {
|
||||
list = $state<ApiStationsNearestStations.StationsNearestStations[]>([]);
|
||||
currentHash = $state('');
|
||||
loading = $state(true);
|
||||
loading = $state(false);
|
||||
error = $state<string | null>(null);
|
||||
|
||||
private watcherId: number | null = null;
|
||||
|
||||
private initGeoConfig: PositionOptions = {
|
||||
enableHighAccuracy: false,
|
||||
timeout: 500,
|
||||
@@ -20,10 +23,35 @@ class NearestStationsState {
|
||||
};
|
||||
|
||||
constructor() {
|
||||
if (typeof window !== 'undefined' && 'geolocation' in navigator) {
|
||||
this.jumpstart();
|
||||
this.initWatcher();
|
||||
if (typeof window === 'undefined' || !('geolocation' in navigator)) return;
|
||||
|
||||
$effect.root(() => {
|
||||
$effect(() => {
|
||||
if (prefs.current.NearToMe) {
|
||||
this.startTracking();
|
||||
} else {
|
||||
this.stopTracking();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private startTracking() {
|
||||
if (this.watcherId !== null) return; // Already tracking
|
||||
this.loading = true;
|
||||
this.jumpstart();
|
||||
this.initWatcher();
|
||||
}
|
||||
|
||||
private stopTracking() {
|
||||
if (this.watcherId !== null) {
|
||||
navigator.geolocation.clearWatch(this.watcherId);
|
||||
this.watcherId = null;
|
||||
}
|
||||
this.list = [];
|
||||
this.currentHash = '';
|
||||
this.loading = false;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
private jumpstart() {
|
||||
@@ -35,7 +63,7 @@ class NearestStationsState {
|
||||
}
|
||||
|
||||
private initWatcher() {
|
||||
navigator.geolocation.watchPosition(
|
||||
this.watcherId = navigator.geolocation.watchPosition(
|
||||
(pos) => this.handleUpdate(pos.coords.latitude, pos.coords.longitude),
|
||||
(err) => this.handleError(err),
|
||||
this.geoConfig
|
||||
|
||||
@@ -31,7 +31,7 @@ export function ThrowApiError(e: unknown): never {
|
||||
|
||||
// Map ApiError 'code' values
|
||||
if (e instanceof ApiError) {
|
||||
console.error(JSON.stringify(e), e.code, "ERRCODE")
|
||||
console.error(JSON.stringify(e), e.code, 'ERRCODE');
|
||||
let status = 500;
|
||||
if (e.code === 'AUTH') {
|
||||
status = 401;
|
||||
@@ -54,7 +54,7 @@ export function ThrowApiError(e: unknown): never {
|
||||
|
||||
// Fallback for other error kind
|
||||
const genericMessage = e instanceof Error ? e.message : 'An unexpected error occurred.';
|
||||
console.log(e)
|
||||
console.log(e);
|
||||
throw error(500, {
|
||||
message: genericMessage,
|
||||
name: 'CRITICAL_ERROR',
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { browser } from '$app/environment';
|
||||
|
||||
export interface Preferences {
|
||||
locationEnabled: boolean;
|
||||
NearToMe: boolean;
|
||||
ShowPassingTrains: boolean;
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'ob_pref';
|
||||
|
||||
const DEFAULT_PREFS: Preferences = {
|
||||
locationEnabled: false
|
||||
NearToMe: false,
|
||||
ShowPassingTrains: true
|
||||
};
|
||||
|
||||
function loadPrefs(): Preferences {
|
||||
@@ -16,7 +18,13 @@ function loadPrefs(): Preferences {
|
||||
if (!stored) return DEFAULT_PREFS;
|
||||
|
||||
try {
|
||||
return { ...DEFAULT_PREFS, ...JSON.parse(stored) };
|
||||
const parsed = JSON.parse(stored);
|
||||
const cleaned = {} as Preferences;
|
||||
|
||||
for (const key of Object.keys(DEFAULT_PREFS) as Array<keyof Preferences>) {
|
||||
cleaned[key] = key in parsed ? parsed[key] : DEFAULT_PREFS[key];
|
||||
}
|
||||
return cleaned;
|
||||
} catch {
|
||||
return DEFAULT_PREFS;
|
||||
}
|
||||
@@ -35,8 +43,12 @@ class PreferencesStore {
|
||||
});
|
||||
}
|
||||
|
||||
toggleLocation(enabled: boolean) {
|
||||
this.current.locationEnabled = enabled;
|
||||
toggleNearToMe(enabled: boolean) {
|
||||
this.current.NearToMe = enabled;
|
||||
}
|
||||
|
||||
toggleShowPassingTrains(enabled: boolean) {
|
||||
this.current.ShowPassingTrains = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user