Improve invoices app loading strategy (#253)

* Invoices: Remove initial loading spinner and postpone NotifyReady event

* Remove graphql provider

* Remove unused Loader component
This commit is contained in:
Lukasz Ostrowski 2023-03-09 13:06:45 +01:00 committed by GitHub
parent f8de90b56b
commit 639dfc33ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 65 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-app-invoices": minor
---
App no longer shows initial loading spinner. It renders nothing until initial required data (channels) are fetched. When this happens, AppBridge informs Dashboard (via NotifyReady action) that it can be displayed.

View file

@ -0,0 +1,5 @@
---
"saleor-app-invoices": patch
---
Removed frontend GraphQL provider, because no frontend queries are used

View file

@ -7,7 +7,6 @@ import { AddressForm } from "./address-form";
import { ChannelsList } from "./channels-list";
import { actions, useAppBridge } from "@saleor/app-sdk/app-bridge";
import { AppColumnsLayout } from "../../ui/app-columns-layout";
import { Loader } from "../../ui/loader";
const useStyles = makeStyles((theme) => {
return {
@ -60,7 +59,7 @@ export const ChannelsConfiguration = () => {
}, [channels.data, activeChannelSlug]);
if (channels.isLoading || !channels.data) {
return <Loader />;
return null;
}
if (!activeChannel) {

View file

@ -1,21 +0,0 @@
import { CircularProgress } from "@material-ui/core";
import { makeStyles } from "@saleor/macaw-ui";
const useStyles = makeStyles({
root: {
display: "flex",
alignItems: "center",
justifyContent: "center",
height: 300,
},
});
export const Loader = () => {
const styles = useStyles();
return (
<div className={styles.root}>
<CircularProgress color="primary" />
</div>
);
};

View file

@ -4,8 +4,6 @@ import { AppBridge, AppBridgeProvider } from "@saleor/app-sdk/app-bridge";
import { RoutePropagator } from "@saleor/app-sdk/app-bridge/next";
import React, { useEffect } from "react";
import { AppProps } from "next/app";
import GraphQLProvider from "../providers/GraphQLProvider";
import { ThemeSynchronizer } from "../lib/theme-synchronizer";
import { NoSSRWrapper } from "../lib/no-ssr-wrapper";
import { trpcClient } from "../modules/trpc/trpc-client";
@ -15,7 +13,8 @@ import { MacawThemeProvider } from "@saleor/apps-shared";
* Ensure instance is a singleton.
* TODO: This is React 18 issue, consider hiding this workaround inside app-sdk
*/
export const appBridgeInstance = typeof window !== "undefined" ? new AppBridge() : undefined;
export const appBridgeInstance =
typeof window !== "undefined" ? new AppBridge({ autoNotifyReady: false }) : undefined;
function NextApp({ Component, pageProps }: AppProps) {
/**
@ -31,13 +30,11 @@ function NextApp({ Component, pageProps }: AppProps) {
return (
<NoSSRWrapper>
<AppBridgeProvider appBridgeInstance={appBridgeInstance}>
<GraphQLProvider>
<MacawThemeProvider>
<ThemeSynchronizer />
<RoutePropagator />
<Component {...pageProps} />
</MacawThemeProvider>
</GraphQLProvider>
</AppBridgeProvider>
</NoSSRWrapper>
);

View file

@ -9,16 +9,15 @@ const ConfigurationPage: NextPage = () => {
const channels = trpcClient.channels.fetch.useQuery();
const router = useRouter();
const { appBridge } = useAppBridge();
const { appBridge, appBridgeState } = useAppBridge();
const openInNewTab = (url: string) => {
appBridge?.dispatch(
actions.Redirect({
to: url,
newContext: true,
})
);
};
useEffect(() => {
if (channels.isFetched && appBridge && !appBridgeState?.ready) {
if (appBridge && channels.isFetched) {
appBridge.dispatch(actions.NotifyReady());
}
}
}, [channels.isFetched, appBridge, appBridgeState?.ready]);
useEffect(() => {
if (channels.isSuccess && channels.data.length === 0) {

View file

@ -4,7 +4,6 @@ import { useEffect } from "react";
import { useIsMounted } from "usehooks-ts";
import { useRouter } from "next/router";
import { isInIframe } from "@saleor/apps-shared";
import { Loader } from "../modules/ui/loader";
const IndexPage: NextPage = () => {
const { appBridgeState } = useAppBridge();
@ -18,7 +17,7 @@ const IndexPage: NextPage = () => {
}, [isMounted, appBridgeState?.ready]);
if (isInIframe()) {
return <Loader />;
return null;
}
return (

View file

@ -1,21 +0,0 @@
import { useAppBridge } from "@saleor/app-sdk/app-bridge";
import { PropsWithChildren } from "react";
import { Provider } from "urql";
import { createClient } from "../lib/graphql";
function GraphQLProvider(props: PropsWithChildren<{}>) {
const { appBridgeState } = useAppBridge();
if (!appBridgeState?.saleorApiUrl) {
return <div {...props}></div>;
}
const client = createClient(appBridgeState?.saleorApiUrl, async () =>
Promise.resolve({ token: appBridgeState?.token! })
);
return <Provider value={client} {...props} />;
}
export default GraphQLProvider;