Skip to content

Commit

Permalink
Fix issue with view posts shortcut going to new post page
Browse files Browse the repository at this point in the history
  • Loading branch information
0xi4o committed Aug 29, 2024
1 parent 5f7d183 commit fc1b796
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 10 additions & 7 deletions app/lib/hooks/useKeyboardShortcuts.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect } from 'react'
import { useCallback, useEffect, useMemo } from 'react'

import {
AllShortcuts,
Expand All @@ -10,7 +10,6 @@ import {

// Centralized record of all shortcuts
const allShortcuts: AllShortcuts = {
// TODO: add all local shortcuts here
[EditorShortcuts.BLUR]: {
description: 'Blur',
key: 'Escape',
Expand All @@ -35,13 +34,11 @@ const allShortcuts: AllShortcuts = {
key: '?',
modifiers: {},
},
// TODO: this is a global shortcut
[EditorShortcuts.MAIN_MENU]: {
description: 'Main Menu',
key: 'm',
modifiers: {},
},
// TODO: this is a global shortcut
[EditorShortcuts.NEW_POST]: {
description: 'New Post',
key: 'p',
Expand All @@ -58,7 +55,6 @@ const allShortcuts: AllShortcuts = {
key: 'r',
modifiers: {},
},
// TODO: this is a global shortcut
[EditorShortcuts.SPLASH_DIALOG]: {
description: 'Quick Start',
key: 'q',
Expand Down Expand Up @@ -96,6 +92,14 @@ export const setGlobalShortcutAction = (
}

const useKeyboardShortcuts = (shortcuts: ShortcutActions) => {
const sortedShortcuts = useMemo(() => {
return Object.entries(allShortcuts).sort(([, a], [, b]) => {
const aModifiers = Object.values(a.modifiers).filter(Boolean).length
const bModifiers = Object.values(b.modifiers).filter(Boolean).length
return bModifiers - aModifiers
})
}, [])

const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
const isInputField =
Expand All @@ -112,8 +116,7 @@ const useKeyboardShortcuts = (shortcuts: ShortcutActions) => {
meta: event.metaKey,
}

for (const shortcutName in allShortcuts) {
const shortcutConfig = allShortcuts[shortcutName]
for (const [shortcutName, shortcutConfig] of sortedShortcuts) {
const {
global,
key: shortcutKey,
Expand Down
4 changes: 3 additions & 1 deletion app/lib/providers/aurelius.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const AureliusProvider = ({ children }: AureliusProviderProps) => {

const handleSplashOpen = (open: boolean) => {
startTransition(() => {
setSplashOpen(open)
setSplashOpen(() => open)
})
}

Expand All @@ -168,13 +168,15 @@ const AureliusProvider = ({ children }: AureliusProviderProps) => {

const viewAllPosts = () => {
setMainMenuOpen(() => false)
handleSplashOpen(false)
startTransition(() => {
navigate(ROUTES.VIEW.POSTS)
})
}

const viewAllWritingSessions = () => {
setMainMenuOpen(() => false)
handleSplashOpen(false)
startTransition(() => {
navigate(ROUTES.VIEW.WRITING_SESSIONS)
})
Expand Down

0 comments on commit fc1b796

Please sign in to comment.