Skip to content
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

feat(loading): show dependency download progress #1146

Merged
merged 2 commits into from
Jul 8, 2024
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
16 changes: 16 additions & 0 deletions sandpack-client/src/clients/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ export type SandpackRuntimeMessage = BaseSandpackMessage &
| SandboxTestMessage
| { type: "sign-in"; teamId: string }
| { type: "sign-out" }
| {
type: "dependencies";
data:
| {
state: "downloading_manifest";
}
| {
state: "downloaded_module";
name: string;
total: number;
progress: number;
}
| {
state: "starting";
};
}
);
32 changes: 11 additions & 21 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,20 @@ export const Basic: React.FC = () => {
return (
<div style={{ height: "400vh" }}>
<Sandpack
files={{
"/App.js": `export default function TodoList() {
return (
// This doesn't quite work!
<h1>Hedy Lamarr's Todos</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
class="photo"
>
<ul>
<li>Invent new traffic lights
<li>Rehearse a movie scene
<li>Improve spectrum technology
</ul>
);
}
`,
}}
options={{
initMode: "user-visible",
bundlerURL: "https://786946de.sandpack-bundler-4bw.pages.dev",
bundlerURL: "https://ymxnqs-3000.csb.app",
}}
template="react"
customSetup={{
dependencies: {
"react-content-loader": "latest",
"radix-ui": "latest",
"styled-components": "latest",
"react-dom": "latest",
react: "latest",
"react-table": "latest",
},
}}
/>
</div>
);
Expand Down
17 changes: 16 additions & 1 deletion sandpack-react/src/hooks/useSandpackPreviewProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,22 @@ export const useSandpackPreviewProgress = (
}, timeout);
}

if (message.type === "shell/progress" && !isReady) {
if (message.type === "dependencies") {
setLoadingMessage(() => {
switch (message.data.state) {
case "downloading_manifest":
return "[1/3] Downloading manifest";

case "downloaded_module":
return `[2/3] Downloaded ${message.data.name} (${message.data.progress}/${message.data.total})`;

case "starting":
return "[3/3] Starting";
}

return null;
});
} else if (message.type === "shell/progress" && !isReady) {
if (!totalDependencies && message.data.state === "downloaded_module") {
setTotalDependencies(message.data.totalPending);
}
Expand Down
Loading