refactor(createFilter): createFilterStore rewrote to runes

This commit is contained in:
Ilia Mashkov
2026-01-07 16:52:17 +03:00
parent d15b90cfcb
commit baff3b9e27
4 changed files with 4 additions and 164 deletions
@@ -1,120 +0,0 @@
import { SvelteSet } from 'svelte/reactivity';
export interface Property {
/**
* Property identifier
*/
id: string;
/**
* Property name
*/
name: string;
/**
* Property selected state
*/
selected?: boolean;
}
export interface FilterModel {
/**
* Search query
*/
searchQuery?: string;
/**
* Properties
*/
properties: Property[];
}
/**
* Create a filter store.
* @param initialState - Initial state of the filter store
*/
export function createFilter<T extends FilterModel>(
initialState: T,
) {
let properties = $state(
initialState.properties.map(p => ({
...p,
selected: p.selected ?? false,
})),
);
const selectedProperties = $derived.by(() => {
const _ = properties;
return properties.filter(p => p.selected);
});
const selectedCount = $derived.by(() => {
const _ = properties;
return selectedProperties.length;
});
return {
/**
* Get all properties.
*/
get properties() {
return properties;
},
/**
* Get selected properties.
*/
get selectedProperties() {
return selectedProperties;
},
/**
* Get selected count.
*/
get selectedCount() {
return selectedCount;
},
/**
* Toggle property selection.
*/
toggleProperty: (id: string) => {
properties = properties.map(p => ({
...p,
selected: p.id === id ? !p.selected : p.selected,
}));
},
/**
* Select property.
*/
selectProperty(id: string) {
properties = properties.map(p => ({
...p,
selected: p.id === id ? true : p.selected,
}));
},
/**
* Deselect property.
*/
deselectProperty(id: string) {
properties = properties.map(p => ({
...p,
selected: p.id === id ? false : p.selected,
}));
},
/**
* Select all properties.
*/
selectAll: () => {
properties = properties.map(p => ({
...p,
selected: true,
}));
},
/**
* Deselect all properties.
*/
deselectAll: () => {
properties = properties.map(p => ({
...p,
selected: false,
}));
},
};
}
export type Filter = ReturnType<typeof createFilter>;