|
1 |
| -import React, { useCallback, useState } from 'react'; |
| 1 | +import React, { useCallback, useMemo, useState } from 'react'; |
2 | 2 | import { AppStateContext } from './AppStateContext';
|
3 | 3 |
|
4 | 4 | type Props = {
|
5 | 5 | children: React.ReactNode;
|
6 | 6 | };
|
7 | 7 |
|
| 8 | +export type SelectedVersion = { |
| 9 | + versionId: string; |
| 10 | + nodeId?: string; |
| 11 | +}; |
| 12 | + |
8 | 13 | export const AppStateProvider = ({ children }: Props) => {
|
9 | 14 | const [selectedContentId, setSelectedContentId] = useState<string>();
|
10 |
| - const [selectedVersionId, setSelectedVersionId] = useState<string>(); |
| 15 | + const [selectedVersion, setSelectedVersion] = useState<SelectedVersion>(); |
11 | 16 |
|
12 |
| - const updateSelectedContentId = useCallback((selectedContentId: string) => { |
13 |
| - setSelectedVersionId(undefined); |
14 |
| - setSelectedContentId(selectedContentId); |
15 |
| - }, []); |
16 |
| - const setSelectedVersionIdMemoized = useCallback(setSelectedVersionId, [setSelectedVersionId]); |
| 17 | + const setSelectedContentIdMemoized = useCallback(setSelectedContentId, [setSelectedContentId]); |
| 18 | + const setSelectedVersionMemoized = useCallback(setSelectedVersion, [setSelectedVersion]); |
| 19 | + const selectedVersionMemoized = useMemo(() => selectedVersion, [selectedVersion]); |
17 | 20 |
|
18 | 21 | return (
|
19 | 22 | <AppStateContext.Provider
|
20 | 23 | value={{
|
21 | 24 | selectedContentId,
|
22 |
| - setSelectedContentId: updateSelectedContentId, |
23 |
| - selectedVersionId, |
24 |
| - setSelectedVersionId: setSelectedVersionIdMemoized, |
| 25 | + setSelectedContentId: setSelectedContentIdMemoized, |
| 26 | + selectedVersion: selectedVersionMemoized, |
| 27 | + setSelectedVersion: setSelectedVersionMemoized, |
25 | 28 | }}
|
26 | 29 | >
|
27 | 30 | {children}
|
|
0 commit comments