|
| 1 | +import { memo, useMemo, useState } from 'react' |
| 2 | +import { MutationObserverWatcher } from '@dimensiondev/holoflows-kit' |
| 3 | +import { createInjectHooksRenderer, Plugin, useActivatedPluginsSiteAdaptor } from '@masknet/plugin-infra/content-script' |
| 4 | +import { EnhanceableSite, ProfileIdentifier } from '@masknet/shared-base' |
| 5 | +import { makeStyles } from '@masknet/theme' |
| 6 | +import { attachReactTreeWithContainer } from '../../../../utils/shadow-root/renderInShadowRoot.js' |
| 7 | +import { querySelectorAll } from '../../utils/selector.js' |
| 8 | +import { startWatch } from '../../../../utils/startWatch.js' |
| 9 | + |
| 10 | +function selector() { |
| 11 | + return querySelectorAll<HTMLElement>('[data-testid=conversation] div:not([tabindex]) div[dir] + div[dir]') |
| 12 | +} |
| 13 | + |
| 14 | +const useStyles = makeStyles()((theme) => ({ |
| 15 | + hide: { |
| 16 | + display: 'none', |
| 17 | + }, |
| 18 | + slot: { |
| 19 | + position: 'relative', |
| 20 | + display: 'flex', |
| 21 | + justifyContent: 'center', |
| 22 | + alignItems: 'center', |
| 23 | + borderRadius: 999, |
| 24 | + marginLeft: theme.spacing(0.5), |
| 25 | + verticalAlign: 'top', |
| 26 | + }, |
| 27 | +})) |
| 28 | + |
| 29 | +interface Props { |
| 30 | + userId: string |
| 31 | +} |
| 32 | + |
| 33 | +function createRootElement() { |
| 34 | + const span = document.createElement('span') |
| 35 | + Object.assign(span.style, { |
| 36 | + verticalAlign: 'bottom', |
| 37 | + height: '21px', |
| 38 | + alignItems: 'center', |
| 39 | + justifyContent: 'center', |
| 40 | + display: 'inline-flex', |
| 41 | + } as CSSStyleDeclaration) |
| 42 | + return span |
| 43 | +} |
| 44 | + |
| 45 | +const ConversationFarcasterSlot = memo(function ConversationFarcasterSlot({ userId }: Props) { |
| 46 | + const [disabled, setDisabled] = useState(true) |
| 47 | + const { classes, cx } = useStyles() |
| 48 | + |
| 49 | + const component = useMemo(() => { |
| 50 | + const Component = createInjectHooksRenderer( |
| 51 | + useActivatedPluginsSiteAdaptor.visibility.useNotMinimalMode, |
| 52 | + (plugin) => plugin.Farcaster?.UI?.Content, |
| 53 | + undefined, |
| 54 | + createRootElement, |
| 55 | + ) |
| 56 | + const identifier = ProfileIdentifier.of(EnhanceableSite.Twitter, userId).unwrapOr(null) |
| 57 | + if (!identifier) return null |
| 58 | + |
| 59 | + return ( |
| 60 | + <Component |
| 61 | + identity={identifier} |
| 62 | + slot={Plugin.SiteAdaptor.FarcasterSlot.Sidebar} |
| 63 | + onStatusUpdate={setDisabled} |
| 64 | + /> |
| 65 | + ) |
| 66 | + }, [userId]) |
| 67 | + |
| 68 | + if (!component) return null |
| 69 | + |
| 70 | + return <span className={cx(classes.slot, disabled ? classes.hide : null)}>{component}</span> |
| 71 | +}) |
| 72 | + |
| 73 | +/** |
| 74 | + * Inject on conversation, including both DM drawer and message page (/messages/xxx) |
| 75 | + */ |
| 76 | +export function injectFarcasterOnConversation(signal: AbortSignal) { |
| 77 | + const watcher = new MutationObserverWatcher(selector()) |
| 78 | + startWatch(watcher, signal) |
| 79 | + watcher.useForeach((node, _, proxy) => { |
| 80 | + const spans = node |
| 81 | + .closest('[data-testid=conversation]') |
| 82 | + ?.querySelectorAll<HTMLElement>('[tabindex] [dir] span:not([data-testid=tweetText])') |
| 83 | + if (!spans) return |
| 84 | + const userId = [...spans].reduce((id, node) => { |
| 85 | + if (id) return id |
| 86 | + if (node.textContent?.match(/@\w/)) { |
| 87 | + return node.textContent.trim().slice(1) |
| 88 | + } |
| 89 | + return '' |
| 90 | + }, '') |
| 91 | + if (!userId) return |
| 92 | + attachReactTreeWithContainer(proxy.afterShadow, { signal, untilVisible: true }).render( |
| 93 | + <ConversationFarcasterSlot userId={userId} />, |
| 94 | + ) |
| 95 | + }) |
| 96 | +} |
0 commit comments