From d0edc52831e26a532dc9581c353bf1de6ec6647e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Wed, 28 Sep 2022 13:37:24 +0200 Subject: [PATCH] Refactor search logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - type events that are handed between components - simplify the `resolve` function interface - before we had return values and exceptions, now just a single union type as return value - move navigation to the Search component to reduce its responsibilities - flatten and simplify multi-level branching - shuffle around code - move the `resolve` function to the `Search` component module, because it's only used there Signed-off-by: Rūdolfs Ošiņš --- src/Header.svelte | 21 ++- src/Search.svelte | 198 +++++++++++++++++++--- src/base/home/Index.svelte | 7 +- src/components/Modal/SearchResults.svelte | 8 +- src/project.ts | 4 +- src/resolver.ts | 125 -------------- 6 files changed, 198 insertions(+), 165 deletions(-) delete mode 100644 src/resolver.ts diff --git a/src/Header.svelte b/src/Header.svelte index b7fca3dfd..809c70136 100644 --- a/src/Header.svelte +++ b/src/Header.svelte @@ -1,6 +1,6 @@ + + -
+
{#if searching} diff --git a/src/base/home/Index.svelte b/src/base/home/Index.svelte index 95ca9a1ee..518981342 100644 --- a/src/base/home/Index.svelte +++ b/src/base/home/Index.svelte @@ -19,7 +19,12 @@ const getProjects = config.projects.pinned.length > 0 - ? Project.getMulti(config.projects.pinned) + ? Project.getMulti( + config.projects.pinned.map(project => ({ + nameOrUrn: project.urn, + seed: project.seed, + })), + ) : Promise.resolve([]); const onClick = (project: ProjectInfo, seed: Host) => { diff --git a/src/components/Modal/SearchResults.svelte b/src/components/Modal/SearchResults.svelte index 01c14143c..295c1d85f 100644 --- a/src/components/Modal/SearchResults.svelte +++ b/src/components/Modal/SearchResults.svelte @@ -6,10 +6,10 @@ import Address from "@app/Address.svelte"; import Button from "@app/Button.svelte"; import { createEventDispatcher } from "svelte"; - import type { ResolvedSearch } from "@app/resolver"; + import type { ProjectsAndProfiles } from "@app/Search.svelte"; export let query: string; - export let results: ResolvedSearch; + export let results: ProjectsAndProfiles; export let config: Config; const dispatch = createEventDispatcher(); @@ -63,10 +63,10 @@ {/each} {/if} - {#if results.ens.length > 0} + {#if results.profiles.length > 0}

ENS names

    - {#each results.ens as profile} + {#each results.profiles as profile}
  • diff --git a/src/project.ts b/src/project.ts index 2a90ea139..80f9a3c75 100644 --- a/src/project.ts +++ b/src/project.ts @@ -485,14 +485,14 @@ export class Project implements ProjectInfo { } static async getMulti( - projs: { urn: Urn; seed: string }[], + projs: { nameOrUrn: Urn; seed: string }[], ): Promise<{ info: ProjectInfo; seed: Host }[]> { const promises = []; for (const proj of projs) { const seed = { host: proj.seed, port: null }; promises.push( - Project.getInfo(proj.urn, seed).then(info => { + Project.getInfo(proj.nameOrUrn, seed).then(info => { return { info, seed }; }), ); diff --git a/src/resolver.ts b/src/resolver.ts deleted file mode 100644 index 692839ac4..000000000 --- a/src/resolver.ts +++ /dev/null @@ -1,125 +0,0 @@ -import type { Host } from "@app/api"; -import type { Config } from "@app/config"; -import type { ProjectInfo } from "@app/project"; - -import { navigate } from "svelte-routing"; -import { ethers } from "ethers"; - -import * as utils from "@app/utils"; -import { Project } from "@app/project"; -import { NotFoundError } from "@app/error"; -import { Profile, ProfileType } from "@app/profile"; - -export interface IProject { - info: ProjectInfo; - seed: Host; -} - -export interface ResolvedSearch { - projects: IProject[]; - ens: Profile[]; -} - -export async function resolve( - q: string | null, - config: Config, -): Promise { - const results: ResolvedSearch = { - projects: [], - ens: [], - }; - - try { - if (q) { - const seeds = Object.keys(config.seeds.pinned).map(seed => ({ - urn: q as string, - seed, - })); - - if (ethers.utils.isAddress(q)) { - navigate(`/${q}`, { replace: true }); - return null; - - // ========= Projects ========= - } else if (utils.isRadicleId(q)) { - const projects = await Project.getMulti(seeds); - if (projects.length === 1) { - navigate(`/seeds/${projects[0].seed.host}/${q}`, { - replace: true, - }); - return null; - } else { - results.projects.push(...projects); - } - } else { - const projects = await Project.getMulti(seeds); - results.projects.push(...projects); - - // ========= ENS Names ========= - const normalizedQuery = q.toLowerCase(); - let profile: Profile | null; - try { - profile = await Profile.get( - normalizedQuery, - ProfileType.Minimal, - config, - ); - } catch (e) { - profile = null; - } - - if (profile) { - if (results.projects.length === 0) { - navigate(`/${profile.address}`, { replace: true }); - return null; - } else { - results.ens.push(profile); - } - } else { - let profiles: Profile[]; - try { - profiles = await Profile.getMulti( - [ - `${normalizedQuery}.${config.registrar.domain}`, - `${normalizedQuery}.eth`, - ], - config, - ); - } catch (e) { - profiles = []; - } - - if (profiles.length > 1) { - results.ens.push(...profiles); - } else if (profiles.length === 1) { - if (results.projects.length === 0) { - navigate(`/${profiles[0].address}`, { replace: true }); - return null; - } else { - results.ens.push(...profiles); - } - } else { - if (results.projects.length === 1) { - navigate(`/seeds/${projects[0].seed.host}/${q}`, { - replace: true, - }); - return null; - } - } - } - } - } - - if (results.projects.length > 0 || results.ens.length > 0) { - return results; - } - - throw new NotFoundError("No search results found"); - } catch (e) { - if (e instanceof NotFoundError) { - throw new NotFoundError(e.message); - } else { - throw Error("Not able to resolve search query"); - } - } -}