Files
HealthLog/src/lib/medications/medication-order.ts
T
Marc-André Bombeck e21dfd040e feat(medications): a sortable table view with an order you choose
The medications page gains a compact table beside the card view — one
row per medication with the same served-due status pill, next-intake
line, cadence-scaled adherence bars, dose-derived stock and the take
and skip actions the cards use, all from the payloads the cards
already read; the list response aggregates stock in the same round
trip. Columns sort tri-state with nulls last, the default order is
yours to arrange — the same drag dialog the dashboard and insights
use, applied to both views — and the chosen view and order persist
per user on their own settings column with field-scoped writes, so
toggling the view never touches the order. Inactive medications pin
to the end, the first column stays put under horizontal scroll, and
the loading skeleton follows the view you left.
2026-06-12 18:10:21 +02:00

38 lines
1.3 KiB
TypeScript

/**
* v1.16.10 — the user-defined manual medication order, applied to BOTH
* /medications views (cards and table).
*
* The persisted layout carries an ordered list of medication ids
* (`MedicationListLayout.order`). Medications named there render first,
* in that order; every other medication (newly created since the last
* save, or never ordered) appends after the ordered block in the
* alphabetical default the list always used. Ids that no longer resolve
* to a medication are simply ignored — a deleted medication must not
* corrupt the order.
*/
/** The default tiebreak the list used before manual ordering existed. */
export function compareMedicationNames(
a: { name: string },
b: { name: string },
): number {
return a.name.localeCompare(b.name, "de", { sensitivity: "base" });
}
export function applyMedicationOrder<T extends { id: string; name: string }>(
medications: readonly T[],
order: readonly string[],
): T[] {
const position = new Map<string, number>();
order.forEach((id, index) => {
if (!position.has(id)) position.set(id, index);
});
const ordered = medications
.filter((m) => position.has(m.id))
.sort((a, b) => position.get(a.id)! - position.get(b.id)!);
const rest = medications
.filter((m) => !position.has(m.id))
.sort(compareMedicationNames);
return [...ordered, ...rest];
}