Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added search to websites dropdown.

Mike Cao6c4a2976971c
diff --git a/src/components/common/Empty.module.css b/src/components/common/Empty.module.css
index 29e9f8eb7..3dccb68ed 100644
--- a/src/components/common/Empty.module.css
+++ b/src/components/common/Empty.module.css
@@ -8,4 +8,5 @@
text-align: center;
width: 100%;
height: 100%;
+ min-height: 70px;
}
diff --git a/src/components/input/WebsiteSelect.tsx b/src/components/input/WebsiteSelect.tsx
index 3a6ca272a..9dd3dd616 100644
--- a/src/components/input/WebsiteSelect.tsx
+++ b/src/components/input/WebsiteSelect.tsx
@@ -1,35 +1,61 @@
+import { useState, Key } from 'react';
import { Dropdown, Item } from 'react-basics';
import useApi from 'components/hooks/useApi';
import useMessages from 'components/hooks/useMessages';
import styles from './WebsiteSelect.module.css';
+import Empty from 'components/common/Empty';
export function WebsiteSelect({
websiteId,
onSelect,
}: {
- websiteId: string;
+ websiteId?: string;
onSelect?: (key: any) => void;
}) {
- const { formatMessage, labels } = useMessages();
+ const [query, setQuery] = useState('');
+ const [selectedId, setSelectedId] = useState<Key>(websiteId);
+ const { formatMessage, labels, messages } = useMessages();
const { get, useQuery } = useApi();
- const { data } = useQuery({
- queryKey: ['websites:me'],
- queryFn: () => get('/me/websites', { pageSize: 100 }),
+ const { data: websites, isLoading } = useQuery({
+ queryKey: ['websites:me', { query }],
+ queryFn: () => get('/me/websites', { query, pageSize: 5 }),
});
+ const { data: website } = useQuery({
+ queryKey: ['websites', { selectedId }],
+ queryFn: () => get(`/websites/${selectedId}`),
+ enabled: !!selectedId,
+ });
+
+ const renderValue = () => {
+ return website?.name;
+ };
+
+ const renderEmpty = () => {
+ return <Empty message={formatMessage(messages.noResultsFound)} />;
+ };
+
+ const handleSelect = (value: any) => {
+ setSelectedId(value);
+ onSelect?.(value);
+ };
- const renderValue = value => {
- return data?.data?.find(({ id }) => id === value)?.name;
+ const handleSearch = (value: string) => {
+ setQuery(value);
};
return (
<Dropdown
menuProps={{ className: styles.dropdown }}
- items={data?.data}
- value={websiteId}
+ items={websites?.data}
+ value={selectedId as string}
renderValue={renderValue}
- onSelect={onSelect}
+ renderEmpty={renderEmpty}
+ onSelect={handleSelect}
alignment="end"
placeholder={formatMessage(labels.selectWebsite)}
+ allowSearch={true}
+ onSearch={handleSearch}
+ isLoading={isLoading}
>
{({ id, name }) => <Item key={id}>{name}</Item>}
</Dropdown>
diff --git a/src/lib/types.ts b/src/lib/types.ts
index af0ea0f78..c4d018a41 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -53,7 +53,7 @@ export interface SearchFilter {
}
export interface FilterResult<T> {
- data: T[];
+ data: T;
count: number;
page: number;
pageSize: number;
diff --git a/src/pages/api/websites/index.ts b/src/pages/api/websites/index.ts
index b30681cfc..d0295b875 100644
--- a/src/pages/api/websites/index.ts
+++ b/src/pages/api/websites/index.ts
@@ -45,10 +45,6 @@ export default async (
req.query.id = userId;
}
- if (!req.query.pageSize) {
- req.query.pageSize = 100;
- }
-
return userWebsites(req as any, res);
}
diff --git a/src/queries/admin/website.ts b/src/queries/admin/website.ts
index 524e2e141..0e7f5124f 100644
--- a/src/queries/admin/website.ts
+++ b/src/queries/admin/website.ts
@@ -21,7 +21,7 @@ export async function getWebsiteByShareId(shareId: string) {
export async function getWebsites(
filters: WebsiteSearchFilter,
options?: { include?: Prisma.WebsiteInclude },
-): Promise<FilterResult<Website>> {
+): Promise<FilterResult<Website[]>> {
const { userId, teamId, includeTeams, onlyTeams, query } = filters;
const mode = prisma.getSearchMode();
@@ -105,7 +105,7 @@ export async function getWebsites(
export async function getWebsitesByUserId(
userId: string,
filters?: WebsiteSearchFilter,
-): Promise<FilterResult<Website>> {
+): Promise<FilterResult<Website[]>> {
return getWebsites(
{ userId, ...filters },
{
@@ -133,7 +133,7 @@ export async function getWebsitesByUserId(
export async function getWebsitesByTeamId(
teamId: string,
filters?: WebsiteSearchFilter,
-): Promise<FilterResult<Website>> {
+): Promise<FilterResult<Website[]>> {
return getWebsites(
{
teamId,