-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3454 from quantified-uncertainty/hub-optimizations
Hub RSC rewrite
- Loading branch information
Showing
389 changed files
with
8,030 additions
and
17,188 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 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
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,84 +1,36 @@ | ||
import axios from "axios"; | ||
import { z } from "zod"; | ||
|
||
export const librariesToImport = ["ozziegooen/sTest", "ozziegooen/helpers"]; | ||
const SERVER = "https://squigglehub.org"; | ||
|
||
const GRAPHQL_URL = "https://squigglehub.org/api/graphql"; | ||
|
||
export function getQuery(owner: string, slug: string) { | ||
return ` | ||
query GetModelCode { | ||
model(input: {owner: "${owner}", slug: "${slug}"}) { | ||
... on Model { | ||
id | ||
currentRevision { | ||
content { | ||
... on SquiggleSnippet { | ||
id | ||
code | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
} | ||
|
||
export async function fetchCodeFromGraphQL( | ||
export async function fetchCodeFromHub( | ||
owner: string, | ||
slug: string | ||
): Promise<string> { | ||
const query = getQuery(owner, slug); | ||
const response = await axios.post(GRAPHQL_URL, { query }); | ||
return response.data.data.model.currentRevision.content.code; | ||
} | ||
const data = await fetch( | ||
`${SERVER}/api/get-source?${new URLSearchParams({ | ||
owner, | ||
slug, | ||
})}` | ||
).then((res) => res.json()); | ||
const parsed = z.object({ code: z.string() }).safeParse(data); | ||
if (!parsed.success) { | ||
throw new Error(`Failed to fetch source for ${owner}/${slug}`); | ||
} | ||
|
||
export function getGroupModelsQuery(groupSlug: string) { | ||
return { | ||
query: ` | ||
query GetGroupModels($groupSlug: String!) { | ||
group(slug: $groupSlug) { | ||
... on Group { | ||
id | ||
models(first: 50) { | ||
edges { | ||
node { | ||
currentRevision { | ||
content { | ||
... on SquiggleSnippet { | ||
code | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
variables: { groupSlug }, | ||
}; | ||
return parsed.data.code; | ||
} | ||
|
||
export async function fetchGroupModels(groupSlug: string): Promise<string[]> { | ||
try { | ||
const query = getGroupModelsQuery(groupSlug); | ||
const response = await axios.post(GRAPHQL_URL, query); | ||
|
||
if (response.status === 200) { | ||
const models = response.data.data.group.models.edges; | ||
console.log(`Fetched ${models.length} models from group ${groupSlug}`); | ||
|
||
return models.map( | ||
(model: { node: { currentRevision: { content: { code: string } } } }) => | ||
model.node.currentRevision.content.code | ||
); | ||
} else { | ||
throw new Error(`Error fetching group models: ${response.statusText}`); | ||
} | ||
} catch (error) { | ||
console.error("Error fetching group models:", error); | ||
throw error; | ||
const data = await fetch( | ||
`${SERVER}/api/get-group-models?${new URLSearchParams({ slug: groupSlug })}` | ||
).then((res) => res.json()); | ||
|
||
const parsed = z | ||
.object({ models: z.array(z.object({ slug: z.string() })) }) | ||
.safeParse(data); | ||
if (!parsed.success) { | ||
throw new Error(`Failed to fetch group models for ${groupSlug}`); | ||
} | ||
|
||
return parsed.data.models.map((item) => item.slug); | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
.vscode | ||
.next | ||
src/__generated__ | ||
schema.graphql | ||
test/gql-gen |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Notes on Next.js | ||
|
||
## Conventions | ||
|
||
### Component files | ||
|
||
- store single use components in `src/app`, next to their `page.tsx` and `layout.tsx` | ||
- if the component is shared between multiple pages, store it in `src/{topic}/components/`, where `{topic}` is something like "models" or "relative-values" | ||
- if the component doesn't have an obvious topic, e.g. if it's a generic UI component, store it in `src/components/` | ||
|
||
### Actions | ||
|
||
- store actions in `src/{topic}/actions/`, where `{topic}` is something like "models" or "relative-values" | ||
- name actions like this: `doSomethingAction` | ||
- use `next-safe-action` to define all actions | ||
- return _something_ from actions, even if it's just `"ok"`; some wrappers check whether `data` on the action is defined | ||
|
||
### Data loading | ||
|
||
- all data loading functions that expose data to the frontend should go in `src/{topic}/data/` | ||
- data loading functions should sanitize the data that they select from the database, to avoid security issues | ||
|
||
## Loading pages | ||
|
||
Avoid generic `loading.tsx` files. Thoughtful loading states are good, but the generic top-level loading state was harmful: | ||
|
||
- it doesn't match the final rendered state so it looks more like a flash of unrelated content than a skeleton | ||
- loading state means that the previous page will disappear faster than necessary, and that's bad; in other words, the loading state is only useful when it hints where the content will appear | ||
- in addition, the loading state means that `<Link>` prefetching won't work | ||
|
||
Avoid nested `loading.tsx` files. I'm not sure why but they might cause double flash of loading states, similar to this thread: https://www.reddit.com/r/nextjs/comments/17hn1a5/nested_loading_states/ |
Oops, something went wrong.