mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
Add a dayTintProvider plugin hook
Lets a plugin colour-code days in the Plan sidebar. trip-segments can already announce "Kanazawa begins" in the day plan, but it can't make day 12 look like it belongs to Kanazawa. dayScheduleProvider can't cover this: it renders a fixed row, and it's capped at 60 items per provider, so one item per day would tint the first 60 days of a six-month trip and silently stop. This hook is bounded by day count instead. GET /api/day-tints/:tripId returns per-day tones for three regions of a day card - the number badge (also the mobile day chip), the header row, and the expanded activity list. `tone` is a shorthand filling every region a contribution doesn't name; unnamed regions render as they do today. Tones only, never hex, so the host can pick the alpha per theme and per region. One contribution per day, resolved whole: first entry wins within a provider, first granted provider wins across plugins.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Zap } from 'lucide-react'
|
||||
import { pluginsApi, type PluginDayScheduleItem } from '../../api/client'
|
||||
import { pluginsApi, type PluginDayScheduleItem, type PluginDayTint, type PluginDayTintTone } from '../../api/client'
|
||||
|
||||
/**
|
||||
* Host-rendered rows for the `dayScheduleProvider` plugin hook — time
|
||||
@@ -62,6 +62,60 @@ export function usePluginDaySchedule(tripId?: number | string | null): PluginDay
|
||||
}, [items])
|
||||
}
|
||||
|
||||
/** dayId → the per-region tones (and optional tooltip) for that day's card. */
|
||||
export type PluginDayTints = Record<number, Omit<PluginDayTint, 'pluginId' | 'dayId'>>
|
||||
|
||||
const EMPTY_TINTS: PluginDayTints = {}
|
||||
|
||||
/**
|
||||
* Host-rendered day colours for the `dayTintProvider` plugin hook — "day 12 belongs
|
||||
* to the Kanazawa leg". A sibling of usePluginDaySchedule in every respect: same
|
||||
* fail-safe fetch, same vetted-data-only contract. Separate from the schedule hook
|
||||
* because its output is bounded by the trip's day count rather than the schedule
|
||||
* hook's ≤60 items, which a long multi-destination trip would blow straight past.
|
||||
*
|
||||
* The server has already resolved precedence (one tint per day, first granted
|
||||
* provider wins), so this is a flat index — the callers just look a day up.
|
||||
*/
|
||||
export function usePluginDayTints(tripId?: number | string | null): PluginDayTints {
|
||||
const [tints, setTints] = useState<PluginDayTint[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
if (tripId == null) { setTints([]); return }
|
||||
let alive = true
|
||||
pluginsApi.dayTints(tripId)
|
||||
.then(r => { if (alive) setTints(r.tints || []) })
|
||||
.catch(() => { if (alive) setTints([]) }) // fail-safe: no tints
|
||||
return () => { alive = false }
|
||||
}, [tripId])
|
||||
|
||||
return useMemo(() => {
|
||||
if (tints.length === 0) return EMPTY_TINTS
|
||||
const out: PluginDayTints = {}
|
||||
for (const t of tints) {
|
||||
const { pluginId: _pluginId, dayId: _dayId, ...regions } = t
|
||||
out[t.dayId] = regions
|
||||
}
|
||||
return out
|
||||
}, [tints])
|
||||
}
|
||||
|
||||
/** The `color-mix` background for one tinted region, or undefined when that region has
|
||||
* no tone — in which case the caller keeps whatever it renders without plugins.
|
||||
*
|
||||
* `alphaVar` names a CSS custom property holding the strength, which varies per theme
|
||||
* AND per region: the tones are fixed hexes, so one alpha cannot serve both themes
|
||||
* (#4F46E5 is itself a dark colour and vanishes on a dark surface at a light theme's
|
||||
* alpha), and a large region behind dense text needs a fainter tint than a small badge. */
|
||||
export function dayTintBackground(
|
||||
tone: PluginDayTintTone | undefined,
|
||||
alphaVar: string,
|
||||
base = 'transparent',
|
||||
): string | undefined {
|
||||
if (!tone) return undefined
|
||||
return `color-mix(in srgb, ${TONE_COLORS[tone] ?? TONE_COLORS.default} var(${alphaVar}), ${base})`
|
||||
}
|
||||
|
||||
export function formatScheduleMinutes(minutes: number): string {
|
||||
const h = Math.floor(minutes / 60)
|
||||
const m = minutes % 60
|
||||
|
||||
Reference in New Issue
Block a user