Browse Source

fix npub search in mentions and get rid of redundant nostr: prefix

master
silberengel 7 months ago
parent
commit
a1733131e9
  1. 39
      src/lib/components/CommentBox.svelte

39
src/lib/components/CommentBox.svelte

@ -264,6 +264,30 @@
let communityStatus: Record<string, boolean> = $state({}); let communityStatus: Record<string, boolean> = $state({});
let isSearching = $state(false); let isSearching = $state(false);
// Reactive search with debouncing
$effect(() => {
// Clear existing timeout
if (mentionSearchTimeout) {
clearTimeout(mentionSearchTimeout);
}
// If search is empty, clear results immediately
if (!mentionSearch.trim()) {
mentionResults = [];
communityStatus = {};
mentionLoading = false;
return;
}
// Set loading state immediately for better UX
mentionLoading = true;
// Debounce the search with 300ms delay
mentionSearchTimeout = setTimeout(() => {
searchMentions();
}, 300);
});
async function searchMentions() { async function searchMentions() {
if (!mentionSearch.trim()) { if (!mentionSearch.trim()) {
mentionResults = []; mentionResults = [];
@ -323,15 +347,15 @@
try { try {
const npub = toNpub(profile.pubkey); const npub = toNpub(profile.pubkey);
if (npub) { if (npub) {
mention = `nostr:${npub}`; mention = `${npub}`;
} else { } else {
// If toNpub fails, fallback to pubkey // If toNpub fails, fallback to pubkey
mention = `nostr:${profile.pubkey}`; mention = `${profile.pubkey}`;
} }
} catch (e) { } catch (e) {
console.error("Error in toNpub:", e); console.error("Error in toNpub:", e);
// Fallback to pubkey if conversion fails // Fallback to pubkey if conversion fails
mention = `nostr:${profile.pubkey}`; mention = `${profile.pubkey}`;
} }
} else { } else {
console.warn("No pubkey in profile, falling back to display name"); console.warn("No pubkey in profile, falling back to display name");
@ -392,8 +416,9 @@
bind:value={mentionSearch} bind:value={mentionSearch}
bind:this={mentionSearchInput} bind:this={mentionSearchInput}
onkeydown={(e) => { onkeydown={(e) => {
if (e.key === "Enter" && mentionSearch.trim() && !isSearching) { if (e.key === "Enter" && mentionSearch.trim()) {
searchMentions(); // The reactive effect will handle the search automatically
e.preventDefault();
} }
}} }}
class="flex-1 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 text-sm focus:border-primary-500 focus:ring-primary-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-primary-500 dark:focus:ring-primary-500 p-2.5" class="flex-1 rounded-lg border border-gray-300 bg-gray-50 text-gray-900 text-sm focus:border-primary-500 focus:ring-primary-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-primary-500 dark:focus:ring-primary-500 p-2.5"
@ -404,9 +429,9 @@
onclick={(e: Event) => { onclick={(e: Event) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
searchMentions(); // The reactive effect will handle the search automatically
}} }}
disabled={isSearching || !mentionSearch.trim()} disabled={!mentionSearch.trim()}
> >
{#if isSearching} {#if isSearching}
Searching... Searching...

Loading…
Cancel
Save