Skip to content

Commit

Permalink
Better handling of the plugin loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
Jør∂¡ committed Feb 20, 2024
1 parent f4378fe commit 2a35009
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions pages/plugins/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,35 @@ const PluginLoader: FC = () => {
const { query, push } = useRouter();
const pluginId = resolveQueryParam(query.id);
const [PageComponent, setPageComponent] = useState<FC | null>(null);
const [showNotFoundError, setShowNotFoundError] = useState(false);
const [componentLoading, setComponentLoading] = useState(true);

useEffect(() => {
if (!pluginId || typeof pluginId !== "string") {
push("/");
return;
}
if (!pluginId) return;

const plugin = plugins.find((p) => p.id === pluginId);
if (!plugin) {
push("/");
return;
}
if (!plugin) return;

import(`@/plugins/${plugin.folderName}`)
.then((mod) => {
setShowNotFoundError(false);
setComponentLoading(true);
setPageComponent(() => mod.default);
})
.catch((err) => {
console.error("Failed to load the page component", err);

setShowNotFoundError(true);
setComponentLoading(false);
});
}, [pluginId]);

if (showNotFoundError) {
if (!PageComponent) {
if (componentLoading) {
return (
<div>
<PleaseWaitSpinner />
</div>
);
}
return <NotFound />;
} else if (!PageComponent) {
return (
<div>
<PleaseWaitSpinner />
</div>
);
}

return <PageComponent />;
Expand Down

0 comments on commit 2a35009

Please sign in to comment.