umami/src/app/(main)/reports/insights/InsightsParameters.tsx

144 lines
5 KiB
TypeScript
Raw Normal View History

import { useFilters, useFormat, useMessages } from 'components/hooks';
import Icons from 'components/icons';
2023-12-03 11:07:03 +00:00
import { useContext } from 'react';
2023-08-09 22:06:19 +00:00
import {
Form,
FormButtons,
FormRow,
2023-08-09 22:06:19 +00:00
Icon,
Popup,
PopupTrigger,
SubmitButton,
2023-08-09 22:06:19 +00:00
TooltipPopup,
} from 'react-basics';
2024-01-29 22:47:52 +00:00
import BaseParameters from '../[reportId]/BaseParameters';
import FieldSelectForm from '../[reportId]/FieldSelectForm';
import FilterSelectForm from '../[reportId]/FilterSelectForm';
import ParameterList from '../[reportId]/ParameterList';
2024-01-29 22:47:52 +00:00
import PopupForm from '../[reportId]/PopupForm';
import { ReportContext } from '../[reportId]/Report';
2023-07-08 03:38:43 +00:00
import styles from './InsightsParameters.module.css';
2023-08-04 07:51:52 +00:00
2023-07-08 03:38:43 +00:00
export function InsightsParameters() {
const { report, runReport, updateReport, isRunning } = useContext(ReportContext);
2023-07-18 16:09:22 +00:00
const { formatMessage, labels } = useMessages();
2023-08-09 22:06:19 +00:00
const { formatValue } = useFormat();
const { filterLabels } = useFilters();
const { id, parameters } = report || {};
2023-08-08 06:02:38 +00:00
const { websiteId, dateRange, fields, filters } = parameters || {};
2023-08-09 22:06:19 +00:00
const { startDate, endDate } = dateRange || {};
const parametersSelected = websiteId && startDate && endDate;
2023-08-08 06:02:38 +00:00
const queryEnabled = websiteId && dateRange && (fields?.length || filters?.length);
2023-07-08 03:38:43 +00:00
2023-08-07 05:52:17 +00:00
const fieldOptions = [
2023-08-10 16:31:25 +00:00
{ name: 'url', type: 'string', label: formatMessage(labels.url) },
{ name: 'title', type: 'string', label: formatMessage(labels.pageTitle) },
{ name: 'referrer', type: 'string', label: formatMessage(labels.referrer) },
{ name: 'query', type: 'string', label: formatMessage(labels.query) },
{ name: 'browser', type: 'string', label: formatMessage(labels.browser) },
{ name: 'os', type: 'string', label: formatMessage(labels.os) },
{ name: 'device', type: 'string', label: formatMessage(labels.device) },
{ name: 'country', type: 'string', label: formatMessage(labels.country) },
{ name: 'region', type: 'string', label: formatMessage(labels.region) },
{ name: 'city', type: 'string', label: formatMessage(labels.city) },
2023-08-07 05:52:17 +00:00
];
2023-07-08 03:38:43 +00:00
const parameterGroups = [
2023-08-08 06:02:38 +00:00
{ id: 'fields', label: formatMessage(labels.fields) },
{ id: 'filters', label: formatMessage(labels.filters) },
2023-07-08 03:38:43 +00:00
];
const parameterData = {
2023-08-08 06:02:38 +00:00
fields,
2023-07-08 03:38:43 +00:00
filters,
};
2024-03-14 09:45:00 +00:00
const handleSubmit = (values: any) => {
2023-07-08 03:38:43 +00:00
runReport(values);
};
2024-03-14 09:45:00 +00:00
const handleAdd = (id: string | number, value: { name: any }) => {
2023-08-08 06:02:38 +00:00
const data = parameterData[id];
2023-07-08 03:38:43 +00:00
if (!data.find(({ name }) => name === value.name)) {
2023-08-08 06:02:38 +00:00
updateReport({ parameters: { [id]: data.concat(value) } });
2023-07-08 03:38:43 +00:00
}
};
2024-03-14 09:45:00 +00:00
const handleRemove = (id: string, index: number) => {
2023-08-08 06:02:38 +00:00
const data = [...parameterData[id]];
2023-07-08 03:38:43 +00:00
data.splice(index, 1);
2023-08-08 06:02:38 +00:00
updateReport({ parameters: { [id]: data } });
2023-07-08 03:38:43 +00:00
};
2023-12-03 11:07:03 +00:00
const AddButton = ({ id, onAdd }) => {
2023-07-08 03:38:43 +00:00
return (
<PopupTrigger>
2023-08-09 22:06:19 +00:00
<TooltipPopup label={formatMessage(labels.add)} position="top">
<Icon>
<Icons.Plus />
</Icon>
</TooltipPopup>
2023-08-17 10:21:20 +00:00
<Popup position="bottom" alignment="start" className={styles.popup}>
2023-10-17 03:44:22 +00:00
<PopupForm>
{id === 'fields' && (
<FieldSelectForm
2023-12-03 11:07:03 +00:00
fields={fieldOptions}
onSelect={onAdd.bind(null, id)}
2023-10-17 03:44:22 +00:00
showType={false}
/>
)}
{id === 'filters' && (
<FilterSelectForm
websiteId={websiteId}
items={fieldOptions}
2023-12-03 11:07:03 +00:00
onSelect={onAdd.bind(null, id)}
2023-10-17 03:44:22 +00:00
/>
)}
</PopupForm>
2023-07-08 03:38:43 +00:00
</Popup>
</PopupTrigger>
);
};
return (
2023-12-03 11:07:03 +00:00
<Form values={parameters} onSubmit={handleSubmit}>
<BaseParameters allowWebsiteSelect={!id} />
2023-08-09 22:06:19 +00:00
{parametersSelected &&
parameterGroups.map(({ id, label }) => {
return (
<FormRow key={label} label={label} action={<AddButton id={id} onAdd={handleAdd} />}>
<ParameterList items={parameterData[id]} onRemove={index => handleRemove(id, index)}>
2023-08-17 10:21:20 +00:00
{({ name, filter, value }) => {
2023-08-09 22:06:19 +00:00
return (
<div className={styles.parameter}>
{id === 'fields' && (
<>
2023-08-11 16:05:56 +00:00
<div>{fieldOptions.find(f => f.name === name)?.label}</div>
2023-08-09 22:06:19 +00:00
</>
)}
{id === 'filters' && (
<>
<div>{fieldOptions.find(f => f.name === name)?.label}</div>
<div className={styles.op}>{filterLabels[filter]}</div>
<div>{formatValue(value, name)}</div>
</>
)}
</div>
);
}}
</ParameterList>
</FormRow>
);
})}
2023-07-08 03:38:43 +00:00
<FormButtons>
2023-09-05 20:53:58 +00:00
<SubmitButton variant="primary" disabled={!queryEnabled} isLoading={isRunning}>
2023-07-08 03:38:43 +00:00
{formatMessage(labels.runQuery)}
</SubmitButton>
</FormButtons>
</Form>
);
}
export default InsightsParameters;