Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(templates): searching the templates gallery now filters the template cards (#15897)

Bartosz Majewski85a8cd939068
diff --git a/packages/web/src/app/routes/templates/index.tsx b/packages/web/src/app/routes/templates/index.tsx
index f5b17bcf178..0816c8d9901 100644
--- a/packages/web/src/app/routes/templates/index.tsx
+++ b/packages/web/src/app/routes/templates/index.tsx
@@ -32,8 +32,6 @@ const TemplatesPage = () => {
isShowingOfficialTemplates ? TemplateType.OFFICIAL : TemplateType.CUSTOM,
);
const selectedCategory = category as string;
- const { data: allOfficialTemplates, isLoading: isAllTemplatesLoading } =
- templatesHooks.useAllOfficialTemplates();
const { mutate: createFlow, isPending: isCreateFlowPending } =
flowHooks.useStartFromScratch(UncategorizedFolderId);
@@ -54,14 +52,17 @@ const TemplatesPage = () => {
[navigate],
);
+ const showAllCategories =
+ isShowingOfficialTemplates && selectedCategory === 'All';
+
const templatesByCategory = useMemo(() => {
const grouped: Record<string, Template[]> = {} as Record<
string,
Template[]
>;
- if (isShowingOfficialTemplates) {
- allOfficialTemplates?.forEach((template: Template) => {
+ if (showAllCategories) {
+ templates?.forEach((template: Template) => {
if (template.categories?.length) {
template.categories?.forEach((category: string) => {
if (!grouped[category]) {
@@ -74,24 +75,15 @@ const TemplatesPage = () => {
}
return grouped;
- }, [allOfficialTemplates, isShowingOfficialTemplates]);
+ }, [templates, showAllCategories]);
const categories = useMemo(() => {
return ['All', ...(templateCategories || [])];
}, [templateCategories]);
- const selectedCategoryTemplates = useMemo(() => {
- if (selectedCategory === 'All') {
- return templates || [];
- }
- return templatesByCategory[selectedCategory] || [];
- }, [selectedCategory, templates, templatesByCategory]);
-
- const showLoading =
- isLoading || (isShowingOfficialTemplates && isAllTemplatesLoading);
- const showAllCategories =
- isShowingOfficialTemplates && selectedCategory === 'All';
- const hasTemplates = templates && templates.length > 0;
+ const hasTemplates = showAllCategories
+ ? Object.keys(templatesByCategory).length > 0
+ : templates && templates.length > 0;
const showCategoryTitleForOfficialTemplates =
isShowingOfficialTemplates && selectedCategory !== 'All';
@@ -134,23 +126,25 @@ const TemplatesPage = () => {
)}
</div>
<div className={DASHBOARD_CONTENT_PADDING_X}>
- {!hasTemplates && !showLoading ? (
+ {!hasTemplates && !isLoading ? (
<EmptyTemplatesView />
) : showAllCategories ? (
<AllCategoriesView
templatesByCategory={templatesByCategory}
- categories={categories}
+ categories={categories.filter(
+ (category) => templatesByCategory[category],
+ )}
onCategorySelect={setCategory}
onTemplateSelect={handleTemplateSelect}
- isLoading={showLoading}
+ isLoading={isLoading}
hideHeader={!isShowingOfficialTemplates}
/>
) : (
<SelectedCategoryView
category={selectedCategory}
- templates={selectedCategoryTemplates}
+ templates={templates ?? []}
onTemplateSelect={handleTemplateSelect}
- isLoading={showLoading}
+ isLoading={isLoading}
showCategoryTitle={showCategoryTitleForOfficialTemplates}
/>
)}
diff --git a/packages/web/src/features/templates/hooks/templates-hook.ts b/packages/web/src/features/templates/hooks/templates-hook.ts
index 752bb3b3a4e..cd59ebda464 100644
--- a/packages/web/src/features/templates/hooks/templates-hook.ts
+++ b/packages/web/src/features/templates/hooks/templates-hook.ts
@@ -26,19 +26,6 @@ export const templatesHooks = {
});
},
- useAllOfficialTemplates: () => {
- return useQuery<Template[], Error>({
- queryKey: ['templates', 'all'],
- queryFn: async () => {
- const result = await templatesApi.list({
- type: TemplateType.OFFICIAL,
- });
- return result.data;
- },
- staleTime: 5 * 60 * 1000,
- });
- },
-
useTemplates: (type?: TemplateType) => {
const [searchParams, setSearchParams] = useSearchParams();
diff --git a/packages/web/test/app/routes/templates/templates-page-search.test.tsx b/packages/web/test/app/routes/templates/templates-page-search.test.tsx
new file mode 100644
index 00000000000..5e5946b1d70
--- /dev/null
+++ b/packages/web/test/app/routes/templates/templates-page-search.test.tsx
@@ -0,0 +1,180 @@
+/**
+ * @vitest-environment jsdom
+ */
+/* eslint-disable testing-library/no-unnecessary-act */
+import { Template, TemplateType } from '@activepieces/shared';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import * as React from 'react';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { MemoryRouter } from 'react-router-dom';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const officialTemplates = vi.hoisted(() => [
+ { id: 't1', name: 'Slack alerts', categories: ['Sales'] },
+ { id: 't2', name: 'Gmail digest', categories: ['Sales', 'Marketing'] },
+ { id: 't3', name: 'Slack standup', categories: ['Marketing'] },
+ { id: 't4', name: 'Uncategorized helper', categories: [] as string[] },
+]);
+
+const list = vi.hoisted(() =>
+ vi.fn(
+ async ({ search, category }: { search?: string; category?: string }) => ({
+ data: officialTemplates.filter(
+ (template) =>
+ (!search ||
+ template.name.toLowerCase().includes(search.toLowerCase())) &&
+ (!category || template.categories.includes(category)),
+ ),
+ }),
+ ),
+);
+
+vi.mock('i18next', () => ({ t: (key: string) => key }));
+vi.mock('@/features/templates', async () => {
+ const { templatesHooks } = await import(
+ '@/features/templates/hooks/templates-hook'
+ );
+ return { templatesHooks, templatesTelemetryApi: { sendEvent: vi.fn() } };
+});
+vi.mock('@/features/templates/api/templates-api', () => ({
+ templatesApi: {
+ list,
+ getCategories: async () => ({ value: ['Sales', 'Marketing', 'Legal'] }),
+ },
+}));
+vi.mock('@/hooks/platform-hooks', () => ({
+ platformHooks: {
+ useCurrentPlatform: () => ({
+ platform: { plan: { manageTemplatesEnabled: false } },
+ }),
+ },
+}));
+vi.mock('@/features/flows', () => ({
+ flowHooks: {
+ useStartFromScratch: () => ({ mutate: vi.fn(), isPending: false }),
+ },
+}));
+vi.mock('@/app/routes/templates/category-filter-carousel', () => ({
+ CategoryFilterCarousel: () => null,
+}));
+vi.mock('@/app/routes/templates/all-categories-view', () => ({
+ AllCategoriesView: ({
+ categories,
+ templatesByCategory,
+ }: {
+ categories: string[];
+ templatesByCategory: Record<string, Template[]>;
+ }) => (
+ <>
+ {categories.map((category) => (
+ <div key={category} data-category={category}>
+ {templatesByCategory[category]
+ ?.map((template) => template.name)
+ .join(',')}
+ </div>
+ ))}
+ </>
+ ),
+}));
+vi.mock('@/app/routes/templates/selected-category-view', () => ({
+ SelectedCategoryView: ({ templates }: { templates: Template[] }) => (
+ <div data-selected-category>
+ {templates.map((template) => template.name).join(',')}
+ </div>
+ ),
+}));
+
+const { TemplatesPage } = await import('@/app/routes/templates');
+
+declare global {
+ // eslint-disable-next-line no-var
+ var IS_REACT_ACT_ENVIRONMENT: boolean;
+}
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true;
+
+let container: HTMLDivElement;
+let root: Root;
+
+async function renderPage(url: string) {
+ const queryClient = new QueryClient();
+ await act(async () => {
+ root.render(
+ <QueryClientProvider client={queryClient}>
+ <MemoryRouter initialEntries={[url]}>
+ <TemplatesPage />
+ </MemoryRouter>
+ </QueryClientProvider>,
+ );
+ });
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ });
+}
+
+function shownByCategory(): Record<string, string> {
+ return Object.fromEntries(
+ Array.from(container.querySelectorAll('[data-category]')).map((element) => [
+ element.getAttribute('data-category') ?? '',
+ element.textContent ?? '',
+ ]),
+ );
+}
+
+function emptyStateTitle(): string | null | undefined {
+ return container.querySelector('[data-slot="empty-title"]')?.textContent;
+}
+
+function shownInSelectedCategory(): string | null | undefined {
+ return container.querySelector('[data-selected-category]')?.textContent;
+}
+
+beforeEach(() => {
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+ list.mockClear();
+});
+
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+});
+
+describe('TemplatesPage search on the official gallery', () => {
+ it('shows every official template under All when nothing is searched', async () => {
+ await renderPage('/templates');
+ expect(shownByCategory()).toEqual({
+ Sales: 'Slack alerts,Gmail digest',
+ Marketing: 'Gmail digest,Slack standup',
+ });
+ });
+
+ it('shows only matching templates under All when a search term is typed', async () => {
+ await renderPage('/templates?search=slack');
+ expect(list).toHaveBeenCalledWith(
+ expect.objectContaining({ type: TemplateType.OFFICIAL, search: 'slack' }),
+ );
+ expect(shownByCategory()).toEqual({
+ Sales: 'Slack alerts',
+ Marketing: 'Slack standup',
+ });
+ });
+
+ it('shows only matching templates inside a selected category', async () => {
+ await renderPage('/templates?search=slack&category=Sales');
+ expect(shownInSelectedCategory()).toBe('Slack alerts');
+ });
+
+ it('shows the whole category when nothing is searched', async () => {
+ await renderPage('/templates?category=Sales');
+ expect(shownInSelectedCategory()).toBe('Slack alerts,Gmail digest');
+ });
+
+ it('shows the empty state when the search only matches templates outside every category', async () => {
+ await renderPage('/templates?search=uncategorized');
+ expect(shownByCategory()).toEqual({});
+ expect(emptyStateTitle()).toBe('No templates found');
+ });
+});