Skip to content

Commit

Permalink
fix: reduce api queries (#11455)
Browse files Browse the repository at this point in the history
* fix: config queries

* refactor: remove missingReportRule

* refactor: condition rendering

* fix: disabled refetch
  • Loading branch information
guanbinrui authored and Jack-Works committed Feb 27, 2024
1 parent a8e23e2 commit b11face
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export const IdentityProviderFacebook: SiteAdaptorUI.CollectingCapabilities.Iden
}

function resolveLastRecognizedIdentityFacebookInner(ref: ValueRef<IdentityResolved>, signal: AbortSignal) {
const self = (isMobileFacebook ? myUsernameLiveSelectorMobile : myUsernameLiveSelectorPC)
.clone()
.map((x) => getProfileIdentifierAtFacebook(x, false))
const self = myUsernameLiveSelectorPC.clone().map((x) => getProfileIdentifierAtFacebook(x, false))
new MutationObserverWatcher(self)
.addListener('onAdd', (e) => assign(e.value))
.addListener('onChange', (e) => assign(e.newValue))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { LiveSelector, MutationObserverWatcher } from '@dimensiondev/holoflows-kit'
import { attachReactTreeWithContainer } from '../../../utils/shadow-root/renderInShadowRoot.js'
import { startWatch } from '../../../utils/startWatch.js'
import { isMobileFacebook } from '../utils/isMobile.js'
import { Banner } from '../../../components/Welcomes/Banner.js'

let composeBox: LiveSelector<Element>
if (isMobileFacebook) {
composeBox = new LiveSelector().querySelector('#structured_composer_form')
} else {
composeBox = new LiveSelector()
.querySelector('[role="dialog"] form')
.querySelectorAll('[role="button"][tabindex="0"], [role="button"][tabindex="-1"]')
.map((x) => x.parentElement)
.at(-1)
}
const composeBox: LiveSelector<Element> = new LiveSelector()
.querySelectorAll(
'[role="dialog"] form [role="button"][tabindex="0"], [role="dialog"] form [role="button"][tabindex="-1"]',
)
.map((x) => x.parentElement)
.at(-1)

export function injectBannerAtFacebook(signal: AbortSignal) {
const watcher = new MutationObserverWatcher(composeBox.clone())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function getNickname(nickname?: string) {

function resolveLastRecognizedIdentityInner(
ref: SiteAdaptorUI.CollectingCapabilities.IdentityResolveProvider['recognized'],
cancel: AbortSignal,
signal: AbortSignal,
) {
const assign = async () => {
await delay(2000)
Expand Down Expand Up @@ -78,13 +78,13 @@ function resolveLastRecognizedIdentityInner(
attributes: true,
attributeFilter: ['src'],
},
cancel,
signal,
)
}

assign()

window.addEventListener('locationchange', assign, { signal: cancel })
window.addEventListener('locationchange', assign, { signal })
createWatcher(searchSelfHandleSelector())
createWatcher(searchWatcherAvatarSelector())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ export function injectBannerAtTwitter(signal: AbortSignal) {
const emptyNode = document.createElement('div')
injectBanner(postEditorInTimelineSelector(), {
signal,
missingReportRule: { name: 'Setup prompt', rule: 'https://twitter.com/home' },
})
injectBanner(
postEditorInPopupSelector().map((x) => (isCompose() && hasEditor() ? x : emptyNode)),
{ signal, missingReportRule: { name: 'Setup prompt', rule: 'https://twitter.com/compose/post' } },
{ signal },
)
}

function injectBanner<T>(ls: LiveSelector<T, true>, options: WatchOptions) {
const watcher = new MutationObserverWatcher(ls)
startWatch(watcher, options)

attachReactTreeWithContainer(watcher.firstDOMProxy.afterShadow, { signal: options.signal }).render(<Banner />)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import { Flags } from '@masknet/flags'
import Services from '#services'
import { startWatch, type WatchOptions } from '../../../utils/startWatch.js'
import { attachReactTreeWithContainer } from '../../../utils/shadow-root/renderInShadowRoot.js'
import {
bioPageUserIDSelector,
bioPageUserNickNameSelector,
floatingBioCardSelector,
isProfilePageLike,
} from '../utils/selector.js'
import { bioPageUserIDSelector, bioPageUserNickNameSelector, floatingBioCardSelector } from '../utils/selector.js'

function Icon(props: { size: number }) {
return (
Expand Down Expand Up @@ -54,7 +49,7 @@ function _(main: () => LiveSelector<HTMLElement, true>, size: number, options: W

export function injectMaskUserBadgeAtTwitter(signal: AbortSignal) {
// profile
_(bioPageUserNickNameSelector, 24, { signal, missingReportRule: { name: 'User badge', rule: isProfilePageLike } })
_(bioPageUserNickNameSelector, 24, { signal })
// floating bio
_(floatingBioCardSelector, 20, { signal })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ function renderPostDialogTo<T>(reason: 'timeline' | 'popup', ls: LiveSelector<T,
export function injectPostDialogAtTwitter(signal: AbortSignal) {
renderPostDialogTo('popup', postEditorContentInPopupSelector(), {
signal,
missingReportRule: { name: 'PostDialog popup', rule: 'https://twitter.com/compose/post' },
})
renderPostDialogTo('timeline', rootSelector(), {
signal,
missingReportRule: { name: 'PostDialog timeline', rule: 'https://twitter.com/home' },
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,13 @@ export function injectPostDialogHintAtTwitter(signal: AbortSignal) {

renderPostDialogHintTo('timeline', searchReplyToolbarSelector(), {
signal,
missingReportRule: { name: 'PostDialog hint timeline', rule: 'https://twitter.com/home' },
})

renderPostDialogHintTo(
'popup',
postEditorInPopupSelector().map((x) => (isCompose() && hasEditor() ? x : emptyNode)),
{
signal,
missingReportRule: {
name: 'PostDialog hint popup',
rule: 'https://twitter.com/compose/post',
},
},
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MutationObserverWatcher } from '@dimensiondev/holoflows-kit'
import { isProfilePageLike, searchProfileCoverSelector } from '../utils/selector.js'
import { searchProfileCoverSelector } from '../utils/selector.js'
import { attachReactTreeWithContainer } from '../../../utils/shadow-root/renderInShadowRoot.js'
import { startWatch } from '../../../utils/startWatch.js'
import { ProfileCover } from '../../../components/InjectedComponents/ProfileCover.js'
Expand All @@ -8,10 +8,6 @@ export function injectProfileCover(signal: AbortSignal) {
const watcher = new MutationObserverWatcher(searchProfileCoverSelector())
startWatch(watcher, {
signal,
missingReportRule: {
name: 'profile page cover',
rule: isProfilePageLike,
},
})
attachReactTreeWithContainer(watcher.firstDOMProxy.afterShadow, { signal }).render(<ProfileCoverAtTwitter />)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
searchProfileTabSelector,
searchProfileTabLoseConnectionPageSelector,
searchNameTag,
isProfilePageLike,
nextTabListSelector,
} from '../utils/selector.js'
import { useCurrentVisitingIdentity } from '../../../components/DataSource/useActivatedUI.js'
Expand Down Expand Up @@ -283,10 +282,6 @@ export function injectProfileTabAtTwitter(signal: AbortSignal) {
const watcher = new MutationObserverWatcher(searchProfileTabListLastChildSelector())
startWatch(watcher, {
signal,
missingReportRule: {
name: 'Last tab in the profile page',
rule: isProfilePageLike,
},
shadowRootDelegatesFocus: false,
})
attachReactTreeWithContainer(watcher.firstDOMProxy.afterShadow, { signal }).render(<InjectProfileTab />)
Expand All @@ -296,7 +291,6 @@ export function injectProfileTabAtTwitter(signal: AbortSignal) {

startWatch(contentWatcher, {
signal,
missingReportRule: { name: 'ProfileTab', rule: isProfilePageLike },
shadowRootDelegatesFocus: false,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ export function injectSearchResultInspectorAtTwitter(signal: AbortSignal) {
const watcher = new MutationObserverWatcher(searchResultHeadingSelector())
startWatch(watcher, {
signal,
missingReportRule: {
name: 'SearchResultInspector',
rule: 'https://twitter.com/search?q=',
},
})
attachReactTreeWithContainer(watcher.firstDOMProxy.beforeShadow, { signal }).render(<SearchResultInspector />)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ export function injectToolboxHintAtTwitter(signal: AbortSignal, category: 'walle

startWatch(watcher, {
signal,
missingReportRule: {
name: 'Sidebar toolbox',
rule() {
// return false where the page should not match. maybe on mobile size?
return true
},
},
})
attachReactTreeWithContainer(watcher.firstDOMProxy.afterShadow, { signal }).render(
<RootWeb3ContextProvider>
Expand Down
4 changes: 4 additions & 0 deletions packages/shared-base-ui/src/data/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const queryClient = new QueryClient({
queries: {
staleTime: 20_000,
gcTime: Number.POSITIVE_INFINITY,
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchIntervalInBackground: false,
refetchOnReconnect: false,
},
},
})
2 changes: 2 additions & 0 deletions packages/shared/src/UI/modals/ConfirmDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const Dialog = memo(function Dialog({
})

export type ConfirmDialogOpenProps = Omit<ConfirmDialogProps, 'open' | 'onConfirm'>

export const ConfirmDialogComponent = forwardRef<SingletonModalRefCreator<ConfirmDialogOpenProps, boolean>>(
function ConfirmDialog(_, ref) {
const [props, setProps] = useState<ConfirmDialogOpenProps>({
Expand All @@ -119,6 +120,7 @@ export const ConfirmDialogComponent = forwardRef<SingletonModalRefCreator<Confir
setProps(p)
},
})
if (!open) return null
return (
<Dialog
open={open}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const WalletRiskWarningModal = forwardRef<SingletonModalRefCreator<Wallet
})

if (!open) return null

return <WalletRiskWarning account={account} pluginID={pluginID} open onClose={() => dispatch?.close()} />
},
)
6 changes: 3 additions & 3 deletions packages/web3-providers/src/CryptoScamDB/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import urlcat from 'urlcat'
import type { ScalableBloomFilter } from 'bloom-filters'
import { fetchSquashedJSON } from '../helpers/fetchJSON.js'
import { fetchCachedJSON } from '../helpers/fetchJSON.js'
import type { ScamWarningAPI } from '../entry-types.js'

const BASE_URL = 'https://scam.mask.r2d2.to/cryptoscam-db'
Expand All @@ -10,7 +10,7 @@ export class CryptoScamDB {

static async getBloomFilter() {
if (this.bloomFilter) return this.bloomFilter
const filter = await fetchSquashedJSON<JSON>(urlcat(BASE_URL, 'filter/config.json'))
const filter = await fetchCachedJSON<JSON>(urlcat(BASE_URL, 'filter/config.json'))

const { ScalableBloomFilter } = await import('bloom-filters')
this.bloomFilter = ScalableBloomFilter.fromJSON(filter)
Expand All @@ -25,7 +25,7 @@ export class CryptoScamDB {
const url = new URL(link)
if (!filter.has(url.host)) return

const result = await fetchSquashedJSON<ScamWarningAPI.Info>(urlcat(BASE_URL, `${url.host}.json`))
const result = await fetchCachedJSON<ScamWarningAPI.Info>(urlcat(BASE_URL, `${url.host}.json`))
if (!result) return

const scamURL = new URL(result.url)
Expand Down
16 changes: 9 additions & 7 deletions packages/web3-providers/src/MaskX/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { MASK_X_DEFAULT_PAGINATION, MASK_X_ROOT_URL } from './constants.js'
import { BaseMaskX } from '../entry-types.js'
import { fetchCachedJSON } from '../helpers/fetchJSON.js'

export class MaskX {
private static async fetchFromMaskX(pathname: string) {
return fetchCachedJSON<BaseMaskX.Response>(urlcat(MASK_X_ROOT_URL, pathname))
}
function fetchFromMaskX(pathname: string) {
return fetchCachedJSON<BaseMaskX.Response>(urlcat(MASK_X_ROOT_URL, pathname))
}

export class MaskX {
private static getOptions({ size = 20, page = 1 }: BaseMaskX.Options = MASK_X_DEFAULT_PAGINATION) {
return {
size,
Expand Down Expand Up @@ -51,7 +51,7 @@ export class MaskX {
platform: BaseMaskX.PlatformType,
initial: BaseMaskX.Options = { size: 20, page: 1 },
): Promise<BaseMaskX.Response> {
const response = await this.fetchFromMaskX(
const response = await fetchFromMaskX(
urlcat('/prod/identity', {
identity: handle,
platform,
Expand All @@ -60,12 +60,13 @@ export class MaskX {
)
return this.getResponse(response)
}

static async getIdentitiesFuzzy(
handle: string,
platform: BaseMaskX.PlatformType,
initial: BaseMaskX.Options = { size: 20, page: 1 },
): Promise<BaseMaskX.Response> {
const response = await this.fetchFromMaskX(
const response = await fetchFromMaskX(
urlcat('/prod/identity/search', {
identity: handle,
platform,
Expand All @@ -74,8 +75,9 @@ export class MaskX {
)
return this.getResponse(response)
}

static async getAllIdentities(initial: BaseMaskX.Options = { size: 20, page: 1 }): Promise<BaseMaskX.Response> {
const response = await this.fetchFromMaskX(urlcat('/prod/identity/all', this.getOptions(initial)))
const response = await fetchFromMaskX(urlcat('/prod/identity/all', this.getOptions(initial)))
return this.getResponse(response)
}
}
4 changes: 2 additions & 2 deletions packages/web3-providers/src/Web3/EVM/state/IdentityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export class EVMIdentityService extends IdentityServiceState<ChainId> {
const userId = identifier?.userId
if (!userId) return

const response = await MaskX.MaskX.getIdentitiesExact(userId, BaseMaskX.PlatformType.Twitter)
const results = response.records.filter((x) => {
const { records } = await MaskX.MaskX.getIdentitiesExact(userId, BaseMaskX.PlatformType.Twitter)
const results = records.filter((x) => {
if (!isValidAddress(x.web3_addr) || !x.is_verified) return false

try {
Expand Down

0 comments on commit b11face

Please sign in to comment.