Fix categories
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
import { browser } from '$app/environment';
|
||||
import { onMount } from 'svelte';
|
||||
import { page } from '$app/stores';
|
||||
import { categories, getCalculatorsByCategory, type CalculatorDef } from '$lib/data/calculators';
|
||||
import { categories, categoryOrder, getCalculatorsByCategory, type CalculatorDef } from '$lib/data/calculators';
|
||||
|
||||
let expandedCategory = '';
|
||||
let expandedUnits: Record<string, string> = {};
|
||||
@@ -31,34 +31,38 @@
|
||||
conversions: CalculatorDef[];
|
||||
};
|
||||
|
||||
$: categoryUnitGroups = Object.entries(categories).map(([key, meta]) => {
|
||||
const buckets = new Map<string, UnitBucket>();
|
||||
const calcs = getCalculatorsByCategory(key);
|
||||
$: categoryUnitGroups = categoryOrder
|
||||
.map(key => {
|
||||
const meta = categories[key];
|
||||
if (!meta) return null;
|
||||
const buckets = new Map<string, UnitBucket>();
|
||||
const calcs = getCalculatorsByCategory(key);
|
||||
|
||||
calcs.forEach(calc => {
|
||||
[calc.labels.in1, calc.labels.in2].forEach(unit => {
|
||||
const key = unit.toLowerCase();
|
||||
const existing = buckets.get(key);
|
||||
if (existing) {
|
||||
existing.conversions.push(calc);
|
||||
} else {
|
||||
buckets.set(key, {
|
||||
label: unit,
|
||||
conversions: [calc],
|
||||
});
|
||||
}
|
||||
calcs.forEach(calc => {
|
||||
[calc.labels.in1, calc.labels.in2].forEach(unit => {
|
||||
const lower = unit.toLowerCase();
|
||||
const existing = buckets.get(lower);
|
||||
if (existing) {
|
||||
existing.conversions.push(calc);
|
||||
} else {
|
||||
buckets.set(lower, {
|
||||
label: unit,
|
||||
conversions: [calc],
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const units = [...buckets.entries()]
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([, bucket]) => ({
|
||||
label: bucket.label,
|
||||
conversions: bucket.conversions.slice().sort((a, b) => a.name.localeCompare(b.name)),
|
||||
}));
|
||||
const units = [...buckets.entries()]
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([, bucket]) => ({
|
||||
label: bucket.label,
|
||||
conversions: bucket.conversions.slice().sort((a, b) => a.name.localeCompare(b.name)),
|
||||
}));
|
||||
|
||||
return { key, meta, units };
|
||||
});
|
||||
return { key, meta, units };
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
function toggle(cat: string) {
|
||||
const wasOpen = expandedCategory === cat;
|
||||
|
||||
Reference in New Issue
Block a user