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
10.7 KB
Raw Blame
<script>
  // Coords & Sky — paste one point in any notation, read it back in every
  // other, jump to the nine external maps, and read what the sun and the moon
  // did there on a date. Conversion runs on the local backend; converting also
  // triggers a Nominatim reverse-geocode for the place name (the app's
  // local-first rule names geocoding as one of the actions that inherently
  // needs the network), reported as "Not available" if that call fails. The
  // sky panel below is pure local computation and never goes out.
  import { api } from '../lib/api.js';
  import { caseState, toast, uiState } from '../lib/state.svelte.js';
  import { copyText } from '../lib/clipboard.js';
  import { mapLinks } from '../lib/maplinks.js';
  import { bidiSafe } from '../lib/bidi.js';
  import Icon from '../components/Icon.svelte';
  import SkyPanel from './coordinates/SkyPanel.svelte';
  import PointPicker from './coordinates/PointPicker.svelte';

  // Mirrors the label order of engine/geo.py's all_formats(), so the empty
  // state can preview the field list before any coordinate is parsed.
  const EMPTY_FORMATS = [
    { id: 'dd', label: 'Decimal' },
    { id: 'ddm', label: 'Deg. decimal min.' },
    { id: 'dms', label: 'Deg. min. sec.' },
    { id: 'utm', label: 'UTM' },
    { id: 'mgrs', label: 'MGRS' },
    { id: 'plus_code', label: 'Plus code' },
    { id: 'geohash', label: 'Geohash' },
  ];

  let text = $state('');
  let pickerRows = $state(null); // the case's saved points, loaded when asked for
  let point = $state(null); // { lat, lon, formats: [{id,label,value}] }
  let parsing = $state(false);
  let place = $state(null); // reverse-geocode result
  let placeLoading = $state(false);

  // Link labels don't depend on lat/lon, so this also drives the empty-state preview.
  const links = $derived(mapLinks(point?.lat ?? 0, point?.lon ?? 0));

  async function parse({ name = true } = {}) {
    const value = text.trim();
    if (!value || parsing) return;
    parsing = true;
    place = null;
    try {
      point = await api.post('/api/geo/parse', { text: value });
      if (name) lookupPlace();
    } catch {
      point = null;
      toast('Could not read a coordinate from that', 'danger');
    } finally {
      parsing = false;
    }
  }

  async function copy(value) {
    await copyText(bidiSafe(value));
  }

  async function lookupPlace() {
    if (!point) return;
    placeLoading = true;
    try {
      const res = await api.get(`/api/geo/reverse?lat=${point.lat}&lon=${point.lon}`);
      place = res.display_name || 'Not available';
    } catch {
      place = 'Not available';
    } finally {
      placeLoading = false;
    }
  }

  // Borrow a coordinate from the case's own saved work rather than retyping it.
  // The index is fetched when the picker is opened, not on mount: a tab that has
  // not been asked anything stays quiet.
  async function openPicker() {
    if (!caseState.current) {
      toast('Open a case to pick one of its points', 'warn');
      return;
    }
    try {
      pickerRows = await api.get(`/api/cases/${caseState.current.id}/satellite/index`);
    } catch (e) {
      toast(e.message, 'danger');
    }
  }

  // The row's own position, read back through the same parser as typed input, so
  // one code path fills every notation and the sky panel below.
  function usePoint(row) {
    text = `${row.lat}, ${row.lon}`;
    parse();
  }

  // The map point this tab last opened on. A tab is never unmounted once
  // visited, so without it the prefill below could only ever fire once.
  let openedOn = null;

  /**
   * Open on the point the map is on, rather than on an empty field: the tab is
   * reached from the map more often than it is typed into.
   *
   * It follows the map rather than filling once: the map moving is the analyst
   * looking somewhere else, and a tab still reading the point before it is a
   * tab answering about the wrong ground. Between two moves the field is
   * theirs — what they typed, or the row they picked, stands.
   *
   * The conversion is local, and that is the whole of what happens here. Naming
   * the place is a Nominatim call, which the local-first rule keeps behind an
   * action — so the row offers it instead of making it.
   */
  $effect(() => {
    if (uiState.tool !== 'coordinates') return;
    const last = uiState.mapPoint;
    if (!last || parsing) return;
    const asked = `${last.lat.toFixed(6)}, ${last.lon.toFixed(6)}`;
    if (asked === openedOn) return;
    // Arriving on a tab that already holds an answer leaves it standing: the
    // map has not moved since, so there is nothing newer to show.
    const held = openedOn === null && (point || text.trim());
    openedOn = asked;
    if (held) return;
    text = asked;
    void parse({ name: false });
  });

  // Hand the point to the Satellite map and switch tabs; it consumes
  // uiState.gotoCoords on the next tick and flies there (lib/navigate.js).
  function openInSatellite() {
    if (!point) return;
    uiState.gotoCoords = { lat: point.lat, lon: point.lon };
    uiState.tool = 'satellite';
  }
</script>

<div class="tool">
  <div class="tool-body">
    <form
      class="go-form"
      data-guide="coords.paste"
      onsubmit={(e) => {
        e.preventDefault();
        parse();
      }}
    >
      <input
        class="input"
        placeholder={'Paste any coordinate: 48.8584, 2.2945  ·  DMS  ·  UTM  ·  MGRS  ·  plus code  ·  geohash'}
        bind:value={text}
        title="Decimal, DMS, DDM, UTM, MGRS, plus code or geohash"
      />
      <button type="submit" class="btn" disabled={!text.trim() || parsing}>
        <Icon name="search" size={15} /> {parsing ? '…' : 'Look up'}
      </button>
      <button
        type="button"
        class="btn btn-ghost"
        title="Open a place or a capture saved in this case"
        data-guide="coords.saved"
        onclick={openPicker}>…</button
      >
    </form>

    <div class="sheet">
      <div class="head-row">
        <span class="place" class:muted={!point || placeLoading}>
          {#if !point}
            Paste a coordinate, or open one this case has saved, to read it back
            in every notation.
          {:else if placeLoading}
            Resolving place…
          {:else if place}
            {place}
          {:else}
            <button class="name-place" onclick={lookupPlace}>Name this place</button>
          {/if}
        </span>
        <button
          class="btn btn-sm"
          disabled={!point}
          onclick={openInSatellite}
          title="Open this point on the Satellite map"
        >
          <Icon name="crosshair" size={14} /> View in Satellite
        </button>
      </div>

      <div class="formats" data-guide="coords.formats">
        {#if point}
          {#each point.formats as f (f.id)}
            <button class="row" onclick={() => copy(f.value)} title="Copy">
              <span class="k">{f.label}</span>
              <span class="v mono">{f.value}</span>
              <Icon name="copy" size={13} />
            </button>
          {/each}
        {:else}
          {#each EMPTY_FORMATS as f (f.id)}
            <div class="row row-empty">
              <span class="k">{f.label}</span>
              <span class="v mono">–</span>
            </div>
          {/each}
        {/if}
      </div>

      <div class="open-on" data-guide="coords.open">
        <span class="open-label">Open on</span>
        <div class="links">
          {#each links as l (l.id)}
            {#if point}
              <a class="ext-link" href={l.url} target="_blank" rel="noreferrer">
                {l.label}<Icon name="external" size={11} />
              </a>
            {:else}
              <span class="ext-link disabled">{l.label}</span>
            {/if}
          {/each}
        </div>
      </div>

      <!-- Mounted with or without a point: with none it previews what it will
           answer, so the sun and moon read as available rather than absent. -->
      {#key point ? `${point.lat},${point.lon}` : 'empty'}
        <SkyPanel {point} />
      {/key}
    </div>
  </div>
  {#if pickerRows}
    <PointPicker rows={pickerRows} onpick={usePoint} onclose={() => (pickerRows = null)} />
  {/if}
</div>

<style>
  .go-form {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 16px 16px 0;
    max-width: 720px;
  }
  .go-form .input {
    flex: 1;
  }
  .sheet {
    display: flex;
    flex-direction: column;
    padding: 16px;
    max-width: 1120px;
  }
  .head-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
  }
  .head-row .place {
    flex: 1;
    min-width: 0;
    font-size: var(--fs-md);
    color: var(--text-1);
    overflow-wrap: anywhere;
  }
  .head-row .place.muted {
    color: var(--text-3);
  }
  /* The place name is a network call, so it is offered as one rather than made
     on arrival. Reads as the line it will become, not as a button. */
  .name-place {
    color: var(--accent);
    font-size: var(--fs-md);
    cursor: pointer;
  }
  .name-place:hover {
    text-decoration: underline;
  }
  .head-row .btn {
    flex-shrink: 0;
    white-space: nowrap;
  }
  .formats {
    display: flex;
    flex-direction: column;
    padding: 4px 0;
    border-bottom: 1px solid var(--border);
  }
  .row {
    display: grid;
    /* The value column is capped rather than free: a row spans the whole sheet,
       so the copy icon that follows it would otherwise land an arm's length from
       the text it copies. */
    grid-template-columns: 170px minmax(0, 480px) auto;
    align-items: center;
    gap: 12px;
    padding: 7px 8px;
    text-align: left;
    background: none;
    border: 0;
    border-radius: 4px;
    color: inherit;
    cursor: pointer;
  }
  .row:hover {
    background: var(--bg-2);
  }
  .row .k {
    color: var(--text-3);
    font-size: var(--fs-sm);
  }
  .row .v {
    overflow-wrap: anywhere;
  }
  .row :global(svg) {
    color: var(--text-3);
    opacity: 0;
  }
  .row:hover :global(svg) {
    opacity: 1;
  }
  .row-empty {
    cursor: default;
  }
  .row-empty .v {
    color: var(--text-3);
  }
  .open-on {
    display: flex;
    align-items: baseline;
    gap: 14px;
    padding-top: 14px;
  }
  .open-label {
    flex-shrink: 0;
    font-size: var(--fs-xs);
    color: var(--text-3);
  }
  .links {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 14px;
  }
  .ext-link {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: var(--fs-sm);
    color: var(--text-2);
    text-decoration: none;
  }
  .ext-link :global(svg) {
    color: var(--text-3);
  }
  .ext-link:hover {
    color: var(--accent);
  }
  .ext-link:hover :global(svg) {
    color: var(--accent);
  }
  .ext-link.disabled {
    color: var(--text-3);
    opacity: 0.6;
  }
</style>