{"version":3,"file":"useChannelListKeyboardNavigation.mjs","names":[],"sources":["../../../../../src/components/ChannelList/hooks/useChannelListKeyboardNavigation.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport type { MouseEvent as ReactMouseEvent, RefObject } from 'react';\n\nimport {\n  createRowActionKeyHandlers,\n  useListboxKeyboardNavigation,\n} from '../../../a11y/hooks/useListboxKeyboardNavigation';\n\nconst OPTION_SELECTOR = '[role=\"option\"]';\nconst ROW_SELECTOR = '.str-chat__channel-list-item-container';\nconst ACTION_BUTTON_SELECTOR = '.str-chat__channel-list-item__action-buttons button';\nconst LOAD_MORE_SELECTOR = '[data-testid=\"load-more-button\"]';\n// Roving covers the channel rows and the Load more button, so Down from the last channel lands on\n// it. The first-new-item focus logic below counts channel rows only.\nconst NAVIGABLE_SELECTOR = `${OPTION_SELECTOR}, ${LOAD_MORE_SELECTOR}`;\n\n// How long a pending \"focus the first item of the next page\" request stays armed after Load more is\n// pressed. Expiry keeps an unrelated later list change (e.g. a channel arriving over the WebSocket)\n// from triggering a stray focus move.\nconst LOAD_MORE_FOCUS_TIMEOUT_MS = 4000;\n\n/**\n * Keyboard navigation for the channel list (`role=\"listbox\"`). ArrowUp, ArrowDown, Home and End\n * move focus across the channel rows (`role=\"option\"`) and the Load more button; ArrowLeft and\n * ArrowRight cycle between a row and its action buttons. Activating Load more moves focus to the\n * first item of the newly loaded page once it renders.\n *\n * @param listboxRef - Ref of the listbox element that contains the rows.\n * @returns The `onClickCapture` and `onKeyDown` handlers to spread onto the listbox element.\n */\nexport const useChannelListKeyboardNavigation = (\n  listboxRef: RefObject<HTMLElement | null>,\n) => {\n  // `enterScope: 'container'` and `resolveActiveOption` keep Up, Down, Home and End working while\n  // focus is on a row's action button, moving relative to that row.\n  const { onKeyDown } = useListboxKeyboardNavigation(listboxRef, {\n    enterScope: 'container',\n    keyHandlers: createRowActionKeyHandlers({\n      actionSelector: ACTION_BUTTON_SELECTOR,\n      rowSelector: ROW_SELECTOR,\n    }),\n    optionSelector: NAVIGABLE_SELECTOR,\n    resolveActiveOption: (active) =>\n      active.closest(ROW_SELECTOR)?.querySelector<HTMLElement>(OPTION_SELECTOR) ?? null,\n  });\n\n  // Channel rows only, without the Load more button.\n  const getChannelOptions = () =>\n    Array.from(listboxRef.current?.querySelectorAll<HTMLElement>(OPTION_SELECTOR) ?? []);\n\n  // Pending \"focus the first item of the next page\" request. It snapshots the pre-load rows rather\n  // than counting them, so a WebSocket-driven insert or reorder is not mistaken for the new page:\n  // `boundary` is the last pre-load option (the new page is appended after it) and `preLoad` holds\n  // every pre-load option node. Rows are keyed by `cid`, so their DOM nodes stay stable.\n  const pendingFocusRef = useRef<{\n    boundary: HTMLElement | null;\n    preLoad: Set<HTMLElement>;\n  } | null>(null);\n  const pendingTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n  const clearPendingFocus = () => {\n    pendingFocusRef.current = null;\n    if (pendingTimeoutRef.current) {\n      clearTimeout(pendingTimeoutRef.current);\n      pendingTimeoutRef.current = null;\n    }\n  };\n\n  // Activating Load more (a click, by mouse or keyboard) arms the focus move.\n  const onClickCapture = (event: ReactMouseEvent<HTMLElement>) => {\n    const target = event.target;\n    if (!(target instanceof Element) || !target.closest(LOAD_MORE_SELECTOR)) return;\n    const options = getChannelOptions();\n    pendingFocusRef.current = {\n      boundary: options[options.length - 1] ?? null,\n      preLoad: new Set(options),\n    };\n    if (pendingTimeoutRef.current) clearTimeout(pendingTimeoutRef.current);\n    pendingTimeoutRef.current = setTimeout(clearPendingFocus, LOAD_MORE_FOCUS_TIMEOUT_MS);\n  };\n\n  // Focuses the first item of the newly loaded page once it renders. Runs after every commit and\n  // does nothing unless a move is pending. The target must be after the pre-load boundary and\n  // absent before the load, so an unrelated row arriving over the WebSocket cannot steal focus.\n  useEffect(() => {\n    const pending = pendingFocusRef.current;\n    if (!pending) return;\n    const options = getChannelOptions();\n    // The list was empty on Load more: the whole list is the new page, so focus its first row.\n    if (!pending.boundary) {\n      if (options.length > 0) {\n        options[0].focus();\n        clearPendingFocus();\n      }\n      return;\n    }\n    const boundaryIndex = options.indexOf(pending.boundary);\n    // The boundary row was removed (e.g. the user left that channel): drop the request rather than\n    // guess where the page starts.\n    if (boundaryIndex === -1) {\n      clearPendingFocus();\n      return;\n    }\n    const firstNewOption = options\n      .slice(boundaryIndex + 1)\n      .find((option) => !pending.preLoad.has(option));\n    // Nothing new after the boundary yet: keep waiting (within the timeout) for the page to render.\n    if (!firstNewOption) return;\n    firstNewOption.focus();\n    clearPendingFocus();\n  });\n\n  useEffect(() => () => clearPendingFocus(), []);\n\n  return { onClickCapture, onKeyDown };\n};\n"],"mappings":";;;AAQA,IAAM,kBAAkB;AACxB,IAAM,eAAe;AACrB,IAAM,yBAAyB;AAC/B,IAAM,qBAAqB;AAG3B,IAAM,qBAAqB,GAAG,gBAAgB,IAAI;AAKlD,IAAM,6BAA6B;;;;;;;;;;AAWnC,IAAa,oCACX,eACG;CAGH,MAAM,EAAE,cAAc,6BAA6B,YAAY;EAC7D,YAAY;EACZ,aAAa,2BAA2B;GACtC,gBAAgB;GAChB,aAAa;EACf,CAAC;EACD,gBAAgB;EAChB,sBAAsB,WACpB,OAAO,QAAQ,YAAY,CAAC,EAAE,cAA2B,eAAe,KAAK;CACjF,CAAC;CAGD,MAAM,0BACJ,MAAM,KAAK,WAAW,SAAS,iBAA8B,eAAe,KAAK,CAAC,CAAC;CAMrF,MAAM,kBAAkB,OAGd,IAAI;CACd,MAAM,oBAAoB,OAA6C,IAAI;CAE3E,MAAM,0BAA0B;EAC9B,gBAAgB,UAAU;EAC1B,IAAI,kBAAkB,SAAS;GAC7B,aAAa,kBAAkB,OAAO;GACtC,kBAAkB,UAAU;EAC9B;CACF;CAGA,MAAM,kBAAkB,UAAwC;EAC9D,MAAM,SAAS,MAAM;EACrB,IAAI,EAAE,kBAAkB,YAAY,CAAC,OAAO,QAAQ,kBAAkB,GAAG;EACzE,MAAM,UAAU,kBAAkB;EAClC,gBAAgB,UAA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