This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Switch TabItems with identical labels together
#380
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { createContext, useState } from "react"; | ||
| import { createHash } from "crypto"; | ||
| import type { BinaryToTextEncoding } from "crypto"; | ||
| import { DataTab } from "./types"; | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| export interface TabContextProps { | ||
| getSelectedLabel: (tabs: Array<string>) => string; | ||
| setSelectedLabel: (tabs: Array<string>, selected: string) => void; | ||
| } | ||
|
|
||
| export const TabContext = createContext<TabContextProps>({ | ||
| getSelectedLabel: (tabs: Array<string>): string => { | ||
| return ""; | ||
| }, | ||
| setSelectedLabel: (tabs: Array<string>, selected: string) => { | ||
| return; | ||
| }, | ||
| }); | ||
|
|
||
| const labelHashKey = (labels: Array<string>): string => { | ||
| const labelsCopy = [...labels]; | ||
| labelsCopy.sort(); | ||
| const hash = createHash("sha256"); | ||
| hash.update(labelsCopy.join("")); | ||
| return hash.digest("utf8" as BinaryToTextEncoding); | ||
| }; | ||
|
|
||
|
Comment on lines
+21
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for hashing? Why not just store as the label string
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I felt weird about having an unbounded key. I guess we'll never have hundreds of labels or anything, so this won't be a big deal. I've changed this to return the joined labels instead of hashing. |
||
| interface TabContextProviderProps { | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| export const TabContextProvider = ({ children }: TabContextProviderProps) => { | ||
| const [stateForAllLabels, setStateForAllLabels] = useState({}); | ||
|
|
||
| const context = { | ||
| getSelectedLabel: (tabs: Array<string>): string => { | ||
| return stateForAllLabels[labelHashKey(tabs)]; | ||
| }, | ||
|
|
||
| setSelectedLabel: (tabs: Array<string>, selected: string): void => { | ||
| const key = labelHashKey(tabs); | ||
| setStateForAllLabels((prevState) => { | ||
| return { ...prevState, [key]: selected }; | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| return <TabContext.Provider value={context}>{children}</TabContext.Provider>; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I haven't created a context with methods like this before. It looked so wrong to me but, the docs say this is how it goes so, 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use this approach in a few places in the docs engine. I'm not super familiar with React best practices, so I'm happy to change this (or create a GitHub issue so we can change this everywhere later) if there's a better way!