Files
raucao bba9a2a444
CI / Lint (pull_request) Successful in 57s
CI / Test (pull_request) Failing after 1m9s
Open Nostr event links from the search field
The full-text search field now detects anything the /e/ handler can open
(raw 64-hex ids, note1/nevent1/naddr1 codes, nostr: URIs, kind:pubkey:d
coordinates and permalinks) and opens it instead of running a Photon or
bookmark search.

As-you-type, a pasted reference resolves through the event store while the
field shows a spinner and the popover offers a single row labelled by kind
("Place photo" / "Place review", or a generic "Nostr event"). A reference
that cannot be found shows the usual not-found toast, and an unlinked event
is flagged as such. Submitting a reference, and direct /search?q=<ref>
links, route straight to the /e/ handler.
2026-11-28 19:36:02 +04:00

432 lines
12 KiB
Plaintext

import Component from '@glimmer/component';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { on } from '@ember/modifier';
import { fn } from '@ember/helper';
import { task, timeout } from 'ember-concurrency';
import Icon from '#components/icon';
import humanizeOsmTag from '../helpers/humanize-osm-tag';
import { POI_CATEGORIES } from '../utils/poi-categories';
import {
parseNostrEventReference,
nostrEventRouteParam,
nostrEventLabel,
} from '../utils/nostr';
import { eq, or } from 'ember-truth-helpers';
// Interactive, as-you-type Nostr lookup. Kept short so a pasted reference
// either resolves quickly or the field stops spinning; a later "Enter" that
// opens /e/ benefits from the store cache populated by this attempt.
const NOSTR_LOOKUP_TIMEOUT_MS = 5000;
export default class SearchBoxComponent extends Component {
@service photon;
@service osm;
@service storage;
@service router;
@service mapUi;
@service map; // Assuming we might need map context, but mostly we use router
@service nostrData;
@service toast;
@tracked _internalQuery = '';
@tracked results = [];
@tracked isFocused = false;
@tracked isLoading = false;
// Separate from `isLoading` so the spinner only appears while resolving a
// Nostr reference, leaving ordinary Photon/category/saved popover UX alone.
@tracked isNostrLookup = false;
// Last completed reference lookup, used to re-apply its outcome on refocus
// without a second network round-trip (or duplicate toast). Reset in clear().
_lastNostrRef = null;
get query() {
return this.args.query ?? this._internalQuery;
}
set query(value) {
this._internalQuery = value;
}
get showPopover() {
return this.isFocused && this.results.length > 0;
}
@action
handleInput(event) {
const value = event.target.value;
this.query = value;
if (this.args.onQueryChange) {
this.args.onQueryChange(value);
}
if (value.length < 2) {
// Cancel any in-flight (e.g. Nostr) lookup so it can't pop a stale row
// or toast over an input the user has since shortened.
this.searchTask.cancelAll();
this.results = [];
return;
}
this.searchTask.perform(value);
}
formatSavedPlace(place) {
const listNames = (place._listIds || [])
.map((id) => this.storage.lists?.find((l) => l.id === id)?.title)
.filter(Boolean)
.join(', ');
const description = listNames
? `Saved place (${listNames})`
: 'Saved place';
return {
source: 'saved',
id: place.id,
title: place.title,
icon: 'bookmark',
description,
osmId: place.osmId,
osmType: place.osmType,
lat: place.lat,
lon: place.lon,
};
}
searchTask = task({ restartable: true }, async (term) => {
await timeout(300);
const query = typeof term === 'string' ? term : this.query;
if (query.length < 2) return;
// A Nostr event reference opens the /e/ handler instead of running a
// Photon/bookmark search.
const routeParam = nostrEventRouteParam(query);
if (routeParam) {
await this.resolveNostrReference(query, routeParam);
return;
}
this.isLoading = true;
try {
// Use map center if available for location bias
let lat, lon;
if (this.mapUi.currentCenter) {
({ lat, lon } = this.mapUi.currentCenter);
}
// Filter categories
const q = query.toLowerCase();
const categoryMatches = POI_CATEGORIES.filter((c) =>
c.label.toLowerCase().includes(q)
).map((c) => ({
source: 'category',
title: c.label,
id: c.id,
icon: 'search',
}));
// Filter saved places (minimum 3 characters)
let savedMatches = [];
if (q.length >= 3) {
savedMatches = this.storage.savedPlaces
.filter((p) => p.title && p.title.toLowerCase().includes(q))
.map((p) => this.formatSavedPlace(p));
}
const results = await this.photon.search(query, lat, lon);
// Deduplicate Photon results that are already in saved matches
const savedOsmIds = new Set(
savedMatches.map((s) => s.osmId).filter(Boolean)
);
const filteredPhotonResults = results.filter(
(r) => !savedOsmIds.has(r.osmId)
);
this.results = [
...categoryMatches,
...savedMatches,
...filteredPhotonResults,
];
} catch (e) {
console.error('Search failed', e);
this.results = [];
} finally {
this.isLoading = false;
}
});
nostrResult(routeParam, event) {
const { title, description } = nostrEventLabel(event);
return {
source: 'nostr',
id: routeParam,
icon: 'nostrich',
title,
description,
};
}
async resolveNostrReference(query, routeParam) {
// Re-apply the cached outcome on refocus rather than re-running a relay
// lookup (and re-toasting) for a reference we have already resolved.
if (this._lastNostrRef?.param === routeParam) {
this.results = this._lastNostrRef.result
? [this._lastNostrRef.result]
: [];
return;
}
this.isNostrLookup = true;
try {
const event = await this.nostrData.loadEventByPointer(
parseNostrEventReference(query),
NOSTR_LOOKUP_TIMEOUT_MS
);
// The query may have changed (or been cleared) while we waited. Drop a
// stale outcome instead of toasting or showing a row for the wrong
// input; this also covers builds where cancellation does not interrupt
// the awaited lookup.
if (nostrEventRouteParam(this.query) !== routeParam) {
return;
}
const result = event ? this.nostrResult(routeParam, event) : null;
this._lastNostrRef = { param: routeParam, result };
if (result) {
this.results = [result];
} else {
this.results = [];
this.toast.show('Could not find this event on any relay.');
}
} finally {
this.isNostrLookup = false;
}
}
@action
handleFocus() {
this.isFocused = true;
this.mapUi.setSearchBoxFocus(true);
if (this.query.length >= 2 && this.results.length === 0) {
this.searchTask.perform();
}
}
@action
handleBlur() {
// Delay hiding so clicks on results can register
setTimeout(() => {
this.isFocused = false;
this.mapUi.setSearchBoxFocus(false);
}, 300);
}
@action
handleSubmit(event) {
event.preventDefault();
if (!this.query) return;
// A Nostr event reference is opened by the /e/ route; the route reports
// its own errors ("not found" / "not linked to a place").
const nostrRef = nostrEventRouteParam(this.query);
if (nostrRef) {
this.results = [];
this.isFocused = false;
this.router.transitionTo('event', nostrRef);
return;
}
let queryParams = { q: this.query, selected: null, category: null };
if (this.mapUi.currentCenter) {
const { lat, lon } = this.mapUi.currentCenter;
queryParams.lat = parseFloat(lat).toFixed(4);
queryParams.lon = parseFloat(lon).toFixed(4);
}
this.router.transitionTo('search', { queryParams });
this.isFocused = false;
}
@action
selectResult(place) {
if (place.source === 'category') {
this.query = place.title;
if (this.args.onQueryChange) {
this.args.onQueryChange(place.title);
}
this.results = [];
let lat = null,
lon = null;
if (this.mapUi.currentCenter) {
({ lat, lon } = this.mapUi.currentCenter);
lat = lat?.toString();
lon = lon?.toString();
}
this.router.transitionTo('search', {
queryParams: {
q: place.title,
category: place.id,
selected: null,
lat: lat,
lon: lon,
},
});
return;
}
if (place.source === 'nostr') {
// Open the /e/ handler. Intentionally skip the shared lines below so we
// don't overwrite the input with the result label.
this.results = [];
this.router.transitionTo('event', place.id);
return;
}
this.query = place.title;
if (this.args.onQueryChange) {
this.args.onQueryChange(place.title);
}
this.results = []; // Hide popover
// If it's a custom saved place without an OSM ID, go to place details via internal ID
if (place.source === 'saved' && place.id && !place.osmId) {
this.router.transitionTo('place', place.id);
}
// If it has an OSM ID, go to place details via OSM ID
else if (place.osmId) {
// Format: osm:node:123
// place.osmType is already normalized to 'node', 'way', or 'relation' by PhotonService
const id = `osm:${place.osmType}:${place.osmId}`;
this.router.transitionTo('place', id);
} else {
// Just a location (e.g. from Photon without OSM ID, though unlikely for Photon)
// Or we can treat it as a search query
this.router.transitionTo('search', {
queryParams: {
q: place.title,
lat: place.lat,
lon: place.lon,
selected: null,
category: null,
},
});
}
}
@action
clear() {
this.searchTask.cancelAll();
this.mapUi.stopLoading();
this.osm.cancelAll();
this.photon.cancelAll();
this._lastNostrRef = null;
this.query = '';
this.results = [];
if (this.args.onQueryChange) {
this.args.onQueryChange('');
}
this.router.transitionTo('index');
}
<template>
<div class="search-box">
<form class="search-form" {{on "submit" this.handleSubmit}}>
<button
type="button"
class="menu-btn-integrated"
aria-label="Menu"
{{on "click" @onToggleMenu}}
>
<Icon @name="menu" @size={{20}} @color="#5f6368" />
</button>
<input
type="search"
class="search-input"
placeholder="Search places..."
aria-label="Search places"
value={{this.query}}
{{on "input" this.handleInput}}
{{on "focus" this.handleFocus}}
{{on "blur" this.handleBlur}}
autocomplete="off"
/>
<button type="submit" class="search-submit-btn" aria-label="Search">
{{#if
(or
(eq this.mapUi.loadingState.type "text")
(eq this.mapUi.loadingState.type "category")
(eq this.mapUi.loadingState.type "nearby")
this.isNostrLookup
)
}}
<Icon @name="loading-ring" @size={{20}} class="search-spinner" />
{{else}}
<Icon @name="search" @size={{20}} @color="#5f6368" />
{{/if}}
</button>
{{#if this.query}}
<button
type="button"
class="search-clear-btn"
{{on "click" this.clear}}
aria-label="Clear"
>
<Icon @name="x" @size={{20}} @color="#5f6368" />
</button>
{{/if}}
</form>
{{#if this.showPopover}}
<div class="search-results-popover">
<ul class="search-results-list">
{{#each this.results as |result|}}
<li>
<button
type="button"
class="search-result-item"
{{on "click" (fn this.selectResult result)}}
>
<div class="result-icon">
<Icon
@name={{if result.icon result.icon "map-pin"}}
@size={{16}}
@color="#666"
/>
</div>
<div class="result-info">
<span class="result-title">{{result.title}}</span>
{{#if (eq result.source "osm")}}
<span class="result-desc">{{humanizeOsmTag
result.type
}}</span>
{{else}}
{{#if result.description}}
<span class="result-desc">{{result.description}}</span>
{{/if}}
{{/if}}
</div>
</button>
</li>
{{/each}}
</ul>
</div>
{{/if}}
</div>
</template>
}