Skip to content

⇧ perf: Listen for Shift in the Open Conversation's Sidebar Row Only - #18718

Draft
danny-avila wants to merge 1 commit into
devfrom
danny-avila/sidebar-shift-listeners
Draft

danny-avila wants to merge 1 commit into
devfrom
danny-avila/sidebar-shift-listeners

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

Every conversation listed in the sidebar registers three listeners on the window (keydown, keyup, blur) to know whether shift is held, and every one of them holds that as state. Each key press anywhere in the app, typing in the composer included, runs one handler per listed conversation, and pressing shift re-renders the whole list. Only one row uses the answer: holding shift swaps the open conversation's menu for archive and delete.

Now only the open conversation's row listens. With 12 conversations listed the window carries one keyup listener where it carried 12; the count no longer grows with the list. It showed up in the first CI run of #18712, where a sidebar holding 24 conversations left by earlier specs added 72 window listeners to a count of the thread.

How it works

-export default function useShiftKey(): boolean {
+export default function useShiftKey(enabled = true): boolean {
   const [isShiftHeld, setIsShiftHeld] = useState(false);
   useEffect(() => {
+    if (!enabled) {
+      return;
+    }
     ...
     return () => {
       ...
+      setIsShiftHeld(false);
     };
-  }, []);
-  return isShiftHeld;
+  }, [enabled]);
+  return enabled && isShiftHeld;

Convo passes isActiveConvo, which it already computed and already required before acting on the key. A row that stops being the open one forgets the key, since it may be released while nothing listens.

Type of change

  • Performance improvement

Testing

Tested environments/configuration:

  • Chromium, mock e2e lane against the production client build, 12 seeded conversations.

Automated tests:

  • New useShiftKey.spec.tsx: follows the key, leaves shift with alt alone, resets on blur, registers nothing while not enabled, and starts, stops and forgets as enabled changes. The last two fail against the previous hook.
  • New e2e sidebar-shift-actions.spec.ts, three scenarios: holding shift offers archive and delete on the open conversation's row alone and letting go removes them; opening another conversation moves them to its row; the window carries one keyup listener with 12 conversations listed. Against the build without the change the first two pass and the third reads 12.
  • client: the Conversations suites (17 suites, 226 tests with the new hook spec).
  • Full static checks, TypeScript included.

Screenshots / recordings

No user-facing change.

Risk / compatibility

A row that becomes the open one while shift is already held learns of it at the key's next repeat rather than at once. Before, every row tracked the key all the time.

Checklist

  • I reviewed my own changes
  • Relevant tests have been added or updated
  • Existing relevant tests pass
  • The change does not introduce new warnings or errors
  • User-facing or complex behavior is documented where necessary
  • Required dependency changes have been merged/published
  • Required documentation PR: N/A

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant