Files
jobboard/app/static/remote.js
T

111 lines
5.4 KiB
JavaScript

/* Where a remote role may be worked from, as the pages say it: the words of
* the badge, the picks of the agent's "Remote, open to someone in" chips, and
* how an agent's gates share out between the Where block and Other rules. No
* DOM in it, so `tests/js/remote.test.mjs` can walk it without a browser.
*
* The area is a place token in three spellings, one per layer: `remote:<place
* token>` on the Roles rail and in the address, `<place token>` in a
* `remote_from` gate, and a label on a row (`remote_where`). `anywhere` is
* the one place that is no gazetteer town, region or country.
*/
(function () {
"use strict";
const ANYWHERE = "anywhere";
/** The badge of a row and the reader: "remote · Europe", "remote · anywhere",
"remote · Denmark" from `remote_where`, "remote" alone when the area is
not stated, and the other words (hybrid, onsite) as they are. */
function remoteBadge(role) {
const word = role && role.remote;
if (!word) return "";
const where = word === "remote" ? String(role.remote_where || "").trim() : "";
return where ? `${word} · ${where}` : word;
}
/** A `/api/suggest/places` answer as the value of a `remote_from` gate, or
null when the Where block does not offer it: countries and regions. A
town or a remote token is left out; the block's own country pick is the
plain one. */
function fromSuggestion(s) {
const token = String(s.token || "");
return token.startsWith("country:") || token.startsWith("region:") ? token : null;
}
/** The chip label of a gate value when the server has not said it yet. */
function fallbackLabel(value, countryName) {
if (value === ANYWHERE) return "Anywhere";
if (value === "country:EU") return "Europe";
if (value.startsWith("country:")) return countryName(value.slice(8));
return value.slice(value.indexOf(":") + 1);
}
/** "remote from Denmark, Sweden or Germany", "remote from anywhere". */
function remoteSentence(values, label) {
const names = values.map((v) => (v === ANYWHERE ? "anywhere" : label(v)));
const list = names.length < 2 ? names.join("")
: `${names.slice(0, -1).join(", ")} or ${names[names.length - 1]}`;
return `remote from ${list}`;
}
/** Europe as the starter agents in config.example.yaml spell it: the EEA,
the UK and Switzerland, and EU for a role open anywhere in Europe. */
const EUROPE = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR",
"HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
"IS", "LI", "NO", "GB", "CH", "EU"];
/** What "Only roles in: Europe" sends: Europe, and the countries line too,
since the require gate holds for every role, not only the remote ones. */
function europeWith(countries) {
return [...EUROPE, ...countries.filter((c) => !EUROPE.includes(c))];
}
/** Share out an agent's gates between the Where block and Other rules. Each
Where line takes the first gate it can say whole; the rest are rows. A
must or one-of gate with a weight other than the default stays a row,
since Where would drop it.
The scope line is the first plain require gate on country: `europe`
when it is exactly Europe and the countries line (`europeWith`),
`countries` with its codes as chips otherwise, `everywhere` when there
is none. Any other require gate on country is a row. The remote line is
`scope` for the one-of on `remote: [remote]` (remote, within the
scope), `from` for a one-of on `remote_from`, else `none`; when both
are there `remote_from` wins and the plain one stays a row. */
function distribute(gates) {
const where = { scope: null, countries: null, places: null, remote: { mode: "none", values: [] }, hybrid: null };
const rest = [];
const plain = (g) => g.weight === undefined || g.weight === 1;
const only = (g, value) => g.values.length === 1 && g.values[0] === value;
let must = null;
let older = null;
let from = null;
for (const g of gates) {
if (g.field === "country" && g.mode === "require" && plain(g) && !must) must = g;
else if (g.field === "country" && g.mode === "any" && plain(g) && !where.countries) where.countries = g;
else if (g.field === "place" && g.mode === "any" && plain(g) && !where.places) where.places = g;
else if (g.field === "remote_from" && g.mode === "any" && plain(g) && !from) from = g;
else if (g.field === "remote" && g.mode === "any" && plain(g) && only(g, "remote") && !older) older = g;
else if (g.field === "remote" && g.mode === "prefer" && only(g, "hybrid") && !where.hybrid) where.hybrid = g;
else rest.push(g);
}
if (from) {
where.remote = { mode: "from", values: from.values.map(String) };
if (older) rest.push(older);
} else if (older) {
where.remote = { mode: "scope", values: [] };
}
if (!must) {
where.scope = { kind: "everywhere", values: [] };
} else {
const values = must.values.map(String);
const within = new Set(europeWith(where.countries ? where.countries.values.map(String) : []));
const europe = values.length === within.size && values.every((v) => within.has(v));
where.scope = europe ? { kind: "europe", values: [] } : { kind: "countries", values };
}
return { where, rest };
}
window.remoteHelpers = { ANYWHERE, EUROPE, europeWith, remoteBadge, fromSuggestion, fallbackLabel, remoteSentence, distribute };
})();