-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: migrate routes to modals #406
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
99f96e8
WIP
elcharitas c03ea53
lint fixes
elcharitas ae93104
restyle
elcharitas 3f4fbd3
change user settings id
elcharitas 62dd88c
fix more links
elcharitas ba70431
add support for history
elcharitas 3be6a57
Merge branch 'dev' into ALPHA-4931-V2
elcharitas 0cd9395
fixes
elcharitas 305f1dd
add comment
elcharitas f5f7c68
Merge branch 'dev' into ALPHA-4931-V2
elcharitas 4ce40e8
rename
elcharitas 8474c00
history.forward to preserve state
elcharitas 9c3a896
reorder
elcharitas e8a6e6d
semantic changes
elcharitas ab9c52c
PR Feedback
elcharitas 205d838
undo chnage
elcharitas 11ff3b0
remove comment
elcharitas f11896c
fix
elcharitas 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,26 +1,14 @@ | ||
import { useCallback } from "react"; | ||
import { useHistory as useRRDHistory } from "react-router-dom"; | ||
import { EMobileTabRoutePaths } from "src/routes"; | ||
import { useControlledModal } from "../store/providers/controlled-modal-provider"; | ||
|
||
export const useHistory = () => { | ||
const history = useRRDHistory(); | ||
const { resetModalHistory } = useControlledModal(); | ||
|
||
/** | ||
* we shouldn't need this ideally, but adding a listener | ||
* ensures route navigation to tabs route paths which is great | ||
*/ | ||
history.listen(() => {}); | ||
history.listen(() => { | ||
// reset modal on route change | ||
resetModalHistory(); | ||
}); | ||
|
||
const backNavigation = useCallback(() => { | ||
if (history.length > 0) { | ||
history.goBack(); | ||
} else { | ||
history.push(EMobileTabRoutePaths.Superfeed); | ||
} | ||
}, [history]); | ||
|
||
return { | ||
...history, | ||
backNavigation, | ||
}; | ||
return history; | ||
}; |
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
91 changes: 91 additions & 0 deletions
91
packages/frontend/src/api/store/providers/controlled-modal-provider.tsx
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
import useEventListener from "src/api/hooks/useEventListener"; | ||
|
||
interface Prop { | ||
activeModal: string | null; | ||
setActiveModal: (modalId: string) => void; | ||
closeActiveModal: () => void; | ||
resetModalHistory: () => void; | ||
} | ||
|
||
export const ControlledModalContext = createContext<Prop>({ | ||
activeModal: null, | ||
setActiveModal: () => {}, | ||
closeActiveModal: () => {}, | ||
resetModalHistory: () => {}, | ||
}); | ||
|
||
export const useControlledModal = () => { | ||
const context = useContext(ControlledModalContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
"controlled-modal-context:useControlledModal: not used within a Provider" | ||
); | ||
} | ||
return context; | ||
}; | ||
|
||
const MODAL_HISTORY = new Set<string>(); | ||
|
||
const ControlledModalProvider: React.FC<{ children?: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [activeModal, setActiveModal] = useState<string | null>(null); | ||
|
||
const closeActiveModal = useCallback(() => { | ||
if (activeModal) { | ||
MODAL_HISTORY.delete(activeModal); | ||
// set active modal to last modal in history | ||
const lastModal = Array.from(MODAL_HISTORY).pop(); | ||
setActiveModal(lastModal || null); | ||
} | ||
}, [activeModal]); | ||
|
||
const setActiveModalWithHistory = (modalId: string) => { | ||
if (!MODAL_HISTORY.has(modalId)) { | ||
setActiveModal(modalId); | ||
MODAL_HISTORY.add(modalId); | ||
} | ||
}; | ||
|
||
const resetModalHistory = () => { | ||
setActiveModal(null); | ||
MODAL_HISTORY.clear(); | ||
}; | ||
|
||
useEventListener("popstate", (e) => { | ||
/** | ||
* If there is an active modal, | ||
* close it when the user swipes back or presses the back button | ||
*/ | ||
if (activeModal) { | ||
e.preventDefault(); | ||
history.forward(); | ||
closeActiveModal(); | ||
} | ||
}); | ||
|
||
return ( | ||
<ControlledModalContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
activeModal, | ||
setActiveModal: setActiveModalWithHistory, | ||
closeActiveModal, | ||
resetModalHistory, | ||
}), | ||
[activeModal, closeActiveModal] | ||
)} | ||
> | ||
{children} | ||
</ControlledModalContext.Provider> | ||
); | ||
}; | ||
|
||
export default ControlledModalProvider; |
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
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.
Any benefits of putting this outside and not using a
useState
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.
useState would be an overkill and wiould cause rerenders.