-
Notifications
You must be signed in to change notification settings - Fork 7
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
refactor: store accordion preference #815
Merged
+790
−1,779
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
454792d
refactor: set accordions opened by default
patricio0312rev 1bca28f
fix: unit tests
patricio0312rev c7242a3
refactor: store use accordion preference on local storage
patricio0312rev 5202eb0
refactor: use accordion implementations
patricio0312rev e420d69
fix: unit tests
patricio0312rev 0788384
fix: unit tests
patricio0312rev bc982c5
style: resolve style guide violations
patricio0312rev a950013
fix: unit tests
patricio0312rev c86f710
Merge branch 'refactor/set-accordions-open-by-default' of https://git…
patricio0312rev fbd32ff
fix unit tests
patricio0312rev aab10b2
Merge branch 'refactor/set-accordions-open-by-default' into refactor/…
patricio0312rev 449f343
style: resolve style guide violations
patricio0312rev 87b7851
fix: unit tests
patricio0312rev 1e53ef0
Merge branch 'refactor/set-accordions-open-by-default' into refactor/…
patricio0312rev 7481d0e
Merge branch 'refactor/store-accordion-preference' of https://github.…
patricio0312rev 8230e50
wip
patricio0312rev ac9be86
Merge branch 'refactor/set-accordions-open-by-default' into refactor/…
patricio0312rev 9196133
fix: wip
patricio0312rev 33d8b7a
wip
patricio0312rev 9f2ed24
fix: unit tests
patricio0312rev 39e8ac0
style: resolve style guide violations
patricio0312rev 5e0d4de
chore: merge with develop
patricio0312rev 0100bf1
Merge branch 'refactor/store-accordion-preference' of https://github.…
patricio0312rev a481e1b
fix: unit tests
patricio0312rev ad3a95b
fix: adjust local storage key per profile id
patricio0312rev d2ec495
refactor: set accordion key per profile
patricio0312rev 7b32045
Merge branch 'develop' into refactor/store-accordion-preference
patricio0312rev 3ab7dd4
chore: eslint
patricio0312rev 5aaa2a3
style: resolve style guide violations
patricio0312rev 8e56c67
Merge branch 'develop' into refactor/store-accordion-preference
shahin-hq 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 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,17 +1,37 @@ | ||
import { useCallback, useState } from "react"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export const useAccordion = () => { | ||
const [isExpanded, setIsExpanded] = useState(true); | ||
const getStorageKey = (key: string) => `accordion_${key}_isExpanded`; | ||
|
||
export const useAccordion = (key: string) => { | ||
const storageKey = getStorageKey(key); | ||
const [isExpanded, setIsExpanded] = useState<boolean>(() => { | ||
const storedValue = localStorage.getItem(storageKey); | ||
return storedValue ? JSON.parse(storedValue) : true; | ||
}); | ||
|
||
const handleHeaderClick = useCallback( | ||
(event: React.MouseEvent) => { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
|
||
setIsExpanded(!isExpanded); | ||
const newValue = !isExpanded; | ||
setIsExpanded(newValue); | ||
localStorage.setItem(storageKey, JSON.stringify(newValue)); | ||
}, | ||
[isExpanded], | ||
[isExpanded, storageKey], | ||
); | ||
|
||
useEffect(() => { | ||
const handleStorageChange = () => { | ||
const storedValue = localStorage.getItem(storageKey); | ||
if (storedValue) { | ||
setIsExpanded(JSON.parse(storedValue)); | ||
} | ||
}; | ||
|
||
window.addEventListener("storage", handleStorageChange); | ||
return () => window.removeEventListener("storage", handleStorageChange); | ||
}, [storageKey]); | ||
|
||
return { handleHeaderClick, isExpanded }; | ||
}; |
This file contains 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
This file contains 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.
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 think you can remove the
JSON.parse
part, because it stores only bool primitives which don't require deserialization, unless your plan was keeping data in a single key.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.
Oh, I am parsing to keep the consistency of the booleans. If I remove this, we could get values like "true", "false" and that could lead to false positives.
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.
In this case storing 1 or 0 could resolve the issue 👀
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.
Although it simplifies the issue with "true" and "false", that would reduce readability, and seeing that there are a lot of components and hooks in the repo, I wouldn't take that path 🤔