add share page options enable filtering and enforce theme
This commit is contained in:
1 parent
5ccfb6d0a5
commit
9b099408c2
75 files changed
+450
-172
No files matched your search
@@ -62,47 +62,47 @@ export function DataGrid({
|
||||
const child = data ? (typeof children === 'function' ? children(data) : children) : null;
|
||||
|
||||
return (
|
||||
<Column gap="4" minHeight="300px">
|
||||
{allowSearch && (
|
||||
<Row alignItems="center" justifyContent="space-between" wrap="wrap" gap>
|
||||
<SearchField
|
||||
value={search}
|
||||
onSearch={handleSearch}
|
||||
delay={searchDelay || DEFAULT_SEARCH_DELAY}
|
||||
autoFocus={autoFocus}
|
||||
placeholder={t(labels.search)}
|
||||
/>
|
||||
{renderActions?.()}
|
||||
</Row>
|
||||
)}
|
||||
<LoadingPanel
|
||||
data={data?.data}
|
||||
isLoading={isLoading}
|
||||
isFetching={isFetching}
|
||||
error={error}
|
||||
renderEmpty={renderEmpty}
|
||||
>
|
||||
{data && (
|
||||
<>
|
||||
<Column gap="4" minHeight="300px" justifyContent="space-between">
|
||||
<Column gap="4">
|
||||
{allowSearch && (
|
||||
<Row alignItems="center" justifyContent="space-between" wrap="wrap" gap>
|
||||
<SearchField
|
||||
value={search}
|
||||
onSearch={handleSearch}
|
||||
delay={searchDelay || DEFAULT_SEARCH_DELAY}
|
||||
autoFocus={autoFocus}
|
||||
placeholder={t(labels.search)}
|
||||
/>
|
||||
{renderActions?.()}
|
||||
</Row>
|
||||
)}
|
||||
<LoadingPanel
|
||||
data={data?.data}
|
||||
isLoading={isLoading}
|
||||
isFetching={isFetching}
|
||||
error={error}
|
||||
renderEmpty={renderEmpty}
|
||||
>
|
||||
{data && (
|
||||
<Column>
|
||||
{isValidElement(child)
|
||||
? cloneElement(child as ReactElement<any>, { displayMode })
|
||||
: child}
|
||||
</Column>
|
||||
{showPager && (
|
||||
<Row marginTop="6">
|
||||
<Pager
|
||||
page={data.page}
|
||||
pageSize={data.pageSize}
|
||||
count={data.count}
|
||||
isCapped={data.isCapped}
|
||||
onPageChange={handlePageChange}
|
||||
/>
|
||||
</Row>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</LoadingPanel>
|
||||
)}
|
||||
</LoadingPanel>
|
||||
</Column>
|
||||
{showPager && (
|
||||
<Row marginTop="4">
|
||||
<Pager
|
||||
page={data.page}
|
||||
pageSize={data.pageSize}
|
||||
count={data.count}
|
||||
isCapped={data.isCapped}
|
||||
onPageChange={handlePageChange}
|
||||
/>
|
||||
</Row>
|
||||
)}
|
||||
</Column>
|
||||
);
|
||||
}
|
||||
@@ -1,27 +1,32 @@
|
||||
import { useMemo } from 'react';
|
||||
import { FILTER_COLUMNS } from '@/lib/constants';
|
||||
import { useShare } from './context/useShare';
|
||||
import { useNavigation } from './useNavigation';
|
||||
|
||||
export function useFilterParameters({ includePagination = true }: { includePagination?: boolean } = {}) {
|
||||
const { query } = useNavigation();
|
||||
const share = useShare();
|
||||
const allowFilter = share?.parameters?.allowFilter !== false;
|
||||
|
||||
return useMemo(() => {
|
||||
const filterParams: Record<string, any> = {};
|
||||
|
||||
for (const key of Object.keys(query)) {
|
||||
const baseName = key.replace(/\d+$/, '');
|
||||
if (FILTER_COLUMNS[baseName]) {
|
||||
filterParams[key] = query[key];
|
||||
if (allowFilter) {
|
||||
for (const key of Object.keys(query)) {
|
||||
const baseName = key.replace(/\d+$/, '');
|
||||
if (FILTER_COLUMNS[baseName]) {
|
||||
filterParams[key] = query[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const params = {
|
||||
...filterParams,
|
||||
search: query.search,
|
||||
segment: query.segment,
|
||||
cohort: query.cohort,
|
||||
excludeBounce: query.excludeBounce,
|
||||
match: query.match,
|
||||
segment: allowFilter ? query.segment : undefined,
|
||||
cohort: allowFilter ? query.cohort : undefined,
|
||||
excludeBounce: allowFilter ? query.excludeBounce : undefined,
|
||||
match: allowFilter ? query.match : undefined,
|
||||
} as Record<string, any>;
|
||||
|
||||
if (includePagination) {
|
||||
@@ -30,5 +35,5 @@ export function useFilterParameters({ includePagination = true }: { includePagin
|
||||
}
|
||||
|
||||
return params;
|
||||
}, [query, includePagination]);
|
||||
}, [allowFilter, includePagination, query]);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { FILTER_COLUMNS, OPERATORS } from '@/lib/constants';
|
||||
import { safeDecodeURIComponent } from '@/lib/url';
|
||||
import { useShare } from './context/useShare';
|
||||
import { useFields } from './useFields';
|
||||
import { useMessages } from './useMessages';
|
||||
import { useNavigation } from './useNavigation';
|
||||
@@ -10,6 +11,8 @@ export function useFilters() {
|
||||
const { query } = useNavigation();
|
||||
const { fields } = useFields();
|
||||
const operatorLabels = useOperatorLabels();
|
||||
const share = useShare();
|
||||
const allowFilter = share?.parameters?.allowFilter !== false;
|
||||
|
||||
const operators = [
|
||||
{ name: 'eq', type: 'string', label: t(labels.is) },
|
||||
@@ -56,30 +59,32 @@ export function useFilters() {
|
||||
uuid: [OPERATORS.equals],
|
||||
};
|
||||
|
||||
const filters = Object.keys(query).reduce((arr, key) => {
|
||||
const baseName = key.replace(/\d+$/, '');
|
||||
if (FILTER_COLUMNS[baseName]) {
|
||||
let operator = 'eq';
|
||||
let value = safeDecodeURIComponent(query[key]);
|
||||
const label = fields.find(({ name }) => name === baseName)?.label;
|
||||
const filters = allowFilter
|
||||
? Object.keys(query).reduce((arr, key) => {
|
||||
const baseName = key.replace(/\d+$/, '');
|
||||
if (FILTER_COLUMNS[baseName]) {
|
||||
let operator = 'eq';
|
||||
let value = safeDecodeURIComponent(query[key]);
|
||||
const label = fields.find(({ name }) => name === baseName)?.label;
|
||||
|
||||
const match = value.match(/^([a-z]+)\.(.*)/);
|
||||
const match = value.match(/^([a-z]+)\.(.*)/);
|
||||
|
||||
if (match) {
|
||||
operator = match[1];
|
||||
value = match[2];
|
||||
}
|
||||
if (match) {
|
||||
operator = match[1];
|
||||
value = match[2];
|
||||
}
|
||||
|
||||
return arr.concat({
|
||||
name: key,
|
||||
type: baseName,
|
||||
operator,
|
||||
value,
|
||||
label,
|
||||
});
|
||||
}
|
||||
return arr;
|
||||
}, []);
|
||||
return arr.concat({
|
||||
name: key,
|
||||
type: baseName,
|
||||
operator,
|
||||
value,
|
||||
label,
|
||||
});
|
||||
}
|
||||
return arr;
|
||||
}, [])
|
||||
: [];
|
||||
|
||||
const getFilters = (type: string) => {
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Button, Icon, Row } from '@umami/react-zen';
|
||||
import { Monitor, Moon, Sun } from '@/components/icons';
|
||||
|
||||
export type ThemeMode = 'light' | 'dark' | 'system';
|
||||
|
||||
export function ThemeModeSelector({
|
||||
value,
|
||||
onChange,
|
||||
includeSystem = false,
|
||||
}: {
|
||||
value?: ThemeMode;
|
||||
onChange: (value: ThemeMode) => void;
|
||||
includeSystem?: boolean;
|
||||
}) {
|
||||
const options: { id: ThemeMode; icon: ReactNode; label: string }[] = [
|
||||
{ id: 'light', icon: <Sun />, label: 'Light' },
|
||||
...(includeSystem ? [{ id: 'system' as const, icon: <Monitor />, label: 'System' }] : []),
|
||||
{ id: 'dark', icon: <Moon />, label: 'Dark' },
|
||||
];
|
||||
|
||||
return (
|
||||
<Row gap>
|
||||
{options.map(({ id, icon, label }) => (
|
||||
<Button
|
||||
key={id}
|
||||
variant={value === id ? 'primary' : undefined}
|
||||
onPress={() => onChange(id)}
|
||||
aria-label={label}
|
||||
>
|
||||
<Icon>{icon}</Icon>
|
||||
</Button>
|
||||
))}
|
||||
</Row>
|
||||
);
|
||||
}
|
||||
@@ -190,6 +190,7 @@ export const labels: Record<string, string> = {
|
||||
type: 'label.type',
|
||||
filter: 'label.filter',
|
||||
filters: 'label.filters',
|
||||
filtersEnabled: 'label.filters-enabled',
|
||||
propertyFilter: 'label.property-filter',
|
||||
activityByProperty: 'label.activity-by-property',
|
||||
breakdown: 'label.breakdown',
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
import { Button, Column, Form, FormField, FormSubmitButton, Row, TextField } from '@umami/react-zen';
|
||||
import {
|
||||
Button,
|
||||
Column,
|
||||
Form,
|
||||
FormField,
|
||||
FormSubmitButton,
|
||||
Row,
|
||||
Switch,
|
||||
TextField,
|
||||
} from '@umami/react-zen';
|
||||
import { useState } from 'react';
|
||||
import { useApi, useMessages, useModified } from '@/components/hooks';
|
||||
import { ThemeModeSelector } from '@/components/input/ThemeModeSelector';
|
||||
|
||||
export function SimpleShareCreateForm({
|
||||
createPath,
|
||||
@@ -15,15 +25,19 @@ export function SimpleShareCreateForm({
|
||||
const { touch } = useModified();
|
||||
const { t, labels, getErrorMessage } = useMessages();
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [error, setError] = useState<unknown>(null);
|
||||
const [error, setError] = useState<any>(null);
|
||||
|
||||
const handleSubmit = async (data: { name: string }) => {
|
||||
const handleSubmit = async (data: { name: string; allowFilter?: boolean; theme?: string }) => {
|
||||
setIsPending(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
await post(createPath, {
|
||||
name: data.name,
|
||||
parameters: {
|
||||
allowFilter: data.allowFilter ?? true,
|
||||
theme: data.theme === 'system' ? undefined : data.theme,
|
||||
},
|
||||
});
|
||||
|
||||
touch('shares');
|
||||
@@ -36,22 +50,45 @@ export function SimpleShareCreateForm({
|
||||
};
|
||||
|
||||
return (
|
||||
<Form onSubmit={handleSubmit} error={getErrorMessage(error)} defaultValues={{ name: '' }}>
|
||||
<Column gap="4">
|
||||
<FormField label={t(labels.name)} name="name" rules={{ required: t(labels.required) }}>
|
||||
<TextField autoComplete="off" autoFocus />
|
||||
</FormField>
|
||||
<Row justifyContent="flex-end" gap="3">
|
||||
{onCancel && (
|
||||
<Button isDisabled={isPending} onPress={onCancel}>
|
||||
{t(labels.cancel)}
|
||||
</Button>
|
||||
)}
|
||||
<FormSubmitButton variant="primary" isDisabled={isPending}>
|
||||
{t(labels.add)}
|
||||
</FormSubmitButton>
|
||||
</Row>
|
||||
</Column>
|
||||
<Form
|
||||
onSubmit={handleSubmit}
|
||||
error={getErrorMessage(error)}
|
||||
defaultValues={{ name: '', allowFilter: true, theme: 'system' }}
|
||||
>
|
||||
{({ watch, setValue }) => (
|
||||
<Column gap="4">
|
||||
<FormField label={t(labels.name)} name="name" rules={{ required: t(labels.required) }}>
|
||||
<TextField autoComplete="off" autoFocus />
|
||||
</FormField>
|
||||
<Row gap="6">
|
||||
<FormField label={t(labels.filters)} name="allowFilter">
|
||||
<Switch
|
||||
isSelected={watch('allowFilter')}
|
||||
onChange={value => setValue('allowFilter', value, { shouldDirty: true })}
|
||||
>
|
||||
{t(labels.filtersEnabled)}
|
||||
</Switch>
|
||||
</FormField>
|
||||
<FormField label={t(labels.theme)} name="theme">
|
||||
<ThemeModeSelector
|
||||
value={watch('theme')}
|
||||
includeSystem
|
||||
onChange={value => setValue('theme', value, { shouldDirty: true })}
|
||||
/>
|
||||
</FormField>
|
||||
</Row>
|
||||
<Row justifyContent="flex-end" gap="3">
|
||||
{onCancel && (
|
||||
<Button isDisabled={isPending} onPress={onCancel}>
|
||||
{t(labels.cancel)}
|
||||
</Button>
|
||||
)}
|
||||
<FormSubmitButton variant="primary" isDisabled={isPending}>
|
||||
{t(labels.add)}
|
||||
</FormSubmitButton>
|
||||
</Row>
|
||||
</Column>
|
||||
)}
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -7,10 +7,12 @@ import {
|
||||
Label,
|
||||
Loading,
|
||||
Row,
|
||||
Switch,
|
||||
TextField,
|
||||
} from '@umami/react-zen';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useApi, useConfig, useMessages, useModified } from '@/components/hooks';
|
||||
import { ThemeModeSelector } from '@/components/input/ThemeModeSelector';
|
||||
|
||||
export function SimpleShareEditForm({
|
||||
shareId,
|
||||
@@ -49,7 +51,7 @@ export function SimpleShareEditForm({
|
||||
loadShare();
|
||||
}, [get, modified, shareId]);
|
||||
|
||||
const handleSubmit = async (data: { name: string }) => {
|
||||
const handleSubmit = async (data: { name: string; allowFilter?: boolean; theme?: string }) => {
|
||||
setIsPending(true);
|
||||
setError(null);
|
||||
|
||||
@@ -57,7 +59,11 @@ export function SimpleShareEditForm({
|
||||
await post(`/share/id/${shareId}`, {
|
||||
name: data.name,
|
||||
slug: share.slug,
|
||||
parameters: share.parameters || {},
|
||||
parameters: {
|
||||
...(share.parameters || {}),
|
||||
allowFilter: data.allowFilter ?? true,
|
||||
theme: data.theme === 'system' ? undefined : data.theme,
|
||||
},
|
||||
});
|
||||
|
||||
touch('shares');
|
||||
@@ -78,27 +84,50 @@ export function SimpleShareEditForm({
|
||||
<Form
|
||||
onSubmit={handleSubmit}
|
||||
error={getErrorMessage(error)}
|
||||
defaultValues={{ name: share?.name || '' }}
|
||||
defaultValues={{
|
||||
name: share?.name || '',
|
||||
allowFilter: share?.parameters?.allowFilter ?? true,
|
||||
theme: share?.parameters?.theme || 'system',
|
||||
}}
|
||||
>
|
||||
<Column gap="6">
|
||||
<Column>
|
||||
<Label>{t(labels.shareUrl)}</Label>
|
||||
<TextField value={getUrl(share?.slug || '')} isReadOnly allowCopy />
|
||||
{({ watch, setValue }) => (
|
||||
<Column gap="6">
|
||||
<Column>
|
||||
<Label>{t(labels.shareUrl)}</Label>
|
||||
<TextField value={getUrl(share?.slug || '')} isReadOnly allowCopy />
|
||||
</Column>
|
||||
<FormField label={t(labels.name)} name="name" rules={{ required: t(labels.required) }}>
|
||||
<TextField autoComplete="off" autoFocus />
|
||||
</FormField>
|
||||
<Row gap="6">
|
||||
<FormField label={t(labels.filters)} name="allowFilter">
|
||||
<Switch
|
||||
isSelected={watch('allowFilter')}
|
||||
onChange={value => setValue('allowFilter', value, { shouldDirty: true })}
|
||||
>
|
||||
{t(labels.filtersEnabled)}
|
||||
</Switch>
|
||||
</FormField>
|
||||
<FormField label={t(labels.theme)} name="theme">
|
||||
<ThemeModeSelector
|
||||
value={watch('theme')}
|
||||
includeSystem
|
||||
onChange={value => setValue('theme', value, { shouldDirty: true })}
|
||||
/>
|
||||
</FormField>
|
||||
</Row>
|
||||
<Row justifyContent="flex-end" paddingTop="3" gap="3">
|
||||
{onClose && (
|
||||
<Button isDisabled={isPending} onPress={onClose}>
|
||||
{t(labels.cancel)}
|
||||
</Button>
|
||||
)}
|
||||
<FormSubmitButton variant="primary" isDisabled={isPending}>
|
||||
{t(labels.save)}
|
||||
</FormSubmitButton>
|
||||
</Row>
|
||||
</Column>
|
||||
<FormField label={t(labels.name)} name="name" rules={{ required: t(labels.required) }}>
|
||||
<TextField autoComplete="off" autoFocus />
|
||||
</FormField>
|
||||
<Row justifyContent="flex-end" paddingTop="3" gap="3">
|
||||
{onClose && (
|
||||
<Button isDisabled={isPending} onPress={onClose}>
|
||||
{t(labels.cancel)}
|
||||
</Button>
|
||||
)}
|
||||
<FormSubmitButton variant="primary" isDisabled={isPending}>
|
||||
{t(labels.save)}
|
||||
</FormSubmitButton>
|
||||
</Row>
|
||||
</Column>
|
||||
)}
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user