Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor FunnelForm and GoalFormModal components to integrate input suggestions

Bill Yang2a885ae34b03
diff --git a/client/src/app/[site]/funnels/components/FunnelForm.tsx b/client/src/app/[site]/funnels/components/FunnelForm.tsx
index 6c5daa233..0d253a1a4 100644
--- a/client/src/app/[site]/funnels/components/FunnelForm.tsx
+++ b/client/src/app/[site]/funnels/components/FunnelForm.tsx
@@ -2,13 +2,8 @@ import { Time } from "@/components/DateSelector/types";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
+import { InputWithSuggestions, SuggestionOption } from "@/components/ui/input-with-suggestions";
+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { ListFilterPlus, Plus, Save, Trash2 } from "lucide-react";
import { useState } from "react";
import { ThreeDotLoader } from "../../../../components/Loaders";
@@ -17,10 +12,8 @@ import { FilterComponent } from "../../components/shared/Filters/FilterComponent
import { Funnel } from "./Funnel";
import { Switch } from "../../../../components/ui/switch";
import { Label } from "../../../../components/ui/label";
-import {
- FunnelResponse,
- FunnelStep,
-} from "../../../../api/analytics/funnels/useGetFunnel";
+import { FunnelResponse, FunnelStep } from "../../../../api/analytics/funnels/useGetFunnel";
+import { useSingleCol } from "../../../../api/analytics/useSingleCol";
interface FunnelFormProps {
name: string;
@@ -66,11 +59,35 @@ export function FunnelForm({
const [showFilters, setShowFilters] = useState(filters.length > 0);
// State to track which event steps have property filtering enabled
const [useProperties, setUseProperties] = useState<boolean[]>(() =>
- steps.map(
- (step) => !!step.eventPropertyKey && step.eventPropertyValue !== undefined
- )
+ steps.map((step) => !!step.eventPropertyKey && step.eventPropertyValue !== undefined)
);
+ // Fetch suggestions for paths and events
+ const { data: pathsData } = useSingleCol({
+ parameter: "pathname",
+ limit: 1000,
+ useFilters: false,
+ });
+
+ const { data: eventsData } = useSingleCol({
+ parameter: "event_name",
+ limit: 1000,
+ useFilters: false,
+ });
+
+ // Transform data into SuggestionOption format
+ const pathSuggestions: SuggestionOption[] =
+ pathsData?.data?.map((item) => ({
+ value: item.value,
+ label: item.value,
+ })) || [];
+
+ const eventSuggestions: SuggestionOption[] =
+ eventsData?.data?.map((item) => ({
+ value: item.value,
+ label: item.value,
+ })) || [];
+
// Handle adding a new step
const addStep = () => {
setSteps([...steps, { type: "page", value: "", name: "" }]);
@@ -90,11 +107,7 @@ export function FunnelForm({
};
// Handle step input changes
- const updateStep = (
- index: number,
- field: keyof FunnelStep,
- value: string
- ) => {
+ const updateStep = (index: number, field: keyof FunnelStep, value: string) => {
const newSteps = [...steps];
newSteps[index] = { ...newSteps[index], [field]: value };
setSteps(newSteps);
@@ -107,9 +120,7 @@ export function FunnelForm({
...newSteps[index],
type,
// Clear property fields if switching from event to page
- ...(type === "page"
- ? { eventPropertyKey: undefined, eventPropertyValue: undefined }
- : {}),
+ ...(type === "page" ? { eventPropertyKey: undefined, eventPropertyValue: undefined } : {}),
};
setSteps(newSteps);
@@ -167,14 +178,7 @@ export function FunnelForm({
let funnelArea = null;
if (funnelData && funnelData.length) {
funnelArea = (
- <Funnel
- data={funnelData}
- isError={isError}
- error={error}
- isPending={isPending}
- time={time}
- setTime={setTime}
- />
+ <Funnel data={funnelData} isError={isError} error={error} isPending={isPending} time={time} setTime={setTime} />
);
}
@@ -184,8 +188,7 @@ export function FunnelForm({
<div className="text-center p-6">
<div className="text-lg font-medium mb-2">Funnel Preview</div>
<p className="text-sm text-neutral-500">
- Configure your funnel steps and click "Query Funnel" to preview
- results
+ Configure your funnel steps and click "Query Funnel" to preview results
</p>
</div>
</div>
@@ -206,22 +209,19 @@ export function FunnelForm({
{/* Left side: Funnel configuration form */}
<div className="space-y-4">
<div>
- <label className="text-sm font-medium mb-1 block">
- Funnel Name
- </label>
- <Input
- value={name}
- onChange={(e) => setName(e.target.value)}
- placeholder="Enter funnel name"
- />
+ <label className="text-sm font-medium mb-1 block">Funnel Name</label>
+ <Input value={name} onChange={(e) => setName(e.target.value)} placeholder="Enter funnel name" />
</div>
{/* Funnel Steps in a boxed container */}
<Card className="border border-neutral-200 dark:border-neutral-800">
- <CardHeader className="p-3">
+ <CardHeader className="p-3 flex flex-row justify-between items-center">
<CardTitle className="text-base">Funnel Steps</CardTitle>
+ <Button onClick={addStep}>
+ <Plus className="mr-2 h-4 w-4" /> Add Step
+ </Button>
</CardHeader>
- <CardContent className="p-3 space-y-4">
+ <CardContent className="p-3 space-y-4 max-h-[calc(100vh-440px)] overflow-y-auto">
{steps.map((step, index) => (
<div
key={index}
@@ -233,9 +233,7 @@ export function FunnelForm({
</div>
<Select
value={step.type}
- onValueChange={(value) =>
- updateStepType(index, value as "page" | "event")
- }
+ onValueChange={(value) => updateStepType(index, value as "page" | "event")}
>
<SelectTrigger className="min-w-[80px] max-w-[80px] dark:border-neutral-700">
<SelectValue placeholder="Type" />
@@ -248,22 +246,16 @@ export function FunnelForm({
<div className="flex-grow space-y-2">
<div>
- <Input
- placeholder={
- step.type === "page"
- ? "Path (e.g. /pricing)"
- : "Event name"
- }
+ <InputWithSuggestions
+ suggestions={step.type === "page" ? pathSuggestions : eventSuggestions}
+ placeholder={step.type === "page" ? "Path (e.g. /pricing)" : "Event name"}
value={step.value}
className="dark:border-neutral-700"
- onChange={(e) =>
- updateStep(index, "value", e.target.value)
- }
+ onChange={(e) => updateStep(index, "value", e.target.value)}
/>
{step.type === "page" && (
<div className="text-xs text-neutral-500 mt-1">
- Use * to match a single path segment (e.g., /blog/*)
- or ** to match multiple segments (e.g.,
+ Use * to match a single path segment (e.g., /blog/*) or ** to match multiple segments (e.g.,
/docs/**/intro)
</div>
)}
@@ -272,9 +264,7 @@ export function FunnelForm({
placeholder="Label (optional)"
className="dark:border-neutral-700"
value={step.name || ""}
- onChange={(e) =>
- updateStep(index, "name", e.target.value)
- }
+ onChange={(e) => updateStep(index, "name", e.target.value)}
/>
{/* Property filtering for event steps */}
@@ -283,14 +273,10 @@ export function FunnelForm({
<div className="flex items-center space-x-2">
<Switch
checked={useProperties[index]}
- onCheckedChange={(checked) =>
- togglePropertyFiltering(index, checked)
- }
+ onCheckedChange={(checked) => togglePropertyFiltering(index, checked)}
id={`use-properties-${index}`}
/>
- <Label htmlFor={`use-properties-${index}`}>
- Filter by event property
- </Label>
+ <Label htmlFor={`use-properties-${index}`}>Filter by event property</Label>
</div>
{useProperties[index] && (
@@ -299,29 +285,13 @@ export function FunnelForm({
placeholder="Property key"
className="dark:border-neutral-700"
value={step.eventPropertyKey || ""}
- onChange={(e) =>
- updateStep(
- index,
- "eventPropertyKey",
- e.target.value
- )
- }
+ onChange={(e) => updateStep(index, "eventPropertyKey", e.target.value)}
/>
<Input
placeholder="Property value"
className="dark:border-neutral-700"
- value={
- step.eventPropertyValue !== undefined
- ? String(step.eventPropertyValue)
- : ""
- }
- onChange={(e) =>
- updateStep(
- index,
- "eventPropertyValue",
- e.target.value
- )
- }
+ value={step.eventPropertyValue !== undefined ? String(step.eventPropertyValue) : ""}
+ onChange={(e) => updateStep(index, "eventPropertyValue", e.target.value)}
/>
</div>
)}
@@ -329,21 +299,12 @@ export function FunnelForm({
)}
</div>
- <Button
- variant="ghost"
- size="icon"
- onClick={() => removeStep(index)}
- disabled={steps.length <= 2}
- >
+ <Button variant="ghost" size="icon" onClick={() => removeStep(index)} disabled={steps.length <= 2}>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</div>
))}
-
- <Button onClick={addStep} className="w-full">
- <Plus className="mr-2 h-4 w-4" /> Add Step
- </Button>
</CardContent>
</Card>
@@ -362,21 +323,11 @@ export function FunnelForm({
<CardContent className="p-3 space-y-4">
<div className="flex flex-col gap-2">
{filters.map((filter, index) => (
- <FilterComponent
- key={index}
- filter={filter}
- index={index}
- updateFilter={updateFilter}
- />
+ <FilterComponent key={index} filter={filter} index={index} updateFilter={updateFilter} />
))}
</div>
<div className="flex justify-between">
- <Button
- variant="ghost"
- size="sm"
- onClick={addFilter}
- className="gap-1"
- >
+ <Button variant="ghost" size="sm" onClick={addFilter} className="gap-1">
<Plus className="w-3 h-3" />
Add Filter
</Button>
@@ -392,13 +343,9 @@ export function FunnelForm({
<div className="text-sm text-red-500">
{(() => {
if (isError) {
- return error instanceof Error
- ? error.message
- : "An error occurred";
+ return error instanceof Error ? error.message : "An error occurred";
} else if (saveError) {
- return saveError instanceof Error
- ? saveError.message
- : "An error occurred while saving";
+ return saveError instanceof Error ? saveError.message : "An error occurred while saving";
}
return null;
})()}
diff --git a/client/src/app/[site]/goals/components/GoalFormModal.tsx b/client/src/app/[site]/goals/components/GoalFormModal.tsx
index 2e26293ab..9959eebb3 100644
--- a/client/src/app/[site]/goals/components/GoalFormModal.tsx
+++ b/client/src/app/[site]/goals/components/GoalFormModal.tsx
@@ -16,19 +16,14 @@ import {
DialogTitle,
DialogTrigger,
} from "../../../../components/ui/dialog";
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "../../../../components/ui/form";
+import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "../../../../components/ui/form";
import { Input } from "../../../../components/ui/input";
import { Label } from "../../../../components/ui/label";
import { Switch } from "../../../../components/ui/switch";
import { cn } from "../../../../lib/utils";
import { useUpdateGoal } from "../../../../api/analytics/goals/useUpdateGoal";
+import { InputWithSuggestions, SuggestionOption } from "../../../../components/ui/input-with-suggestions";
+import { useSingleCol } from "../../../../api/analytics/useSingleCol";
// Define form schema
const formSchema = z
@@ -65,22 +60,42 @@ interface GoalFormModalProps {
trigger: React.ReactNode;
}
-export default function GoalFormModal({
- siteId,
- goal,
- trigger,
-}: GoalFormModalProps) {
+export default function GoalFormModal({ siteId, goal, trigger }: GoalFormModalProps) {
const [isOpen, setIsOpen] = useState(false);
const [useProperties, setUseProperties] = useState(
!!goal?.config.eventPropertyKey && !!goal?.config.eventPropertyValue
);
+ // Fetch suggestions for paths and events
+ const { data: pathsData } = useSingleCol({
+ parameter: "pathname",
+ limit: 1000,
+ useFilters: false,
+ });
+
+ const { data: eventsData } = useSingleCol({
+ parameter: "event_name",
+ limit: 1000,
+ useFilters: false,
+ });
+
+ // Transform data into SuggestionOption format
+ const pathSuggestions: SuggestionOption[] =
+ pathsData?.data?.map((item) => ({
+ value: item.value,
+ label: item.value,
+ })) || [];
+
+ const eventSuggestions: SuggestionOption[] =
+ eventsData?.data?.map((item) => ({
+ value: item.value,
+ label: item.value,
+ })) || [];
+
// Reinitialize useProperties when goal changes or modal opens
useEffect(() => {
if (isOpen) {
- setUseProperties(
- !!goal?.config.eventPropertyKey && !!goal?.config.eventPropertyValue
- );
+ setUseProperties(!!goal?.config.eventPropertyKey && !!goal?.config.eventPropertyValue);
}
}, [isOpen, goal?.config.eventPropertyKey, goal?.config.eventPropertyValue]);
@@ -104,9 +119,7 @@ export default function GoalFormModal({
eventName: goal.config.eventName || "",
eventPropertyKey: goal.config.eventPropertyKey || "",
eventPropertyValue:
- goal.config.eventPropertyValue !== undefined
- ? String(goal.config.eventPropertyValue)
- : "",
+ goal.config.eventPropertyValue !== undefined ? String(goal.config.eventPropertyValue) : "",
},
}
: {
@@ -231,9 +244,7 @@ export default function GoalFormModal({
<div className="flex gap-3 mt-1">
<Button
type="button"
- variant={
- field.value === "path" ? "default" : "outline"
- }
+ variant={field.value === "path" ? "default" : "outline"}
className={cn(
"flex-1 flex items-center justify-center gap-2",
field.value === "path" && "border-blue-500"
@@ -245,9 +256,7 @@ export default function GoalFormModal({
</Button>
<Button
type="button"
- variant={
- field.value === "event" ? "default" : "outline"
- }
+ variant={field.value === "event" ? "default" : "outline"}
className={cn(
"flex-1 flex items-center justify-center gap-2",
field.value === "event" && "border-amber-500"
@@ -273,15 +282,15 @@ export default function GoalFormModal({
<FormItem>
<FormLabel>Path Pattern</FormLabel>
<FormControl>
- <Input
+ <InputWithSuggestions
+ suggestions={pathSuggestions}
placeholder="/checkout/complete or /product/*/view"
{...field}
/>
</FormControl>
<FormMessage />
<div className="text-xs text-gray-500 mt-1">
- Use * to match a single path segment. Use ** to match
- across segments.
+ Use * to match a single path segment. Use ** to match across segments.
</div>
</FormItem>
)}
@@ -297,7 +306,8 @@ export default function GoalFormModal({
<FormItem>
<FormLabel>Event Name</FormLabel>
<FormControl>
- <Input
+ <InputWithSuggestions
+ suggestions={eventSuggestions}
placeholder="e.g., sign_up_completed"
{...field}
/>
@@ -309,14 +319,8 @@ export default function GoalFormModal({
<div className="mt-4">
<div className="flex items-center space-x-2 mb-4">
- <Switch
- id="use-properties"
- checked={useProperties}
- onCheckedChange={setUseProperties}
- />
- <Label htmlFor="use-properties">
- Match specific event property
- </Label>
+ <Switch id="use-properties" checked={useProperties} onCheckedChange={setUseProperties} />
+ <Label htmlFor="use-properties">Match specific event property</Label>
</div>
{useProperties && (
@@ -358,16 +362,8 @@ export default function GoalFormModal({
<Button variant="outline" type="button" onClick={onClose}>
Cancel
</Button>
- <Button
- type="submit"
- disabled={createGoal.isPending || updateGoal.isPending}
- variant="success"
- >
- {createGoal.isPending || updateGoal.isPending
- ? "Saving..."
- : isEditMode
- ? "Update"
- : "Create"}
+ <Button type="submit" disabled={createGoal.isPending || updateGoal.isPending} variant="success">
+ {createGoal.isPending || updateGoal.isPending ? "Saving..." : isEditMode ? "Update" : "Create"}
</Button>
</div>
</form>
diff --git a/client/src/components/ui/input-with-suggestions.tsx b/client/src/components/ui/input-with-suggestions.tsx
new file mode 100644
index 000000000..ce9170c76
--- /dev/null
+++ b/client/src/components/ui/input-with-suggestions.tsx
@@ -0,0 +1,94 @@
+"use client";
+
+import * as React from "react";
+import { cn } from "@/lib/utils";
+import { Input } from "@/components/ui/input";
+
+export interface SuggestionOption {
+ value: string;
+ label?: string;
+}
+
+interface InputWithSuggestionsProps extends React.InputHTMLAttributes<HTMLInputElement> {
+ suggestions: SuggestionOption[];
+ onValueChange?: (value: string) => void;
+}
+
+export const InputWithSuggestions = React.forwardRef<
+ HTMLInputElement,
+ InputWithSuggestionsProps
+>(({ suggestions, onValueChange, className, value: controlledValue, ...props }, ref) => {
+ const [open, setOpen] = React.useState(false);
+ const [internalValue, setInternalValue] = React.useState("");
+ const wrapperRef = React.useRef<HTMLDivElement>(null);
+
+ // Use controlled value if provided, otherwise use internal state
+ const value = controlledValue !== undefined ? controlledValue : internalValue;
+
+ const filteredSuggestions = suggestions.filter((suggestion) =>
+ suggestion.value.toLowerCase().includes(String(value).toLowerCase())
+ );
+
+ React.useEffect(() => {
+ const handleClickOutside = (event: MouseEvent) => {
+ if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) {
+ setOpen(false);
+ }
+ };
+
+ document.addEventListener("mousedown", handleClickOutside);
+ return () => document.removeEventListener("mousedown", handleClickOutside);
+ }, []);
+
+ const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+ const newValue = e.target.value;
+ if (controlledValue === undefined) {
+ setInternalValue(newValue);
+ }
+ onValueChange?.(newValue);
+ props.onChange?.(e);
+ setOpen(true);
+ };
+
+ const handleSuggestionClick = (suggestion: string) => {
+ if (controlledValue === undefined) {
+ setInternalValue(suggestion);
+ }
+ onValueChange?.(suggestion);
+ if (props.onChange) {
+ const syntheticEvent = {
+ target: { value: suggestion },
+ } as React.ChangeEvent<HTMLInputElement>;
+ props.onChange(syntheticEvent);
+ }
+ setOpen(false);
+ };
+
+ return (
+ <div ref={wrapperRef} className="relative w-full">
+ <Input
+ ref={ref}
+ value={value}
+ onChange={handleInputChange}
+ onFocus={() => setOpen(true)}
+ className={className}
+ {...props}
+ />
+ {open && filteredSuggestions.length > 0 && (
+ <div className="absolute z-50 mt-1 w-full max-h-60 overflow-auto rounded-md border border-neutral-200 bg-white shadow-md dark:border-neutral-800 dark:bg-neutral-900">
+ {filteredSuggestions.map((suggestion) => (
+ <div
+ key={suggestion.value}
+ className="relative cursor-pointer px-3 py-2 text-sm hover:bg-neutral-100 dark:hover:bg-neutral-800"
+ onClick={() => handleSuggestionClick(suggestion.value)}
+ >
+ {suggestion.label || suggestion.value}
+ </div>
+ ))}
+ </div>
+ )}
+ </div>
+ );
+});
+
+InputWithSuggestions.displayName = "InputWithSuggestions";
\ No newline at end of file
diff --git a/client/src/components/ui/label.tsx b/client/src/components/ui/label.tsx
index 892294421..25487ccf8 100644
--- a/client/src/components/ui/label.tsx
+++ b/client/src/components/ui/label.tsx
@@ -7,7 +7,7 @@ import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
const labelVariants = cva(
- "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 text-muted-foreground"
+ "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 text-neutral-500"
);
const Label = React.forwardRef<
diff --git a/docs-v2/content/blog/real-time-reports.mdx b/docs-v2/content/blog/real-time-reports.mdx
index f8f0b2d95..162ba1a14 100644
--- a/docs-v2/content/blog/real-time-reports.mdx
+++ b/docs-v2/content/blog/real-time-reports.mdx
@@ -94,7 +94,15 @@ They brought in a new system to analyze transaction patterns as they happen. Now
## The Technology Powering Instant Insights
-<iframe width="100%" style="aspect-ratio: 16 / 9;" src="https://www.youtube.com/embed/0aa8MeioS-M" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
+<iframe
+ width="100%"
+ height="400"
+ style={{ aspectRatio: '16 / 9' }}
+ src="https://www.youtube.com/embed/0aa8MeioS-M"
+ frameBorder="0"
+ allow="autoplay; encrypted-media"
+ allowFullScreen
+></iframe>
So, how does the magic behind real-time reports actually happen? It’s not really magic at all. It's more like producing a live news broadcast, where raw data is gathered, processed, and beamed to a dashboard you can understand in seconds.