diff options
| author | Adrien Hopkins <[email protected]> | 2026-03-07 14:19:36 -0500 |
|---|---|---|
| committer | Adrien Hopkins <[email protected]> | 2026-03-07 14:19:36 -0500 |
| commit | bc9fd03d02b0fd5883cfac42ef50f4a5b0471015 (patch) | |
| tree | 6df7aba5450861f7f17e028fd1d1477bc48c2909 /src/routes | |
| parent | 7ab556b6bd00a58dec8e16c2612082194585f1c1 (diff) | |
| download | coinos-ui-nip17dm.tar.gz coinos-ui-nip17dm.zip | |
Prioritize matches at beginning of namenip17dm
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/(app)/dm/+page.svelte | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/routes/(app)/dm/+page.svelte b/src/routes/(app)/dm/+page.svelte index caf74d2c..fa5a18a8 100644 --- a/src/routes/(app)/dm/+page.svelte +++ b/src/routes/(app)/dm/+page.svelte @@ -300,18 +300,36 @@ return chatName.includes(lowerQuery); } + const nameStartsWith = (chat: object, query: string) => { + const lowerQuery = query.toLowerCase(); + const chatName = name(chat).toLowerCase(); + return chatName.startsWith(lowerQuery); + } + const nip05Matches = (chat: object, query: string) => { const lowerQuery = query.toLowerCase(); const chatNip05 = chat?.nip05; return chatNip05 && chatNip05.includes(lowerQuery); } + const nip05StartsWith = (chat: object, query: string) => { + const lowerQuery = query.toLowerCase(); + const chatNip05 = chat?.nip05; + return chatNip05 && chatNip05.startsWith(lowerQuery); + } + const pubkeyMatches = (chat: object, query: string) => { const lowerQuery = query.toLowerCase(); return chat.pubkey.includes(lowerQuery) || npubEncode(chat.pubkey).includes(lowerQuery); } + const pubkeyStartsWith = (chat: object, query: string) => { + const lowerQuery = query.toLowerCase(); + return chat.pubkey.startsWith(lowerQuery) || + npubEncode(chat.pubkey).startsWith(lowerQuery); + } + const searchMatches = (chat: object, query: string) => nameMatches(chat, query) || nip05Matches(chat, query) || pubkeyMatches(chat, query); @@ -324,6 +342,14 @@ // Generates and returns a comparator based on a search query const searchCompare = (query: string) => (a, b) => { + const aNameStartsWith = nameStartsWith(a, query); + const bNameStartsWith = nameStartsWith(b, query); + if (aNameStartsWith && !bNameStartsWith) { + return -1; + } else if (!aNameStartsWith && bNameStartsWith) { + return 1; + } + const aNameMatches = nameMatches(a, query); const bNameMatches = nameMatches(b, query); if (aNameMatches && !bNameMatches) { @@ -332,6 +358,14 @@ return 1; } + const aNip05StartsWith = nip05StartsWith(a, query); + const bNip05StartsWith = nip05StartsWith(b, query); + if (aNip05StartsWith && !bNip05StartsWith) { + return -1; + } else if (!aNip05StartsWith && bNip05StartsWith) { + return 1; + } + const aNip05Matches = nip05Matches(a, query); const bNip05Matches = nip05Matches(b, query); if (aNip05Matches && !bNip05Matches) { @@ -340,6 +374,14 @@ return 1; } + const aPubkeyStartsWith = pubkeyStartsWith(a, query); + const bPubkeyStartsWith = pubkeyStartsWith(b, query); + if (aPubkeyStartsWith && !bPubkeyStartsWith) { + return -1; + } else if (!aPubkeyStartsWith && bPubkeyStartsWith) { + return 1; + } + const aPubkeyMatches = pubkeyMatches(a, query); const bPubkeyMatches = pubkeyMatches(b, query); if (aPubkeyMatches && !bPubkeyMatches) { |
