Skip to content

Commit 8a872b1

Browse files
committed
prepare for determining the starting file path on the server
1 parent b72463c commit 8a872b1

3 files changed

Lines changed: 50 additions & 19 deletions

File tree

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ const fileQueryOptions = (repo: string, branch: string, filePath: string) => {
4444
const repoDirApiContentsQueryOptions = (
4545
repo: string,
4646
branch: string,
47-
startingPath: string
47+
startingPath: string,
48+
candidateStartingFilePath: string
4849
) =>
4950
queryOptions({
5051
queryKey: ['repo-api-contents', repo, branch, startingPath],
5152
queryFn: () =>
52-
fetchRepoDirectoryContents({ data: { repo, branch, startingPath } }),
53+
fetchRepoDirectoryContents({
54+
data: { repo, branch, startingPath, candidateStartingFilePath },
55+
}),
5356
})
5457

5558
export const Route = createFileRoute(
@@ -85,38 +88,40 @@ export const Route = createFileRoute(
8588
)
8689

8790
// Used for fetching the file content of the initial file
88-
const sandboxStartingFilePath = `examples/${examplePath}/${sandboxFirstFileName}`
91+
const explorerStartingFilePath = `examples/${examplePath}/${sandboxFirstFileName}`
8992

9093
// Used to indicate from where should the directory tree start.
9194
// i.e. from `examples/react/quickstart` or `examples/react/quickstart/src`
92-
const directoryStartingPath = `examples/${examplePath}${sandboxFirstDirectory}`
95+
const explorerDirectoryStartingPath = `examples/${examplePath}${sandboxFirstDirectory}`
9396

9497
await Promise.allSettled([
9598
queryClient.ensureQueryData(
96-
fileQueryOptions(library.repo, branch, sandboxStartingFilePath)
99+
fileQueryOptions(library.repo, branch, explorerStartingFilePath)
97100
),
98101
queryClient.ensureQueryData(
99102
repoDirApiContentsQueryOptions(
100103
library.repo,
101104
branch,
102-
directoryStartingPath
105+
explorerDirectoryStartingPath,
106+
explorerStartingFilePath
103107
)
104108
),
105109
])
106110

107111
return {
108-
directoryStartingPath,
109-
sandboxStartingFilePath,
112+
explorerDirectoryStartingPath,
113+
explorerStartingFilePath,
110114
}
111115
},
116+
staleTime: 1000 * 60 * 5, // 5 minutes
112117
})
113118

114119
const bannedDefaultOpenFolders = new Set(['public', '.vscode', 'tests', 'spec'])
115120

116121
function RouteComponent() {
117122
// Not sure why this inferred type is not working
118123
// @ts-expect-error
119-
const { directoryStartingPath, sandboxStartingFilePath } =
124+
const { explorerDirectoryStartingPath, explorerStartingFilePath } =
120125
Route.useLoaderData()
121126

122127
const navigate = Route.useNavigate()
@@ -132,8 +137,15 @@ function RouteComponent() {
132137
libraryId
133138
)
134139

135-
const { data: gitHubFiles } = useSuspenseQuery(
136-
repoDirApiContentsQueryOptions(library.repo, branch, directoryStartingPath)
140+
const {
141+
data: { githubContents, startingFilePath },
142+
} = useSuspenseQuery(
143+
repoDirApiContentsQueryOptions(
144+
library.repo,
145+
branch,
146+
explorerDirectoryStartingPath,
147+
explorerStartingFilePath
148+
)
137149
)
138150

139151
const [isDark, setIsDark] = React.useState(true)
@@ -160,7 +172,9 @@ function RouteComponent() {
160172
}
161173

162174
const currentPath = Route.useSearch({
163-
select: (s) => s.path || sandboxStartingFilePath,
175+
select: (s) => {
176+
return s.path || startingFilePath || explorerStartingFilePath
177+
},
164178
})
165179

166180
const setCurrentPath = (path: string) => {
@@ -194,8 +208,8 @@ function RouteComponent() {
194208
const [expandedFolders, setExpandedFolders] = React.useState<Set<string>>(
195209
() => {
196210
const expanded = new Set<string>()
197-
if (gitHubFiles) {
198-
gitHubFiles.forEach((file: GitHubFileNode) => {
211+
if (githubContents) {
212+
githubContents.forEach((file: GitHubFileNode) => {
199213
if (
200214
file.type === 'dir' &&
201215
file.depth === 0 &&
@@ -457,10 +471,10 @@ function RouteComponent() {
457471
isResizing ? '' : 'transition-all duration-300'
458472
} ${isSidebarOpen ? '' : 'w-0 pr-0'}`}
459473
>
460-
{gitHubFiles && isSidebarOpen ? (
474+
{githubContents && isSidebarOpen ? (
461475
<div className="p-2">
462476
<RenderFileTree
463-
files={gitHubFiles}
477+
files={githubContents}
464478
libraryColor={libraryColor}
465479
toggleFolder={toggleFolder}
466480
prefetchFileContent={prefetchFileContent}

app/utils/docs.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,17 @@ export const fetchRepoDirectoryContents = createServerFn({
111111
repo: z.string(),
112112
branch: z.string(),
113113
startingPath: z.string(),
114+
candidateStartingFilePath: z.string(),
114115
})
115116
)
116-
.handler(async ({ data: { repo, branch, startingPath } }) => {
117-
return fetchApiContents(repo, branch, startingPath)
118-
})
117+
.handler(
118+
async ({
119+
data: { repo, branch, startingPath, candidateStartingFilePath },
120+
}) => {
121+
const githubContents = await fetchApiContents(repo, branch, startingPath)
122+
123+
const startingFilePath: string | null = candidateStartingFilePath
124+
125+
return { githubContents, startingFilePath }
126+
}
127+
)

app/utils/documents.server.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs'
12
import fsp from 'node:fs/promises'
23
import path from 'node:path'
34
// import { fileURLToPath } from 'node:url'
@@ -47,6 +48,13 @@ async function fetchFs(repo: string, filepath: string) {
4748
// const __dirname = fileURLToPath(new URL('.', import.meta.url))
4849
const dirname = import.meta.url.split('://').at(-1)!
4950
const localFilePath = path.resolve(dirname, `../../../../${repo}`, filepath)
51+
const exists = fs.existsSync(localFilePath)
52+
if (!exists) {
53+
console.warn(
54+
`[fetchFs] Tried to read file that does not exist: ${localFilePath}\n`
55+
)
56+
return ''
57+
}
5058
const file = await fsp.readFile(localFilePath)
5159
return file.toString()
5260
}

0 commit comments

Comments
 (0)