Skip to content

Conversation

george-hub331
Copy link
Contributor

@george-hub331 george-hub331 commented Apr 6, 2025

Fixes #2350

Summary

This PR adds a centralized keyboard shortcuts system using React Context, making it easier to manage and customize keyboard shortcuts throughout the application.

Changes

  • Added ShortcutsContext to manage global keyboard shortcuts
  • Created useShortcuts hook for easy shortcut registration
  • Converted ShortcutModal to TypeScript
  • Added support for key combinations (e.g. Shift + ?)
  • Improved modal layout using CSS Grid
  • Added proper TypeScript types and interfaces
  • Prevented shortcut execution in modal inputs
  • Added automatic grouping of shortcuts by category

Implementation Details

  • Used React Context for centralized state management
  • Added support for modifier keys (Shift, Ctrl, Alt, Meta)
  • Added proper cleanup of event listeners
  • Made shortcuts configurable per component
  • Maintained backwards compatibility with existing shortcuts

New improvements

  • Resolved bugs on shortcut hooks, prevent shortcuts from interfering with inputs / modals / other operations
  • Added new general shortcuts for showing tour help(shift + H), focusing on QmHash or CID input(on all screens)(/)
  • Added shortcuts on peers page
  • Rearraged shortcut sections

Short Demo

demo.mp4

george-hub331 and others added 9 commits February 20, 2025 10:59
Introduces a new grid view mode for the files page, allowing users to switch between list and grid views. Includes:
- New FilesGrid component
- View mode toggle buttons
- Shared props between list and grid views
- Updated rendering logic to support both view modes
- Added a new "General" shortcut label in the app localization.
- Refactored shortcut handling in the FilesGrid and FilesList components to use refs for focused file management.
- Improved keyboard shortcut handling by integrating the useShortcuts hook for better organization and functionality.
- Adjusted the order of shortcut groups in the shortcut modal for improved user experience.
@george-hub331 george-hub331 marked this pull request as ready for review April 13, 2025 19:52
@george-hub331 george-hub331 requested a review from a team as a code owner April 13, 2025 19:52
@george-hub331
Copy link
Contributor Author

Hi @SgtPooki, I’ve made an attempt at addressing the issue and would appreciate your review when you have a moment.

@SgtPooki
Copy link
Member

Hi @SgtPooki, I’ve made an attempt at addressing the issue and would appreciate your review when you have a moment.

It might take me a while to get to review this one thoroughly.

@george-hub331
Copy link
Contributor Author

No worries at all, I understand. Thanks for the heads-up, and I’ll be around if you need anything clarified when you get to it!

@SgtPooki SgtPooki changed the title Add Keyboard Shortcuts System feat: Keyboard Shortcuts System Aug 12, 2025
Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for this @george-hub331, this is great. Sorry it took me so long to only partially get through it 😅

I only got through 16/26 files but it's looking pretty useful. Some changes requested, and some comments could use some discussion.

Comment on lines 22 to 40
useShortcuts([{
keys: ['Meta', 'c'],
label: t('addConnection'),
action: () => {
setIsOpen((prev) => !prev)
},
group: t('app:shortcutModal.general')
}, {
keys: ['Shift', 'F'],
label: t('peers:filterPeers'),
action: () => {
const filterInput = document.getElementById('peers-filter')

if (filterInput) {
filterInput.focus()
}
},
group: t('app:shortcutModal.general')
}])
Copy link
Member

@SgtPooki SgtPooki Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component shows just how simple it is to use.. very nice work.

Some potential improvements here:

  • use useCallback to ensure the actions are handled appropriately in the component's lifecycles.
  • use a ref instead of document.getElementById to keep react in the loop..

maybe the API could be updated to accept a ref and automatically focus it?


is it too crazy to have the shortcuts system modular so we could useShortcuts and pass an ID so actions could be added in the exact component impacted by the shortcut?

that way, AddConnection would just useShortCuts to add the shortcut needed for it, instead of adding them ALL at the top level, any component could easily add shortcut support by specifying an ID. If that shortcut module is enabled currently, it would add to it.. (we may not even need to do this.. I still need to dive into the actual hook)

for components that doen't show until someone acts on it, they could add or remove the shortcut based on state only they need to be aware of, instead of propdrilling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook was designed to work on a page by page basis, so in most cases, we’re not adding shortcuts globally. That said, I do like the idea of making it more modular so components can register/unregister their own shortcuts without prop drilling, could be a nice addition actually.. working on it🤔

@SgtPooki
Copy link
Member

So that you know, I'm having some ts migration issues while adding the diagnostics screen in #2392. I am considering allowing // @ts-nocheck since, without it, PRs can easily get out of hand if we're forcing folks to address all that tech debt in new PRs.

I want all new code to be typechecked so we're not exacerbating the problem of untyped code, but without needing new PRs to enlarge unnecessarily..

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.

feat: create shortcut component that can be used on any page

2 participants