Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript
11.6 KB
Raw Blame
/**
 * The layers the analyst added themselves, as the panel reads them.
 *
 * The curated stack in `tools.js` is what Azimut chose; this is what the analyst
 * chose — a KMZ somebody sent them, a My Maps they follow. That is the line
 * between the panel's two sections, not local versus remote: both halves of this
 * feature land in the same list, because the question a row answers is "who put
 * this on my map".
 *
 * Everything here is arithmetic over what the backend returned, so the rules the
 * row states — how fresh it is, how many of its features are drawn, what colour
 * a legend line is — are read off a test rather than off a map
 * (`addedLayers.test.js`). The MapLibre side is `addedLayer.js`; the fetching and
 * parsing are the backend's (`engine/maplayers.py`), and the browser never sees
 * a byte of KML.
 */
import { timeAgo } from '../analysisViews.js';
import { matchesTerms } from '../folderBrowse.js';
import { inPeriod } from './layerDates.js';

/**
 * Colours for a category whose source painted it nothing.
 *
 * A My Maps creator groups by colour far more than by icon, so an honoured
 * colour is the meaning; this is only what fills in for a source that stated
 * none. Picked to stay apart from each other and off the case's own marks, which
 * are the accent — a foreign layer must never read as this case's work.
 */
export const PALETTE = [
  '#5ac8fa',
  '#ffcc66',
  '#a0e57a',
  '#ff8fa3',
  '#c58af9',
  '#6fd3c7',
  '#f3a26d',
  '#9aa7ff',
];

/**
 * Whether a layer follows its source rather than holding a file: a pasted
 * address, or a GeoConfirmed conflict. Those are re-read on Refresh and on case
 * open, and go stale; a file does neither.
 */
export function followed(layer) {
  const kind = layer?.source?.kind;
  return kind === 'url' || kind === 'geoconfirmed';
}

/**
 * The colour a category is drawn in: what the source said, else the palette.
 *
 * Keyed by position rather than by name so two layers holding a "Checkpoints"
 * each do not come out the same colour — the index is within one layer.
 */
export function categoryColour(category, index) {
  return category?.colour || PALETTE[index % PALETTE.length];
}

/** The legend, which is also the filter: one row per category, in the source's
 *  own order of weight. `on` is what the eye beside it shows. */
export function legend(layer) {
  const hidden = new Set(layer?.hidden ?? []);
  // while a period is set, each group counts what the period leaves of it
  const inPeriod = layer?.shownBy;
  return (layer?.categories ?? []).map((category, index) => ({
    name: category.name,
    count: inPeriod ? (inPeriod[category.name] ?? 0) : (category.count ?? 0),
    kinds: category.kinds ?? [],
    colour: categoryColour(category, index),
    // a personal group drawn in one pictogram, shown as that pictogram
    ...(category.symbol ? { symbol: category.symbol } : {}),
    ...(category.icon ? { icon: category.icon, iconMode: category.icon_mode ?? 'image' } : {}),
    on: !hidden.has(category.name),
  }));
}

/**
 * What is loaded, and what is drawn — never the one passing for the other.
 *
 * Visible means "not filtered out here". It is deliberately *not* what is on
 * screen: the engine culls to the viewport and drops symbols that collide, so a
 * screen count would change on every pan and cost a render query per frame to
 * read. A number that moved while nobody touched anything would be worse than no
 * number at all.
 */
export function counts(layer) {
  const loaded = layer?.features ?? 0;
  // a period set on the row, counted by the store from the features in hand
  if (typeof layer?.shown === 'number') return { loaded, visible: layer.shown };
  const hidden = new Set(layer?.hidden ?? []);
  const dropped = (layer?.categories ?? [])
    .filter((category) => hidden.has(category.name))
    .reduce((total, category) => total + (category.count ?? 0), 0);
  return { loaded, visible: Math.max(0, loaded - dropped) };
}

/**
 * `3 200` — grouped, because five digits of features run together otherwise.
 *
 * A narrow no-break space (U+202F), so a count never wraps across two lines of a
 * panel this narrow and reads as two separate numbers.
 */
export const GROUP_SPACE = '\u202f';

export function grouped(value) {
  return String(Math.round(Number(value) || 0)).replace(
    /\B(?=(\d{3})+(?!\d))/g,
    GROUP_SPACE
  );
}

/** The row's counter, stating both numbers whenever they differ. */
export function countLabel(layer) {
  const { loaded, visible } = counts(layer);
  const features = `${grouped(loaded)} ${loaded === 1 ? 'feature' : 'features'}`;
  return visible === loaded ? features : `${features} · ${grouped(visible)} shown`;
}

/**
 * How fresh what is drawn actually is.
 *
 * The worst thing this feature could do is draw week-old data on a map where
 * decisions get made and say nothing, so a stale subscription says so on the row
 * — before anyone reads the marks, not after. A file says when it was opened and
 * nothing more: it is exactly what the analyst dropped in, and it was never
 * going to change on its own.
 */
export function freshness(layer, now = Date.now()) {
  if (!layer) return '';
  if (!followed(layer)) {
    const at = timeAgo(layer.fetched_at, now);
    return at ? `opened ${at}` : '';
  }
  const at = layer.checked_at || layer.fetched_at;
  const said = timeAgo(at, now);
  if (!said) return 'never read';
  return layer.stale ? `out of date, last read ${said}` : `read ${said}`;
}

/** Where a row came from, in the width a row has. */
export function sourceLabel(layer) {
  const source = layer?.source ?? {};
  if (source.kind === 'geoconfirmed') {
    return source.area ? 'GeoConfirmed · limited area' : 'GeoConfirmed';
  }
  if (source.kind !== 'url') return source.name || 'a file on this computer';
  if (source.my_maps) return 'Google My Maps';
  try {
    return new URL(source.url).hostname.replace(/^www\./, '');
  } catch {
    return source.url || '';
  }
}

/**
 * The credit line a layer owes its source.
 *
 * The curated overlays each declare one (`basemap.js`), and a map somebody else
 * drew is owed the same. A My Maps has an author we cannot read from the feed,
 * so the map's own title and the platform are what can honestly be stated.
 */
export function attribution(layer) {
  const title = layer?.title || 'Added layer';
  const source = layer?.source ?? {};
  if (source.kind === 'geoconfirmed') return `${title} — geoconfirmed.org`;
  if (source.my_maps) return `${title} — Google My Maps`;
  if (source.kind === 'url') return `${title} — ${sourceLabel(layer)}`;
  return `${title} — added by the analyst`;
}

/**
 * Whether switching this layer on for the first time in a session re-reads it:
 * a followed layer that did not ask to move only on Refresh. The spec still
 * calls that `on_open`, from when it happened on case open.
 */
export function renewsOnShow(layer) {
  return followed(layer) && layer?.refresh?.on_open !== false;
}

/** Which layers the map should actually be drawing. */
export function drawable(layers = []) {
  return layers.filter((layer) => layer.enabled);
}

/**
 * The names in their new order once one row is dropped at `to`, which counts
 * places in the list as it stood before the drag. Top of the map first.
 */
export function moveName(names, name, to) {
  const from = names.indexOf(name);
  if (from < 0) return [...names];
  const rest = names.filter((entry) => entry !== name);
  const at = Math.max(0, Math.min(rest.length, to > from ? to - 1 : to));
  return [...rest.slice(0, at), name, ...rest.slice(at)];
}

/** What a Refresh on every followed layer found, in one toast. */
export function refreshedLabel({ moved = 0, failed = 0, total = 0 }) {
  if (total && failed === total) {
    return total === 1 ? 'That source could not be reached' : 'No source could be reached';
  }
  const found = moved ? `${moved} updated` : 'Nothing new';
  return failed ? `${found}, ${failed} could not be reached` : found;
}

/** One category switched, as the PATCH body expects it: the whole hidden list. */
export function toggleCategory(layer, name) {
  const hidden = new Set(layer?.hidden ?? []);
  if (hidden.has(name)) hidden.delete(name);
  else hidden.add(name);
  return [...hidden];
}

/** What a feature the source never named is called, wherever it is shown. */
export const UNNAMED = 'Unnamed feature';

/**
 * How many matches one search lists.
 *
 * The panel is 300px wide and a match is a line in it. Past this the list is
 * something to scroll rather than something to read, so the tally says how many
 * were found and the search is what narrows them.
 */
export const SEARCH_LIMIT = 25;

/**
 * The features of one layer whose name or group matches what was typed.
 *
 * **A finder, not a filter.** The legend beside it is the filter: it is
 * persisted on the spec and it is what `countLabel` counts. A search is
 * ephemeral and changes nothing about what the map draws — a row that said
 * "3 200 features · 412 shown" while somebody typed would be stating a filter
 * nobody set.
 *
 * **Names and groups only, never descriptions.** A description runs to
 * `MAX_DESCRIPTION` characters and a layer to `MAX_FEATURES` of them
 * (`engine/maplayers.py`), so reading them on every keystroke would make this
 * the most expensive thing in the app — for a gain an analyst rarely asks for,
 * since what anyone remembers of somebody else's map is what it called a place.
 *
 * Counted in full and listed in part: the tally is what says to narrow the
 * search rather than scroll it.
 */
export function searchFeatures(collection, query, options = {}) {
  const { categories = [], hidden = [], period = null, limit = SEARCH_LIMIT } = options;
  const terms = String(query ?? '').trim();
  if (!terms) return { total: 0, results: [] };

  const colours = new Map(
    (categories ?? []).map((category, index) => [category.name, categoryColour(category, index)])
  );
  const off = new Set(hidden ?? []);
  const results = [];
  let total = 0;

  (collection?.features ?? []).forEach((feature, position) => {
    const properties = feature?.properties ?? {};
    const category = properties.category ?? '';
    if (!matchesTerms(`${properties.name ?? ''} ${category}`, terms)) return;
    total += 1;
    if (results.length >= limit) return;
    results.push({
      // What the backend numbered this feature, so going to it is a lookup
      // rather than a second copy of the geometry in the panel.
      index: properties.index ?? position,
      name: properties.name || UNNAMED,
      category,
      // The source's own colour where it painted one, its group's where it did
      // not — the same rule the marks are drawn by (`addedLayer.js`).
      colour: properties.colour || colours.get(category) || categoryColour(null, 0),
      // A match in a group the legend switched off, or outside the period: it
      // is a real feature, it is simply not on the map until that comes back.
      hidden: off.has(category) || !inPeriod(properties, period),
      outside: !inPeriod(properties, period),
    });
  });
  return { total, results };
}

/**
 * What the `+` accepts. A pasted address subscribes; anything else is a file.
 *
 * Said here so the button and the dialog cannot disagree about which half of the
 * feature a given input lands in.
 */
export function looksLikeUrl(value) {
  return /^https?:\/\/\S+$/i.test(String(value ?? '').trim());
}

/** A Google My Maps link in any of its forms, the same test the backend makes. */
export function isMyMapsLink(value) {
  return /^https?:\/\/(www\.)?google\.[^/]+\/maps\/d\/(u\/\d+\/)?(viewer|edit|embed|kml)?\?[^#]*\bmid=[\w-]+/i
    .test(String(value ?? '').trim());
}

/** The formats the file picker offers, and the parser accepts. */
export const ACCEPTS = '.geojson,.json,.kml,.kmz,.gpx';