|
1 | 1 | 'use client';
|
2 | 2 |
|
3 |
| -import {createContext, useEffect, useState} from 'react'; |
| 3 | +import {createContext, useEffect, useReducer, useState} from 'react'; |
4 | 4 | import Cookies from 'js-cookie';
|
5 | 5 |
|
6 | 6 | type ProjectCodeKeywords = {
|
@@ -88,10 +88,12 @@ export const DEFAULTS: CodeKeywords = {
|
88 | 88 | USER: undefined,
|
89 | 89 | };
|
90 | 90 |
|
| 91 | +type SeledtedCodeTabs = Record<string, string | undefined>; |
| 92 | + |
91 | 93 | type CodeContextType = {
|
92 | 94 | codeKeywords: CodeKeywords;
|
93 | 95 | isLoading: boolean;
|
94 |
| - sharedCodeSelection: [string | null, React.Dispatch<string | null>]; |
| 96 | + sharedCodeSelection: [SeledtedCodeTabs, React.Dispatch<[string, string]>]; |
95 | 97 | sharedKeywordSelection: [
|
96 | 98 | Record<string, number>,
|
97 | 99 | React.Dispatch<Record<string, number>>,
|
@@ -297,8 +299,18 @@ export function CodeContextProvider({children}: {children: React.ReactNode}) {
|
297 | 299 | // that is the only namespace that actually has a list
|
298 | 300 | const sharedKeywordSelection = useState<Record<string, number>>({});
|
299 | 301 |
|
| 302 | + const storedSelections = Object.fromEntries( |
| 303 | + Object.entries( |
| 304 | + // default to an empty object if localStorage is not available on the server |
| 305 | + typeof localStorage === 'undefined' ? {} : localStorage |
| 306 | + ).filter(([key]) => key.startsWith('Tabgroup:')) |
| 307 | + ); |
| 308 | + |
300 | 309 | // Maintains the global selection for which code block tab is selected
|
301 |
| - const sharedCodeSelection = useState<string | null>(null); |
| 310 | + // const sharedCodeSelection = useState<SeledtedCodeTabs>(storedSelections); |
| 311 | + const sharedCodeSelection = useReducer((tabs: SeledtedCodeTabs, [groupId, value]: [string, string]) => { |
| 312 | + return {...tabs, [groupId]: value}; |
| 313 | + }, storedSelections) |
302 | 314 |
|
303 | 315 | const result: CodeContextType = {
|
304 | 316 | codeKeywords,
|
|
0 commit comments