diff --git a/ui/src/pages/apps/Browse.tsx b/ui/src/pages/apps/Browse.tsx index f62a97da27c..09eeaabe081 100644 --- a/ui/src/pages/apps/Browse.tsx +++ b/ui/src/pages/apps/Browse.tsx @@ -64,6 +64,7 @@ import { useMemoryConnectorsEnabled } from "@/hooks/useMemoryConnectorsEnabled"; import { useConnectorAvailability } from "@/hooks/useConnectorAvailability"; import { useCloudInstance } from "@/hooks/useCloudInstance"; import { appCopyFor } from "@/lib/app-gallery-copy"; +import { RequestPluginDialog } from "./RequestPluginDialog"; import { useCompany } from "@/context/CompanyContext"; import { useBreadcrumbs } from "@/context/BreadcrumbContext"; import { useToast } from "@/context/ToastContext"; @@ -1209,7 +1210,10 @@ export function Browse({ renderAccountDetails = (connection) => connection.conne ) : nothingMatches && !pluginKey ? (

No connectors match “{query.trim()}”.

- +
+ + +
) : ( openRow ? ( diff --git a/ui/src/pages/apps/RequestPluginDialog.test.tsx b/ui/src/pages/apps/RequestPluginDialog.test.tsx new file mode 100644 index 00000000000..47bd7d7c7ea --- /dev/null +++ b/ui/src/pages/apps/RequestPluginDialog.test.tsx @@ -0,0 +1,57 @@ +// @vitest-environment jsdom +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { expect, it, vi } from "vitest"; +import { resetProductAnalyticsForTest, startProductAnalytics } from "@/lib/analytics/product-analytics"; +import { RequestPluginDialog } from "./RequestPluginDialog"; + +const createIssue = vi.hoisted(() => vi.fn()); +vi.mock("@/api/issues", () => ({ issuesApi: { create: createIssue } })); +vi.mock("@/context/CompanyContext", () => ({ useCompany: () => ({ selectedCompanyId: "local-qa" }) })); + +it("captures one plugin request after the issue is saved, with the submitted flags", async () => { + vi.stubGlobal("ResizeObserver", class { observe() {} unobserve() {} disconnect() {} }); + const capture = vi.fn(); + startProductAnalytics({ token: "local", apiHost: "/_ph", uiHost: null, environment: "test", stackId: "local", replayEnabled: false }, { + load: async () => ({ init: vi.fn(), register: vi.fn(), identify: vi.fn(), reset: vi.fn(), group: vi.fn(), capture }), + schedule: (start) => { start(); return () => {}; }, + }); + let finishSave!: (issue: { identifier: string }) => void; + createIssue.mockReturnValue(new Promise(resolve => { finishSave = resolve; })); + const client = new QueryClient(); + const container = document.body.appendChild(document.createElement("div")); + const root = createRoot(container); + try { + await act(async () => root.render()); + await act(async () => container.querySelector("button")!.click()); + const form = document.querySelector("form")!; + expect(form.querySelector('[name="name"]')!.value).toBe("QA plugin"); + form.querySelector('[name="url"]')!.value = "https://example.com"; + await act(async () => form.querySelector('[role="checkbox"]')!.click()); + form.querySelector('[name="email"]')!.value = "qa@example.com"; + await act(async () => form.querySelector('[type="submit"]')!.click()); + expect(createIssue).toHaveBeenCalledTimes(1); + expect(createIssue).toHaveBeenCalledWith("local-qa", expect.objectContaining({ + allowDuplicate: true, + idempotencyKey: expect.any(String), + description: "Website: https://example.com\n\nEmail me when available: qa@example.com", + })); + expect(capture).not.toHaveBeenCalled(); + await act(async () => { + finishSave({ identifier: "QA-1" }); + await vi.waitFor(() => expect(capture).toHaveBeenCalledTimes(1)); + }); + expect(document.body.textContent).toContain("Request submitted"); + expect(capture).toHaveBeenCalledTimes(1); + expect(capture).toHaveBeenCalledWith("plugin_request_submitted", { + source: "plugins_empty_search", has_website: "true", notification_opt_in: "true", + }, expect.anything()); + } finally { + await act(async () => root.unmount()); + container.remove(); + client.clear(); + resetProductAnalyticsForTest(); + vi.unstubAllGlobals(); + } +}); diff --git a/ui/src/pages/apps/RequestPluginDialog.tsx b/ui/src/pages/apps/RequestPluginDialog.tsx new file mode 100644 index 00000000000..3ea97248005 --- /dev/null +++ b/ui/src/pages/apps/RequestPluginDialog.tsx @@ -0,0 +1,119 @@ +import { useState } from "react"; +import { Button } from "@/components/ui/button"; +import { Checkbox } from "@/components/ui/checkbox"; +import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { createUuid } from "@/lib/uuid"; +import { describeError } from "@/api/errors"; +import { issuesApi } from "@/api/issues"; +import { useCompany } from "@/context/CompanyContext"; +import { queryKeys } from "@/lib/queryKeys"; +import { ANALYTICS_MASK_CLASS } from "@/lib/analytics/privacy"; +import { captureProductAnalyticsEvent } from "@/lib/analytics/product-analytics"; + +interface PluginRequestSubmission { + idempotencyKey: string; + companyId: string; + title: string; + description: string; + hasWebsite: boolean; + notificationOptIn: boolean; +} + +export function RequestPluginDialog({ searchedName }: { searchedName: string }) { + const { selectedCompanyId } = useCompany(); + const queryClient = useQueryClient(); + const submission = useMutation({ + mutationFn: ({ companyId, title, description, idempotencyKey }: PluginRequestSubmission) => + issuesApi.create(companyId, { title, description, status: "backlog", allowDuplicate: true, idempotencyKey }), + onSuccess: (_, { companyId, hasWebsite, notificationOptIn }) => { + captureProductAnalyticsEvent("plugin_request_submitted", { + source: "plugins_empty_search", + has_website: String(hasWebsite), + notification_opt_in: String(notificationOptIn), + }); + void queryClient.invalidateQueries({ queryKey: queryKeys.issues.list(companyId) }); + }, + }); + const [open, setOpen] = useState(false); + const [notify, setNotify] = useState(false); + + return ( + { + if (submission.isPending) return; + if (next) { + submission.reset(); + setNotify(false); + } + setOpen(next); + }}> + + + {submission.isSuccess ? <> + + Request submitted + Your request was saved as company task {submission.data.identifier}. + + + : <> + + Request a plugin or tool + Your request will be saved as a company task. + +
{ + event.preventDefault(); + if (!selectedCompanyId || submission.isPending) return; + const data = new FormData(event.currentTarget); + const name = String(data.get("name") ?? "").trim(); + const url = String(data.get("url") ?? "").trim(); + const description = [ + url && `Website: ${url}`, + notify && `Email me when available: ${data.get("email")}`, + ].filter(Boolean).join("\n\n"); + const title = name ? `Request plugin/tool: ${name}` : "Request plugin/tool"; + const previous = submission.variables; + const idempotencyKey = previous?.companyId === selectedCompanyId + && previous.title === title && previous.description === description + ? previous.idempotencyKey : createUuid(); + submission.mutate({ + idempotencyKey, + companyId: selectedCompanyId, + title, + description, + hasWebsite: Boolean(url), + notificationOptIn: notify, + }); + }}> +
+ + +
+
+ + +
+
+ setNotify(checked === true)} /> + +
+ {notify &&
+ + +
} + {submission.error &&

+ Couldn’t submit your request. {describeError(submission.error).body} +

} + + + + +
+ } +
+
+ ); +}