@@ -1,45 +1,65 @@
11<script lang="ts">
2− import { Groups, Rolodex } from '@urbit/api';
2+
3+ import type { Patp, Rolodex } from '@urbit/api';
34 import { searchPoints } from '../lib/api';
45 import store from '../stores/store';
5− import ShipListing from './ShipListing.svelte';
6− import SearchResultNavButtons from './SearchResultNavButtons.svelte';
7− import ShipView from './ShipView.svelte';
6+ import ShipListings from './ShipListings.svelte';
7+
88 export let query: RegExp;
99 export let search: string;
1010 // export let patp: string;
11− let contactsList = [];
11+ let combinedSearchResults = [];
12+ let searchedPoints: Patp[] = [];
1213 let selectedShipIndex: number;
1314
1415 $: searchedPointsP = searchPoints(search)
15− // $: {
16− // // if (!patp) {
17− // store.search(query)
18− // // }
19− // // if (searchQuery) {
20− // // store.search(searchQuery)
21− // // } else {
22− // // store.reset();
23− // // }
24− // }
25− // $: {
26− // contactsList = Object.entries($store?.contacts).map(([patp, ship]) => {
27− // return {
28− // ...ship,
29− // patp,
30− // };
31− // });
32− // if (contactsList.length === 1) {
33− // selectedShipIndex = 0;
34− // } else {
35− // selectedShipIndex = null;
36− // }
37− // }
38− // $: selectedShip = contactsList[selectedShipIndex]
16+ $: {
17+ searchedPointsP.then((points) => {
18+ searchedPoints = points;
19+ });
20+ }
21+
22+ $: {
23+ // if (!patp) {
24+ store.search(query)
25+ // }
26+ // if (searchQuery) {
27+ // store.search(searchQuery)
28+ // } else {
29+ // store.reset();
30+ // }
31+ }
32+ $: {
33+ const contactSearchResults = JSON.parse(JSON.stringify($store.contactSearchResults));
34+ const contacts = [];
35+ const nonContacts = [];
36+ searchedPoints.forEach(patp => {
37+ const contact = contactSearchResults[patp];
38+ if (contact) {
39+ contacts.push({ ...contact, patp });
40+ delete contactSearchResults[patp];
41+ } else {
42+ nonContacts.push({ patp });
43+ }
44+ });
45+ const nonAzimuthContacts = Object.entries(contactSearchResults).map(([patp, ship]) => {
46+ return {
47+ ...ship,
48+ patp,
49+ };
50+ });
51+ combinedSearchResults = [...contacts, ...nonAzimuthContacts, ...nonContacts];
52+ if (combinedSearchResults.length === 1) {
53+ selectedShipIndex = 0;
54+ } else {
55+ selectedShipIndex = null;
56+ }
57+ }
58+ $: selectedShip = combinedSearchResults[selectedShipIndex]
3959
40− // function updateIndex(e) {
41− // selectedShipIndex = e.detail.index;
42− // }
60+ function updateIndex(e) {
61+ selectedShipIndex = e.detail.index;
62+ }
4363</script>
4464
4565<!-- should actually switch on whether field is empty -->
@@ -51,32 +71,36 @@
5171{/if} -->
5272<!-- {#if patp}
5373 <ShipView {patp} />
54−{:else if selectedShip}
55− {#if contactsList.length > 1}
74+{:else if selectedShip}-->
75+ <!-- {#if contactsList.length > 1}
5676 <SearchResultNavButtons
5777 index={selectedShipIndex}
5878 totalResults={contactsList.length}
5979 on:updateIndex={updateIndex}
6080 />
61− {/if}
62− <ShipView patp={selectedShip.patp} />
81+ {/if} -->
82+<!-- <ShipView patp={selectedShip.patp} />
6383{:else} -->
64−{query}
65−{search}
66−{#await searchedPointsP}
67−Loading...
68−{:then { points }}
69− <div class="max-w-md w-full rounded-lg bg-white">
70− <!-- {#each contactsList as contact, i (contact.patp)}
71− <ShipListing ship={contact} on:click={() => selectedShipIndex = i}/>
72− {/each} -->
73− {#each points as patp, i (patp)}
74− <ShipListing ship={{ patp }} linkToShip />
75− {/each}
76− </div>
77−{:catch}
78− <div class="max-w-md w-full rounded-lg bg-white">
79− Search Too Broad
80− </div>
81−{/await}
82−<!-- {/if} -->
84+<div class="max-w-md w-full rounded-lg bg-white overflow-hidden">
85+ {#await searchedPointsP}
86+ <p class="p-8 text-center">
87+ Searching all spawned points...
88+ </p>
89+ {:catch}
90+ <p class="p-8 text-center">
91+ Your search is too broad, please be more specific.
92+ </p>
93+ {/await}
94+ {#if combinedSearchResults.length === 0}
95+ <p class="p-8 text-center">
96+ No spawned ships match your search.
97+ </p>
98+ {:else}
99+ <ShipListings
100+ ships={combinedSearchResults}
101+ on:click={({ detail: i }) => selectedShipIndex = i}
102+ linkToShips differentiateContacts
103+ />
104+ {/if}
105+</div>
106+ <!-- {/if} -->