()
| 24 | * marks just that push read; j/k walks the currently-displayed order. |
| 25 | */ |
| 26 | export function InboxSidebar() { |
| 27 | const { t } = useTranslation() |
| 28 | const entries = inboxLive.useStore((s) => s.entries) |
| 29 | const loading = inboxLive.useStore((s) => s.loading) |
| 30 | const selectedId = useInboxSelection((s) => s.selectedEntryId) |
| 31 | const select = useInboxSelection((s) => s.select) |
| 32 | const markRead = useInboxRead((s) => s.markRead) |
| 33 | const mode = useInboxViewMode((s) => s.mode) |
| 34 | |
| 35 | const threads = useMemo(() => groupThreads(entries), [entries]) |
| 36 | const readIds = useMemo(() => { |
| 37 | const ids: Record<string, true> = {} |
| 38 | for (const entry of entries) { |
| 39 | if (entry.readAt) ids[entry.id] = true |
| 40 | } |
| 41 | return ids |
| 42 | }, [entries]) |
| 43 | |
| 44 | // The visible order j/k and default-select walk: clustered order in |
| 45 | // workspace mode, plain newest-first in time mode. |
| 46 | const ordered = useMemo( |
| 47 | () => (mode === 'workspace' ? threads.flatMap((th) => th.entries) : entries), |
| 48 | [mode, threads, entries], |
| 49 | ) |
| 50 | |
| 51 | /** select + mark read in one. Used by every selection mutation site. */ |
| 52 | const selectAndRead = (id: string) => { |
| 53 | select(id) |
| 54 | markRead(id) |
| 55 | } |
| 56 | |
| 57 | // Default-select the first row on first non-empty load. Latch once. |
| 58 | const everSelectedRef = useRef(false) |
| 59 | useEffect(() => { |
| 60 | if (everSelectedRef.current) return |
| 61 | if (ordered.length === 0) return |
| 62 | if (!selectedId) selectAndRead(ordered[0]!.id) |
| 63 | everSelectedRef.current = true |
| 64 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 65 | }, [ordered, selectedId]) |
| 66 | |
| 67 | // Keyboard nav — j/k move within the currently-displayed order. |
| 68 | useEffect(() => { |
| 69 | function onKey(e: KeyboardEvent) { |
| 70 | if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) return |
| 71 | if (e.metaKey || e.ctrlKey || e.altKey) return |
| 72 | if (e.key !== 'j' && e.key !== 'k') return |
| 73 | if (ordered.length === 0) return |
| 74 | const idx = ordered.findIndex((x) => x.id === selectedId) |
| 75 | const next = e.key === 'j' ? Math.min(ordered.length - 1, idx + 1) : Math.max(0, idx - 1) |
| 76 | if (next !== idx && ordered[next]) selectAndRead(ordered[next]!.id) |
| 77 | } |
| 78 | window.addEventListener('keydown', onKey) |
| 79 | return () => window.removeEventListener('keydown', onKey) |
| 80 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 81 | }, [ordered, selectedId]) |
| 82 | |
| 83 | if (loading && entries.length === 0) { |
nothing calls this directly
no test coverage detected