fix(monitors): stop selecting more regions than the limit allows
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m31s
ci / test (pull_request) Successful in 13m28s
ci / build (pull_request) Successful in 10m23s
ci / image (pull_request) Successful in 5m51s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m31s
ci / test (pull_request) Successful in 13m28s
ci / build (pull_request) Successful in 10m23s
ci / image (pull_request) Successful in 5m51s
The monitor form let users tick any number of regions and only the save failed with the region limit. The form now knows the organization's regions-per-monitor limit: it says how many regions a monitor may use, counts the ticked ones, and once the limit is reached locks the other regions that count towards it, in the list and on the map, until one is unticked. Own regions stay free unless they count. The server still enforces the limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
e9984f8271
commit
3a7bed5a3e
9 files changed
+1383
-1187
No files matched your search
@@ -180,6 +180,13 @@
|
||||
.is-flipped > .region-pin { padding: 6px 8px 6px 11px; }
|
||||
.is-compact > .region-pin { padding: 6px 8px; }
|
||||
}
|
||||
/* Regions locked by the monitor's region limit. */
|
||||
label.is-locked { opacity: 0.5; cursor: not-allowed; }
|
||||
/* Opaque, so stacked pins do not show through each other. */
|
||||
.region-pin.is-disabled { color: var(--color-muted); cursor: not-allowed; box-shadow: none; }
|
||||
.region-pin.is-disabled .region-pin-dot { background: var(--color-unknown); }
|
||||
.is-hover > .region-pin.is-disabled,
|
||||
.region-pin.is-disabled:hover { transform: none; }
|
||||
.region-pin:focus-visible { outline: 2px solid var(--color-maintenance); outline-offset: 2px; }
|
||||
.region-pick {
|
||||
width: 14px;
|
||||
|
||||
@@ -726,8 +726,12 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
|
||||
pin.classList.toggle("is-checked", item.checked);
|
||||
row.classList.toggle("is-checked", item.checked);
|
||||
pin.setAttribute("aria-pressed", String(item.checked));
|
||||
// Locked once the monitor uses as many regions as it may.
|
||||
pin.classList.toggle("is-disabled", item.disabled);
|
||||
pin.setAttribute("aria-disabled", String(item.disabled));
|
||||
};
|
||||
paint();
|
||||
item.closest("[data-region-limit]")?.addEventListener("regionlimit", paint);
|
||||
item.addEventListener("change", () => {
|
||||
paint();
|
||||
// A region ticked in the list comes into view on the map.
|
||||
@@ -802,5 +806,24 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
|
||||
pick.setLngLat(point);
|
||||
});
|
||||
};
|
||||
// A monitor may use only so many counted regions: once they are all
|
||||
// ticked, the other counted ones are locked until one is unticked. The
|
||||
// server enforces the limit either way.
|
||||
for (const list of document.querySelectorAll("[data-region-limit]")) {
|
||||
const limit = Number(list.dataset.regionLimit);
|
||||
const counted = [...list.querySelectorAll("input[data-counted]")];
|
||||
const count = list.closest("fieldset")?.querySelector("[data-region-count]");
|
||||
const update = () => {
|
||||
const n = counted.filter((i) => i.checked).length;
|
||||
for (const i of counted) {
|
||||
i.disabled = !i.checked && n >= limit;
|
||||
i.closest("label")?.classList.toggle("is-locked", i.disabled);
|
||||
}
|
||||
if (count) count.textContent = `(${n}/${limit})`;
|
||||
list.dispatchEvent(new Event("regionlimit"));
|
||||
};
|
||||
list.addEventListener("change", update);
|
||||
update();
|
||||
}
|
||||
for (const box of document.querySelectorAll("[data-region-map]")) initRegionMap(box);
|
||||
})();
|
||||
@@ -726,6 +726,9 @@ type RegionOption struct {
|
||||
Lat, Lon *float64
|
||||
}
|
||||
|
||||
// Counted reports whether r counts towards the form's region limit.
|
||||
func (f MonitorForm) Counted(r RegionOption) bool { return !r.Own || f.OwnRegionsCount }
|
||||
|
||||
// RegionsMapped reports whether any region of the form has a location.
|
||||
func (f MonitorForm) RegionsMapped() bool {
|
||||
for _, r := range f.Regions {
|
||||
@@ -753,6 +756,10 @@ type MonitorForm struct {
|
||||
TopLevel bool
|
||||
Regions []RegionOption
|
||||
MinInterval int
|
||||
// RegionLimit caps the counted regions a monitor uses; 0 is unlimited.
|
||||
// Own regions count only with OwnRegionsCount.
|
||||
RegionLimit int
|
||||
OwnRegionsCount bool
|
||||
// Type-specific settings.
|
||||
Method string
|
||||
ExpectStatus string
|
||||
@@ -1132,10 +1139,25 @@ templ MonitorEditor(p Page, me Me, f MonitorForm) {
|
||||
<fieldset class="mb-4" data-hide-for="heartbeat alertmanager" hidden?={ passiveType(f.Type) } disabled?={ f.Type == "heartbeat" }>
|
||||
<legend class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.regions") }</legend>
|
||||
<p class="mb-2 text-xs text-muted">{ i18n.T(ctx, "monitors.regions_hint") }</p>
|
||||
if f.RegionLimit > 0 {
|
||||
<p class="mb-2 text-xs text-muted" data-region-limit-hint>
|
||||
if f.OwnRegionsCount {
|
||||
{ i18n.T(ctx, "monitors.regions_limit", "Max", f.RegionLimit) }
|
||||
} else {
|
||||
{ i18n.T(ctx, "monitors.platform_regions_limit", "Max", f.RegionLimit) }
|
||||
}
|
||||
<span class="font-mono" data-region-count></span>
|
||||
</p>
|
||||
}
|
||||
if f.RegionsMapped() {
|
||||
@regionMap("select", "#monitor-regions")
|
||||
}
|
||||
<div id="monitor-regions">
|
||||
<div
|
||||
id="monitor-regions"
|
||||
if f.RegionLimit > 0 {
|
||||
data-region-limit={ strconv.Itoa(f.RegionLimit) }
|
||||
}
|
||||
>
|
||||
for _, r := range f.Regions {
|
||||
<label class="-mx-2 mb-0.5 flex items-center gap-2 px-2 py-1 text-sm">
|
||||
<input
|
||||
@@ -1151,6 +1173,9 @@ templ MonitorEditor(p Page, me Me, f MonitorForm) {
|
||||
}
|
||||
data-name={ r.Name }
|
||||
data-live={ strconv.FormatInt(r.Live, 10) }
|
||||
if f.Counted(r) {
|
||||
data-counted
|
||||
}
|
||||
/>
|
||||
{ r.Name }
|
||||
<span class="font-mono text-xs text-muted">
|
||||
|
||||
+1265
-1186
File diff suppressed because it is too large.
Load diff
+40
@@ -0,0 +1,40 @@
|
||||
<!doctype html><html><body data-msg-network="net">
|
||||
<form id="monitor"><fieldset>
|
||||
<p><span data-region-count></span></p>
|
||||
<div id="monitor-regions" data-region-limit="2">
|
||||
<label><input type="checkbox" name="regions" value="own" data-multi checked></label>
|
||||
<label><input type="checkbox" name="regions" value="a" data-multi data-counted checked></label>
|
||||
<label><input type="checkbox" name="regions" value="b" data-multi data-counted></label>
|
||||
<label><input type="checkbox" name="regions" value="c" data-multi data-counted></label>
|
||||
</div>
|
||||
</fieldset></form>
|
||||
<pre id="out"></pre>
|
||||
<script>window.axios = { create: () => ({ interceptors: { request: { use() {} } }, request: async () => ({ data: {} }) }) };</script>
|
||||
<script src="../assets/app.js"></script>
|
||||
<script>
|
||||
(async () => {
|
||||
const out = [];
|
||||
const check = (name, got, want) => {
|
||||
const g = JSON.stringify(got), w = JSON.stringify(want);
|
||||
out.push((g === w ? "PASS " : "FAIL ") + name + (g === w ? "" : " got=" + g + " want=" + w));
|
||||
};
|
||||
const box = (v) => document.querySelector(`[value="${v}"]`);
|
||||
const locked = () => ["own", "a", "b", "c"].filter((v) => box(v).disabled);
|
||||
const count = () => document.querySelector("[data-region-count]").textContent;
|
||||
check("below the limit nothing is locked", locked(), []);
|
||||
check("count shown", count(), "(1/2)");
|
||||
box("b").click();
|
||||
check("at the limit the other counted regions lock", locked(), ["c"]);
|
||||
check("locked row marked", box("c").closest("label").classList.contains("is-locked"), true);
|
||||
check("count follows", count(), "(2/2)");
|
||||
box("c").click();
|
||||
check("a locked region cannot be ticked", box("c").checked, false);
|
||||
box("own").click();
|
||||
box("own").click();
|
||||
check("uncounted regions stay free", box("own").checked, true);
|
||||
box("a").click();
|
||||
check("unticking frees them again", locked(), []);
|
||||
document.getElementById("out").textContent = out.join("\n");
|
||||
})();
|
||||
</script>
|
||||
</body></html>
|
||||
Reference in new issue
Block a user