-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: useCurrentApp not working reliably and bake names into parent route
Hopefully the root files for the apps can be moved to the actual files soonish. Am discussing with tanstack-router people
- Loading branch information
Showing
13 changed files
with
50 additions
and
41 deletions.
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 was deleted.
Oops, something went wrong.
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,29 +1,16 @@ | ||
import { useState, useEffect } from "react"; | ||
import { useMatchRoute, useRouterState } from "@tanstack/react-router"; | ||
import { Apps } from "@/types/app.ts"; | ||
import { appRoutes } from "@/types/common.ts"; | ||
import { appNavMapping } from "@/config/nav.ts"; | ||
import { useMatches } from "@tanstack/react-router"; | ||
|
||
const useCurrentApp = (): Apps | undefined => { | ||
const [currentApp, setCurrentApp] = useState<Apps | undefined>(undefined); | ||
const matchRoute = useMatchRoute(); | ||
const matchWithTitle = useMatches() | ||
.reverse() | ||
.find((d) => d.staticData.title); | ||
|
||
const routerChanged = useRouterState().resolvedLocation.pathname; | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: Needed to include router state or else this doesn't update quick enough (side effect is it updates too much)> | ||
useEffect(() => { | ||
for (const [routeSegment, app] of Object.entries(appNavMapping)) { | ||
const route = routeSegment as appRoutes; | ||
const match = matchRoute({ to: `/${route}`, fuzzy: true }); | ||
if (match) { | ||
console.log("updating"); | ||
setCurrentApp(app); | ||
return; // Stop the loop once a match is found | ||
} | ||
} | ||
}, [matchRoute, routerChanged]); | ||
|
||
return currentApp; | ||
const title = matchWithTitle?.staticData.title; | ||
if (!title) { | ||
throw new Error("Page has no title. This should have been set in the root directory in a file with the app's name"); | ||
} | ||
return title; | ||
}; | ||
|
||
export default useCurrentApp; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/admin")({ | ||
staticData: { title: "Admin" }, | ||
}); |
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,5 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/auth")({ | ||
staticData: { title: "Auth" }, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/printing")({ | ||
staticData: { title: "Printing" }, | ||
}); |
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,5 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/signin")({ | ||
staticData: { title: "Sign In" }, | ||
}); |
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,3 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/socials")({ staticData: { title: "Socials" } }); |
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,5 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/training")({ | ||
staticData: { title: "Training" }, | ||
}); |
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,5 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
|
||
export const Route = createFileRoute("/user")({ | ||
component: () => ({ title: "User" }), | ||
}); |
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 +1 @@ | ||
export type Apps = "Main" | "Sign In" | "Printing" | "User" | "Admin" | "Training" | "Auth"; | ||
export type Apps = "Main" | "Sign In" | "Printing" | "User" | "Admin" | "Training" | "Auth" | "Socials"; |