From d86ed522e5615e28636500200430bb835ed74da0 Mon Sep 17 00:00:00 2001 From: AnhMTV Date: Fri, 10 Jan 2025 11:53:35 +0700 Subject: [PATCH] [Issue-249] Update and fix log problems --- .../src/connector/booka/sdk.ts | 73 ++++++++++++------- .../contexts/AuthenticationMythProvider.tsx | 5 +- packages/webapp/webpack.config.cjs | 1 + 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/packages/extension-koni-ui/src/connector/booka/sdk.ts b/packages/extension-koni-ui/src/connector/booka/sdk.ts index 6e79d9fe9c..6edeaa73ca 100644 --- a/packages/extension-koni-ui/src/connector/booka/sdk.ts +++ b/packages/extension-koni-ui/src/connector/booka/sdk.ts @@ -18,6 +18,7 @@ import { BehaviorSubject } from 'rxjs'; import { stringToU8a, u8aToString } from '@polkadot/util'; export const DEFAULT_INIT_DATA = process.env.DEFAULT_INIT_DATA; +export const DEBUG_REPORT_URL = process.env.DEBUG_REPORT_URL || ''; export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api.anhmtv.xyz'; export const MYTHICAL_API_HOST = process.env.MYTHICAL_API_HOST || 'https://nflrivals.client.mythical.dev'; export const TELEGRAM_WEBAPP_LINK = process.env.TELEGRAM_WEBAPP_LINK || 'Playnation_bot/app'; @@ -91,16 +92,27 @@ export function getRewardStatus (status: RewardStatus) { const metadataHandler = MetadataHandler.instance; +interface DebugData { + id: string; + datas: { type: string, input: any }[]; + errors: { type: string, input: any }[]; +} + +// Todo: Create env to point or disable debugs const DebugLogHandler = { - debugUrl: 'https://mythical-debug.playnation.app/debug', + debugUrl: DEBUG_REPORT_URL, debugData: { id: '_none_', datas: [], errors: [] - }, + } as DebugData, debugLazy: undefined, initHandler: createPromiseHandler(), initDebugLog: async () => { + if (!DebugLogHandler.debugUrl) { + return; + } + const { datas, errors } = DebugLogHandler.debugData; const initLogData = { @@ -123,7 +135,7 @@ const DebugLogHandler = { if (rs.status > 400) { console.error('Failed to push debug log', rs); } else { - const rsData = await rs.json(); + const rsData = (await rs.json()) as DebugData; DebugLogHandler.debugData.id = rsData.id; } @@ -131,41 +143,47 @@ const DebugLogHandler = { DebugLogHandler.initHandler.resolve(); }, sendTimeout: undefined, - sendDebugLog: (type: string, input: any, isError?: boolean) => { + sendDebugLog: (type: string, input: unknown, isError?: boolean) => { + if (!DebugLogHandler.debugUrl) { + return; + } + DebugLogHandler.sendTimeout && clearTimeout(DebugLogHandler.sendTimeout); const debugUrl = DebugLogHandler.debugUrl; const { datas, errors } = DebugLogHandler.debugData; if (isError) { - // @ts-ignore errors.push({ - type, input + type, + input }); } else { - // @ts-ignore datas.push({ - type, input + type, + input }); } // @ts-ignore - DebugLogHandler.sendTimeout = setTimeout(async () => { - await DebugLogHandler.initHandler.promise; - const id = DebugLogHandler.debugData.id; - - const rs = await fetch(`${debugUrl}/${id}`, { - method: 'PUT', - body: JSON.stringify({ - datas, - errors - }) - }); + DebugLogHandler.sendTimeout = setTimeout(() => { + (async () => { + await DebugLogHandler.initHandler.promise; + const id = DebugLogHandler.debugData.id; + + const rs = await fetch(`${debugUrl}/${id}`, { + method: 'PUT', + body: JSON.stringify({ + datas, + errors + }) + }); - if (rs.status > 400) { - console.error('Failed to push debug log', rs); - } + if (rs.status > 400) { + console.error('Failed to push debug log', rs); + } - DebugLogHandler.sendTimeout = undefined; + DebugLogHandler.sendTimeout = undefined; + })().catch(console.error); }, 300); } }; @@ -212,7 +230,6 @@ export class BookaSdk { this.initMetadataHandling(); const version = localStorage.getItem('cache-version'); - // Todo #249: Try to reset cache if (cacheVersion === version) { const account = parseCache(CACHE_KEYS.account); const taskCategoryList = parseCache(CACHE_KEYS.taskCategoryList); @@ -373,7 +390,7 @@ export class BookaSdk { const data = await response.json() as T; - this.pushDebugLog(url, { request: body, response: '__OK__' }); + this.pushDebugLog(url, { request: body as string, response: '__OK__' }); return data; } @@ -568,6 +585,7 @@ export class BookaSdk { async fetchMythicalBalance (token?: string) { await this.waitForSync; + try { const rs = await this.postRequest(`${GAME_API_HOST}/api/mythical-account/fetch`, { token: token }); @@ -831,7 +849,7 @@ export class BookaSdk { errors: [] }; - pushDebugLog (type: string, input: any, isError?: boolean) { + pushDebugLog (type: string, input: unknown, isError?: boolean) { DebugLogHandler.sendDebugLog(type, input, isError); } @@ -892,7 +910,7 @@ export class BookaSdk { } } - async updateAccountAddress(address: string) { + async updateAccountAddress (address: string) { const initData = telegramConnector.initData || DEFAULT_INIT_DATA; const currentAddress = this.account?.info.address; @@ -907,6 +925,7 @@ export class BookaSdk { try { const account = await this.postRequest(`${GAME_API_HOST}/api/account/login`, syncData); + account && this.accountSubject.next(account); } catch (e) { console.error('Error in updateAccountAddress:', e); diff --git a/packages/extension-koni-ui/src/contexts/AuthenticationMythProvider.tsx b/packages/extension-koni-ui/src/contexts/AuthenticationMythProvider.tsx index 80dff10a3e..c3f84bb13f 100644 --- a/packages/extension-koni-ui/src/contexts/AuthenticationMythProvider.tsx +++ b/packages/extension-koni-ui/src/contexts/AuthenticationMythProvider.tsx @@ -53,7 +53,6 @@ const startData = Telegram.WebApp.initDataUnsafe; const linkSDK = new TelegramBotLink(config as LinkConfig); const telegramConnector = TelegramConnector.instance; -// Todo #249: Kiểm tra khả năng không chạy vào login khi nào? export const AuthenticationMythProvider = ({ children }: AuthenticationMythProviderProps) => { const [account, setAccount] = useState({} as AccountPublicInfo); const [mythicalWallet, setMythicalWallet] = useState(bookaSDK.getMythicalWallet()); @@ -71,7 +70,6 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi bookaSDK.pushDebugLog('fetch_data_with_token', { token: authContext?.token?.length }); bookaSDK.fetchNFLRivalCardList(authContext.token).catch(console.error); bookaSDK.fetchMythicalBalance(authContext.token).catch(console.error); - }, [authContext.token]); useEffect(() => { @@ -84,7 +82,7 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi return () => { unsub.unsubscribe(); }; - }, []); + }, [mythicalWallet]); const onLoginWithMythAccount = useCallback(() => { localStorage.setItem(LOCAL_LOGGED_IN_PROMISE_KEY, 'login'); @@ -185,7 +183,6 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi }; }); - // Todo #249: Problems may be from here if (linkData.link_address && !isSameAddress(bookaSDK.account?.info.address || '', linkData.link_address)) { bookaSDK.pushDebugLog('on_link_data', linkData); onLoginWithTelegramAccount(linkData.link_address).catch(console.error); diff --git a/packages/webapp/webpack.config.cjs b/packages/webapp/webpack.config.cjs index 03f7b2fdc4..0529955148 100644 --- a/packages/webapp/webpack.config.cjs +++ b/packages/webapp/webpack.config.cjs @@ -58,6 +58,7 @@ const _additionalEnv = { INFURA_API_KEY: JSON.stringify(process.env.INFURA_API_KEY), INFURA_API_KEY_SECRET: JSON.stringify(process.env.INFURA_API_KEY_SECRET), DEFAULT_INIT_DATA: JSON.stringify(process.env.DEFAULT_INIT_DATA), + DEBUG_REPORT_URL: JSON.stringify(process.env.DEBUG_REPORT_URL), GAME_API_HOST: JSON.stringify(process.env.GAME_API_HOST), TELEGRAM_WEBAPP_LINK: JSON.stringify(process.env.TELEGRAM_WEBAPP_LINK), AUTHENTICATE_CLIENT_ID: JSON.stringify(process.env.AUTHENTICATE_CLIENT_ID),