7 changed files with 621 additions and 5 deletions
@ -0,0 +1,363 @@
@@ -0,0 +1,363 @@
|
||||
<script lang="ts"> |
||||
import { searchEvents } from '../../services/cache/search-index.js'; |
||||
import { getEvent } from '../../services/cache/event-cache.js'; |
||||
import { nostrClient } from '../../services/nostr/nostr-client.js'; |
||||
import { relayManager } from '../../services/nostr/relay-manager.js'; |
||||
import { goto } from '$app/navigation'; |
||||
import type { NostrEvent } from '../../types/nostr.js'; |
||||
|
||||
interface Props { |
||||
open: boolean; |
||||
onClose: () => void; |
||||
} |
||||
|
||||
let { open, onClose }: Props = $props(); |
||||
|
||||
let query = $state(''); |
||||
let results = $state<NostrEvent[]>([]); |
||||
let loading = $state(false); |
||||
let searchInput: HTMLInputElement | null = $state(null); |
||||
|
||||
$effect(() => { |
||||
if (open && searchInput) { |
||||
// Focus input when modal opens |
||||
setTimeout(() => searchInput?.focus(), 100); |
||||
} |
||||
}); |
||||
|
||||
async function handleSearch() { |
||||
if (!query.trim()) { |
||||
results = []; |
||||
return; |
||||
} |
||||
|
||||
loading = true; |
||||
try { |
||||
// Search in index |
||||
const eventIds = await searchEvents(query.trim(), 20); |
||||
|
||||
// Fetch events |
||||
const events: NostrEvent[] = []; |
||||
for (const id of eventIds) { |
||||
try { |
||||
const cached = await getEvent(id); |
||||
if (cached) { |
||||
events.push(cached.event); |
||||
} else { |
||||
// Try to fetch from relays |
||||
const relays = relayManager.getThreadReadRelays(); |
||||
const event = await nostrClient.getEventById(id, relays); |
||||
if (event) { |
||||
events.push(event); |
||||
} |
||||
} |
||||
} catch { |
||||
// Skip if event not found |
||||
} |
||||
} |
||||
|
||||
results = events.sort((a, b) => b.created_at - a.created_at); |
||||
} catch (error) { |
||||
console.error('Error searching:', error); |
||||
results = []; |
||||
} finally { |
||||
loading = false; |
||||
} |
||||
} |
||||
|
||||
function handleKeydown(e: KeyboardEvent) { |
||||
if (e.key === 'Enter') { |
||||
e.preventDefault(); |
||||
handleSearch(); |
||||
} else if (e.key === 'Escape') { |
||||
e.preventDefault(); |
||||
onClose(); |
||||
} |
||||
} |
||||
|
||||
function handleResultClick(event: NostrEvent) { |
||||
// Navigate to thread if kind 11, or show event |
||||
if (event.kind === 11) { |
||||
goto(`/thread/${event.id}`); |
||||
} else if (event.kind === 1) { |
||||
// Could navigate to feed and highlight, or show in modal |
||||
goto(`/feed`); |
||||
} |
||||
onClose(); |
||||
} |
||||
|
||||
function getEventPreview(event: NostrEvent): string { |
||||
const content = event.content || ''; |
||||
const preview = content.slice(0, 150); |
||||
return preview + (content.length > 150 ? '...' : ''); |
||||
} |
||||
|
||||
function getEventType(event: NostrEvent): string { |
||||
switch (event.kind) { |
||||
case 1: |
||||
return 'Post'; |
||||
case 11: |
||||
return 'Thread'; |
||||
case 1111: |
||||
return 'Comment'; |
||||
default: |
||||
return 'Event'; |
||||
} |
||||
} |
||||
</script> |
||||
|
||||
{#if open} |
||||
<div |
||||
class="search-modal-overlay" |
||||
onclick={(e) => { |
||||
if (e.target === e.currentTarget) onClose(); |
||||
}} |
||||
role="dialog" |
||||
aria-modal="true" |
||||
aria-labelledby="search-title" |
||||
> |
||||
<div class="search-modal"> |
||||
<div class="search-header"> |
||||
<h2 id="search-title" class="text-xl font-bold mb-4">Search</h2> |
||||
<button |
||||
onclick={onClose} |
||||
class="close-button" |
||||
aria-label="Close search" |
||||
> |
||||
× |
||||
</button> |
||||
</div> |
||||
|
||||
<div class="search-input-container"> |
||||
<input |
||||
bind:this={searchInput} |
||||
type="text" |
||||
bind:value={query} |
||||
onkeydown={handleKeydown} |
||||
placeholder="Search posts, threads, comments..." |
||||
class="search-input" |
||||
aria-label="Search query" |
||||
/> |
||||
<button |
||||
onclick={handleSearch} |
||||
class="search-button" |
||||
disabled={loading || !query.trim()} |
||||
> |
||||
{loading ? 'Searching...' : 'Search'} |
||||
</button> |
||||
</div> |
||||
|
||||
{#if loading} |
||||
<div class="search-results"> |
||||
<p class="text-center text-fog-text-light dark:text-fog-dark-text-light py-4"> |
||||
Searching... |
||||
</p> |
||||
</div> |
||||
{:else if results.length > 0} |
||||
<div class="search-results"> |
||||
<p class="text-sm text-fog-text-light dark:text-fog-dark-text-light mb-2"> |
||||
Found {results.length} {results.length === 1 ? 'result' : 'results'} |
||||
</p> |
||||
<div class="results-list"> |
||||
{#each results as event (event.id)} |
||||
<button |
||||
onclick={() => handleResultClick(event)} |
||||
class="result-item" |
||||
> |
||||
<div class="result-header"> |
||||
<span class="result-type">{getEventType(event)}</span> |
||||
<span class="result-time"> |
||||
{new Date(event.created_at * 1000).toLocaleDateString()} |
||||
</span> |
||||
</div> |
||||
<div class="result-content"> |
||||
{getEventPreview(event)} |
||||
</div> |
||||
</button> |
||||
{/each} |
||||
</div> |
||||
</div> |
||||
{:else if query.trim() && !loading} |
||||
<div class="search-results"> |
||||
<p class="text-center text-fog-text-light dark:text-fog-dark-text-light py-4"> |
||||
No results found |
||||
</p> |
||||
</div> |
||||
{/if} |
||||
</div> |
||||
</div> |
||||
{/if} |
||||
|
||||
<style> |
||||
.search-modal-overlay { |
||||
position: fixed; |
||||
top: 0; |
||||
left: 0; |
||||
right: 0; |
||||
bottom: 0; |
||||
background: rgba(0, 0, 0, 0.5); |
||||
display: flex; |
||||
align-items: flex-start; |
||||
justify-content: center; |
||||
padding: 2rem; |
||||
z-index: 1000; |
||||
overflow-y: auto; |
||||
} |
||||
|
||||
.search-modal { |
||||
background: var(--fog-post, #ffffff); |
||||
border-radius: 0.5rem; |
||||
padding: 1.5rem; |
||||
width: 100%; |
||||
max-width: 600px; |
||||
margin-top: 5vh; |
||||
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2); |
||||
} |
||||
|
||||
:global(.dark) .search-modal { |
||||
background: var(--fog-dark-post, #1f2937); |
||||
} |
||||
|
||||
.search-header { |
||||
display: flex; |
||||
justify-content: space-between; |
||||
align-items: center; |
||||
margin-bottom: 1rem; |
||||
} |
||||
|
||||
.close-button { |
||||
background: none; |
||||
border: none; |
||||
font-size: 2rem; |
||||
line-height: 1; |
||||
cursor: pointer; |
||||
color: var(--fog-text, #1f2937); |
||||
padding: 0; |
||||
width: 2rem; |
||||
height: 2rem; |
||||
display: flex; |
||||
align-items: center; |
||||
justify-content: center; |
||||
} |
||||
|
||||
:global(.dark) .close-button { |
||||
color: var(--fog-dark-text, #f9fafb); |
||||
} |
||||
|
||||
.close-button:hover { |
||||
opacity: 0.7; |
||||
} |
||||
|
||||
.search-input-container { |
||||
display: flex; |
||||
gap: 0.5rem; |
||||
margin-bottom: 1rem; |
||||
} |
||||
|
||||
.search-input { |
||||
flex: 1; |
||||
padding: 0.75rem; |
||||
border: 1px solid var(--fog-border, #e5e7eb); |
||||
border-radius: 0.25rem; |
||||
background: var(--fog-post, #ffffff); |
||||
color: var(--fog-text, #1f2937); |
||||
font-size: 1rem; |
||||
} |
||||
|
||||
:global(.dark) .search-input { |
||||
background: var(--fog-dark-post, #1f2937); |
||||
border-color: var(--fog-dark-border, #374151); |
||||
color: var(--fog-dark-text, #f9fafb); |
||||
} |
||||
|
||||
.search-input:focus { |
||||
outline: none; |
||||
border-color: var(--fog-accent, #3b82f6); |
||||
} |
||||
|
||||
.search-button { |
||||
padding: 0.75rem 1.5rem; |
||||
background: var(--fog-accent, #3b82f6); |
||||
color: white; |
||||
border: none; |
||||
border-radius: 0.25rem; |
||||
cursor: pointer; |
||||
font-size: 1rem; |
||||
} |
||||
|
||||
.search-button:hover:not(:disabled) { |
||||
opacity: 0.9; |
||||
} |
||||
|
||||
.search-button:disabled { |
||||
opacity: 0.5; |
||||
cursor: not-allowed; |
||||
} |
||||
|
||||
.search-results { |
||||
max-height: 60vh; |
||||
overflow-y: auto; |
||||
} |
||||
|
||||
.results-list { |
||||
display: flex; |
||||
flex-direction: column; |
||||
gap: 0.5rem; |
||||
} |
||||
|
||||
.result-item { |
||||
text-align: left; |
||||
padding: 1rem; |
||||
border: 1px solid var(--fog-border, #e5e7eb); |
||||
border-radius: 0.25rem; |
||||
background: var(--fog-post, #ffffff); |
||||
cursor: pointer; |
||||
transition: background 0.2s; |
||||
} |
||||
|
||||
:global(.dark) .result-item { |
||||
background: var(--fog-dark-post, #1f2937); |
||||
border-color: var(--fog-dark-border, #374151); |
||||
} |
||||
|
||||
.result-item:hover { |
||||
background: var(--fog-highlight, #f3f4f6); |
||||
} |
||||
|
||||
:global(.dark) .result-item:hover { |
||||
background: var(--fog-dark-highlight, #374151); |
||||
} |
||||
|
||||
.result-header { |
||||
display: flex; |
||||
justify-content: space-between; |
||||
align-items: center; |
||||
margin-bottom: 0.5rem; |
||||
} |
||||
|
||||
.result-type { |
||||
font-size: 0.75rem; |
||||
font-weight: 600; |
||||
color: var(--fog-accent, #3b82f6); |
||||
text-transform: uppercase; |
||||
} |
||||
|
||||
.result-time { |
||||
font-size: 0.75rem; |
||||
color: var(--fog-text-light, #6b7280); |
||||
} |
||||
|
||||
:global(.dark) .result-time { |
||||
color: var(--fog-dark-text-light, #9ca3af); |
||||
} |
||||
|
||||
.result-content { |
||||
color: var(--fog-text, #1f2937); |
||||
font-size: 0.875rem; |
||||
line-height: 1.5; |
||||
} |
||||
|
||||
:global(.dark) .result-content { |
||||
color: var(--fog-dark-text, #f9fafb); |
||||
} |
||||
</style> |
||||
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
/** |
||||
* Global keyboard shortcuts handler |
||||
* Handles j/k navigation, r reply, z zap, / search, etc. |
||||
*/ |
||||
|
||||
export interface KeyboardShortcut { |
||||
key: string; |
||||
ctrl?: boolean; |
||||
shift?: boolean; |
||||
alt?: boolean; |
||||
meta?: boolean; |
||||
handler: (e: KeyboardEvent) => void; |
||||
description?: string; |
||||
} |
||||
|
||||
class KeyboardShortcutsManager { |
||||
private shortcuts: Map<string, KeyboardShortcut> = new Map(); |
||||
private enabled = true; |
||||
|
||||
/** |
||||
* Register a keyboard shortcut |
||||
*/ |
||||
register(shortcut: KeyboardShortcut): () => void { |
||||
const key = this.getKeyString(shortcut); |
||||
this.shortcuts.set(key, shortcut); |
||||
|
||||
// Return unregister function
|
||||
return () => { |
||||
this.shortcuts.delete(key); |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* Unregister a keyboard shortcut |
||||
*/ |
||||
unregister(key: string, modifiers?: { ctrl?: boolean; shift?: boolean; alt?: boolean; meta?: boolean }): void { |
||||
const keyString = this.getKeyString({ key, ...modifiers, handler: () => {} }); |
||||
this.shortcuts.delete(keyString); |
||||
} |
||||
|
||||
/** |
||||
* Enable/disable shortcuts |
||||
*/ |
||||
setEnabled(enabled: boolean): void { |
||||
this.enabled = enabled; |
||||
} |
||||
|
||||
/** |
||||
* Check if shortcuts are enabled |
||||
*/ |
||||
isEnabled(): boolean { |
||||
return this.enabled; |
||||
} |
||||
|
||||
/** |
||||
* Handle keyboard event |
||||
*/ |
||||
handleKeydown(e: KeyboardEvent): void { |
||||
if (!this.enabled) return; |
||||
|
||||
// Ignore if user is typing in an input, textarea, or contenteditable
|
||||
const target = e.target as HTMLElement; |
||||
if ( |
||||
target.tagName === 'INPUT' || |
||||
target.tagName === 'TEXTAREA' || |
||||
target.isContentEditable |
||||
) { |
||||
// Allow / for search even in inputs
|
||||
if (e.key === '/' && !e.ctrlKey && !e.metaKey) { |
||||
// Let it through
|
||||
} else { |
||||
return; |
||||
} |
||||
} |
||||
|
||||
const keyString = this.getKeyString({ |
||||
key: e.key.toLowerCase(), |
||||
ctrl: e.ctrlKey, |
||||
shift: e.shiftKey, |
||||
alt: e.altKey, |
||||
meta: e.metaKey, |
||||
handler: () => {} |
||||
}); |
||||
|
||||
const shortcut = this.shortcuts.get(keyString); |
||||
if (shortcut) { |
||||
e.preventDefault(); |
||||
e.stopPropagation(); |
||||
shortcut.handler(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Get key string for shortcut lookup |
||||
*/ |
||||
private getKeyString(shortcut: KeyboardShortcut): string { |
||||
const parts: string[] = []; |
||||
if (shortcut.ctrl || shortcut.meta) parts.push('ctrl'); |
||||
if (shortcut.shift) parts.push('shift'); |
||||
if (shortcut.alt) parts.push('alt'); |
||||
parts.push(shortcut.key.toLowerCase()); |
||||
return parts.join('+'); |
||||
} |
||||
|
||||
/** |
||||
* Initialize global keyboard handler |
||||
*/ |
||||
initialize(): void { |
||||
if (typeof window === 'undefined') return; |
||||
|
||||
const handler = (e: KeyboardEvent) => this.handleKeydown(e); |
||||
window.addEventListener('keydown', handler); |
||||
|
||||
// Return cleanup function
|
||||
return () => { |
||||
window.removeEventListener('keydown', handler); |
||||
}; |
||||
} |
||||
} |
||||
|
||||
export const keyboardShortcuts = new KeyboardShortcutsManager(); |
||||
|
||||
// Initialize on module load (browser only)
|
||||
if (typeof window !== 'undefined') { |
||||
keyboardShortcuts.initialize(); |
||||
} |
||||
Loading…
Reference in new issue