Browse Source

apply isKindEnabledFn and isKindEnabledStore

master
limina1 8 months ago
parent
commit
60a73f23c0
  1. 106
      src/lib/stores/visualizationConfig.ts

106
src/lib/stores/visualizationConfig.ts

@ -32,16 +32,13 @@ function createVisualizationConfig() {
searchThroughFetched: true, searchThroughFetched: true,
}; };
const { subscribe, set, update } = const { subscribe, set, update } = writable<VisualizationConfig>(initialConfig);
writable<VisualizationConfig>(initialConfig);
return { function reset() {
subscribe, set(initialConfig);
update, }
reset: () => set(initialConfig),
// Add a new event kind with default limit function addEventKind(kind: number, limit: number = 10) {
addEventKind: (kind: number, limit: number = 10) =>
update((config) => { update((config) => {
// Check if kind already exists // Check if kind already exists
if (config.eventConfigs.some((ec) => ec.kind === kind)) { if (config.eventConfigs.some((ec) => ec.kind === kind)) {
@ -49,91 +46,104 @@ function createVisualizationConfig() {
} }
const newConfig: EventKindConfig = { kind, limit, enabled: true }; const newConfig: EventKindConfig = { kind, limit, enabled: true };
// Add nestedLevels for 30040 // Add nestedLevels for 30040
if (kind === 30040) { if (kind === 30040) {
newConfig.nestedLevels = 1; newConfig.nestedLevels = 1;
} }
// Add depth for kind 3 // Add depth for kind 3
if (kind === 3) { if (kind === 3) {
newConfig.depth = 0; newConfig.depth = 0;
} }
const updated = { return {
...config, ...config,
eventConfigs: [...config.eventConfigs, newConfig], eventConfigs: [...config.eventConfigs, newConfig],
}; };
return updated; });
}), }
// Remove an event kind function removeEventKind(kind: number) {
removeEventKind: (kind: number) => update((config) => ({
update((config) => {
const updated = {
...config, ...config,
eventConfigs: config.eventConfigs.filter((ec) => ec.kind !== kind), eventConfigs: config.eventConfigs.filter((ec) => ec.kind !== kind),
}; }));
return updated; }
}),
// Update limit for a specific kind function updateEventLimit(kind: number, limit: number) {
updateEventLimit: (kind: number, limit: number) =>
update((config) => ({ update((config) => ({
...config, ...config,
eventConfigs: config.eventConfigs.map((ec) => eventConfigs: config.eventConfigs.map((ec) =>
ec.kind === kind ? { ...ec, limit } : ec, ec.kind === kind ? { ...ec, limit } : ec,
), ),
})), }));
}
// Update nested levels for kind 30040 function updateNestedLevels(levels: number) {
updateNestedLevels: (levels: number) =>
update((config) => ({ update((config) => ({
...config, ...config,
eventConfigs: config.eventConfigs.map((ec) => eventConfigs: config.eventConfigs.map((ec) =>
ec.kind === 30040 ? { ...ec, nestedLevels: levels } : ec, ec.kind === 30040 ? { ...ec, nestedLevels: levels } : ec,
), ),
})), }));
}
// Update depth for kind 3 function updateFollowDepth(depth: number) {
updateFollowDepth: (depth: number) =>
update((config) => ({ update((config) => ({
...config, ...config,
eventConfigs: config.eventConfigs.map((ec) => eventConfigs: config.eventConfigs.map((ec) =>
ec.kind === 3 ? { ...ec, depth: depth } : ec, ec.kind === 3 ? { ...ec, depth: depth } : ec,
), ),
})), }));
}
// Toggle showAll for content kinds (30041, 30818) function toggleShowAllContent(kind: number) {
toggleShowAllContent: (kind: number) =>
update((config) => ({ update((config) => ({
...config, ...config,
eventConfigs: config.eventConfigs.map((ec) => eventConfigs: config.eventConfigs.map((ec) =>
ec.kind === kind ? { ...ec, showAll: !ec.showAll } : ec, ec.kind === kind ? { ...ec, showAll: !ec.showAll } : ec,
), ),
})), }));
}
// Get config for a specific kind function getEventConfig(kind: number) {
getEventConfig: (kind: number) => {
let config: EventKindConfig | undefined; let config: EventKindConfig | undefined;
subscribe((c) => { subscribe((c) => {
config = c.eventConfigs.find((ec) => ec.kind === kind); config = c.eventConfigs.find((ec) => ec.kind === kind);
})(); })();
return config; return config;
}, }
toggleSearchThroughFetched: () => function toggleSearchThroughFetched() {
update((config) => ({ update((config) => ({
...config, ...config,
searchThroughFetched: !config.searchThroughFetched, searchThroughFetched: !config.searchThroughFetched,
})), }));
}
// Toggle enabled state for a specific kind function toggleKind(kind: number) {
toggleKind: (kind: number) =>
update((config) => ({ update((config) => ({
...config, ...config,
eventConfigs: config.eventConfigs.map((ec) => eventConfigs: config.eventConfigs.map((ec) =>
ec.kind === kind ? { ...ec, enabled: !ec.enabled } : ec, ec.kind === kind ? { ...ec, enabled: !ec.enabled } : ec,
), ),
})), }));
}
return {
subscribe,
update,
reset,
addEventKind,
removeEventKind,
updateEventLimit,
updateNestedLevels,
updateFollowDepth,
toggleShowAllContent,
getEventConfig,
toggleSearchThroughFetched,
toggleKind,
}; };
} }
@ -146,11 +156,19 @@ export const enabledEventKinds = derived(visualizationConfig, ($config) =>
.map((ec) => ec.kind), .map((ec) => ec.kind),
); );
// Helper to check if a kind is enabled /**
export const isKindEnabled = derived( * Returns true if the given event kind is enabled in the config.
* @param config - The VisualizationConfig object.
* @param kind - The event kind number to check.
*/
export function isKindEnabledFn(config: VisualizationConfig, kind: number): boolean {
const eventConfig = config.eventConfigs.find((ec) => ec.kind === kind);
// If not found, return false. Otherwise, return true unless explicitly disabled.
return !!eventConfig && eventConfig.enabled !== false;
}
// Derived store: returns a function that checks if a kind is enabled in the current config.
export const isKindEnabledStore = derived(
visualizationConfig, visualizationConfig,
($config) => (kind: number) => { ($config) => (kind: number) => isKindEnabledFn($config, kind)
const eventConfig = $config.eventConfigs.find((ec) => ec.kind === kind);
return eventConfig != false;
},
); );

Loading…
Cancel
Save