Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unified actions selectors #2749

Merged
merged 24 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 113 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@playwright/test": "^1.25.0",
"@sveltejs/adapter-static": "^1.0.0",
"@sveltejs/kit": "^1.5.0",
"@tanstack/svelte-query": "^4.27.0",
"@rilldata/svelte-query": "^4.29.20-0.0.1",
"@tanstack/svelte-query": "npm:@rilldata/[email protected]",
"@types/cookie": "^0.5.1",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
Expand Down
3 changes: 2 additions & 1 deletion web-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@storybook/svelte": "^7.0.18",
"@storybook/svelte-vite": "^7.0.18",
"@storybook/testing-library": "^0.0.14-next.2",
"@tanstack/svelte-query": "^4.27.0",
"@rilldata/svelte-query": "^4.29.20-0.0.1",
"@tanstack/svelte-query": "npm:@rilldata/[email protected]",
"@testing-library/svelte": "^3.2.2",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
Expand Down
113 changes: 113 additions & 0 deletions web-common/src/features/dashboards/actions/index.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { get } from "svelte/store";
import type { StateManagers } from "../state-managers/state-managers";
import { cancelDashboardQueries } from "../dashboard-queries";
import { removeIfExists } from "@rilldata/web-common/lib/arrayUtils";

export function clearFilterForDimension(
ctx: StateManagers,
dimensionId: string,
include: boolean
) {
const metricViewName = get(ctx.metricsViewName);
cancelDashboardQueries(ctx.queryClient, metricViewName);
ctx.updateDashboard((dashboard) => {
if (include) {
removeIfExists(
dashboard.filters.include,
(dimensionValues) => dimensionValues.name === dimensionId
);
} else {
removeIfExists(
dashboard.filters.exclude,
(dimensionValues) => dimensionValues.name === dimensionId
);
}
});
}

export function clearAllFilters(ctx: StateManagers) {
const filters = get(ctx.dashboardStore).filters;
const hasFilters =
(filters && filters.include.length > 0) || filters.exclude.length > 0;
const metricViewName = get(ctx.metricsViewName);
if (hasFilters) {
cancelDashboardQueries(ctx.queryClient, metricViewName);
ctx.updateDashboard((dashboard) => {
dashboard.filters.include = [];
dashboard.filters.exclude = [];
dashboard.dimensionFilterExcludeMode.clear();
});
}
}

export function toggleDimensionValue(
ctx: StateManagers,
dimensionName: string,
dimensionValue: string
) {
const metricViewName = get(ctx.metricsViewName);
cancelDashboardQueries(ctx.queryClient, metricViewName);

ctx.updateDashboard((dashboard) => {
const relevantFilterKey = dashboard.dimensionFilterExcludeMode.get(
dimensionName
)
? "exclude"
: "include";

const dimensionEntryIndex = dashboard.filters[relevantFilterKey].findIndex(
(filter) => filter.name === dimensionName
);

if (dimensionEntryIndex >= 0) {
if (
removeIfExists(
dashboard.filters[relevantFilterKey][dimensionEntryIndex].in,
(value) => value === dimensionValue
)
) {
if (
dashboard.filters[relevantFilterKey][dimensionEntryIndex].in
.length === 0
) {
dashboard.filters[relevantFilterKey].splice(dimensionEntryIndex, 1);
}
return;
}

dashboard.filters[relevantFilterKey][dimensionEntryIndex].in.push(
dimensionValue
);
} else {
dashboard.filters[relevantFilterKey].push({
name: dimensionName,
in: [dimensionValue],
});
}
});
}

export function toggleFilterMode(ctx: StateManagers, dimensionName: string) {
const metricViewName = get(ctx.metricsViewName);
cancelDashboardQueries(ctx.queryClient, metricViewName);
ctx.updateDashboard((dashboard) => {
const exclude = dashboard.dimensionFilterExcludeMode.get(dimensionName);
dashboard.dimensionFilterExcludeMode.set(dimensionName, !exclude);

const relevantFilterKey = exclude ? "exclude" : "include";
const otherFilterKey = exclude ? "include" : "exclude";

const otherFilterEntryIndex = dashboard.filters[
relevantFilterKey
].findIndex((filter) => filter.name === dimensionName);
// if relevant filter is not present then return
if (otherFilterEntryIndex === -1) return;

// push relevant filters to other filter
dashboard.filters[otherFilterKey].push(
dashboard.filters[relevantFilterKey][otherFilterEntryIndex]
);
// remove entry from relevant filter
dashboard.filters[relevantFilterKey].splice(otherFilterEntryIndex, 1);
});
}
Loading