diff --git a/packages/extension-koni-ui/src/Popup/Home/Mission/TaskItem.tsx b/packages/extension-koni-ui/src/Popup/Home/Mission/TaskItem.tsx index 4e65b3b90b..d5cb6ec301 100644 --- a/packages/extension-koni-ui/src/Popup/Home/Mission/TaskItem.tsx +++ b/packages/extension-koni-ui/src/Popup/Home/Mission/TaskItem.tsx @@ -4,7 +4,7 @@ import { SWTransactionResponse } from '@subwallet/extension-base/services/transaction-service/types'; import { GamePoint } from '@subwallet/extension-koni-ui/components'; import { BookaSdk } from '@subwallet/extension-koni-ui/connector/booka/sdk'; -import { Task, TaskHistoryStatus } from '@subwallet/extension-koni-ui/connector/booka/types'; +import { ShareLeaderboard, Task, TaskHistoryStatus } from '@subwallet/extension-koni-ui/connector/booka/types'; import { TelegramConnector } from '@subwallet/extension-koni-ui/connector/telegram'; import { useNotification, useSetCurrentPage, useTranslation } from '@subwallet/extension-koni-ui/hooks'; import { ThemeProps } from '@subwallet/extension-koni-ui/types'; @@ -112,6 +112,15 @@ const _TaskItem = ({ actionReloadPoint, className, task }: Props): React.ReactEl extrinsicHash = res.extrinsicHash || ''; } + let shareLeaderboard: ShareLeaderboard | null = null; + + if (task.share_leaderboard) { + try { + shareLeaderboard = JSON.parse(task.share_leaderboard) as ShareLeaderboard; + } catch (e) { + console.error('shareLeaderboard', e); + } + } apiSDK.finishTask(taskId, extrinsicHash, networkKey) .finally(() => { setTaskLoading(false); @@ -129,11 +138,11 @@ const _TaskItem = ({ actionReloadPoint, className, task }: Props): React.ReactEl let urlRedirect = task.url; if (urlRedirect) { - if (urlRedirect === 'share_airdrop') { - const startEnv = process.env.KARURA_PLAYDROP_START_DATE || '2024-06-01 03:00:00' as string; - const endEnv = process.env.KARURA_PLAYDROP_END_DATE || '2024-06-15 00:00:00' as string; + if (shareLeaderboard && shareLeaderboard.content) { + const startEnv = shareLeaderboard.start_time; + const endEnv = shareLeaderboard.end_time; - urlRedirect = await apiSDK.getShareTwitterURL(startEnv, endEnv); + urlRedirect = await apiSDK.getShareTwitterURL(startEnv, endEnv, shareLeaderboard.content, task.gameId ?? 0, shareLeaderboard.url); } telegramConnector.openLink(urlRedirect); diff --git a/packages/extension-koni-ui/src/connector/booka/sdk.ts b/packages/extension-koni-ui/src/connector/booka/sdk.ts index ef6c0053ae..11cb768ffa 100644 --- a/packages/extension-koni-ui/src/connector/booka/sdk.ts +++ b/packages/extension-koni-ui/src/connector/booka/sdk.ts @@ -10,6 +10,7 @@ import { InGameItem } from '@subwallet/extension-koni-ui/Popup/Home/Games/types' import { calculateStartAndEnd, formatDateFully } from '@subwallet/extension-koni-ui/utils/date'; import fetch from 'cross-fetch'; import { BehaviorSubject } from 'rxjs'; +import { populateTemplateString } from '@subwallet/extension-koni-ui/utils'; export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api.anhmtv.xyz'; export const TELEGRAM_WEBAPP_LINK = process.env.TELEGRAM_WEBAPP_LINK || 'Playnation_bot/app'; @@ -336,25 +337,26 @@ export class BookaSdk { return `http://x.com/share?text=${content}&url=${linkApp}`; } - async getShareTwitterURL (startDate: string, endDate: string) { + async getShareTwitterURL (startDate: string, endDate: string, content: string, gameId: number, url: string) { const start = formatDateFully(new Date(startDate)); const end = formatDateFully(new Date(endDate)); - const leaderBoard = await this.postRequest(`${GAME_API_HOST}/api/game/leader-board`, { startDate: start, endDate: end, limit: 1 }); + const leaderBoard = await this.postRequest(`${GAME_API_HOST}/api/game/leader-board`, + { + startDate: start, + endDate: end, + gameId: gameId, + limit: 1 }); const personMine = leaderBoard.find((item) => item.mine); - let content = 'A new exciting game is in town, Karura Token Playdrop! Want some fun and a chance to win Karura airdrop? Join me NOW 👇%0A'; + let contentShare = ''; if (personMine) { - const result = `Wooho, I got ${personMine.point} points and ranked ${personMine.rank} on the Karura Token Playdrop leaderboard 🔥`; - - content = `${result} Want some fun and a chance to win Karura airdrop? Join me NOW 👇%0A`; + contentShare = `text=${populateTemplateString(content, personMine)}%0A&`; } - const urlShareImage = 'https://x.playnation.app/playnation-share-karura'; - - const linkShare = `${urlShareImage}?startApp=${this.account?.info.inviteCode || 'booka'}`; + const linkShare = `${url}?startApp=${this.account?.info.inviteCode || 'booka'}`; - return `http://x.com/share?text=${content}&url=${linkShare}`; + return `http://x.com/share?${contentShare}url=${linkShare}`; } async fetchReferalList () { diff --git a/packages/extension-koni-ui/src/connector/booka/types.ts b/packages/extension-koni-ui/src/connector/booka/types.ts index 04723f009c..c22180a03b 100644 --- a/packages/extension-koni-ui/src/connector/booka/types.ts +++ b/packages/extension-koni-ui/src/connector/booka/types.ts @@ -88,6 +88,14 @@ export enum TaskHistoryStatus { COMPLETED = 'completed', } +export interface ShareLeaderboard { + content: string; + url: string; + start_time: string; + end_time: string; + +} + export interface Task { id: number; // id on db contentId: number; @@ -109,6 +117,7 @@ export interface Task { status: TaskHistoryStatus; completedAt?: string; taskHistoryId?: number; + share_leaderboard?: string | null; } export interface TaskCategory { diff --git a/packages/extension-koni-ui/src/utils/common/index.ts b/packages/extension-koni-ui/src/utils/common/index.ts index 2e6f934146..362b5cd8b9 100644 --- a/packages/extension-koni-ui/src/utils/common/index.ts +++ b/packages/extension-koni-ui/src/utils/common/index.ts @@ -11,3 +11,4 @@ export * from './function'; export * from './getLanguageOptions'; export * from './i18n'; export * from './number'; +export * from './string'; diff --git a/packages/extension-koni-ui/src/utils/common/string.ts b/packages/extension-koni-ui/src/utils/common/string.ts new file mode 100644 index 0000000000..f86aafd92f --- /dev/null +++ b/packages/extension-koni-ui/src/utils/common/string.ts @@ -0,0 +1,27 @@ +// [object Object] +// SPDX-License-Identifier: Apache-2.0 + +// eslint-disable-next-line header/header +export function populateTemplateString (template: string, data: any): string { + if (!data) { + return template; + } + + return template.replace(/{(.*?)}/g, (match: string, p1: string): string => { + const keys = p1.split('.'); + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + let value = data; + + for (const key of keys) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access + value = value[key]; + + if (value === undefined) { + return ''; + } + } + + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return value; + }); +} diff --git a/packages/extension-koni-ui/src/utils/date/index.ts b/packages/extension-koni-ui/src/utils/date/index.ts index 1b35283afe..9773632cc4 100644 --- a/packages/extension-koni-ui/src/utils/date/index.ts +++ b/packages/extension-koni-ui/src/utils/date/index.ts @@ -2,19 +2,19 @@ // SPDX-License-Identifier: Apache-2.0 const formatDate = (date: Date) => { const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0 - const day = String(date.getDate()).padStart(2, '0'); + const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0 + const day = String(date.getUTCDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }; export const formatDateFully = (date: Date) => { const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0 - const day = String(date.getDate()).padStart(2, '0'); - const hour = String(date.getHours()).padStart(2, '0'); - const minute = String(date.getMinutes()).padStart(2, '0'); - const second = String(date.getSeconds()).padStart(2, '0'); + const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Tháng bắt đầu từ 0 + const day = String(date.getUTCDate()).padStart(2, '0'); + const hour = String(date.getUTCHours()).padStart(2, '0'); + const minute = String(date.getUTCMinutes()).padStart(2, '0'); + const second = String(date.getUTCSeconds()).padStart(2, '0'); return `${year}-${month}-${day} ${hour}:${minute}:${second}`; };