Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 5 #1057

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
19 changes: 3 additions & 16 deletions src/HotkeysProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,31 @@ interface Props {
}

export const HotkeysProvider = ({ initiallyActiveScopes = ['*'], children }: Props) => {
const [internalActiveScopes, setInternalActiveScopes] = useState(

Choose a reason for hiding this comment

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

Maybe we could have this state wrapped in a React Context to allow separating enableScope and disableScope, thus allowing us to avoid re-renders where we use them.

This is a very painful problem right now, we cannot call useHotkeysContext() without having re-renders because of enabledScopes changing and causing every component calling useHotkeysContext() to re-render, even if they don't care about this dependency and just want to enable/disable a scope.

Choose a reason for hiding this comment

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

I would be glad to collaborate on this because we do an extensive usage of scopes at https://github.com/twentyhq/twenty and we have problems managing re-renders.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I don't think I quite understand this issue. enableScope and disableScope are already decoupled.

enabledScopes is a state, so changing the value will retrigger depend components. I understand that this causes re renders when calling enableScope, but the only fix would be to have the enabledScopes in another context, which, API wise, seems weird.

Maybe I am missing something?

initiallyActiveScopes?.length > 0 ? initiallyActiveScopes : ['*']
)
const [internalActiveScopes, setInternalActiveScopes] = useState(initiallyActiveScopes)
const [boundHotkeys, setBoundHotkeys] = useState<Hotkey[]>([])

const enableScope = useCallback((scope: string) => {
setInternalActiveScopes((prev) => {
if (prev.includes('*')) {
return [scope]
}

return Array.from(new Set([...prev, scope]))
})
}, [])

const disableScope = useCallback((scope: string) => {
setInternalActiveScopes((prev) => {
if (prev.filter((s) => s !== scope).length === 0) {
return ['*']
} else {
return prev.filter((s) => s !== scope)
}
return prev.filter((s) => s !== scope)
})
}, [])

const toggleScope = useCallback((scope: string) => {
setInternalActiveScopes((prev) => {
if (prev.includes(scope)) {
if (prev.filter((s) => s !== scope).length === 0) {
return ['*']
} else {
return prev.filter((s) => s !== scope)
}
return prev.filter((s) => s !== scope)
} else {
if (prev.includes('*')) {
return [scope]
}

return Array.from(new Set([...prev, scope]))
}
})
Expand Down
57 changes: 25 additions & 32 deletions tests/HotkeysProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react'
Copy link
Contributor

@kachkaev kachkaev Dec 7, 2023

Choose a reason for hiding this comment

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

It might be a bit better to do a namespace import:

Suggested change
import React from 'react'
import * as React from 'react'

Source: https://twitter.com/dan_abramov/status/1308739731551858689

This can make the difference in the future (React v19 or v20).


What is the current roadmap for v5? Just curious 🙃

Copy link
Owner Author

@JohannesKlauss JohannesKlauss Jan 4, 2024

Choose a reason for hiding this comment

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

Thanks for the heads up. I have very limited time & motivation to improve on version 5. I hope this will change in the next months, but no promises.

import { render, act, renderHook } from '@testing-library/react'
import { HotkeysProvider, useHotkeysContext, useHotkeys } from '../src'
import { ReactNode } from 'react'
Expand All @@ -21,17 +22,6 @@ test('should default to wildcard scope', () => {
expect(result.current.enabledScopes).toEqual(['*'])
})

test('should default to wildcard scope if empty array is provided as initialActiveScopes', () => {
const wrapper = ({ children }: { children: ReactNode }) => (
<HotkeysProvider initiallyActiveScopes={[]}>{children}</HotkeysProvider>
)
const { result } = renderHook(() => useHotkeysContext(), {
wrapper,
})

expect(result.current.enabledScopes).toEqual(['*'])
})

test('should return active scopes and scope modifying functions', () => {
const { result } = renderHook(() => useHotkeysContext(), {
wrapper: HotkeysProvider,
Expand All @@ -55,24 +45,6 @@ test('should activate scope by calling activateScope', () => {
expect(result.current.enabledScopes).toEqual(['foo'])
})

test('should reactivate wildcard scope if all other scopes are deactivated', () => {
const { result } = renderHook(() => useHotkeysContext(), {
wrapper: HotkeysProvider,
})

act(() => {
result.current.enableScope('foo')
})

expect(result.current.enabledScopes).toEqual(['foo'])

act(() => {
result.current.disableScope('foo')
})

expect(result.current.enabledScopes).toEqual(['*'])
})

test('should return multiple scopes if different scopes are activated', () => {
const { result } = renderHook(() => useHotkeysContext(), {
wrapper: HotkeysProvider,
Expand Down Expand Up @@ -128,7 +100,7 @@ test('should toggle scope by calling toggleScope', () => {
result.current.toggleScope('foo')
})

expect(result.current.enabledScopes).toEqual(['*'])
expect(result.current.enabledScopes).toEqual([])

act(() => {
result.current.toggleScope('foo')
Expand All @@ -137,7 +109,7 @@ test('should toggle scope by calling toggleScope', () => {
expect(result.current.enabledScopes).toEqual(['foo'])
})

test('should keep wildcard scope active when all is the only active scope and gets deactivated', () => {
test('should be able to disable wildcard like any other scope', () => {
const { result } = renderHook(() => useHotkeysContext(), {
wrapper: HotkeysProvider,
})
Expand All @@ -146,7 +118,7 @@ test('should keep wildcard scope active when all is the only active scope and ge
result.current.disableScope('*')
})

expect(result.current.enabledScopes).toEqual(['*'])
expect(result.current.enabledScopes).toEqual([])
})

test('should return initially set scopes', () => {
Expand Down Expand Up @@ -239,3 +211,24 @@ test('should return descriptions for bound hotkeys', () => {

expect(result.current.hotkeys[0].description).toEqual('bar')
})

test('should have no active scopes after deactivating all current scopes', () => {
const { result } = renderHook(() => useHotkeysContext(), {
wrapper: HotkeysProvider,
})
act(() => {
result.current.enableScope('foo')
})
act(() => {
result.current.enableScope('bar')
})
expect(result.current.enabledScopes).toEqual(['foo', 'bar'])
act(() => {
result.current.disableScope('foo')
})
act(() => {
result.current.disableScope('bar')
})

expect(result.current.enabledScopes).toEqual([])
})