Skip to content

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

improve filters

Bill Yang7ad9fbcc8cf7
diff --git a/client/src/app/[site]/components/SubHeader/Filters/NewFilterButton.tsx b/client/src/app/[site]/components/SubHeader/Filters/NewFilterButton.tsx
index a27bca44f..40cd3a5b5 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/NewFilterButton.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/NewFilterButton.tsx
@@ -26,7 +26,11 @@ const FilterOptions: { label: string; value: FilterParameter }[] = [
},
{
label: "Query",
- value: "query",
+ value: "querystring",
+ },
+ {
+ label: "Country",
+ value: "country",
},
{
label: "Region",
@@ -36,10 +40,6 @@ const FilterOptions: { label: string; value: FilterParameter }[] = [
label: "City",
value: "city",
},
- {
- label: "Country",
- value: "country",
- },
{
label: "Device Type",
value: "device_type",
diff --git a/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx b/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx
index f213ad6c7..6d14279f0 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx
@@ -1,10 +1,10 @@
"use client";
-import { useState, useEffect } from "react";
+import { useMemo, useState } from "react";
import { Button } from "../../../../../components/ui/button";
-import { Check, ChevronsUpDown, Plus, X } from "lucide-react";
+import { Check, ChevronsUpDown, Plus } from "lucide-react";
import { cn } from "../../../../../lib/utils";
@@ -25,94 +25,60 @@ import {
import { FilterParameter, addFilter } from "../../../../../lib/store";
-import { useRouter } from "next/navigation";
+import { useSingleCol } from "../../../../../hooks/api";
+import { countries } from "countries-list";
interface ValueFilterProps {
- parameter?: FilterParameter;
+ parameter: FilterParameter;
- type?: "equals" | "not_equals" | "contains" | "not_contains";
+ type: "equals" | "not_equals" | "contains" | "not_contains";
onComplete?: () => void;
}
export function ValueFilter({ parameter, type, onComplete }: ValueFilterProps) {
+ const { data, isLoading } = useSingleCol(parameter, 1000);
+
const [open, setOpen] = useState(false);
const [value, setValue] = useState("");
- const [suggestions, setSuggestions] = useState<string[]>([]);
-
- const router = useRouter();
-
- // This would be replaced with actual data fetching based on the parameter
-
- useEffect(() => {
- // Mock suggestions based on parameter type
-
- if (parameter) {
- // In a real implementation, these would come from an API call based on the parameter
-
- const mockSuggestions: Record<FilterParameter, string[]> = {
- pathname: ["/", "/about", "/pricing", "/contact", "/blog"],
-
- query: ["utm_source=google", "ref=homepage", "campaign=spring"],
-
- region: ["California", "New York", "Texas", "Florida"],
-
- city: ["San Francisco", "New York", "Austin", "Miami"],
-
- country: [
- "United States",
- "United Kingdom",
- "Canada",
- "Germany",
- "Japan",
- ],
-
- device_type: ["desktop", "mobile", "tablet"],
-
- operating_system: ["Windows", "macOS", "iOS", "Android", "Linux"],
-
- browser: ["Chrome", "Safari", "Firefox", "Edge"],
-
- referrer: ["google.com", "facebook.com", "twitter.com", "direct"],
- };
-
- setSuggestions(mockSuggestions[parameter] || []);
- }
- }, [parameter]);
+ const suggestions = useMemo(() => {
+ return (
+ data?.data
+ ?.map((item) => item.value)
+ .filter(Boolean)
+ .map((val) => {
+ if (parameter === "country") {
+ return {
+ value: val,
+ label: countries[val as keyof typeof countries].name,
+ };
+ }
+ return {
+ value: val,
+ label: val,
+ };
+ }) || []
+ );
+ }, [data]);
const handleSelect = (currentValue: string) => {
setValue(currentValue);
-
setOpen(false);
};
const handleApply = () => {
if (parameter && type && value.trim()) {
- // Add the filter using the store function
-
addFilter({
parameter,
-
type,
-
value: value.trim(),
});
-
- // Reset the component
-
setValue("");
-
- // Call the onComplete callback (if provided)
-
if (onComplete) {
onComplete();
}
-
- // Refresh the page to apply the filter
-
- router.refresh();
}
};
@@ -131,8 +97,7 @@ export function ValueFilter({ parameter, type, onComplete }: ValueFilterProps) {
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
-
- <PopoverContent className="w-[200px] p-0">
+ <PopoverContent className="p-0" align="start">
<Command>
<CommandInput
placeholder="Search values..."
@@ -140,26 +105,16 @@ export function ValueFilter({ parameter, type, onComplete }: ValueFilterProps) {
onValueChange={setValue}
className="h-9"
/>
-
<CommandList>
<CommandEmpty>No matching results</CommandEmpty>
-
<CommandGroup>
{suggestions.map((suggestion) => (
<CommandItem
- key={suggestion}
- value={suggestion}
+ key={suggestion.value}
+ value={suggestion.value}
onSelect={handleSelect}
>
- <Check
- className={cn(
- "mr-2 h-4 w-4",
-
- value === suggestion ? "opacity-100" : "opacity-0"
- )}
- />
-
- {suggestion}
+ {suggestion.label}
</CommandItem>
))}
</CommandGroup>
diff --git a/client/src/app/[site]/components/sections/Referrers/Referrers.tsx b/client/src/app/[site]/components/sections/Referrers/Referrers.tsx
index 9da93fa94..5560cda97 100644
--- a/client/src/app/[site]/components/sections/Referrers/Referrers.tsx
+++ b/client/src/app/[site]/components/sections/Referrers/Referrers.tsx
@@ -23,6 +23,9 @@ export function Referrers() {
href={`https://${e.value}`}
target="_blank"
className="hover:underline"
+ onClick={(e) => {
+ e.stopPropagation();
+ }}
>
{e.value}
</a>
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index 218f813db..b9423998e 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -23,6 +23,7 @@ type PeriodTime = "current" | "previous";
export function useSingleCol(
parameter: FilterParameter,
+ limit?: number,
periodTime?: PeriodTime
): UseQueryResult<
APIResponse<{ value: string; count: number; percentage: number }[]>
@@ -32,7 +33,7 @@ export function useSingleCol(
const { startDate, endDate } = getStartAndEndDate(timeToUse);
return useQuery({
- queryKey: [parameter, timeToUse, site, filters],
+ queryKey: [parameter, timeToUse, site, filters, limit],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return authedFetch(
@@ -42,7 +43,7 @@ export function useSingleCol(
endDate ? `endDate=${endDate}&` : ""
}timezone=${timezone}&site=${site}&parameter=${parameter}&filters=${JSON.stringify(
filters
- )}`
+ )}${limit ? `&limit=${limit}` : ""}`
).then((res) => res.json());
},
staleTime: Infinity,
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 89d96c3cd..9d1f69f64 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -66,7 +66,7 @@ export type FilterParameter =
| "device_type"
| "referrer"
| "pathname"
- | "query";
+ | "querystring";
export type Filter = {
parameter: FilterParameter;
diff --git a/server/src/api/getSingleCol.ts b/server/src/api/getSingleCol.ts
index ddd90a271..da6bb1f81 100644
--- a/server/src/api/getSingleCol.ts
+++ b/server/src/api/getSingleCol.ts
@@ -16,7 +16,7 @@ type GetSingleColResponse = {
export async function getSingleCol(
{
- query: { startDate, endDate, timezone, site, filters, parameter },
+ query: { startDate, endDate, timezone, site, filters, parameter, limit },
}: FastifyRequest<GenericRequest>,
res: FastifyReply
) {
@@ -32,7 +32,8 @@ export async function getSingleCol(
site_id = ${site}
${filterStatement}
${getTimeStatement(startDate, endDate, timezone)}
- GROUP BY value ORDER BY count desc;
+ GROUP BY value ORDER BY count desc
+ ${limit ? `LIMIT ${limit}` : ""};
`;
try {
diff --git a/server/src/api/types.ts b/server/src/api/types.ts
index b91a4054a..84c2cb972 100644
--- a/server/src/api/types.ts
+++ b/server/src/api/types.ts
@@ -9,7 +9,7 @@ export type FilterParameter =
| "device_type"
| "referrer"
| "pathname"
- | "query";
+ | "querystring";
export type Filter = {
parameter: FilterParameter;
@@ -25,5 +25,6 @@ export interface GenericRequest {
site: string;
filters: string;
parameter: FilterParameter;
+ limit?: number;
};
}