@@ -4,6 +4,10 @@ import { useEffect, useRef, useState, type MouseEvent } from "react";
44import { useInView } from "react-intersection-observer" ;
55
66import { Button } from "@/shared/ui/button" ;
7+ import {
8+ useCommandRequest ,
9+ useCommandRequestId ,
10+ } from "@/shared/hooks/use-command-request" ;
711import { searchWordsFromQuery } from "@/shared/lib/search" ;
812import {
913 type NoteListNavigationDirection ,
@@ -155,10 +159,7 @@ export function NotesPane({
155159 const pendingNotesPaneSelectionRef = useRef < "first" | "selected" | null > (
156160 null ,
157161 ) ;
158- const lastHandledFocusNotesPaneRequestIdRef = useRef ( 0 ) ;
159- const lastHandledFocusNotesSearchRequestIdRef = useRef ( 0 ) ;
160162 const noteRowRefs = useRef ( new Map < string , HTMLButtonElement | null > ( ) ) ;
161- const shouldRestoreSelectedRowFocusRef = useRef ( false ) ;
162163 const [ isSearchFocused , setIsSearchFocused ] = useState ( false ) ;
163164 const scrollContainerRef = useRef < HTMLDivElement | null > ( null ) ;
164165 const { ref : loadMoreRef , inView } = useInView ( {
@@ -245,34 +246,15 @@ export function NotesPane({
245246 searchInputRef . current ?. select ( ) ;
246247 } , [ isSearchOpen ] ) ;
247248
248- useEffect ( ( ) => {
249- if (
250- focusNotesSearchRequestId === 0 ||
251- lastHandledFocusNotesSearchRequestIdRef . current ===
252- focusNotesSearchRequestId
253- ) {
254- return ;
255- }
256-
257- lastHandledFocusNotesSearchRequestIdRef . current = focusNotesSearchRequestId ;
249+ useCommandRequestId ( focusNotesSearchRequestId , ( ) => {
258250 setFocusedPane ( "notes" ) ;
259251 setIsSearchOpen ( true ) ;
260252 setIsSearchFocused ( true ) ;
261253 focusSearchInput ( ) ;
262- } , [ focusNotesSearchRequestId , focusSearchInput , setFocusedPane ] ) ;
263-
264- useEffect ( ( ) => {
265- if (
266- ! focusNotesPaneRequest ||
267- lastHandledFocusNotesPaneRequestIdRef . current ===
268- focusNotesPaneRequest . requestId
269- ) {
270- return ;
271- }
254+ } ) ;
272255
273- lastHandledFocusNotesPaneRequestIdRef . current =
274- focusNotesPaneRequest . requestId ;
275- let selection = focusNotesPaneRequest . selection ?? "selected" ;
256+ useCommandRequest ( focusNotesPaneRequest , ( request ) => {
257+ let selection = request . selection ?? "selected" ;
276258
277259 // If the selected note isn't in the current filtered list, fall back
278260 // to selecting the first visible note instead of focusing an empty
@@ -302,14 +284,7 @@ export function NotesPane({
302284 setFocusedPane ( "notes" ) ;
303285 setIsSearchFocused ( false ) ;
304286 focusNotesPaneTarget ( scrollContainerRef . current ) ;
305- } , [
306- focusNotesPaneRequest ,
307- filteredNotes ,
308- isNotesPlaceholderData ,
309- selectFirstVisibleNote ,
310- selectedNoteId ,
311- setFocusedPane ,
312- ] ) ;
287+ } ) ;
313288
314289 useEffect ( ( ) => {
315290 setShowHeaderBorder ( ( scrollContainerRef . current ?. scrollTop ?? 0 ) > 0 ) ;
@@ -320,29 +295,21 @@ export function NotesPane({
320295 return ;
321296 }
322297
323- if ( pendingNotesPaneSelectionRef . current && isNotesPlaceholderData ) {
298+ if ( ! pendingNotesPaneSelectionRef . current ) {
324299 return ;
325300 }
326301
327- // Only respond to filteredNotes.length changes when there is a
328- // pending selection (initial data load). Without this guard,
329- // loading more notes via infinite scroll would scroll back to
330- // the selected note.
331- if (
332- ! pendingNotesPaneSelectionRef . current &&
333- ! shouldRestoreSelectedRowFocusRef . current
334- ) {
302+ if ( isNotesPlaceholderData ) {
335303 return ;
336304 }
337305
338- if ( selectedNoteId || shouldRestoreSelectedRowFocusRef . current ) {
306+ if ( selectedNoteId ) {
339307 pendingNotesPaneSelectionRef . current = null ;
340- shouldRestoreSelectedRowFocusRef . current = false ;
341308 focusSelectedNoteRow ( scrollContainerRef . current ) ;
342309 return ;
343310 }
344311
345- if ( pendingNotesPaneSelectionRef . current && selectFirstVisibleNote ( ) ) {
312+ if ( selectFirstVisibleNote ( ) ) {
346313 pendingNotesPaneSelectionRef . current = null ;
347314 return ;
348315 }
@@ -550,9 +517,6 @@ export function NotesPane({
550517 selectedNoteId = { selectedNoteId }
551518 setSlideInNoteId = { setSlideInNoteId }
552519 shouldSkipAnimation = { shouldSkipAnimation }
553- setShouldRestoreSelectedRowFocus = { ( ) => {
554- shouldRestoreSelectedRowFocusRef . current = true ;
555- } }
556520 />
557521 ) ;
558522 } ) }
0 commit comments