1
1
import { MutationObserverWatcher } from '@dimensiondev/holoflows-kit'
2
- import { createInjectHooksRenderer , Plugin , useActivatedPluginsSNSAdaptor } from '@masknet/plugin-infra/content-script'
2
+ import { Plugin , createInjectHooksRenderer , useActivatedPluginsSNSAdaptor } from '@masknet/plugin-infra/content-script'
3
3
import { EnhanceableSite , ProfileIdentifier } from '@masknet/shared-base'
4
4
import { makeStyles } from '@masknet/theme'
5
- import { useMemo , useState } from 'react'
5
+ import { memo , useMemo , useState } from 'react'
6
6
import { attachReactTreeWithContainer } from '../../../../utils/index.js'
7
7
import { startWatch } from '../../../../utils/watcher.js'
8
8
import { querySelectorAll } from '../../utils/selector.js'
@@ -11,31 +11,6 @@ const selector = () => {
11
11
return querySelectorAll < HTMLElement > ( '[data-testid=conversation] div:not([tabindex]) div[dir] + div[dir]' )
12
12
}
13
13
14
- /**
15
- * Inject on conversation, including both DM drawer and message page (/messages/xxx)
16
- */
17
- export function injectLensOnConversation ( signal : AbortSignal ) {
18
- const watcher = new MutationObserverWatcher ( selector ( ) )
19
- startWatch ( watcher , signal )
20
- watcher . useForeach ( ( node , _ , proxy ) => {
21
- const spans = node
22
- . closest ( '[data-testid=conversation]' )
23
- ?. querySelectorAll < HTMLElement > ( '[tabindex] [dir] span:not([data-testid=tweetText])' )
24
- if ( ! spans ) return
25
- const userId = [ ...spans ] . reduce ( ( id , node ) => {
26
- if ( id ) return id
27
- if ( node . textContent ?. match ( / @ \w / ) ) {
28
- return node . textContent . trim ( ) . slice ( 1 )
29
- }
30
- return ''
31
- } , '' )
32
- if ( ! userId ) return
33
- attachReactTreeWithContainer ( proxy . afterShadow , { signal, untilVisible : true } ) . render (
34
- < ConversationLensSlot userId = { userId } /> ,
35
- )
36
- } )
37
- }
38
-
39
14
const useStyles = makeStyles ( ) ( ( theme ) => ( {
40
15
hide : {
41
16
display : 'none' ,
@@ -67,7 +42,7 @@ const createRootElement = () => {
67
42
return span
68
43
}
69
44
70
- function ConversationLensSlot ( { userId } : Props ) {
45
+ const ConversationLensSlot = memo ( function ConversationLensSlot ( { userId } : Props ) {
71
46
const [ disabled , setDisabled ] = useState ( true )
72
47
const { classes, cx } = useStyles ( )
73
48
@@ -78,7 +53,7 @@ function ConversationLensSlot({ userId }: Props) {
78
53
undefined ,
79
54
createRootElement ,
80
55
)
81
- const identifier = ProfileIdentifier . of ( EnhanceableSite . Twitter , userId ) . unwrap ( )
56
+ const identifier = ProfileIdentifier . of ( EnhanceableSite . Twitter , userId ) . unwrapOr ( null )
82
57
if ( ! identifier ) return null
83
58
84
59
return (
@@ -89,4 +64,29 @@ function ConversationLensSlot({ userId }: Props) {
89
64
if ( ! component ) return null
90
65
91
66
return < span className = { cx ( classes . slot , disabled ? classes . hide : null ) } > { component } </ span >
67
+ } )
68
+
69
+ /**
70
+ * Inject on conversation, including both DM drawer and message page (/messages/xxx)
71
+ */
72
+ export function injectLensOnConversation ( signal : AbortSignal ) {
73
+ const watcher = new MutationObserverWatcher ( selector ( ) )
74
+ startWatch ( watcher , signal )
75
+ watcher . useForeach ( ( node , _ , proxy ) => {
76
+ const spans = node
77
+ . closest ( '[data-testid=conversation]' )
78
+ ?. querySelectorAll < HTMLElement > ( '[tabindex] [dir] span:not([data-testid=tweetText])' )
79
+ if ( ! spans ) return
80
+ const userId = [ ...spans ] . reduce ( ( id , node ) => {
81
+ if ( id ) return id
82
+ if ( node . textContent ?. match ( / @ \w / ) ) {
83
+ return node . textContent . trim ( ) . slice ( 1 )
84
+ }
85
+ return ''
86
+ } , '' )
87
+ if ( ! userId ) return
88
+ attachReactTreeWithContainer ( proxy . afterShadow , { signal, untilVisible : true } ) . render (
89
+ < ConversationLensSlot userId = { userId } /> ,
90
+ )
91
+ } )
92
92
}
0 commit comments