/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
shared/util/featured-bots.tsx
98 строк
3 KB
chrisnojima
build(mobile): harden gobuild.sh and go bind pipeline 2 (#29364)
08 июл 2026, 15:25
Не верифицирован
08 июл 2026, 15:25
19b55b3
Код
Авторство
О чём код?
import * as C from '@/constants' import * as React from 'react' import * as T from '@/constants/types' import logger from '@/logger' import {useRPCLoad} from '@/util/use-rpc-load' const featuredBotPageSize = 100 const mergeFeaturedBots = ( previous: ReadonlyArray<T.RPCGen.FeaturedBot>, next: ReadonlyArray<T.RPCGen.FeaturedBot> ): ReadonlyArray<T.RPCGen.FeaturedBot> => { const byUsername = new Map(previous.map(bot => [bot.botUsername, bot] as const)) next.forEach(bot => { byUsername.set(bot.botUsername, bot) }) return [...byUsername.values()] } const pickFeaturedBot = (botUsername: string, bots: ReadonlyArray<T.RPCGen.FeaturedBot>) => bots.find(bot => bot.botUsername === botUsername) ?? bots[0] export const getFeaturedSorted = (featuredBots: ReadonlyArray<T.RPCGen.FeaturedBot>) => { const featured = [...featuredBots] featured.sort((a: T.RPCGen.FeaturedBot, b: T.RPCGen.FeaturedBot) => { if (a.rank < b.rank) { return 1 } else if (a.rank > b.rank) { return -1 } return 0 }) return featured } export const useFeaturedBot = (botUsername?: string) => { const {data} = useRPCLoad( T.RPCGen.featuredBotSearchRpcPromise, [{limit: 10, offset: 0, query: botUsername ?? ''}], { enabled: !!botUsername, key: botUsername ?? '', map: result => pickFeaturedBot(botUsername ?? '', result.bots ?? []), onError: error => logger.info(`Featured bot load failed for ${botUsername}: ${error.message}`), } ) return botUsername ? data : undefined } export const useFeaturedBotPage = () => { const [featuredBots, setFeaturedBots] = React.useState<ReadonlyArray<T.RPCGen.FeaturedBot>>([]) const [featuredBotsPage, setFeaturedBotsPage] = React.useState(-1) const [loadedAllBots, setLoadedAllBots] = React.useState(false) const [pendingFeaturedBotsPage, setPendingFeaturedBotsPage] = React.useState<number | undefined>(0) const loadFeaturedBots = C.useRPC(T.RPCGen.featuredBotFeaturedBotsRpcPromise) const loadingBots = pendingFeaturedBotsPage !== undefined const loadNextBotPage = () => { if (loadingBots || loadedAllBots) { return } setPendingFeaturedBotsPage(featuredBotsPage + 1) } React.useEffect(() => { if (pendingFeaturedBotsPage === undefined) { return } let canceled = false loadFeaturedBots( [{limit: featuredBotPageSize, offset: pendingFeaturedBotsPage * featuredBotPageSize, skipCache: false}], result => { if (canceled) { return } const bots = result.bots ?? [] setFeaturedBots(previous => mergeFeaturedBots(previous, bots)) setFeaturedBotsPage(pendingFeaturedBotsPage) setLoadedAllBots(bots.length < featuredBotPageSize) setPendingFeaturedBotsPage(undefined) }, error => { if (canceled) { return } logger.info(`Featured bots page load failed: ${error.message}`) setPendingFeaturedBotsPage(undefined) } ) return () => { canceled = true } }, [loadFeaturedBots, pendingFeaturedBotsPage]) return {featuredBots, loadNextBotPage, loadedAllBots, loadingBots} }