memoize options

This commit is contained in:
Alex Holliday committed 2026-08-11 17:50:47 -07:00
1 parent 7dd378f085
commit 521827e1d0
4 files changed
+111 -83

No files matched your search

+90 -55
View File
@@ -57,6 +57,11 @@ import { FormSelectField } from "@/Components/inputs/forms/FormSelectField";
import { FormSliderField } from "@/Components/inputs/forms/FormSliderField"; import { FormSliderField } from "@/Components/inputs/forms/FormSliderField";
import { FormSwitchField } from "@/Components/inputs/forms/FormSwitchField"; import { FormSwitchField } from "@/Components/inputs/forms/FormSwitchField";
const httpMethodOptions = HttpMethods.map((method) => ({
value: method,
label: method,
}));
interface GeneralSettingsConfig { interface GeneralSettingsConfig {
urlLabel: string; urlLabel: string;
urlPlaceholder: string; urlPlaceholder: string;
@@ -375,6 +380,83 @@ const CreateMonitorPage = () => {
[t] [t]
); );
const typeOptions = useMemo(
() =>
SelectableMonitorTypes.map((type) => ({
value: type,
label: t(`pages.common.monitors.monitorTypes.${monitorTypeLabelKey[type]}`),
description: t(
`pages.createMonitor.form.type.${monitorTypeLabelKey[type]}Description`
),
})),
[t]
);
const strategyOptions = useMemo(
() =>
PageSpeedStrategies.map((strategy) => ({
value: strategy,
label: t(`pages.createMonitor.form.general.option.strategy.${strategy}`),
})),
[t]
);
const gameOptions = useMemo(
() => [
{
value: "",
label: t("pages.createMonitor.form.general.option.game.placeholder"),
},
...Object.entries(games ?? {}).map(([key, game]) => ({
value: key,
label: game.name,
})),
],
[games, t]
);
const dnsRecordTypeOptions = useMemo(
() =>
DnsRecordTypes.map((recordType) => ({
value: recordType,
label: t(
`pages.createMonitor.form.general.option.dnsRecordType.value.${recordType}`
),
})),
[t]
);
const intervalOptions = useMemo(
() =>
MonitorIntervalOptions.map((option) => ({
value: option.value,
label: t(
`pages.createMonitor.form.frequency.option.frequency.value.${option.labelKey}`
),
})),
[t]
);
const matchMethodOptions = useMemo(
() =>
MonitorMatchMethods.map((method) => ({
value: method,
label: t(`pages.createMonitor.form.advanced.option.matchMethod.${method}`),
})),
[t]
);
const geoCheckIntervalOptions = useMemo(
() =>
GeoCheckIntervalOptions.map((option) => ({
value: option.value,
label: t(
`pages.createMonitor.form.geoChecks.option.interval.value.${option.labelKey}`
),
})),
[t]
);
return ( return (
<FormProvider {...form}> <FormProvider {...form}>
<BasePage <BasePage
@@ -401,15 +483,7 @@ const CreateMonitorPage = () => {
rightContent={ rightContent={
<FormRadioGroup <FormRadioGroup
name={"type"} name={"type"}
options={SelectableMonitorTypes.map((type) => ({ options={typeOptions}
value: type,
label: t(
`pages.common.monitors.monitorTypes.${monitorTypeLabelKey[type]}`
),
description: t(
`pages.createMonitor.form.type.${monitorTypeLabelKey[type]}Description`
),
}))}
/> />
} }
/> />
@@ -451,12 +525,7 @@ const CreateMonitorPage = () => {
fieldLabel={t( fieldLabel={t(
"pages.createMonitor.form.general.option.strategy.label" "pages.createMonitor.form.general.option.strategy.label"
)} )}
options={PageSpeedStrategies.map((strategy) => ({ options={strategyOptions}
value: strategy,
label: t(
`pages.createMonitor.form.general.option.strategy.${strategy}`
),
}))}
fallbackValue={DefaultPageSpeedStrategy} fallbackValue={DefaultPageSpeedStrategy}
/> />
)} )}
@@ -477,18 +546,7 @@ const CreateMonitorPage = () => {
<FormSelectField <FormSelectField
name="gameId" name="gameId"
fieldLabel={t("pages.createMonitor.form.general.option.game.label")} fieldLabel={t("pages.createMonitor.form.general.option.game.label")}
options={[ options={gameOptions}
{
value: "",
label: t(
"pages.createMonitor.form.general.option.game.placeholder"
),
},
...Object.entries(games ?? {}).map(([key, game]) => ({
value: key,
label: game.name,
})),
]}
/> />
)} )}
@@ -532,12 +590,7 @@ const CreateMonitorPage = () => {
fieldLabel={t( fieldLabel={t(
"pages.createMonitor.form.general.option.dnsRecordType.label" "pages.createMonitor.form.general.option.dnsRecordType.label"
)} )}
options={DnsRecordTypes.map((recordType) => ({ options={dnsRecordTypeOptions}
value: recordType,
label: t(
`pages.createMonitor.form.general.option.dnsRecordType.value.${recordType}`
),
}))}
/> />
)} )}
<FormTextField <FormTextField
@@ -560,12 +613,7 @@ const CreateMonitorPage = () => {
fieldLabel={t( fieldLabel={t(
"pages.createMonitor.form.frequency.option.frequency.label" "pages.createMonitor.form.frequency.option.frequency.label"
)} )}
options={MonitorIntervalOptions.map((option) => ({ options={intervalOptions}
value: option.value,
label: t(
`pages.createMonitor.form.frequency.option.frequency.value.${option.labelKey}`
),
}))}
/> />
} }
/> />
@@ -729,10 +777,7 @@ const CreateMonitorPage = () => {
setValue("jsonPath", ""); setValue("jsonPath", "");
} }
}} }}
options={HttpMethods.map((method) => ({ options={httpMethodOptions}
value: method,
label: method,
}))}
/> />
<Typography <Typography
component="span" component="span"
@@ -771,12 +816,7 @@ const CreateMonitorPage = () => {
"pages.createMonitor.form.advanced.option.matchMethod.label" "pages.createMonitor.form.advanced.option.matchMethod.label"
)} )}
fallbackValue={DefaultMonitorMatchMethod} fallbackValue={DefaultMonitorMatchMethod}
options={MonitorMatchMethods.map((method) => ({ options={matchMethodOptions}
value: method,
label: t(
`pages.createMonitor.form.advanced.option.matchMethod.${method}`
),
}))}
/> />
<FormTextField <FormTextField
@@ -842,12 +882,7 @@ const CreateMonitorPage = () => {
fieldLabel={t( fieldLabel={t(
"pages.createMonitor.form.geoChecks.option.interval.label" "pages.createMonitor.form.geoChecks.option.interval.label"
)} )}
options={GeoCheckIntervalOptions.map((option) => ({ options={geoCheckIntervalOptions}
value: option.value,
label: t(
`pages.createMonitor.form.geoChecks.option.interval.value.${option.labelKey}`
),
}))}
/> />
</Stack> </Stack>
)} )}
+7 -17
View File
@@ -35,7 +35,8 @@ import {
type ThemeMode, type ThemeMode,
type ChartType, type ChartType,
} from "@/Features/UI/uiSlice.js"; } from "@/Features/UI/uiSlice.js";
import timezones from "@/Utils/timezones.json"; import { timezoneOptions } from "@/Utils/timezoneOptions";
import type { TimezoneOption } from "@/Utils/timezoneOptions";
import type { RootState } from "@/Types/state"; import type { RootState } from "@/Types/state";
import { CHECK_TTL_SENTINEL } from "@/Types/Check"; import { CHECK_TTL_SENTINEL } from "@/Types/Check";
import { FormTextField } from "@/Components/inputs/forms/FormTextField"; import { FormTextField } from "@/Components/inputs/forms/FormTextField";
@@ -43,11 +44,6 @@ import { FormSliderField } from "@/Components/inputs/forms/FormSliderField";
import { FormSwitchField } from "@/Components/inputs/forms/FormSwitchField"; import { FormSwitchField } from "@/Components/inputs/forms/FormSwitchField";
import { FormNumberField } from "@/Components/inputs/forms/FormNumberField"; import { FormNumberField } from "@/Components/inputs/forms/FormNumberField";
interface Timezone {
id: string;
name: string;
}
interface SettingsResponse { interface SettingsResponse {
settings: any; settings: any;
pagespeedKeySet: boolean; pagespeedKeySet: boolean;
@@ -141,16 +137,10 @@ export const SettingsPage = () => {
const user = useSelector((state: RootState) => state.auth.user); const user = useSelector((state: RootState) => state.auth.user);
// Convert timezones to match AutoComplete format (id instead of _id)
const timezoneOptions: Timezone[] = timezones.map((tz) => ({
id: tz._id,
name: tz.name,
}));
const selectedTimezone = const selectedTimezone =
timezoneOptions.find((tz) => tz.id === selectedTimezoneId) ?? null; timezoneOptions.find((tz) => tz.id === selectedTimezoneId) ?? null;
const handleTimezoneChange = (newValue: Timezone | null) => { const handleTimezoneChange = (newValue: TimezoneOption | null) => {
if (!newValue?.id) return; if (!newValue?.id) return;
dispatch(setTimezone({ timezone: newValue.id })); dispatch(setTimezone({ timezone: newValue.id }));
}; };
@@ -317,12 +307,12 @@ export const SettingsPage = () => {
<Autocomplete <Autocomplete
value={selectedTimezone} value={selectedTimezone}
options={timezoneOptions} options={timezoneOptions}
getOptionLabel={(option: Timezone) => option.name} getOptionLabel={(option: TimezoneOption) => option.name}
isOptionEqualToValue={(option: Timezone, value: Timezone) => isOptionEqualToValue={(option: TimezoneOption, value: TimezoneOption) =>
option.id === value.id option.id === value.id
} }
onChange={(_, newValue) => { onChange={(_, newValue: TimezoneOption | null) => {
handleTimezoneChange(newValue as Timezone | null); handleTimezoneChange(newValue);
}} }}
fieldLabel={t("pages.settings.form.timezone.option.timezone.label")} fieldLabel={t("pages.settings.form.timezone.option.timezone.label")}
/> />
+1 -11
View File
@@ -17,7 +17,7 @@ import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi";
import type { Monitor } from "@/Types/Monitor"; import type { Monitor } from "@/Types/Monitor";
import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage"; import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage";
import { getMonitorTypeLabel } from "@/Types/StatusPage"; import { getMonitorTypeLabel } from "@/Types/StatusPage";
import timezones from "@/Utils/timezones.json"; import { timezoneOptions } from "@/Utils/timezoneOptions";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { mutate } from "swr"; import { mutate } from "swr";
import axios from "axios"; import axios from "axios";
@@ -66,16 +66,6 @@ const monitorsUrl = (() => {
return `/monitors/team?${params.toString()}`; return `/monitors/team?${params.toString()}`;
})(); })();
interface TimezoneOption {
_id: string;
name: string;
}
const timezoneOptions = timezones.map((tz: TimezoneOption) => ({
id: tz._id,
name: tz.name,
}));
const buildStatusPageKey = (slug: string | null | undefined) => const buildStatusPageKey = (slug: string | null | undefined) =>
slug ? `/status-page/${slug}?type=uptime&type=infrastructure` : null; slug ? `/status-page/${slug}?type=uptime&type=infrastructure` : null;
+13
View File
@@ -0,0 +1,13 @@
import timezones from "@/Utils/timezones.json";
export interface TimezoneOption {
id: string;
name: string;
}
// timezones.json uses Mongo-style _id; remap once at module load to the
// { id, name } shape AutoComplete expects.
export const timezoneOptions: TimezoneOption[] = timezones.map((tz) => ({
id: tz._id,
name: tz.name,
}));