Skip to content

fix: load selected path on code explorer #461

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 2 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ export function CodeBlock({

const [codeElement, setCodeElement] = React.useState(
<>
<pre ref={ref} className={`shiki github-light`}>
<pre ref={ref} className={`shiki github-light h-full`}>
<code>{code}</code>
</pre>
<pre className={`shiki tokyo-night bg-gray-900 text-gray-400`}>
<pre className={`shiki tokyo-night h-full bg-gray-900 text-gray-400`}>
<code>{code}</code>
</pre>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,17 @@ const repoDirApiContentsQueryOptions = (
})

export const Route = createFileRoute({
head: ({ params }) => {
const library = getLibrary(params.libraryId)

return {
meta: seo({
title: `${capitalize(params.framework)} ${library.name} ${slugToTitle(
params._splat || ''
)} Example | ${library.name} Docs`,
description: `An example showing how to implement ${slugToTitle(
params._splat || ''
)} in ${capitalize(params.framework)} using ${library.name}.`,
}),
}
},
component: RouteComponent,
validateSearch: z.object({
path: z.string().optional(),
panel: z.string().optional(),
}),
loader: async ({ params, context: { queryClient } }) => {
loaderDeps: ({ search }) => ({ path: search.path }),
loader: async ({ params, context: { queryClient }, deps: { path } }) => {
const library = getLibrary(params.libraryId)
const branch = getBranch(library, params.version)
const examplePath = [params.framework, params._splat].join('/')

// Used to determine the starting file name for the explorer
// i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
// This value is not absolutely guaranteed to be available, so further resolution may be necessary
const explorerCandidateStartingFileName = getExampleStartingPath(
params.framework as Framework,
params.libraryId
)

// Used to tell the github contents api where to start looking for files in the target repository
const repoStartingDirPath = `examples/${examplePath}`

Expand All @@ -85,9 +64,17 @@ export const Route = createFileRoute({
repoDirApiContentsQueryOptions(library.repo, branch, repoStartingDirPath)
)

// Used to determine the starting file name for the explorer
// It's either the selected path in the search params or a default we can derive
// i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
// This value is not absolutely guaranteed to be available, so further resolution may be necessary
const explorerCandidateStartingFileName =
path ||
getExampleStartingPath(params.framework as Framework, params.libraryId)

// Using the fetched contents, get the actual starting file-path for the explorer
// The `explorerCandidateStartingFileName` is used for matching, but the actual file-path may differ
const repoStartingFilePath = determineStartingFilePath(
const currentPath = determineStartingFilePath(
githubContents,
explorerCandidateStartingFileName,
params.framework as Framework,
Expand All @@ -97,12 +84,23 @@ export const Route = createFileRoute({
// Now that we've resolved the starting file path, we can
// fetching and caching the file content for the starting file path
await queryClient.ensureQueryData(
fileQueryOptions(library.repo, branch, repoStartingFilePath)
fileQueryOptions(library.repo, branch, currentPath)
)

return { repoStartingDirPath, currentPath }
},
head: ({ params }) => {
const library = getLibrary(params.libraryId)

return {
repoStartingDirPath,
repoStartingFilePath,
meta: seo({
title: `${capitalize(params.framework)} ${library.name} ${slugToTitle(
params._splat || ''
)} Example | ${library.name} Docs`,
description: `An example showing how to implement ${slugToTitle(
params._splat || ''
)} in ${capitalize(params.framework)} using ${library.name}.`,
}),
}
},
staleTime: 1000 * 60 * 5, // 5 minutes
Expand All @@ -114,9 +112,7 @@ function RouteComponent() {
}

function PageComponent() {
// Not sure why this inferred type is not working
// @ts-expect-error
const { repoStartingDirPath, repoStartingFilePath } = Route.useLoaderData()
const { repoStartingDirPath, currentPath } = Route.useLoaderData()

const navigate = Route.useNavigate()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -158,12 +154,6 @@ function PageComponent() {
})
}

const currentPath = Route.useSearch({
select: (s) => {
return s.path || repoStartingFilePath
},
})

const setCurrentPath = (path: string) => {
navigate({
search: { path, panel: undefined },
Expand Down
Loading