Skip to content

Commit

Permalink
Merge pull request #78 from tweedegolf/categories-in-frontend
Browse files Browse the repository at this point in the history
Add category filters
  • Loading branch information
tdittr authored Nov 1, 2024
2 parents f2ec0d7 + 1594c94 commit d7b2ae0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
52 changes: 52 additions & 0 deletions backend/src/driver_db/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,60 @@ pub enum Category {
Oled,

// Timer
/// Chips measuring time
Timer,

#[serde(rename = "Timer::RTC")]
/// Clocks that keep track of wall time
///
/// Often allow to measure time with an external battery.
Rtc,
}

impl Category {
/// Get a list of all categories that contain this category
pub fn parents(&self) -> Vec<Self> {
let mut parents = vec![];

let mut me = *self;
while let Some(parent) = me.parent() {
parents.push(parent);
me = parent;
}

parents
}

fn parent(&self) -> Option<Self> {
Some(match self {
Self::Analog => {
return None;
}
Self::Adc => Self::Analog,
Self::Dac => Self::Analog,
Self::Sensor => {
return None;
}
Self::PowerMeter => Self::Sensor,
Self::Accelerometer => Self::Sensor,
Self::Gyroscope => Self::Sensor,
Self::Magnetometer => Self::Sensor,
Self::IoExpander => {
return None;
}
Self::PwmExpander => Self::IoExpander,
Self::Actor => {
return None;
}
Self::MotorController => Self::Actor,
Self::Display => {
return None;
}
Self::Oled => Self::Display,
Self::Timer => {
return None;
}
Self::Rtc => Self::Timer,
})
}
}
5 changes: 4 additions & 1 deletion backend/src/website_db/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ impl From<&[FullCrate]> for Indexes {
let mut has_dev_board = BTreeSet::new();

for (i, krate) in value.iter().enumerate() {
// TODO also match sub categories
for cat in &krate.chip_meta.categories {
category.add(*cat, i);

for parent in cat.parents() {
category.add(parent, i);
}
}

for l in krate.licenses() {
Expand Down
17 changes: 10 additions & 7 deletions driver-db-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@
},
"Category": {
"oneOf": [
{
"type": "string",
"enum": [
"Timer",
"Timer::RTC"
]
},
{
"description": "Devices interacting with analog signals",
"type": "string",
Expand Down Expand Up @@ -124,6 +117,16 @@
"description": "OLED Screens",
"type": "string",
"const": "Display::OLED"
},
{
"description": "Chips measuring time",
"type": "string",
"const": "Timer"
},
{
"description": "Clocks that keep track of wall time\n\n Often allow to measure time with an external battery.",
"type": "string",
"const": "Timer::RTC"
}
]
},
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const t_crates = crates as FullCrate[];
let t_indexes = indexes as Indexes;
let selected_c: number[][] = [];
let selected_d: number[][] = [];
let selected_l: number[][] = [];
let selected_r: number[][] = [];
Expand Down Expand Up @@ -42,7 +43,7 @@
}
}
$: selected_crates = combine_filters(t_crates.length, [selected_d, selected_l, selected_r, selected_i, selected_f]);
$: selected_crates = combine_filters(t_crates.length, [selected_c, selected_d, selected_l, selected_r, selected_i, selected_f]);
</script>

Expand All @@ -53,8 +54,9 @@
<h1>{selected_crates.length} awesome drivers waiting for you!</h1>
<main>
<div class="filters">
<SortFilter name="Sort by" />
<SortFilter name="Sort by"/>
<TextFilter crates={t_crates} bind:selected={selected_f}/>
<Filter name="Categories" values={t_indexes.category} bind:selected={selected_c}/>
<Filter name="Dependencies" values={t_indexes.dependencies} bind:selected={selected_d}/>
<Filter name="Interfaces" values={t_indexes.interfaces} bind:selected={selected_i}/>
<Filter name="👮 License" values={t_indexes.license} bind:selected={selected_l}/>
Expand Down

0 comments on commit d7b2ae0

Please sign in to comment.