createCrudStore captures the places store's pattern and now backs places, job applications and job references. JobApplications.vue no longer calls gql() itself and is split into the applications table, the references table and the add form, with the status vocabulary in jobStatus.js. api.js is a thin REST client that turns failures into an ApiError carrying the backend's message; the admin views and Listening.vue use it in place of raw axios and fetch. The router guard waited on the home query's loaded flag, which is never set when that query fails, so /admin and /cv/jobs could hang. It now awaits a standalone me query owned by the auth store, and homeData gains a settled flag for widgets that only need to know the fetch finished. The Vite proxy targets are configurable through VITE_PROXY_*, and build:js runs the Vite build without wasm-pack. Co-Authored-By: Claude Opus 5.5 <[email protected]>
354 lines
10 KiB
Vue
354 lines
10 KiB
Vue
<script setup>
|
|
/**
|
|
* The live "Rowing" widget on /stp: a hand-drawn SVG line chart of rowing
|
|
* sessions, with a metric selector and a hover tooltip.
|
|
*
|
|
* No chart library — points are projected by hand into a fixed 290x120 viewBox
|
|
* (W/H) inset by the PL/PT/PR/PB padding constants. The y axis is normalised to
|
|
* the visible min..max of the selected metric, so `range || 1` guards the
|
|
* divide-by-zero when every session has the same value, and a single data point
|
|
* is centred rather than pinned to x=0.
|
|
*
|
|
* Rows are reversed (oldest first) for the chart; `slice()` first so the store's
|
|
* array is not mutated.
|
|
*
|
|
* The admin create form is a defineAsyncComponent so CreateRowing.vue is only
|
|
* downloaded when an admin actually opens it.
|
|
*/
|
|
import { ref, computed, defineAsyncComponent } from "vue";
|
|
import Header from "@/components/text/Header.vue";
|
|
import Modal from "@/components/util/Modal.vue";
|
|
import CreateToggle from "@/components/input/CreateToggle.vue";
|
|
import { useHomeDataStore } from "@/stores/homeData";
|
|
import { storeToRefs } from "pinia";
|
|
|
|
const CreateRowing = defineAsyncComponent(
|
|
() => import("@/views/admin/CreateRowing.vue"),
|
|
);
|
|
|
|
const store = useHomeDataStore();
|
|
const { settled, error, rowingSessions } = storeToRefs(store);
|
|
const showCreate = ref(false);
|
|
|
|
// slice() first: reverse() mutates in place and would reorder the store's array.
|
|
const rows = computed(() => rowingSessions.value.slice().reverse());
|
|
// `settled`, not `loaded`: a home query that fails never sets `loaded`, and the
|
|
// error branch below would be unreachable behind a permanent spinner.
|
|
const loading = computed(() => !settled.value);
|
|
|
|
const metric = ref("distance");
|
|
const hovered = ref(null);
|
|
|
|
const METRICS = [
|
|
{ key: "distance", label: "Distance (m)", color: "var(--color-primary)" },
|
|
{ key: "timePer500m", label: "Pace /500m", color: "var(--color-tertiary)" },
|
|
{ key: "calories", label: "Calories", color: "var(--color-secondary)" },
|
|
];
|
|
|
|
const activeMetric = computed(() =>
|
|
METRICS.find((m) => m.key === metric.value),
|
|
);
|
|
|
|
// SVG layout constants
|
|
const W = 290;
|
|
const H = 120;
|
|
const PL = 46; // padding left
|
|
const PT = 8; // padding top
|
|
const PR = 8; // padding right
|
|
const PB = 28; // padding bottom
|
|
const PLOT_W = W - PL - PR;
|
|
const PLOT_H = H - PT - PB;
|
|
|
|
const chartData = computed(() =>
|
|
rows.value.map((r) => ({
|
|
date: new Date(r.date),
|
|
value: r[metric.value],
|
|
raw: r,
|
|
})),
|
|
);
|
|
|
|
const minVal = computed(() => Math.min(...chartData.value.map((d) => d.value)));
|
|
const maxVal = computed(() => Math.max(...chartData.value.map((d) => d.value)));
|
|
|
|
// Project each session into SVG coordinates inside the PL/PT/PR/PB padded plot
|
|
// area. `range || 1` avoids a divide-by-zero when every value is identical, and
|
|
// a single point is centred horizontally rather than pinned to the left edge.
|
|
const points = computed(() => {
|
|
const data = chartData.value;
|
|
const n = data.length;
|
|
if (!n) return [];
|
|
const min = minVal.value;
|
|
const range = maxVal.value - min || 1;
|
|
return data.map((d, i) => ({
|
|
x: PL + (n <= 1 ? PLOT_W / 2 : (i / (n - 1)) * PLOT_W),
|
|
y: PT + PLOT_H - ((d.value - min) / range) * PLOT_H,
|
|
date: d.date,
|
|
value: d.value,
|
|
raw: d.raw,
|
|
}));
|
|
});
|
|
|
|
const polyline = computed(() =>
|
|
points.value.map((p) => `${p.x},${p.y}`).join(" "),
|
|
);
|
|
|
|
const xLabels = computed(() => {
|
|
const data = chartData.value;
|
|
const pts = points.value;
|
|
if (!data.length) return [];
|
|
const indices = new Set([
|
|
0,
|
|
Math.floor((data.length - 1) / 2),
|
|
data.length - 1,
|
|
]);
|
|
return [...indices].map((i) => ({
|
|
x: pts[i].x,
|
|
label: data[i].date.toLocaleDateString("en-GB", {
|
|
month: "short",
|
|
day: "numeric",
|
|
}),
|
|
}));
|
|
});
|
|
|
|
const yLabels = computed(() => {
|
|
const min = minVal.value;
|
|
const max = maxVal.value;
|
|
return [0, 0.5, 1].map((t) => {
|
|
const raw = Math.round(min + t * (max - min));
|
|
return {
|
|
y: PT + PLOT_H - t * PLOT_H,
|
|
label: metric.value === "timePer500m" ? formatTime(raw) : raw,
|
|
};
|
|
});
|
|
});
|
|
|
|
function formatTime(secs) {
|
|
const m = Math.floor(secs / 60);
|
|
const s = Math.round(secs % 60);
|
|
return `${m}:${String(s).padStart(2, "0")}`;
|
|
}
|
|
|
|
function formatValue(key, val) {
|
|
if (key === "timePer500m") return formatTime(val) + " /500m";
|
|
if (key === "distance") return val + " m";
|
|
if (key === "calories") return Math.round(val) + " kcal";
|
|
return val;
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="flex flex-col h-full overflow-hidden">
|
|
<Header>
|
|
Rowing
|
|
<template #action><CreateToggle v-model="showCreate" /></template>
|
|
</Header>
|
|
|
|
<Modal v-model="showCreate">
|
|
<CreateRowing @done="showCreate = false" @cancel="showCreate = false" />
|
|
</Modal>
|
|
<div v-if="loading" class="flex-1 flex items-center justify-center">
|
|
<p>Loading...</p>
|
|
</div>
|
|
<div v-else-if="error" class="flex-1 flex items-center justify-center">
|
|
<p class="text-tertiary text-xs">{{ error }}</p>
|
|
</div>
|
|
<div v-else class="flex flex-col flex-1 px-1 pb-1 gap-1 overflow-hidden">
|
|
<!-- Metric tabs -->
|
|
<div class="flex gap-1 pt-1">
|
|
<button
|
|
v-for="m in METRICS"
|
|
:key="m.key"
|
|
class="metric-btn text-xs px-2 py-0.5 font-heading border"
|
|
:style="{
|
|
borderColor: m.color,
|
|
color: metric === m.key ? 'var(--color-surface)' : m.color,
|
|
backgroundColor: metric === m.key ? m.color : 'transparent',
|
|
}"
|
|
@click="metric = m.key"
|
|
>
|
|
{{ m.label }}
|
|
</button>
|
|
</div>
|
|
|
|
<!-- SVG Chart -->
|
|
<div class="flex-1 relative">
|
|
<svg
|
|
:viewBox="`0 0 ${W} ${H}`"
|
|
width="100%"
|
|
height="100%"
|
|
preserveAspectRatio="none"
|
|
class="overflow-visible"
|
|
>
|
|
<!-- Grid lines -->
|
|
<line
|
|
v-for="yl in yLabels"
|
|
:key="yl.y"
|
|
:x1="PL"
|
|
:y1="yl.y"
|
|
:x2="W - PR"
|
|
:y2="yl.y"
|
|
stroke="var(--color-quaternary)"
|
|
stroke-width="0.5"
|
|
/>
|
|
|
|
<!-- Area fill -->
|
|
<polygon
|
|
v-if="points.length"
|
|
:points="`${PL},${PT + PLOT_H} ${polyline} ${W - PR},${PT + PLOT_H}`"
|
|
:fill="activeMetric.color"
|
|
fill-opacity="0.08"
|
|
/>
|
|
|
|
<!-- Line -->
|
|
<polyline
|
|
v-if="points.length"
|
|
:points="polyline"
|
|
:stroke="activeMetric.color"
|
|
stroke-width="1.5"
|
|
fill="none"
|
|
stroke-linejoin="round"
|
|
stroke-linecap="round"
|
|
/>
|
|
|
|
<!-- Data points -->
|
|
<circle
|
|
v-for="(p, i) in points"
|
|
:key="i"
|
|
:cx="p.x"
|
|
:cy="p.y"
|
|
:r="hovered === i ? 4 : 2"
|
|
:fill="activeMetric.color"
|
|
style="cursor: pointer"
|
|
@mouseenter="hovered = i"
|
|
@mouseleave="hovered = null"
|
|
/>
|
|
|
|
<!-- Y axis labels -->
|
|
<text
|
|
v-for="yl in yLabels"
|
|
:key="`y${yl.y}`"
|
|
:x="PL - 3"
|
|
:y="yl.y + 3"
|
|
text-anchor="end"
|
|
font-size="10"
|
|
fill="var(--color-primary)"
|
|
font-family="var(--font-heading)"
|
|
>
|
|
{{ yl.label }}
|
|
</text>
|
|
|
|
<!-- X axis labels -->
|
|
<text
|
|
v-for="xl in xLabels"
|
|
:key="`x${xl.x}`"
|
|
:x="xl.x"
|
|
:y="H - 4"
|
|
text-anchor="middle"
|
|
font-size="10"
|
|
fill="var(--color-primary)"
|
|
font-family="var(--font-heading)"
|
|
>
|
|
{{ xl.label }}
|
|
</text>
|
|
|
|
<!-- Axes -->
|
|
<line
|
|
:x1="PL"
|
|
:y1="PT"
|
|
:x2="PL"
|
|
:y2="PT + PLOT_H"
|
|
stroke="var(--color-primary)"
|
|
stroke-width="0.5"
|
|
/>
|
|
<line
|
|
:x1="PL"
|
|
:y1="PT + PLOT_H"
|
|
:x2="W - PR"
|
|
:y2="PT + PLOT_H"
|
|
stroke="var(--color-primary)"
|
|
stroke-width="0.5"
|
|
/>
|
|
|
|
<!-- Tooltip -->
|
|
<g v-if="hovered !== null && points[hovered]">
|
|
<rect
|
|
:x="Math.min(points[hovered].x + 4, W - 85)"
|
|
:y="points[hovered].y - 20"
|
|
width="82"
|
|
height="32"
|
|
fill="var(--color-surface)"
|
|
:stroke="activeMetric.color"
|
|
stroke-width="0.5"
|
|
rx="1"
|
|
/>
|
|
<text
|
|
:x="Math.min(points[hovered].x + 7, W - 82)"
|
|
:y="points[hovered].y - 6"
|
|
font-size="12"
|
|
fill="var(--color-secondary)"
|
|
font-family="var(--font-heading)"
|
|
>
|
|
{{
|
|
points[hovered].date.toLocaleDateString("en-GB", {
|
|
day: "numeric",
|
|
month: "short",
|
|
year: "2-digit",
|
|
})
|
|
}}
|
|
</text>
|
|
<text
|
|
:x="Math.min(points[hovered].x + 7, W - 82)"
|
|
:y="points[hovered].y + 8"
|
|
font-size="14"
|
|
:fill="activeMetric.color"
|
|
font-family="var(--font-heading)"
|
|
>
|
|
{{ formatValue(metric, points[hovered].value) }}
|
|
</text>
|
|
</g>
|
|
</svg>
|
|
</div>
|
|
|
|
<!-- Summary stats -->
|
|
<div class="flex justify-between text-xs border-t border-quaternary pt-1">
|
|
<div class="flex flex-col items-center">
|
|
<span class="text-primary font-heading">{{ rows.length }}</span>
|
|
<span class="text-quaternary text-[0.6rem]"
|
|
>sessions</span
|
|
>
|
|
</div>
|
|
<div class="flex flex-col items-center">
|
|
<span class="text-primary font-heading"
|
|
>{{
|
|
rows.reduce((s, r) => s + r.distance, 0).toLocaleString()
|
|
}}m</span
|
|
>
|
|
<span class="text-quaternary text-[0.6rem]"
|
|
>total dist</span
|
|
>
|
|
</div>
|
|
<div class="flex flex-col items-center">
|
|
<span class="text-primary font-heading">{{
|
|
formatTime(
|
|
rows.reduce((s, r) => s + r.timePer500m, 0) / (rows.length || 1),
|
|
)
|
|
}}</span>
|
|
<span class="text-quaternary text-[0.6rem]"
|
|
>avg pace</span
|
|
>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.metric-btn {
|
|
cursor: pointer;
|
|
transition:
|
|
background-color 0.15s,
|
|
color 0.15s;
|
|
letter-spacing: 0.03em;
|
|
}
|
|
</style>
|