aboutsummaryrefslogtreecommitdiff
path: root/src/components/hooks/queries/useWebsiteValuesQuery.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-01-24 13:09:50 +0000
committerFuwn <[email protected]>2026-01-24 13:09:50 +0000
commit396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch)
treeb9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/components/hooks/queries/useWebsiteValuesQuery.ts
downloadumami-main.tar.xz
umami-main.zip
Initial commitHEADmain
Created from https://vercel.com/new
Diffstat (limited to 'src/components/hooks/queries/useWebsiteValuesQuery.ts')
-rw-r--r--src/components/hooks/queries/useWebsiteValuesQuery.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/components/hooks/queries/useWebsiteValuesQuery.ts b/src/components/hooks/queries/useWebsiteValuesQuery.ts
new file mode 100644
index 0000000..1e09736
--- /dev/null
+++ b/src/components/hooks/queries/useWebsiteValuesQuery.ts
@@ -0,0 +1,62 @@
+import { useCountryNames } from '@/components/hooks/useCountryNames';
+import { useRegionNames } from '@/components/hooks/useRegionNames';
+import { useApi } from '../useApi';
+import { useLocale } from '../useLocale';
+
+export function useWebsiteValuesQuery({
+ websiteId,
+ type,
+ startDate,
+ endDate,
+ search,
+}: {
+ websiteId: string;
+ type: string;
+ startDate: Date;
+ endDate: Date;
+ search?: string;
+}) {
+ const { get, useQuery } = useApi();
+ const { locale } = useLocale();
+ const { countryNames } = useCountryNames(locale);
+ const { regionNames } = useRegionNames(locale);
+
+ const names = {
+ country: countryNames,
+ region: regionNames,
+ };
+
+ const getSearch = (type: string, value: string) => {
+ if (value) {
+ const values = names[type];
+
+ if (values) {
+ return (
+ Object.keys(values)
+ .reduce((arr: string[], key: string) => {
+ if (values[key].toLowerCase().includes(value.toLowerCase())) {
+ return arr.concat(key);
+ }
+ return arr;
+ }, [])
+ .slice(0, 5)
+ .join(',') || value
+ );
+ }
+
+ return value;
+ }
+ };
+
+ return useQuery({
+ queryKey: ['websites:values', { websiteId, type, startDate, endDate, search }],
+ queryFn: () =>
+ get(`/websites/${websiteId}/values`, {
+ type,
+ startAt: +startDate,
+ endAt: +endDate,
+ search: getSearch(type, search),
+ }),
+ enabled: !!(websiteId && type && startDate && endDate),
+ });
+}