Skip to content

Commit 54d3370

Browse files
committed
fix: enhance example loading with fallback handling and availability check
1 parent a6229f4 commit 54d3370

1 file changed

Lines changed: 56 additions & 31 deletions

File tree

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

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { isNotFound, notFound, createFileRoute } from '@tanstack/react-router'
2-
import {
3-
queryOptions,
4-
useQuery,
5-
useQueryClient,
6-
useSuspenseQuery,
7-
} from '@tanstack/react-query'
2+
import { queryOptions, useQuery, useQueryClient } from '@tanstack/react-query'
83
import React from 'react'
94
import { DocTitle } from '~/components/DocTitle'
105
import { Framework, getBranch, getLibrary } from '~/libraries'
@@ -61,6 +56,9 @@ export const Route = createFileRoute(
6156
const library = getLibrary(params.libraryId)
6257
const branch = getBranch(library, params.version)
6358
const examplePath = [params.framework, params._splat].join('/')
59+
const fallbackPath =
60+
path ||
61+
getExampleStartingPath(params.framework as Framework, params.libraryId)
6462

6563
// Used to tell the github contents api where to start looking for files in the target repository
6664
const repoStartingDirPath = `examples/${examplePath}`
@@ -79,9 +77,7 @@ export const Route = createFileRoute(
7977
// It's either the selected path in the search params or a default we can derive
8078
// i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
8179
// This value is not absolutely guaranteed to be available, so further resolution may be necessary
82-
const explorerCandidateStartingFileName =
83-
path ||
84-
getExampleStartingPath(params.framework as Framework, params.libraryId)
80+
const explorerCandidateStartingFileName = fallbackPath
8581

8682
// Using the fetched contents, get the actual starting file-path for the explorer
8783
// The `explorerCandidateStartingFileName` is used for matching, but the actual file-path may differ
@@ -98,15 +94,29 @@ export const Route = createFileRoute(
9894
fileQueryOptions(library.repo, branch, currentPath),
9995
)
10096

101-
return { repoStartingDirPath, currentPath }
97+
return {
98+
repoStartingDirPath,
99+
currentPath,
100+
githubContentsAvailable: true,
101+
}
102102
} catch (error) {
103103
const isNotFoundError =
104104
isNotFound(error) ||
105105
(error && typeof error === 'object' && 'isNotFound' in error)
106106
if (isNotFoundError) {
107107
throw notFound()
108108
}
109-
throw error
109+
110+
console.warn(
111+
`Failed to fetch example contents for ${library.repo}@${branch}:${repoStartingDirPath}`,
112+
error,
113+
)
114+
115+
return {
116+
repoStartingDirPath,
117+
currentPath: fallbackPath,
118+
githubContentsAvailable: false,
119+
}
110120
}
111121
},
112122
head: ({ params }) => {
@@ -159,7 +169,8 @@ function RouteComponent() {
159169
}
160170

161171
function PageComponent() {
162-
const { repoStartingDirPath, currentPath } = Route.useLoaderData()
172+
const { repoStartingDirPath, currentPath, githubContentsAvailable } =
173+
Route.useLoaderData()
163174

164175
const navigate = Route.useNavigate()
165176
const queryClient = useQueryClient()
@@ -174,9 +185,14 @@ function PageComponent() {
174185
libraryId,
175186
)
176187

177-
const { data: githubContents } = useSuspenseQuery(
178-
repoDirApiContentsQueryOptions(library.repo, branch, repoStartingDirPath),
179-
)
188+
const { data: githubContents } = useQuery({
189+
...repoDirApiContentsQueryOptions(
190+
library.repo,
191+
branch,
192+
repoStartingDirPath,
193+
),
194+
enabled: githubContentsAvailable,
195+
})
180196

181197
const [isDark, setIsDark] = React.useState(true)
182198
const [deployDialogOpen, setDeployDialogOpen] = React.useState(false)
@@ -222,9 +238,10 @@ function PageComponent() {
222238
})
223239
}
224240

225-
const { data: currentCode } = useQuery(
226-
fileQueryOptions(library.repo, branch, currentPath),
227-
)
241+
const { data: currentCode } = useQuery({
242+
...fileQueryOptions(library.repo, branch, currentPath),
243+
enabled: githubContentsAvailable,
244+
})
228245

229246
const prefetchFileContent = React.useCallback(
230247
(path: string) => {
@@ -337,19 +354,27 @@ function PageComponent() {
337354
</DocTitle>
338355
</div>
339356
<div className="flex-1 lg:px-6 flex flex-col min-h-0">
340-
<CodeExplorer
341-
activeTab={activeTab as 'code' | 'sandbox'}
342-
codeSandboxUrl={codeSandboxUrl}
343-
currentCode={currentCode || ''}
344-
currentPath={currentPath}
345-
examplePath={examplePath}
346-
githubContents={githubContents || undefined}
347-
library={library}
348-
prefetchFileContent={prefetchFileContent}
349-
setActiveTab={setActiveTab}
350-
setCurrentPath={setCurrentPath}
351-
stackBlitzUrl={stackBlitzUrl}
352-
/>
357+
{githubContentsAvailable && githubContents ? (
358+
<CodeExplorer
359+
activeTab={activeTab as 'code' | 'sandbox'}
360+
codeSandboxUrl={codeSandboxUrl}
361+
currentCode={currentCode || ''}
362+
currentPath={currentPath}
363+
examplePath={examplePath}
364+
githubContents={githubContents}
365+
library={library}
366+
prefetchFileContent={prefetchFileContent}
367+
setActiveTab={setActiveTab}
368+
setCurrentPath={setCurrentPath}
369+
stackBlitzUrl={stackBlitzUrl}
370+
/>
371+
) : (
372+
<div className="flex-1 rounded-xl border border-border/60 bg-background/70 p-6 text-sm text-muted-foreground">
373+
The example source browser is temporarily unavailable. You can still
374+
open this example on GitHub, StackBlitz, or CodeSandbox using the
375+
links above.
376+
</div>
377+
)}
353378
</div>
354379
{deployProvider && (
355380
<ExampleDeployDialog

0 commit comments

Comments
 (0)