Add link and pixel selectors to top nav

This commit is contained in:
Mike Cao committed 2026-02-12 17:01:37 -08:00
1 parent dd2d6bca45
commit a1f3ad89f6
6 files changed
+255 -17

No files matched your search

+10 -3
View File
@@ -3,14 +3,21 @@ import { useApi } from '../useApi';
import { useModified } from '../useModified';
import { usePagedQuery } from '../usePagedQuery';
export function useLinksQuery({ teamId }: { teamId?: string }, options?: ReactQueryOptions) {
export function useLinksQuery(
{ teamId }: { teamId?: string },
params?: Record<string, any>,
options?: ReactQueryOptions,
) {
const { modified } = useModified('links');
const { get } = useApi();
return usePagedQuery({
queryKey: ['links', { teamId, modified }],
queryKey: ['links', { teamId, modified, ...params }],
queryFn: pageParams => {
return get(teamId ? `/teams/${teamId}/links` : '/links', pageParams);
return get(teamId ? `/teams/${teamId}/links` : '/links', {
...pageParams,
...params,
});
},
...options,
});
+10 -3
View File
@@ -3,14 +3,21 @@ import { useApi } from '../useApi';
import { useModified } from '../useModified';
import { usePagedQuery } from '../usePagedQuery';
export function usePixelsQuery({ teamId }: { teamId?: string }, options?: ReactQueryOptions) {
export function usePixelsQuery(
{ teamId }: { teamId?: string },
params?: Record<string, any>,
options?: ReactQueryOptions,
) {
const { modified } = useModified('pixels');
const { get } = useApi();
return usePagedQuery({
queryKey: ['pixels', { teamId, modified }],
queryKey: ['pixels', { teamId, modified, ...params }],
queryFn: pageParams => {
return get(teamId ? `/teams/${teamId}/pixels` : '/pixels', pageParams);
return get(teamId ? `/teams/${teamId}/pixels` : '/pixels', {
...pageParams,
...params,
});
},
...options,
});
+4
View File
@@ -8,6 +8,8 @@ export function useNavigation() {
const searchParams = useSearchParams();
const [, teamId] = pathname.match(/\/teams\/([a-f0-9-]+)/) || [];
const [, websiteId] = pathname.match(/\/websites\/([a-f0-9-]+)/) || [];
const [, linkId] = pathname.match(/\/links\/([a-f0-9-]+)/) || [];
const [, pixelId] = pathname.match(/\/pixels\/([a-f0-9-]+)/) || [];
const [queryParams, setQueryParams] = useState(Object.fromEntries(searchParams));
const updateParams = (params?: Record<string, string | number>) => {
@@ -36,6 +38,8 @@ export function useNavigation() {
query: queryParams,
teamId,
websiteId,
linkId,
pixelId,
updateParams,
replaceParams,
renderUrl,
+93
View File
@@ -0,0 +1,93 @@
import { Icon, ListItem, Row, Select, type SelectProps, Text } from '@umami/react-zen';
import { useEffect, useState } from 'react';
import { Empty } from '@/components/common/Empty';
import { useLinkQuery, useLinksQuery, useMessages } from '@/components/hooks';
import { Link } from '@/components/icons';
export function LinkSelect({
linkId,
teamId,
onChange,
isCollapsed,
buttonProps,
listProps,
...props
}: {
linkId?: string;
teamId?: string;
isCollapsed?: boolean;
} & SelectProps) {
const { t, messages } = useMessages();
const { data: link } = useLinkQuery(linkId);
const [name, setName] = useState<string>(link?.name);
const [search, setSearch] = useState('');
const { data, isLoading } = useLinksQuery({ teamId }, { search, pageSize: 20 });
const listItems: { id: string; name: string }[] = data?.data || [];
useEffect(() => {
setName(link?.name);
}, [link?.name]);
const handleOpenChange = () => {
setSearch('');
};
const handleChange = (id: string) => {
setName(listItems.find(item => item.id === id)?.name);
onChange?.(id);
};
const renderValue = () => {
if (isCollapsed) {
return '';
}
return (
<Row alignItems="center" gap>
<Icon>
<Link />
</Icon>
<Text truncate>{name}</Text>
</Row>
);
};
return (
<Select
{...props}
value={linkId}
isLoading={isLoading}
allowSearch={true}
searchValue={search}
onSearch={setSearch}
onChange={handleChange}
onOpenChange={handleOpenChange}
renderValue={renderValue}
buttonProps={{
...buttonProps,
style: {
minHeight: 40,
gap: 0,
justifyContent: isCollapsed ? 'start' : undefined,
...buttonProps?.style,
},
}}
listProps={{
...listProps,
renderEmptyState:
listProps?.renderEmptyState || (() => <Empty message={t(messages.noResultsFound)} />),
style: {
maxHeight: 'calc(42vh - 65px)',
width: 280,
...listProps?.style,
},
}}
>
{listItems.map(({ id, name }) => (
<ListItem key={id} id={id}>
{name}
</ListItem>
))}
</Select>
);
}
+93
View File
@@ -0,0 +1,93 @@
import { Icon, ListItem, Row, Select, type SelectProps, Text } from '@umami/react-zen';
import { useEffect, useState } from 'react';
import { Empty } from '@/components/common/Empty';
import { useMessages, usePixelQuery, usePixelsQuery } from '@/components/hooks';
import { Grid2x2 } from '@/components/icons';
export function PixelSelect({
pixelId,
teamId,
onChange,
isCollapsed,
buttonProps,
listProps,
...props
}: {
pixelId?: string;
teamId?: string;
isCollapsed?: boolean;
} & SelectProps) {
const { t, messages } = useMessages();
const { data: pixel } = usePixelQuery(pixelId);
const [name, setName] = useState<string>(pixel?.name);
const [search, setSearch] = useState('');
const { data, isLoading } = usePixelsQuery({ teamId }, { search, pageSize: 20 });
const listItems: { id: string; name: string }[] = data?.data || [];
useEffect(() => {
setName(pixel?.name);
}, [pixel?.name]);
const handleOpenChange = () => {
setSearch('');
};
const handleChange = (id: string) => {
setName(listItems.find(item => item.id === id)?.name);
onChange?.(id);
};
const renderValue = () => {
if (isCollapsed) {
return '';
}
return (
<Row alignItems="center" gap>
<Icon>
<Grid2x2 />
</Icon>
<Text truncate>{name}</Text>
</Row>
);
};
return (
<Select
{...props}
value={pixelId}
isLoading={isLoading}
allowSearch={true}
searchValue={search}
onSearch={setSearch}
onChange={handleChange}
onOpenChange={handleOpenChange}
renderValue={renderValue}
buttonProps={{
...buttonProps,
style: {
minHeight: 40,
gap: 0,
justifyContent: isCollapsed ? 'start' : undefined,
...buttonProps?.style,
},
}}
listProps={{
...listProps,
renderEmptyState:
listProps?.renderEmptyState || (() => <Empty message={t(messages.noResultsFound)} />),
style: {
maxHeight: 'calc(42vh - 65px)',
width: 280,
...listProps?.style,
},
}}
>
{listItems.map(({ id, name }) => (
<ListItem key={id} id={id}>
{name}
</ListItem>
))}
</Select>
);
}