Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix Alerts

Blue Mouse39b8104c7d6d
diff --git a/web/app/pages/Project/tabs/Alerts/ProjectAlertsSettings.tsx b/web/app/pages/Project/tabs/Alerts/ProjectAlertsSettings.tsx
index dc3052605..091b53591 100644
--- a/web/app/pages/Project/tabs/Alerts/ProjectAlertsSettings.tsx
+++ b/web/app/pages/Project/tabs/Alerts/ProjectAlertsSettings.tsx
@@ -266,6 +266,35 @@ const ProjectAlertsSettings = ({
form.alertOnEveryCustomEvent
)
+ useEffect(() => {
+ if (!shouldIncludeQueryFields) {
+ return
+ }
+
+ if (
+ form.queryCondition !== undefined &&
+ form.queryCondition !== null &&
+ form.queryTime !== undefined &&
+ form.queryTime !== null &&
+ form.queryValue !== undefined &&
+ form.queryValue !== null
+ ) {
+ return
+ }
+
+ setForm((prevForm) => ({
+ ...prevForm,
+ queryCondition: prevForm.queryCondition ?? QUERY_CONDITION.GREATER_THAN,
+ queryValue: prevForm.queryValue ?? 10,
+ queryTime: prevForm.queryTime ?? QUERY_TIME.LAST_1_HOUR,
+ }))
+ }, [
+ shouldIncludeQueryFields,
+ form.queryCondition,
+ form.queryValue,
+ form.queryTime,
+ ])
+
const onDeleteAlert = () => {
if (!alertId || fetcher.state !== 'idle') return
@@ -390,6 +419,21 @@ const ProjectAlertsSettings = ({
name='queryMetric'
value={form.queryMetric || QUERY_METRIC.PAGE_VIEWS}
/>
+ <input
+ type='hidden'
+ name='active'
+ value={String(Boolean(form.active))}
+ />
+ <input
+ type='hidden'
+ name='alertOnEveryCustomEvent'
+ value={String(Boolean(form.alertOnEveryCustomEvent))}
+ />
+ <input
+ type='hidden'
+ name='alertOnNewErrorsOnly'
+ value={String(Boolean(form.alertOnNewErrorsOnly))}
+ />
{shouldIncludeQueryFields && form.queryCondition ? (
<input
type='hidden'
@@ -435,7 +479,6 @@ const ProjectAlertsSettings = ({
active: checked,
}))
}
- name='active'
classes={{
label: 'mt-4',
}}
@@ -490,7 +533,6 @@ const ProjectAlertsSettings = ({
alertOnEveryCustomEvent: checked,
}))
}
- name='alertOnEveryCustomEvent'
classes={{
label: 'mt-4',
}}
@@ -507,7 +549,6 @@ const ProjectAlertsSettings = ({
alertOnNewErrorsOnly: checked,
}))
}
- name='alertOnNewErrorsOnly'
classes={{
label: 'mt-4',
}}
@@ -529,7 +570,7 @@ const ProjectAlertsSettings = ({
title={
form.queryCondition
? queryConditionTMapping[form.queryCondition]
- : ''
+ : queryConditionTMapping[QUERY_CONDITION.GREATER_THAN]
}
onSelect={(item) => {
const key = _findKey(
@@ -547,14 +588,14 @@ const ProjectAlertsSettings = ({
selectedItem={
form.queryCondition
? queryConditionTMapping[form.queryCondition]
- : undefined
+ : queryConditionTMapping[QUERY_CONDITION.GREATER_THAN]
}
/>
</div>
<Input
name='queryValue'
label={t('alert.threshold')}
- value={form.queryValue || ''}
+ value={form.queryValue ?? ''}
placeholder='10'
className='mt-4'
onChange={handleInput}
@@ -565,7 +606,11 @@ const ProjectAlertsSettings = ({
id='queryTime'
label={t('alert.time')}
items={_values(queryTimeTMapping)}
- title={form.queryTime ? queryTimeTMapping[form.queryTime] : ''}
+ title={
+ form.queryTime
+ ? queryTimeTMapping[form.queryTime]
+ : queryTimeTMapping[QUERY_TIME.LAST_1_HOUR]
+ }
onSelect={(item) => {
const key = _findKey(
queryTimeTMapping,
@@ -580,7 +625,9 @@ const ProjectAlertsSettings = ({
}}
capitalise
selectedItem={
- form.queryTime ? queryTimeTMapping[form.queryTime] : undefined
+ form.queryTime
+ ? queryTimeTMapping[form.queryTime]
+ : queryTimeTMapping[QUERY_TIME.LAST_1_HOUR]
}
/>
</div>
diff --git a/web/app/pages/Project/tabs/Alerts/ProjectAlertsView.tsx b/web/app/pages/Project/tabs/Alerts/ProjectAlertsView.tsx
index f0dd6579e..4b764aeb6 100644
--- a/web/app/pages/Project/tabs/Alerts/ProjectAlertsView.tsx
+++ b/web/app/pages/Project/tabs/Alerts/ProjectAlertsView.tsx
@@ -385,6 +385,24 @@ const ProjectAlertsInner = ({ deferredData }: ProjectAlertsInnerProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [page, canManageAlerts])
+ // Fallback load for client-side tab switches where deferred data is not present yet.
+ useEffect(() => {
+ if (!canManageAlerts) return
+ if (deferredData.alertsData) return
+ if (isSearchMode) return
+ if (fetcher.state !== 'idle') return
+ if (!_isEmpty(alerts)) return
+
+ loadAlerts(DEFAULT_ALERTS_TAKE, 0)
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [
+ canManageAlerts,
+ deferredData.alertsData,
+ isSearchMode,
+ fetcher.state,
+ alerts,
+ ])
+
const queryMetricTMapping: Record<string, string> = useMemo(() => {
const values = _values(QUERY_METRIC)
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index 82a9c07bb..cd8489ae4 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -83,6 +83,18 @@ function formatDateForBackend(dateStr: string): string {
return `${yyyy}-${mm}-${dd}`
}
+function parseBooleanFormValue(
+ value: FormDataEntryValue | null,
+ fallback = false,
+): boolean {
+ if (value === null) {
+ return fallback
+ }
+
+ const normalised = String(value).toLowerCase()
+ return normalised === 'true' || normalised === 'on'
+}
+
export const links: LinksFunction = () => [
{ rel: 'stylesheet', href: ProjectViewStyle },
]
@@ -723,10 +735,15 @@ export async function action({ request, params }: ActionFunctionArgs) {
const queryTime = formData.get('queryTime')?.toString() || null
const queryCustomEvent =
formData.get('queryCustomEvent')?.toString() || null
- const alertOnNewErrorsOnly =
- formData.get('alertOnNewErrorsOnly') === 'true'
- const alertOnEveryCustomEvent =
- formData.get('alertOnEveryCustomEvent') === 'true'
+ const active = parseBooleanFormValue(formData.get('active'), true)
+ const alertOnNewErrorsOnly = parseBooleanFormValue(
+ formData.get('alertOnNewErrorsOnly'),
+ true,
+ )
+ const alertOnEveryCustomEvent = parseBooleanFormValue(
+ formData.get('alertOnEveryCustomEvent'),
+ false,
+ )
const result = await serverFetch(request, 'alert', {
method: 'POST',
@@ -740,7 +757,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
queryCustomEvent,
alertOnNewErrorsOnly,
alertOnEveryCustomEvent,
- active: true,
+ active,
},
})
@@ -760,7 +777,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
case 'update-alert': {
const alertId = formData.get('alertId')?.toString()
const name = formData.get('name')?.toString()
- const active = formData.get('active') === 'true'
+ const active = parseBooleanFormValue(formData.get('active'))
const queryMetric = formData.get('queryMetric')?.toString()
const queryCondition = formData.get('queryCondition')?.toString() || null
const queryValue = formData.get('queryValue')
@@ -770,10 +787,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
const queryCustomEvent =
formData.get('queryCustomEvent')?.toString() || null
const alertOnNewErrorsOnly = formData.has('alertOnNewErrorsOnly')
- ? formData.get('alertOnNewErrorsOnly') === 'true'
+ ? parseBooleanFormValue(formData.get('alertOnNewErrorsOnly'))
: undefined
const alertOnEveryCustomEvent = formData.has('alertOnEveryCustomEvent')
- ? formData.get('alertOnEveryCustomEvent') === 'true'
+ ? parseBooleanFormValue(formData.get('alertOnEveryCustomEvent'))
: undefined
const body: Record<string, unknown> = {}