Skip to content

Commit f564338

Browse files
authored
fix: load selected path in code explorer (#461)
1 parent 7d1d92f commit f564338

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed

src/components/Markdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ export function CodeBlock({
104104

105105
const [codeElement, setCodeElement] = React.useState(
106106
<>
107-
<pre ref={ref} className={`shiki github-light`}>
107+
<pre ref={ref} className={`shiki github-light h-full`}>
108108
<code>{code}</code>
109109
</pre>
110-
<pre className={`shiki tokyo-night bg-gray-900 text-gray-400`}>
110+
<pre className={`shiki tokyo-night h-full bg-gray-900 text-gray-400`}>
111111
<code>{code}</code>
112112
</pre>
113113
</>

src/routes/$libraryId/$version.docs.framework.$framework.examples.$.tsx

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,17 @@ const repoDirApiContentsQueryOptions = (
4545
})
4646

4747
export const Route = createFileRoute({
48-
head: ({ params }) => {
49-
const library = getLibrary(params.libraryId)
50-
51-
return {
52-
meta: seo({
53-
title: `${capitalize(params.framework)} ${library.name} ${slugToTitle(
54-
params._splat || ''
55-
)} Example | ${library.name} Docs`,
56-
description: `An example showing how to implement ${slugToTitle(
57-
params._splat || ''
58-
)} in ${capitalize(params.framework)} using ${library.name}.`,
59-
}),
60-
}
61-
},
6248
component: RouteComponent,
6349
validateSearch: z.object({
6450
path: z.string().optional(),
6551
panel: z.string().optional(),
6652
}),
67-
loader: async ({ params, context: { queryClient } }) => {
53+
loaderDeps: ({ search }) => ({ path: search.path }),
54+
loader: async ({ params, context: { queryClient }, deps: { path } }) => {
6855
const library = getLibrary(params.libraryId)
6956
const branch = getBranch(library, params.version)
7057
const examplePath = [params.framework, params._splat].join('/')
7158

72-
// Used to determine the starting file name for the explorer
73-
// i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
74-
// This value is not absolutely guaranteed to be available, so further resolution may be necessary
75-
const explorerCandidateStartingFileName = getExampleStartingPath(
76-
params.framework as Framework,
77-
params.libraryId
78-
)
79-
8059
// Used to tell the github contents api where to start looking for files in the target repository
8160
const repoStartingDirPath = `examples/${examplePath}`
8261

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

67+
// Used to determine the starting file name for the explorer
68+
// It's either the selected path in the search params or a default we can derive
69+
// i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
70+
// This value is not absolutely guaranteed to be available, so further resolution may be necessary
71+
const explorerCandidateStartingFileName =
72+
path ||
73+
getExampleStartingPath(params.framework as Framework, params.libraryId)
74+
8875
// Using the fetched contents, get the actual starting file-path for the explorer
8976
// The `explorerCandidateStartingFileName` is used for matching, but the actual file-path may differ
90-
const repoStartingFilePath = determineStartingFilePath(
77+
const currentPath = determineStartingFilePath(
9178
githubContents,
9279
explorerCandidateStartingFileName,
9380
params.framework as Framework,
@@ -97,12 +84,23 @@ export const Route = createFileRoute({
9784
// Now that we've resolved the starting file path, we can
9885
// fetching and caching the file content for the starting file path
9986
await queryClient.ensureQueryData(
100-
fileQueryOptions(library.repo, branch, repoStartingFilePath)
87+
fileQueryOptions(library.repo, branch, currentPath)
10188
)
10289

90+
return { repoStartingDirPath, currentPath }
91+
},
92+
head: ({ params }) => {
93+
const library = getLibrary(params.libraryId)
94+
10395
return {
104-
repoStartingDirPath,
105-
repoStartingFilePath,
96+
meta: seo({
97+
title: `${capitalize(params.framework)} ${library.name} ${slugToTitle(
98+
params._splat || ''
99+
)} Example | ${library.name} Docs`,
100+
description: `An example showing how to implement ${slugToTitle(
101+
params._splat || ''
102+
)} in ${capitalize(params.framework)} using ${library.name}.`,
103+
}),
106104
}
107105
},
108106
staleTime: 1000 * 60 * 5, // 5 minutes
@@ -114,9 +112,7 @@ function RouteComponent() {
114112
}
115113

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

121117
const navigate = Route.useNavigate()
122118
const queryClient = useQueryClient()
@@ -158,12 +154,6 @@ function PageComponent() {
158154
})
159155
}
160156

161-
const currentPath = Route.useSearch({
162-
select: (s) => {
163-
return s.path || repoStartingFilePath
164-
},
165-
})
166-
167157
const setCurrentPath = (path: string) => {
168158
navigate({
169159
search: { path, panel: undefined },

0 commit comments

Comments
 (0)