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

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-10-17 07:46:49 +02:00
import { Button, Icon, Icons, Popup, PopupTrigger, Text } from 'react-basics';
2024-02-04 09:44:20 +01:00
import PopupForm from 'app/(main)/reports/[reportId]/PopupForm';
import FilterSelectForm from 'app/(main)/reports/[reportId]/FilterSelectForm';
import { useFields, useMessages, useNavigation } from 'components/hooks';
import { OPERATORS } from 'lib/constants';
2023-10-17 07:46:49 +02:00
2023-12-03 12:07:03 +01:00
export function WebsiteFilterButton({
websiteId,
className,
}: {
websiteId: string;
className?: string;
}) {
2023-10-17 07:46:49 +02:00
const { formatMessage, labels } = useMessages();
2024-01-30 09:10:25 +01:00
const { renderUrl, router } = useNavigation();
const { fields } = useFields();
2023-10-17 07:46:49 +02:00
const handleAddFilter = ({ name, operator, value }) => {
let prefix = '';
2023-10-17 07:46:49 +02:00
if (operator === OPERATORS.notEquals) {
prefix = '!';
} else if (operator === OPERATORS.contains) {
prefix = '~';
} else if (operator === OPERATORS.doesNotContain) {
prefix = '!~';
}
router.push(renderUrl({ [name]: prefix + value }));
2023-10-17 07:46:49 +02:00
};
return (
<PopupTrigger>
<Button className={className}>
<Icon>
<Icons.Plus />
</Icon>
<Text>{formatMessage(labels.filter)}</Text>
</Button>
<Popup position="bottom" alignment="end">
2023-12-03 12:07:03 +01:00
{(close: () => void) => {
2023-10-17 07:46:49 +02:00
return (
2023-12-03 12:07:03 +01:00
<PopupForm>
2023-10-17 07:46:49 +02:00
<FilterSelectForm
websiteId={websiteId}
fields={fields}
onChange={value => {
2023-10-17 07:46:49 +02:00
handleAddFilter(value);
close();
}}
/>
</PopupForm>
);
}}
</Popup>
</PopupTrigger>
);
}
export default WebsiteFilterButton;