|
1 | | -import React, { useContext } from 'react' |
2 | | -import { Item } from '../types' |
3 | | -import TreeNode from './TreeNode' |
4 | | -import { DocsContext } from './Layout' |
| 1 | +import { List } from '@committed/components' |
5 | 2 | import { withPrefix } from 'gatsby' |
6 | | -import { firstUrl, firstInfo } from '../util/tree' |
| 3 | +import React, { useCallback, useContext } from 'react' |
7 | 4 | import { useHotkeys } from 'react-hotkeys-hook' |
8 | | -import { List } from '@committed/components' |
| 5 | +import { Item } from '../types' |
| 6 | +import { firstInfo, firstUrl } from '../util/tree' |
| 7 | +import { DocsContext } from './Layout' |
| 8 | +import TreeNode from './TreeNode' |
9 | 9 |
|
10 | 10 | export interface TreeProps { |
11 | 11 | location: any |
@@ -42,23 +42,28 @@ export const Tree = ({ |
42 | 42 |
|
43 | 43 | const index = data.items.findIndex(item => isParent(item)) |
44 | 44 |
|
45 | | - if (index == -1) { |
46 | | - useHotkeys('shift+down', () => navigateItem(0)) |
47 | | - } else { |
48 | | - if (index == 0) { |
49 | | - useHotkeys('shift+up', () => navigate('/')) |
| 45 | + const navigateDown = useCallback(() => { |
| 46 | + if (index === -1) { |
| 47 | + navigateItem(0) |
| 48 | + } else if (index < data.items.length - 1) { |
| 49 | + navigateItem(index + 1) |
50 | 50 | } |
51 | | - if (index > 0) { |
| 51 | + }, [navigateItem, index, data]) |
| 52 | + |
| 53 | + const navigateUp = useCallback(() => { |
| 54 | + if (index === 0) { |
| 55 | + navigate('/') |
| 56 | + } else if (index > 0) { |
52 | 57 | if (isActive(firstInfo(data.items[index]).id)) { |
53 | | - useHotkeys('shift+up', () => navigateItem(index - 1)) |
| 58 | + navigateItem(index - 1) |
54 | 59 | } else { |
55 | | - useHotkeys('shift+up', () => navigateItem(index)) |
| 60 | + navigateItem(index) |
56 | 61 | } |
57 | 62 | } |
58 | | - if (index < data.items.length - 1) { |
59 | | - useHotkeys('shift+down', () => navigateItem(index + 1)) |
60 | | - } |
61 | | - } |
| 63 | + }, [navigateItem, index, data, firstInfo, isActive]) |
| 64 | + |
| 65 | + useHotkeys('shift+down', navigateDown, [navigateDown]) |
| 66 | + useHotkeys('shift+up', navigateUp, [navigateUp]) |
62 | 67 |
|
63 | 68 | return ( |
64 | 69 | <List dense> |
|
0 commit comments