Skip to content

Commit

Permalink
feat(loading): show dependency download progress (#1146)
Browse files Browse the repository at this point in the history
* feat(loading): show dependency download progress

* fix
  • Loading branch information
danilowoz committed Jul 8, 2024
1 parent 78c9242 commit a811267
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
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

0 comments on commit a811267

Please sign in to comment.