Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(desktop): let the source filter select several sources (#106644)

Shy Alterd10aa268ba85
diff --git a/products/desktop/packages/core/src/canvas/channelItems.test.ts b/products/desktop/packages/core/src/canvas/channelItems.test.ts
index 6b1c49b3d9f3..da88aa6a2ddf 100644
--- a/products/desktop/packages/core/src/canvas/channelItems.test.ts
+++ b/products/desktop/packages/core/src/canvas/channelItems.test.ts
@@ -10,6 +10,7 @@ import {
DESKTOP_SOURCE,
filterChannelItems,
groupChannelItems,
+ hasActiveChannelItemFilters,
sortChannelItems,
} from "./channelItems";
import type { DashboardRecord } from "./dashboardSchemas";
@@ -398,6 +399,7 @@ describe("filterChannelItems", () => {
model({ id: "local", environment: "local" }),
model({ id: "cloud", environment: "cloud" }),
model({ id: "from-slack", source: "slack" }),
+ model({ id: "from-tracker", source: "error_tracking" }),
model({ id: "quiet" }),
];
@@ -407,7 +409,11 @@ describe("filterChannelItems", () => {
{ filter: { pinned: "pinned" }, kept: ["pinned"] },
{ filter: { environment: "local" }, kept: ["local"] },
{ filter: { environment: "cloud" }, kept: ["cloud"] },
- { filter: { source: "slack" }, kept: ["from-slack"] },
+ { filter: { sources: ["slack"] }, kept: ["from-slack"] },
+ {
+ filter: { sources: ["slack", "error_tracking"] },
+ kept: ["from-slack", "from-tracker"],
+ },
] as const)("keeps only $kept for $filter", ({ filter, kept }) => {
const result = filterChannelItems(CANDIDATES, {
query: "",
@@ -436,6 +442,22 @@ describe("filterChannelItems", () => {
});
});
+describe("hasActiveChannelItemFilters", () => {
+ it.each([
+ { sources: [DESKTOP_SOURCE, "slack"], active: false },
+ { sources: ["slack", DESKTOP_SOURCE], active: false },
+ { sources: ["slack"], active: true },
+ { sources: [], active: true },
+ ])("is $active for sources $sources", ({ sources, active }) => {
+ expect(
+ hasActiveChannelItemFilters(filters({ sources }), {
+ ...DEFAULT_CHANNEL_ITEM_FILTERS,
+ sources: [DESKTOP_SOURCE, "slack"],
+ }),
+ ).toBe(active);
+ });
+});
+
describe("channelItemSources", () => {
it("offers each source once", () => {
const items = [
diff --git a/products/desktop/packages/core/src/canvas/channelItems.ts b/products/desktop/packages/core/src/canvas/channelItems.ts
index 384f5141c710..36a01c5c8baf 100644
--- a/products/desktop/packages/core/src/canvas/channelItems.ts
+++ b/products/desktop/packages/core/src/canvas/channelItems.ts
@@ -224,13 +224,12 @@ export type CreatedByFilter = "anyone" | "me" | "others";
export type AttentionFilter = "any" | "needs_input" | "unread";
export type PinnedFilter = "any" | "pinned";
export type EnvironmentFilter = "any" | ChannelItemEnvironment;
-/** `ANY_SOURCE`, or an `origin_product` key like `slack`. */
-export type SourceFilter = string;
+export type SourceFilter = readonly string[];
export type ChannelItemSort = "recent" | "created" | "alpha";
export type KindFilter = "any" | "task" | "canvas";
-export const ANY_SOURCE = "any";
+export const ANY_SOURCE: SourceFilter = [];
export const DESKTOP_SOURCE = "user_created";
export interface ChannelItemFilters {
@@ -239,7 +238,7 @@ export interface ChannelItemFilters {
attention: AttentionFilter;
pinned: PinnedFilter;
environment: EnvironmentFilter;
- source: SourceFilter;
+ sources: SourceFilter;
}
export const DEFAULT_CHANNEL_ITEM_FILTERS: ChannelItemFilters = {
@@ -248,7 +247,7 @@ export const DEFAULT_CHANNEL_ITEM_FILTERS: ChannelItemFilters = {
attention: "any",
pinned: "any",
environment: "any",
- source: ANY_SOURCE,
+ sources: ANY_SOURCE,
};
/** Newest activity first, which is what a session list is for. */
@@ -286,10 +285,26 @@ export function hasActiveChannelItemFilters(
filters.attention !== defaults.attention ||
filters.pinned !== defaults.pinned ||
filters.environment !== defaults.environment ||
- filters.source !== defaults.source
+ !sameSources(filters.sources, defaults.sources)
);
}
+export function sameSources(a: SourceFilter, b: SourceFilter): boolean {
+ if (a.length !== b.length) return false;
+ const set = new Set(a);
+ return b.every((source) => set.has(source));
+}
+
+export function migrateSourceFilter({
+ source,
+ ...rest
+}: Partial<ChannelItemFilters> & {
+ source?: unknown;
+}): Partial<ChannelItemFilters> {
+ if (typeof source !== "string" || rest.sources) return rest;
+ return { ...rest, sources: source === "any" ? ANY_SOURCE : [source] };
+}
+
/**
* The sources present in a list, so the menu only offers the ones that can
* actually match. Keyed by `origin_product`; sorted so the menu is stable
@@ -346,7 +361,10 @@ export function filterChannelItems(
) {
return false;
}
- if (filters.source !== ANY_SOURCE && item.source !== filters.source) {
+ if (
+ filters.sources.length > 0 &&
+ (item.source === null || !filters.sources.includes(item.source))
+ ) {
return false;
}
return true;
diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.stories.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.stories.tsx
index b6679b1def38..05c3347345e3 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.stories.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.stories.tsx
@@ -98,11 +98,24 @@ export const DesktopDefault: Story = {
args: {
initialFilters: {
...DEFAULT_CHANNEL_ITEM_FILTERS,
- source: DESKTOP_SOURCE,
+ sources: [DESKTOP_SOURCE],
},
defaultFilters: {
...DEFAULT_CHANNEL_ITEM_FILTERS,
- source: DESKTOP_SOURCE,
+ sources: [DESKTOP_SOURCE],
+ },
+ },
+};
+
+export const SeveralSources: Story = {
+ args: {
+ initialFilters: {
+ ...DEFAULT_CHANNEL_ITEM_FILTERS,
+ sources: [DESKTOP_SOURCE, "slack"],
+ },
+ defaultFilters: {
+ ...DEFAULT_CHANNEL_ITEM_FILTERS,
+ sources: [DESKTOP_SOURCE],
},
},
};
diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.tsx
index 28213d95295b..be7aa7ceb9e1 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelFilterMenu.tsx
@@ -11,11 +11,14 @@ import {
type EnvironmentFilter,
type KindFilter,
type PinnedFilter,
+ type SourceFilter,
+ sameSources,
} from "@posthog/core/canvas/channelItems";
import {
Button,
cn,
DropdownMenu,
+ DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuRadioGroup,
@@ -128,6 +131,27 @@ function OptionDot({ tone }: { tone: DotTone }) {
);
}
+function SubmenuTrigger({
+ label,
+ value,
+ narrowed,
+}: {
+ label: string;
+ value: string;
+ narrowed: boolean;
+}) {
+ return (
+ <DropdownMenuSubTrigger className="pr-1">
+ <span>{label}</span>
+ <span
+ className={`flex-1 pl-4 text-right ${narrowed ? "text-primary" : "text-muted-foreground/80"}`}
+ >
+ {value}
+ </span>
+ </DropdownMenuSubTrigger>
+ );
+}
+
/**
* One filter as a submenu: its name, the choice currently in force, and the
* radio group behind it. A group per submenu keeps the top level a list of
@@ -153,14 +177,11 @@ function FilterSubmenu<T extends string>({
return (
<DropdownMenuSub>
- <DropdownMenuSubTrigger className="pr-1">
- <span>{label}</span>
- <span
- className={`flex-1 pl-4 text-right ${narrowed ? "text-primary" : "text-muted-foreground/80"}`}
- >
- {labelOf(options, value)}
- </span>
- </DropdownMenuSubTrigger>
+ <SubmenuTrigger
+ label={label}
+ value={labelOf(options, value)}
+ narrowed={narrowed}
+ />
<DropdownMenuSubContent>
<DropdownMenuRadioGroup
value={value}
@@ -180,6 +201,66 @@ function FilterSubmenu<T extends string>({
);
}
+function sourcesLabel(
+ options: readonly Option<string>[],
+ value: SourceFilter,
+): string {
+ if (value.length === 0) return "Any source";
+ if (value.length > 2) return `${value.length} sources`;
+ return options
+ .filter((option) => value.includes(option.value))
+ .map((option) => option.label)
+ .join(", ");
+}
+
+function SourceSubmenu({
+ options,
+ value,
+ defaultValue,
+ onChange,
+}: {
+ options: readonly Option<string>[];
+ value: SourceFilter;
+ defaultValue: SourceFilter;
+ onChange: (value: SourceFilter) => void;
+}) {
+ return (
+ <DropdownMenuSub>
+ <SubmenuTrigger
+ label="Source"
+ value={sourcesLabel(options, value)}
+ narrowed={!sameSources(value, defaultValue)}
+ />
+ <DropdownMenuSubContent>
+ <DropdownMenuCheckboxItem
+ checked={value.length === 0}
+ closeOnClick={false}
+ onCheckedChange={() => onChange(ANY_SOURCE)}
+ >
+ Any source
+ </DropdownMenuCheckboxItem>
+ <DropdownMenuSeparator />
+ {options.map((option) => (
+ <DropdownMenuCheckboxItem
+ key={option.value}
+ checked={value.includes(option.value)}
+ closeOnClick={false}
+ onCheckedChange={(checked) =>
+ onChange(
+ checked
+ ? [...value, option.value]
+ : value.filter((source) => source !== option.value),
+ )
+ }
+ >
+ {option.label}
+ </DropdownMenuCheckboxItem>
+ ))}
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ );
+}
+
/**
* The sessions list's filters and sort order, behind the funnel button.
*
@@ -242,13 +323,12 @@ export function ChannelFilterMenu({
groupings ?? (showRunFilters ? DEFAULT_GROUPINGS : [])
).map((value) => ({ value, label: GROUPING_LABELS[value] }));
- const sourceOptions: Option<string>[] = [
- { value: ANY_SOURCE, label: "Any source" },
- ...Array.from(new Set([DESKTOP_SOURCE, ...sources])).map((source) => ({
- value: source,
- label: sourceLabel(source),
- })),
- ];
+ const sourceOptions: Option<string>[] = Array.from(
+ new Set([DESKTOP_SOURCE, ...sources]),
+ ).map((source) => ({
+ value: source,
+ label: sourceLabel(source),
+ }));
return (
<DropdownMenu>
@@ -338,12 +418,11 @@ export function ChannelFilterMenu({
defaultValue={defaultFilters.environment}
onChange={(value) => onFilterChange("environment", value)}
/>
- <FilterSubmenu
- label="Source"
+ <SourceSubmenu
options={sourceOptions}
- value={filters.source}
- defaultValue={defaultFilters.source}
- onChange={(value) => onFilterChange("source", value)}
+ value={filters.sources}
+ defaultValue={defaultFilters.sources}
+ onChange={(value) => onFilterChange("sources", value)}
/>
</>
)}
diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelItemsPane.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelItemsPane.tsx
index f57c13006996..4a87301e0a95 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelItemsPane.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelItemsPane.tsx
@@ -160,18 +160,21 @@ export function ChannelItemsPane({
const sources = useMemo(() => channelItemSources(items), [items]);
const filters = useMemo<ChannelItemFilters>(() => {
- const sourceMissing =
- rawFilters.source !== ANY_SOURCE &&
- rawFilters.source !== DESKTOP_SOURCE &&
- !sources.includes(rawFilters.source);
const scoped: ChannelItemFilters = {
...rawFilters,
...(hasMultipleAuthors ? {} : { createdBy: "anyone" as const }),
- ...(sourceMissing ? { source: ANY_SOURCE } : {}),
+ sources: rawFilters.sources.filter(
+ (source) => source === DESKTOP_SOURCE || sources.includes(source),
+ ),
};
return hasRuns
? scoped
- : { ...scoped, attention: "any", environment: "any", source: ANY_SOURCE };
+ : {
+ ...scoped,
+ attention: "any",
+ environment: "any",
+ sources: ANY_SOURCE,
+ };
}, [rawFilters, hasMultipleAuthors, hasRuns, sources]);
const filtersActive = hasActiveChannelItemFilters(
filters,
diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelSidebar.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelSidebar.test.tsx
index 6deffc0ce571..56897dc28a42 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelSidebar.test.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelSidebar.test.tsx
@@ -269,7 +269,12 @@ describe("ChannelSidebar", () => {
await user.click(screen.getByRole("button", { name: "Filter" }));
await user.click(await screen.findByRole("menuitem", { name: /Source/ }));
fireEvent.click(
- await screen.findByRole("menuitemradio", { name: "Slack" }),
+ await screen.findByRole("menuitemcheckbox", { name: "Slack" }),
+ );
+ expect(screen.getByText("Filed from Slack")).toBeInTheDocument();
+ expect(screen.getByText("Started here")).toBeInTheDocument();
+ fireEvent.click(
+ await screen.findByRole("menuitemcheckbox", { name: "Desktop" }),
);
expect(screen.queryByText("Started here")).not.toBeInTheDocument();
@@ -300,7 +305,10 @@ describe("ChannelSidebar", () => {
await user.click(screen.getByRole("button", { name: "Filter" }));
await user.click(await screen.findByRole("menuitem", { name: /Source/ }));
fireEvent.click(
- await screen.findByRole("menuitemradio", { name: "Slack" }),
+ await screen.findByRole("menuitemcheckbox", { name: "Slack" }),
+ );
+ fireEvent.click(
+ await screen.findByRole("menuitemcheckbox", { name: "Desktop" }),
);
expect(screen.queryByText("Started here")).not.toBeInTheDocument();
diff --git a/products/desktop/packages/ui/src/features/canvas/stores/spaceActivityViewStore.ts b/products/desktop/packages/ui/src/features/canvas/stores/spaceActivityViewStore.ts
index 15c36b4613db..0f084e9f397e 100644
--- a/products/desktop/packages/ui/src/features/canvas/stores/spaceActivityViewStore.ts
+++ b/products/desktop/packages/ui/src/features/canvas/stores/spaceActivityViewStore.ts
@@ -4,6 +4,7 @@ import {
type ChannelItemSort,
DEFAULT_CHANNEL_ITEM_FILTERS,
DEFAULT_CHANNEL_ITEM_SORT,
+ migrateSourceFilter,
} from "@posthog/core/canvas/channelItems";
import type { SpaceActivityType } from "@posthog/ui/features/canvas/components/channelFeedDisplay";
import { ALL_SPACE_ACTIVITY_TYPES } from "@posthog/ui/features/canvas/components/channelFeedDisplay";
@@ -53,7 +54,7 @@ export const useSpaceActivityViewStore = create<SpaceActivityViewState>()(
savedTypes && savedTypes.length > 0 ? savedTypes : current.types,
filters: {
...current.filters,
- ...saved?.filters,
+ ...(saved?.filters ? migrateSourceFilter(saved.filters) : {}),
kind: "any",
},
};
diff --git a/products/desktop/packages/ui/src/features/sidebar/sidebarStore.test.ts b/products/desktop/packages/ui/src/features/sidebar/sidebarStore.test.ts
index 2f142272e57d..f7b32848b7cd 100644
--- a/products/desktop/packages/ui/src/features/sidebar/sidebarStore.test.ts
+++ b/products/desktop/packages/ui/src/features/sidebar/sidebarStore.test.ts
@@ -1,5 +1,4 @@
import {
- ANY_SOURCE,
DEFAULT_CHANNEL_ITEM_FILTERS,
DESKTOP_SOURCE,
} from "@posthog/core/canvas/channelItems";
@@ -31,30 +30,34 @@ describe("sidebarStore", () => {
{
label: "the old default",
version: 0,
- source: ANY_SOURCE,
- expected: DESKTOP_SOURCE,
+ source: "any",
+ expected: [DESKTOP_SOURCE],
},
{
label: "an old explicit source",
version: 0,
source: "slack",
- expected: "slack",
+ expected: ["slack"],
},
{
- label: "the new Any source choice",
+ label: "the old Any source choice",
version: 1,
- source: ANY_SOURCE,
- expected: ANY_SOURCE,
+ source: "any",
+ expected: [],
+ },
+ {
+ label: "an old single source",
+ version: 1,
+ source: "slack",
+ expected: ["slack"],
},
])("rehydration preserves $label", async ({ version, source, expected }) => {
+ const { sources: _, ...filters } = DEFAULT_CHANNEL_ITEM_FILTERS;
localStorage.setItem(
"sidebar-storage",
JSON.stringify({
state: {
- channelItemFilters: {
- ...DEFAULT_CHANNEL_ITEM_FILTERS,
- source,
- },
+ channelItemFilters: { ...filters, source },
},
version,
}),
@@ -62,7 +65,9 @@ describe("sidebarStore", () => {
await useSidebarStore.persist.rehydrate();
- expect(useSidebarStore.getState().channelItemFilters.source).toBe(expected);
+ const { channelItemFilters } = useSidebarStore.getState();
+ expect(channelItemFilters.sources).toEqual(expected);
+ expect(channelItemFilters).not.toHaveProperty("source");
localStorage.removeItem("sidebar-storage");
});
});
diff --git a/products/desktop/packages/ui/src/features/sidebar/sidebarStore.ts b/products/desktop/packages/ui/src/features/sidebar/sidebarStore.ts
index baba6299b052..142938218323 100644
--- a/products/desktop/packages/ui/src/features/sidebar/sidebarStore.ts
+++ b/products/desktop/packages/ui/src/features/sidebar/sidebarStore.ts
@@ -1,5 +1,4 @@
import {
- ANY_SOURCE,
type ChannelItemFilters,
type ChannelItemGrouping,
type ChannelItemSort,
@@ -7,6 +6,7 @@ import {
DEFAULT_CHANNEL_ITEM_GROUPING,
DEFAULT_CHANNEL_ITEM_SORT,
DESKTOP_SOURCE,
+ migrateSourceFilter,
} from "@posthog/core/canvas/channelItems";
import { ALL_WORKSPACE_MODES } from "@posthog/core/sidebar/buildSidebarData";
import type { WorkspaceMode } from "@posthog/shared";
@@ -74,7 +74,7 @@ type SidebarStore = SidebarStoreState & SidebarStoreActions;
export const DEFAULT_SIDEBAR_CHANNEL_ITEM_FILTERS: ChannelItemFilters = {
...DEFAULT_CHANNEL_ITEM_FILTERS,
- source: DESKTOP_SOURCE,
+ sources: [DESKTOP_SOURCE],
};
export const useSidebarStore = create<SidebarStore>()(
@@ -167,21 +167,22 @@ export const useSidebarStore = create<SidebarStore>()(
}),
{
name: "sidebar-storage",
- version: 1,
+ version: 2,
migrate: (persisted, version) => {
const state = persisted as {
- channelItemFilters?: Partial<ChannelItemFilters>;
+ channelItemFilters?: Partial<ChannelItemFilters> & {
+ source?: unknown;
+ };
};
- const source = state.channelItemFilters?.source;
- if (version >= 1 || (source !== undefined && source !== ANY_SOURCE)) {
- return state;
- }
+ const saved = state.channelItemFilters;
+ if (version >= 2 || !saved) return state;
+ const filters = migrateSourceFilter(saved);
return {
...state,
- channelItemFilters: {
- ...state.channelItemFilters,
- source: DESKTOP_SOURCE,
- },
+ channelItemFilters:
+ version === 0 && !filters.sources?.length
+ ? { ...filters, sources: [DESKTOP_SOURCE] }
+ : filters,
};
},
partialize: (state) => ({