Skip to content

Commit

Permalink
fix(WebAppProvider): loading script
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzwan committed Nov 13, 2023
1 parent 9eacc63 commit ae5f823
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@altiore/twa",
"version": "1.2.8",
"version": "1.2.9",
"description": "React components for Telegram WebApp",
"source": "./src/index.ts",
"type": "module",
Expand Down
14 changes: 11 additions & 3 deletions src/WebAppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const WebAppProvider = ({
return () => window.removeEventListener('beforeunload', forceHideButtons);
}, [options?.smoothButtonsTransition, webApp]);

const [isLoading, setIsLoading] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);

const subscribeScriptLoading = useCallback(
Expand All @@ -94,11 +94,17 @@ const WebAppProvider = ({
};

scriptEle.addEventListener('load', successListener);

scriptEle.addEventListener('error', errorListener);

return () => {
scriptEle.removeEventListener('load', successListener);
scriptEle.removeEventListener('error', errorListener);
};
} catch (err) {
console.error(err);
}

return () => {};
},
[setIsLoaded, setIsLoading, setWebApp],
);
Expand All @@ -110,7 +116,7 @@ const WebAppProvider = ({
}
setIsLoading(true);
const existingScripts: NodeListOf<HTMLScriptElement> =
window.document.querySelectorAll(`script[src~='${SCRIPT}']`);
window.document.querySelectorAll(`script[src='${SCRIPT}']`);
if (existingScripts[0]) {
return subscribeScriptLoading(existingScripts[0]);
}
Expand All @@ -129,6 +135,8 @@ const WebAppProvider = ({
setIsLoaded(false);
setIsLoading(false);
}

return () => {};
}, [subscribeScriptLoading, isLoading, isLoaded]);

const systemValue = useMemo(createSystemContextValue, []);
Expand Down

0 comments on commit ae5f823

Please sign in to comment.