Skip to content

Commit

Permalink
fix(useTwa): fix for correct usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzwan committed Nov 13, 2023
1 parent 713c783 commit ae3d7b4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ jobs:
# This expects you to have a script called release which does a build for your packages and calls changeset publish
publish: npm run release
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
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.0",
"version": "1.2.1",
"description": "React components for Telegram WebApp",
"source": "./src/index.ts",
"type": "module",
Expand Down
24 changes: 12 additions & 12 deletions src/TwaLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import useWebApp from './useWebApp';
import React, { FC, useContext, useEffect } from 'react';
import React, { FC, useEffect } from 'react';

import { systemContext } from './core';
import { useTwa } from './useTwa';
import { useVersionAtLeast } from './useVersionAtLeast';
import useWebApp from './useWebApp';

interface IProps {
loading?: React.JSX.Element;
isTWApp?: React.JSX.Element;
noTWApp?: React.JSX.Element;
minVersion?: string | number;
}

export const TwaLoader: FC<IProps> = ({
loading = null,
isTWApp = null,
noTWApp = null,
minVersion = null,
}): React.JSX.Element | null => {
useEffect(() => {
if (!loading && !isTWApp && !noTWApp) {
Expand All @@ -23,21 +26,18 @@ export const TwaLoader: FC<IProps> = ({
}
}, [loading, isTWApp, noTWApp]);

const system = useContext(systemContext);
const { isLoaded, isLoading } = useTwa();

const isCorrectVersion = useVersionAtLeast(minVersion ?? '6.2');

const webApp = useWebApp();

if (system.isTwaLoading) {
if (isLoading) {
return loading;
}

// Вызов инициализации SDK еще не происходил. Нам пока нечего показать.
if (system.isTwaLoaded) {
if (webApp) {
return isTWApp;
}

return loading;
if (isLoaded && webApp && (!minVersion || isCorrectVersion)) {
return isTWApp;
}

return noTWApp;
Expand Down
1 change: 1 addition & 0 deletions src/WebAppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const WebAppProvider = ({
setIsLoading(false);
});
} catch (err) {
console.error(err);
setIsLoaded(false);
setIsLoading(false);
}
Expand Down
9 changes: 3 additions & 6 deletions src/useTwa.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { systemContext } from './core';
import { useContext, useMemo } from 'react';
import { useContext } from 'react';

/**
* A hook that shows version minimum check
Expand All @@ -11,10 +11,7 @@ export type VersionAtLeastFunction = (version: string | number) => boolean;
* @group Hooks
*/
export const useTwa = (): { isLoading: boolean; isLoaded: boolean } => {
const system = useContext(systemContext);
const { isTwaLoaded, isTwaLoading } = useContext(systemContext);

const isLoading = useMemo(() => system.isTwaLoading, [system]);
const isLoaded = useMemo(() => system.isTwaLoaded, [system]);

return { isLoading, isLoaded };
return { isLoading: isTwaLoading, isLoaded: isTwaLoaded };
};

0 comments on commit ae3d7b4

Please sign in to comment.