-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(Relative routing): Default relative routing to current location #4978
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
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b8af575
enhance relative navigation from determination
nlynzaad c53266f
update react-router tests
nlynzaad 9e1b1dd
some more test refining
nlynzaad 55ba9af
code cleanup
nlynzaad ba524f1
test cleanup
nlynzaad 745107c
replicate changes to Solid
nlynzaad b4d41f8
update docs
nlynzaad 44ad37c
ci: apply automated fixes
autofix-ci[bot] d5701da
Merge branch 'main' into relative-routing-to-current-location
nlynzaad e0b8814
resolve merge issue
nlynzaad 0c9b67a
apply code rabbit suggestions
nlynzaad 983b7ce
Merge branch 'main' into relative-routing-to-current-location
nlynzaad 723540c
ci: apply automated fixes
autofix-ci[bot] 944af34
apply code rabbit doc suggestion
nlynzaad f5c5559
apply code rabbit doc suggestion
nlynzaad 0ae150e
reactivity and code cleanup
nlynzaad ad7eea5
ci: apply automated fixes
autofix-ci[bot] 8f41f21
cleanup tests
nlynzaad de884b8
consolidate from logic
nlynzaad d3391ea
Merge branch 'main' into relative-routing-to-current-location
nlynzaad 8feb3c0
revert change to SolidJS useNavigate
nlynzaad 452f876
ci: apply automated fixes
autofix-ci[bot] 41481f7
Merge branch 'main' into relative-routing-to-current-location
nlynzaad ab6f27b
rersolve inconsistency between solid and react implementation
nlynzaad 3d93911
ci: apply automated fixes
autofix-ci[bot] 87d51b9
formatting
nlynzaad 29dfcf9
refactor based on coderabbit recommendations
nlynzaad 9c46060
ci: apply automated fixes
autofix-ci[bot] 3e66462
resolve test failure
nlynzaad 508ee37
Merge branch 'main' into relative-routing-to-current-location
nlynzaad 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
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
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,57 @@ | ||
import { last } from '@tanstack/router-core' | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { useRouter } from './useRouter' | ||
import { useMatch } from './useMatch' | ||
import { useRouterState } from './useRouterState' | ||
import type { ParsedLocation } from '@tanstack/router-core' | ||
|
||
export type UseActiveLocationResult = { | ||
activeLocation: ParsedLocation | ||
getFromPath: (from?: string) => string | ||
setActiveLocation: (location?: ParsedLocation) => void | ||
} | ||
|
||
export const useActiveLocation = ( | ||
location?: ParsedLocation, | ||
): UseActiveLocationResult => { | ||
const router = useRouter() | ||
const routerLocation = useRouterState({ select: (state) => state.location }) | ||
const [activeLocation, setActiveLocation] = useState<ParsedLocation>( | ||
location ?? routerLocation, | ||
) | ||
const [customActiveLocation, setCustomActiveLocation] = useState< | ||
ParsedLocation | undefined | ||
>(location) | ||
|
||
useEffect(() => { | ||
setActiveLocation(customActiveLocation ?? routerLocation) | ||
}, [routerLocation, customActiveLocation]) | ||
|
||
const matchIndex = useMatch({ | ||
strict: false, | ||
select: (match) => match.index, | ||
}) | ||
nlynzaad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const getFromPath = useCallback( | ||
(from?: string) => { | ||
const activeLocationMatches = router.matchRoutes(activeLocation, { | ||
_buildLocation: false, | ||
}) | ||
|
||
const activeLocationMatch = last(activeLocationMatches) | ||
|
||
return ( | ||
from ?? | ||
activeLocationMatch?.fullPath ?? | ||
router.state.matches[matchIndex]!.fullPath | ||
) | ||
}, | ||
[activeLocation, matchIndex, router], | ||
) | ||
nlynzaad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
activeLocation, | ||
getFromPath, | ||
setActiveLocation: setCustomActiveLocation, | ||
} | ||
} |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.