Skip to content

Commit 0a43ef7

Browse files
committed
Filtering via FilterBar.
1 parent da17377 commit 0a43ef7

9 files changed

Lines changed: 83 additions & 26 deletions

File tree

src/app/(main)/websites/[websiteId]/WebsiteChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function WebsiteChart({
5353
<PageviewsChart
5454
key={value}
5555
data={chartData}
56-
minDate={value === 'all' ? undefined : startDate}
56+
minDate={startDate}
5757
maxDate={endDate}
5858
unit={unit}
5959
/>

src/components/common/FilterEditForm.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, Key } from 'react';
22
import { Grid, Row, Column, Label, List, ListItem, Button, Heading } from '@umami/react-zen';
3-
import { useFilters, useMessages } from '@/components/hooks';
3+
import { useDateRange, useFilters, useMessages } from '@/components/hooks';
44
import { FilterRecord } from '@/components/common/FilterRecord';
55
import { Empty } from '@/components/common/Empty';
66

@@ -11,10 +11,13 @@ export interface FilterEditFormProps {
1111
onClose?: () => void;
1212
}
1313

14-
export function FilterEditForm({ data = [], onChange, onClose }: FilterEditFormProps) {
14+
export function FilterEditForm({ websiteId, data = [], onChange, onClose }: FilterEditFormProps) {
1515
const { formatMessage, labels, messages } = useMessages();
1616
const [filters, setFilters] = useState(data);
1717
const { fields } = useFilters();
18+
const {
19+
dateRange: { startDate, endDate },
20+
} = useDateRange(websiteId);
1821

1922
const updateFilter = (name: string, props: { [key: string]: any }) => {
2023
setFilters(filters =>
@@ -66,6 +69,10 @@ export function FilterEditForm({ data = [], onChange, onClose }: FilterEditFormP
6669
return (
6770
<FilterRecord
6871
key={filter.name}
72+
websiteId={websiteId}
73+
type={filter.name}
74+
startDate={startDate}
75+
endDate={endDate}
6976
{...filter}
7077
onSelect={handleSelect}
7178
onRemove={handleRemove}

src/components/common/FilterRecord.tsx

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { Grid, Column, TextField, Label, ListItem, Select, Icon, Button } from '@umami/react-zen';
2-
import { useFilters } from '@/components/hooks';
1+
import { useState } from 'react';
2+
import { Grid, Column, TextField, Label, Select, Icon, Button, ListItem } from '@umami/react-zen';
3+
import { useFilters, useFormat, useWebsiteValuesQuery } from '@/components/hooks';
34
import { Close } from '@/components/icons';
5+
import { isSearchOperator } from '@/lib/params';
46

57
export interface FilterRecordProps {
8+
websiteId: string;
9+
type: string;
10+
startDate: Date;
11+
endDate: Date;
612
name: string;
713
operator: string;
814
value: string;
@@ -12,6 +18,10 @@ export interface FilterRecordProps {
1218
}
1319

1420
export function FilterRecord({
21+
websiteId,
22+
type,
23+
startDate,
24+
endDate,
1525
name,
1626
operator,
1727
value,
@@ -20,16 +30,40 @@ export function FilterRecord({
2030
onChange,
2131
}: FilterRecordProps) {
2232
const { fields, operators } = useFilters();
33+
const [selected, setSelected] = useState(value);
34+
const [search, setSearch] = useState('');
35+
const { formatValue } = useFormat();
36+
const { data, isLoading } = useWebsiteValuesQuery({
37+
websiteId,
38+
type,
39+
search,
40+
startDate,
41+
endDate,
42+
});
43+
const isSearch = isSearchOperator(operator);
44+
45+
const handleSearch = value => {
46+
setSearch(value);
47+
};
48+
49+
const handleSelectOperator = value => {
50+
onSelect?.(name, value);
51+
};
52+
53+
const handleSelectValue = value => {
54+
setSelected(value);
55+
onChange?.(name, value);
56+
};
2357

2458
return (
25-
<>
59+
<Column>
2660
<Label>{fields.find(f => f.name === name)?.label}</Label>
2761
<Grid columns="1fr auto" gap>
2862
<Grid columns="200px 1fr" gap>
2963
<Select
3064
items={operators.filter(({ type }) => type === 'string')}
3165
value={operator}
32-
onSelectionChange={value => onSelect?.(name, value)}
66+
onSelectionChange={handleSelectOperator}
3367
>
3468
{({ name, label }: any) => {
3569
return (
@@ -39,7 +73,28 @@ export function FilterRecord({
3973
);
4074
}}
4175
</Select>
42-
<TextField value={value} onChange={e => onChange?.(name, e.target.value)} />
76+
{isSearch && (
77+
<TextField value={selected} defaultValue={selected} onChange={handleSelectValue} />
78+
)}
79+
{!isSearch && (
80+
<Select
81+
items={data}
82+
value={selected}
83+
onChange={handleSelectValue}
84+
searchValue={search}
85+
onSearch={handleSearch}
86+
isLoading={isLoading}
87+
allowSearch
88+
>
89+
{data?.map(({ value }) => {
90+
return (
91+
<ListItem key={value} id={value}>
92+
{formatValue(value, type)}
93+
</ListItem>
94+
);
95+
})}
96+
</Select>
97+
)}
4398
</Grid>
4499
<Column justifyContent="flex-end">
45100
<Button variant="quiet" onPress={() => onRemove?.(name)}>
@@ -49,6 +104,6 @@ export function FilterRecord({
49104
</Button>
50105
</Column>
51106
</Grid>
52-
</>
107+
</Column>
53108
);
54109
}

src/components/hooks/queries/useLoginQuery.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { useApi } from '../useApi';
33

44
const selector = (state: { user: any }) => state.user;
55

6-
export function useLoginQuery(): {
7-
user: any;
8-
setUser: (data: any) => void;
9-
} {
6+
export function useLoginQuery() {
107
const { post, useQuery } = useApi();
118
const user = useApp(selector);
129

src/components/hooks/useDateRange.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ export function useDateRange(websiteId?: string) {
2727

2828
const startDate = new Date(mindate);
2929
const endDate = new Date(maxdate);
30+
const unit = getMinimumUnit(startDate, endDate);
3031

3132
dateRange = {
3233
startDate,
3334
endDate,
34-
unit: getMinimumUnit(startDate, endDate),
35+
unit,
3536
value,
3637
};
3738
} else {

src/components/input/DateFilter.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ export interface DateFilterProps {
99
value: string;
1010
startDate: Date;
1111
endDate: Date;
12-
offset?: number;
13-
className?: string;
1412
onChange?: (value: string) => void;
1513
showAllTime?: boolean;
16-
alignment?: 'start' | 'center' | 'end';
1714
}
1815

1916
export function DateFilter({
17+
value,
2018
startDate,
2119
endDate,
22-
value,
2320
onChange,
2421
showAllTime = false,
2522
}: DateFilterProps) {

src/components/input/FilterBar.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ export function FilterBar() {
2424
}
2525

2626
return (
27-
<Row gap alignItems="center" justifyContent="space-between" paddingY="3">
28-
<Row alignItems="center" gap="3" wrap="wrap">
27+
<Row gap alignItems="center" justifyContent="space-between" padding backgroundColor="3">
28+
<Row alignItems="center" gap="2" wrap="wrap">
2929
{Object.keys(filters).map(key => {
3030
const filter = filters[key];
3131
const { name, label, operator, value } = filter;
32-
const paramValue = isSearchOperator(operator) ? value : formatValue(value, key);
32+
const paramValue = isSearchOperator(operator) ? value : formatValue(value, name);
3333

3434
return (
3535
<Row
3636
key={name}
3737
border
38-
padding
38+
padding="2"
39+
color
3940
backgroundColor
4041
borderRadius
4142
alignItems="center"
@@ -61,7 +62,7 @@ export function FilterBar() {
6162
})}
6263
</Row>
6364
<TooltipTrigger delay={0}>
64-
<Button variant="quiet" onPress={handleResetFilter}>
65+
<Button variant="wrapper" onPress={handleResetFilter} style={{ alignSelf: 'flex-start' }}>
6566
<Icon>
6667
<Close />
6768
</Icon>

src/components/input/WebsiteDateFilter.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export function WebsiteDateFilter({
7878
value={value}
7979
startDate={startDate}
8080
endDate={endDate}
81-
offset={offset}
8281
onChange={handleChange}
8382
showAllTime={showAllTime}
8483
/>

src/components/metrics/EventsChart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface EventsChartProps extends BarChartProps {
1313

1414
export function EventsChart({ websiteId, focusLabel }: EventsChartProps) {
1515
const {
16-
dateRange: { startDate, endDate, unit, value },
16+
dateRange: { startDate, endDate, unit },
1717
} = useDateRange(websiteId);
1818
const { locale } = useLocale();
1919
const { data, isLoading, error } = useWebsiteEventsSeriesQuery(websiteId);
@@ -59,7 +59,7 @@ export function EventsChart({ websiteId, focusLabel }: EventsChartProps) {
5959
{chartData && (
6060
<BarChart
6161
chartData={chartData}
62-
minDate={value === 'all' ? undefined : startDate}
62+
minDate={startDate}
6363
maxDate={endDate}
6464
unit={unit}
6565
stacked={true}

0 commit comments

Comments
 (0)