Fix categories
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
import { browser } from '$app/environment';
|
import { browser } from '$app/environment';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { page } from '$app/stores';
|
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 expandedCategory = '';
|
||||||
let expandedUnits: Record<string, string> = {};
|
let expandedUnits: Record<string, string> = {};
|
||||||
@@ -31,18 +31,21 @@
|
|||||||
conversions: CalculatorDef[];
|
conversions: CalculatorDef[];
|
||||||
};
|
};
|
||||||
|
|
||||||
$: categoryUnitGroups = Object.entries(categories).map(([key, meta]) => {
|
$: categoryUnitGroups = categoryOrder
|
||||||
|
.map(key => {
|
||||||
|
const meta = categories[key];
|
||||||
|
if (!meta) return null;
|
||||||
const buckets = new Map<string, UnitBucket>();
|
const buckets = new Map<string, UnitBucket>();
|
||||||
const calcs = getCalculatorsByCategory(key);
|
const calcs = getCalculatorsByCategory(key);
|
||||||
|
|
||||||
calcs.forEach(calc => {
|
calcs.forEach(calc => {
|
||||||
[calc.labels.in1, calc.labels.in2].forEach(unit => {
|
[calc.labels.in1, calc.labels.in2].forEach(unit => {
|
||||||
const key = unit.toLowerCase();
|
const lower = unit.toLowerCase();
|
||||||
const existing = buckets.get(key);
|
const existing = buckets.get(lower);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
existing.conversions.push(calc);
|
existing.conversions.push(calc);
|
||||||
} else {
|
} else {
|
||||||
buckets.set(key, {
|
buckets.set(lower, {
|
||||||
label: unit,
|
label: unit,
|
||||||
conversions: [calc],
|
conversions: [calc],
|
||||||
});
|
});
|
||||||
@@ -58,7 +61,8 @@
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
return { key, meta, units };
|
return { key, meta, units };
|
||||||
});
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
function toggle(cat: string) {
|
function toggle(cat: string) {
|
||||||
const wasOpen = expandedCategory === cat;
|
const wasOpen = expandedCategory === cat;
|
||||||
|
|||||||
@@ -39,6 +39,29 @@ export const categories: Record<string, { label: string; icon: string }> = {
|
|||||||
other: { label: 'Other', icon: '🔄' },
|
other: { label: 'Other', icon: '🔄' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const categoryOrder = [
|
||||||
|
'length',
|
||||||
|
'weight',
|
||||||
|
'temperature',
|
||||||
|
'volume',
|
||||||
|
'fluids',
|
||||||
|
'area',
|
||||||
|
'speed',
|
||||||
|
'pressure',
|
||||||
|
'energy',
|
||||||
|
'magnetism',
|
||||||
|
'power',
|
||||||
|
'data',
|
||||||
|
'time',
|
||||||
|
'angle',
|
||||||
|
'number-systems',
|
||||||
|
'radiation',
|
||||||
|
'electrical',
|
||||||
|
'force',
|
||||||
|
'light',
|
||||||
|
'other',
|
||||||
|
] as const;
|
||||||
|
|
||||||
export const calculators: CalculatorDef[] = [
|
export const calculators: CalculatorDef[] = [
|
||||||
{"slug": "inches-to-feet", "name": "Inches to Feet", "category": "length", "type": "standard", "teaser": "If a shelf spans 36 inches, how many feet of trim does it cover?", "labels": {"in1": "Inches", "in2": "Feet"}, "factor": 12.0},
|
{"slug": "inches-to-feet", "name": "Inches to Feet", "category": "length", "type": "standard", "teaser": "If a shelf spans 36 inches, how many feet of trim does it cover?", "labels": {"in1": "Inches", "in2": "Feet"}, "factor": 12.0},
|
||||||
{"slug": "kilograms-to-pounds", "name": "Kilograms to Pounds", "category": "weight", "type": "standard", "labels": {"in1": "Kilograms", "in2": "Pounds"}, "factor": 2.20462262},
|
{"slug": "kilograms-to-pounds", "name": "Kilograms to Pounds", "category": "weight", "type": "standard", "labels": {"in1": "Kilograms", "in2": "Pounds"}, "factor": 2.20462262},
|
||||||
@@ -1319,11 +1342,14 @@ export function getCalculatorsByCategory(category: string): CalculatorDef[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getCategoriesWithCounts(): { key: string; label: string; icon: string; count: number }[] {
|
export function getCategoriesWithCounts(): { key: string; label: string; icon: string; count: number }[] {
|
||||||
return Object.entries(categories).map(([key, meta]) => ({
|
return categoryOrder
|
||||||
key,
|
.map(key => {
|
||||||
...meta,
|
const meta = categories[key];
|
||||||
count: calculators.filter(c => c.category === key && !c.hidden).length,
|
if (!meta) return null;
|
||||||
}));
|
const count = calculators.filter(c => c.category === key && !c.hidden).length;
|
||||||
|
return { key, ...meta, count };
|
||||||
|
})
|
||||||
|
.filter(Boolean) as { key: string; label: string; icon: string; count: number }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function searchCalculators(query: string): CalculatorDef[] {
|
export function searchCalculators(query: string): CalculatorDef[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user