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
8.9 KB
Raw Blame
<script>
  /**
   * A point, moved on the map instead of typed.
   *
   * Typing a coordinate is the one act in the composer that has no picture under
   * it: six decimals is a tenth of a metre, and nobody edits a building's corner
   * by counting digits. So the row opens the ground it names — the same map the
   * rest of the app draws (`MapSurface`), the same basemaps, the same reference
   * layers — with the pin on what the row already says, and hands the point back.
   *
   * Read-only about everything else. No capture, no save, no place: the dialog
   * exists to answer one question, and the map that owns those acts is the Map
   * tool. The camera opens where the caller says (the row's own point, else the
   * one above it) so a row added with `+` starts beside the point it belongs with
   * rather than on the empty Atlantic.
   *
   * The same map checks a point before a save or a post (`check`). A point the
   * imagery proposed is the middle of a capture until somebody looks, so the
   * composer walks each one through here and the analyst either moves the pin or
   * says the point is right.
   */
  import { onMount, untrack } from 'svelte';
  import Modal from '../../components/Modal.svelte';
  import Icon from '../../components/Icon.svelte';
  import TurnGuide from '../../components/TurnGuide.svelte';
  import MapSurface from '../satellite/MapSurface.svelte';
  import MapLayers from '../satellite/MapLayers.svelte';
  import { createImageryState, FALLBACK_PROVIDER } from '../satellite/state/imagery.svelte.js';
  import { createSurface } from '../../lib/map/surface.js';
  import { turnFromKey, turnFromPress } from '../../lib/map/gestures.js';
  import { markerGeometry, markerSvg } from '../../lib/mapMarkers.js';
  // The reference layers, named where they are already named for a reader
  // (`lib/map/compare.js`): the same seven key-less overlays, the same words.
  import { layerInfo, LAYERS } from '../../lib/map/layerCatalog.js';
  import { api } from '../../lib/api.js';
  import { formatCoords } from '../../lib/coords.js';
  import { prefs } from '../../lib/state.svelte.js';

  let {
    /** Where the map opens, `{ lat, lon, zoom }`. The pin starts on it. */
    view,
    /** The point chosen, `{ lat, lon }`. The dialog closes itself after, unless it
     *  is checking: then the caller moves on to the next point. */
    onpick,
    onclose,
    /** `{ step, label }` when checking a point before a save or a post. */
    check = null,
    /** Checking before a save only: save now, leaving the point unchecked. */
    onlater = null,
  } = $props();

  const PIN = 'point';
  /** The reference layers; the roads and the place names read wrongly over a street
   *  basemap, which draws its own (`imageryOnly`). */
  const REFERENCE = LAYERS.filter((layer) => layer.group === 'reference');

  const imagery = createImageryState({ api });
  let providerId = $state(FALLBACK_PROVIDER);
  let engine = $state(null);
  let element = $state(null);
  let ready = $state(false);
  let rotating = $state(null); // the grabbed point while the map turns
  // Where the pin starts: the opening camera, read once. The dialog is opened on
  // one row and closed on it, so a later view would be a different question.
  let at = $state(untrack(() => ({ lat: view.lat, lon: view.lon })));
  // The borders, which are what finding a spot needs first.
  let on = $state(['boundaries']);
  let layersOpen = $state(false);
  let surface = null;

  const baseIsImagery = $derived(imagery.find(providerId)?.imagery ?? true);
  const usable = $derived(on.filter((id) => baseIsImagery || !layerInfo(id)?.imageryOnly));
  const reading = $derived(formatCoords(at.lat, at.lon, prefs.coordFormat));
  const layerRows = $derived(
    REFERENCE.map((layer) => ({
      id: layer.id,
      label: layer.label,
      title: layer.imageryOnly && !baseIsImagery ? 'Only useful over satellite imagery' : layer.title,
      on: on.includes(layer.id),
      disabled: layer.imageryOnly && !baseIsImagery,
      toggle: () =>
        (on = on.includes(layer.id) ? on.filter((id) => id !== layer.id) : [...on, layer.id]),
    }))
  );

  onMount(() => {
    imagery.loadProviders().catch(() => {
      /* the free basemap still draws; the chip simply has nothing to offer */
    });
    imagery.refreshUsage();
  });

  // The pin is drawn once the map is up, then moved in place: rebuilt on every
  // drag frame it would be dropped and re-added mid-gesture, which is why its
  // opening position is read untracked.
  $effect(() => {
    if (!ready || !engine) return;
    const layer = createSurface(engine);
    const { size, anchor } = markerGeometry('pin');
    layer.set([
      {
        id: PIN,
        kind: 'marker',
        at: untrack(() => at),
        className: 'point-mark',
        html: markerSvg('pin'),
        size,
        anchor,
        draggable: true,
        zIndex: 570,
        onDrag: (moved) => (at = moved),
      },
    ]);
    surface = layer;
    return () => {
      layer.destroy();
      surface = null;
    };
  });

  // The same turn as every other map: a middle-drag, or Shift and the left
  // button, about the grabbed point; Shift and an arrow from the keyboard.
  $effect(() => {
    if (!element || !engine) return;
    const surface = element;
    const down = (event) =>
      turnFromPress(engine, event, {
        onPivot: (point) => (rotating = point),
        onEnd: () => (rotating = null),
      });
    surface.addEventListener('mousedown', down, true);
    return () => surface.removeEventListener('mousedown', down, true);
  });

  // Read before the guard: behind `surface?.` the position would never be read
  // at all while the map is still being built, and the pin would then stop
  // following anything.
  $effect(() => {
    const moved = at;
    surface?.patch(PIN, { at: moved });
  });
</script>

<svelte:window onkeydown={(event) => turnFromKey(engine, event)} />

<Modal title={check ? (check.step ? `Check point ${check.step}` : 'Check the point') : 'Move the point'} {onclose} width="900px">
  {#if check}
    <p class="check">
      {#if check.label}<strong>{check.label}</strong>{/if}
      <span>Taken from the imagery. Put the pin on what the proof shows, or keep it if it is right.</span>
    </p>
  {/if}
  <div class="stage">
    <MapSurface
      bind:engine
      bind:element
      bind:ready
      bind:providerId
      {imagery}
      home={view}
      overlays={usable}
      imperial={prefs.units === 'imperial'}
      controlsTop={8}
      onclick={(clicked) => (at = clicked)}
    />
    {#if rotating}
      <TurnGuide x={rotating.x} y={rotating.y} />
    {/if}
    <div class="layers" class:open={layersOpen}>
      {#if layersOpen}
        <MapLayers rows={layerRows} bind:open={layersOpen} />
      {:else}
        <button type="button" class="layers-btn" onclick={() => (layersOpen = true)}>
          <Icon name="stack" size={13} />
          <span>Layers</span>
        </button>
      {/if}
    </div>
  </div>

  <div class="foot">
    <span class="hint">Click the map to move the pin.</span>
    <span class="reading mono">{reading}</span>
    {#if onlater}
      <button class="btn btn-ghost btn-sm" title="Save now and keep the point off the case map" onclick={onlater}>Later</button>
    {/if}
    <button class="btn btn-ghost btn-sm" onclick={onclose}>Cancel</button>
    <button
      class="btn btn-ok btn-sm"
      onclick={() => {
        onpick(at);
        if (!check) onclose();
      }}>Use this point</button
    >
  </div>
</Modal>

<style>
  .check {
    display: flex;
    gap: 8px;
    margin: 0 0 10px;
    font-size: var(--fs-sm);
    color: var(--text-2);
  }
  .check strong {
    color: var(--text-1);
  }
  .stage {
    position: relative;
    display: flex;
    height: min(62vh, 520px);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    overflow: hidden;
  }
  /* Under the zoom buttons, aligned with them. The right-hand corner is the
     surface's own: it stacks the basemap, the imagery date and the compass
     there, and a control of ours over that column hides whichever of them the
     map happens to be showing. */
  .layers {
    position: absolute;
    top: 76px;
    left: 12px;
    z-index: 500;
    max-height: calc(100% - 92px);
    overflow: auto;
  }
  .layers.open {
    width: 208px;
    padding: 6px 8px 8px;
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
  }
  .layers-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 9px;
    font-size: var(--fs-xs);
    color: var(--text-1);
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    cursor: pointer;
  }
  .foot {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-top: 10px;
  }
  .hint {
    font-size: var(--fs-sm);
    color: var(--text-3);
  }
  .reading {
    margin-left: auto;
    font-size: var(--fs-sm);
    color: var(--text-1);
  }
  :global(.point-mark) {
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.5));
  }
</style>