clean-up session replay settings options

This commit is contained in:
Francis Cao committed 2026-03-02 18:41:51 -08:00
1 parent 39015ccedb
commit e9ac09ec5d
4 files changed
+78 -71

No files matched your search

@@ -1,10 +1,11 @@
import {
Button,
Column,
Form,
FormButtons,
FormField,
FormSubmitButton,
Label,
ListItem,
Row,
Select,
Slider,
Switch,
TextField,
} from '@umami/react-zen';
@@ -16,27 +17,30 @@ interface ReplayConfig {
maskLevel?: string;
maxDuration?: number;
blockSelector?: string;
retentionDays?: number;
}
export function WebsiteReplaySettings({ websiteId }: { websiteId: string }) {
const website = useWebsite();
const { t, labels, messages } = useMessages();
const { mutateAsync, touch, toast } = useUpdateQuery(`/websites/${websiteId}`);
const { mutateAsync, touch, toast, isPending } = useUpdateQuery(`/websites/${websiteId}`);
const [enabled, setEnabled] = useState(website?.replayEnabled ?? false);
const config = (website?.replayConfig as ReplayConfig) || {};
const handleSubmit = async (data: any) => {
const [sampleRate, setSampleRate] = useState(config.sampleRate ?? 0.15);
const [maskLevel, setMaskLevel] = useState(config.maskLevel ?? 'strict');
const [maxDuration, setMaxDuration] = useState(String(config.maxDuration ?? 300000));
const [blockSelector, setBlockSelector] = useState(config.blockSelector ?? '');
const handleSave = async () => {
await mutateAsync(
{
replayEnabled: enabled,
replayConfig: {
sampleRate: parseFloat(data.sampleRate) || 1,
maskLevel: data.maskLevel || 'moderate',
maxDuration: parseInt(data.maxDuration, 10) || 600000,
blockSelector: data.blockSelector || '',
retentionDays: parseInt(data.retentionDays, 10) || 30,
sampleRate,
maskLevel,
maxDuration: parseInt(maxDuration, 10) || 300000,
...(blockSelector && { blockSelector }),
},
},
{
@@ -50,47 +54,51 @@ export function WebsiteReplaySettings({ websiteId }: { websiteId: string }) {
};
return (
<Form
onSubmit={handleSubmit}
values={{
sampleRate: String(config.sampleRate ?? 1),
maskLevel: config.maskLevel ?? 'moderate',
maxDuration: String(config.maxDuration ?? 600000),
blockSelector: config.blockSelector ?? '',
retentionDays: String(config.retentionDays ?? 30),
}}
>
<Column gap="4">
<Label>{t(labels.replays)}</Label>
<Switch isSelected={enabled} onChange={setEnabled}>
{t(labels.replayEnabled)}
</Switch>
{enabled && (
<>
<FormField name="sampleRate" label={t(labels.sampleRate)}>
<TextField />
</FormField>
<FormField
name="maskLevel"
label={`${t(labels.maskLevel)} (strict / moderate / relaxed)`}
>
<TextField />
</FormField>
<FormField name="maxDuration" label={`${t(labels.maxDuration)} (ms)`}>
<TextField />
</FormField>
<FormField name="blockSelector" label={t(labels.blockSelector)}>
<TextField />
</FormField>
<FormField name="retentionDays" label={t(labels.retentionDays)}>
<TextField />
</FormField>
</>
)}
</Column>
<FormButtons>
<FormSubmitButton variant="primary">{t(labels.save)}</FormSubmitButton>
</FormButtons>
</Form>
<Column gap="4">
<Label>{t(labels.replays)}</Label>
<Switch isSelected={enabled} onChange={setEnabled}>
{t(labels.replayEnabled)}
</Switch>
{enabled && (
<>
<Slider
label={t(labels.sampleRate)}
minValue={0.05}
maxValue={1}
step={0.05}
value={sampleRate}
onChange={v => setSampleRate(Array.isArray(v) ? v[0] : v)}
showValue
formatOptions={{ style: 'percent', maximumFractionDigits: 0 }}
style={{ maxWidth: '360px' }}
/>
<Column gap="1">
<Label>{t(labels.maskLevel)}</Label>
<Select value={maskLevel} onChange={setMaskLevel} style={{ maxWidth: '360px' }}>
<ListItem id="strict">strict</ListItem>
<ListItem id="moderate">moderate</ListItem>
</Select>
</Column>
<Column gap="1">
<Label>{t(labels.maxDuration)}</Label>
<Select value={maxDuration} onChange={setMaxDuration} style={{ maxWidth: '360px' }}>
<ListItem id="300000">5 minutes</ListItem>
<ListItem id="600000">10 minutes</ListItem>
<ListItem id="900000">15 minutes</ListItem>
<ListItem id="1200000">20 minutes</ListItem>
</Select>
</Column>
<Column gap="1">
<Label>{t(labels.blockSelector)}</Label>
<TextField value={blockSelector} onChange={setBlockSelector} />
</Column>
</>
)}
<Row>
<Button variant="primary" onPress={handleSave} isDisabled={isPending}>
{t(labels.save)}
</Button>
</Row>
</Column>
);
}
@@ -34,7 +34,16 @@ export function WebsiteTrackingCode({
if (website?.replayEnabled) {
const recorderUrl = getUrl(RECORDER_NAME);
code += `\n<script defer src="${recorderUrl}" data-website-id="${websiteId}"></script>`;
const replayConfig = (website.replayConfig as any) || {};
let recorderAttrs = `data-website-id="${websiteId}"`;
if (replayConfig.sampleRate !== undefined)
recorderAttrs += ` data-sample-rate="${replayConfig.sampleRate}"`;
if (replayConfig.maskLevel) recorderAttrs += ` data-mask-level="${replayConfig.maskLevel}"`;
if (replayConfig.maxDuration !== undefined)
recorderAttrs += ` data-max-duration="${replayConfig.maxDuration}"`;
if (replayConfig.blockSelector)
recorderAttrs += ` data-block-selector="${replayConfig.blockSelector}"`;
code += `\n<script defer src="${recorderUrl}" ${recorderAttrs}></script>`;
}
return (
+1 -2
View File
@@ -46,10 +46,9 @@ export async function POST(
replayConfig: z
.object({
sampleRate: z.number().min(0).max(1).optional(),
maskLevel: z.enum(['strict', 'moderate', 'relaxed']).optional(),
maskLevel: z.enum(['strict', 'moderate']).optional(),
maxDuration: z.number().int().positive().optional(),
blockSelector: z.string().optional(),
retentionDays: z.number().int().positive().optional(),
})
.nullable()
.optional(),
+5 -14
View File
@@ -10,9 +10,9 @@ import { record } from 'rrweb';
const website = attr(`${_data}website-id`);
const hostUrl = attr(`${_data}host-url`);
const sampleRate = parseFloat(attr(`${_data}sample-rate`) || '1');
const maskLevel = attr(`${_data}mask-level`) || 'moderate';
const maxDuration = parseInt(attr(`${_data}max-duration`) || '600000', 10);
const sampleRate = parseFloat(attr(`${_data}sample-rate`) || '0.15');
const maskLevel = attr(`${_data}mask-level`) || 'strict';
const maxDuration = parseInt(attr(`${_data}max-duration`) || '300000', 10);
const blockSelector = attr(`${_data}block-selector`) || '';
if (!website) return;
@@ -85,20 +85,11 @@ import { record } from 'rrweb';
case 'strict':
return {
maskAllInputs: true,
maskTextContent: true,
maskAllText: true,
maskTextSelector: '*',
};
case 'relaxed':
default: // moderate
return {
maskAllInputs: true,
maskTextContent: false,
maskAllText: false,
};
default:
return {
maskAllInputs: true,
maskTextContent: false,
maskAllText: false,
};
}
};