@@ -9,107 +9,140 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
9
9
Please see LICENSE files in the repository root for full details.
10
10
*/
11
11
12
- import React , { ReactNode } from "react" ;
12
+ import React , { ReactNode , JSX } from "react" ;
13
13
import { Room } from "matrix-js-sdk/src/matrix" ;
14
14
15
- import { _t , _td } from "../../../languageHandler" ;
15
+ import { _t } from "../../../languageHandler" ;
16
16
import SettingsStore from "../../../settings/SettingsStore" ;
17
17
import dis from "../../../dispatcher/dispatcher" ;
18
- import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
19
18
import { Action } from "../../../dispatcher/actions" ;
20
19
import { SettingLevel } from "../../../settings/SettingLevel" ;
21
20
import SettingsFlag from "../elements/SettingsFlag" ;
22
21
import SettingsFieldset from "../settings/SettingsFieldset" ;
23
22
import AccessibleButton , { ButtonEvent } from "../elements/AccessibleButton" ;
23
+ import { useIsEncrypted } from "../../../hooks/useIsEncrypted.ts" ;
24
+ import { useMatrixClientContext } from "../../../contexts/MatrixClientContext.tsx" ;
25
+ import { useSettingValueAt } from "../../../hooks/useSettings.ts" ;
24
26
25
- interface IProps {
27
+ /**
28
+ * The URL preview settings for a room
29
+ */
30
+ interface UrlPreviewSettingsProps {
31
+ /**
32
+ * The room.
33
+ */
26
34
room : Room ;
27
35
}
28
36
29
- export default class UrlPreviewSettings extends React . Component < IProps > {
30
- private onClickUserSettings = ( e : ButtonEvent ) : void => {
31
- e . preventDefault ( ) ;
32
- e . stopPropagation ( ) ;
33
- dis . fire ( Action . ViewUserSettings ) ;
34
- } ;
35
-
36
- public render ( ) : ReactNode {
37
- const roomId = this . props . room . roomId ;
38
- const isEncrypted = MatrixClientPeg . safeGet ( ) . isRoomEncrypted ( roomId ) ;
39
-
40
- let previewsForAccount : ReactNode | undefined ;
41
- let previewsForRoom : ReactNode | undefined ;
42
-
43
- if ( ! isEncrypted ) {
44
- // Only show account setting state and room state setting state in non-e2ee rooms where they apply
45
- const accountEnabled = SettingsStore . getValueAt ( SettingLevel . ACCOUNT , "urlPreviewsEnabled" ) ;
46
- if ( accountEnabled ) {
47
- previewsForAccount = _t (
48
- "room_settings|general|user_url_previews_default_on" ,
49
- { } ,
50
- {
51
- a : ( sub ) => (
52
- < AccessibleButton kind = "link_inline" onClick = { this . onClickUserSettings } >
53
- { sub }
54
- </ AccessibleButton >
55
- ) ,
56
- } ,
57
- ) ;
58
- } else {
59
- previewsForAccount = _t (
60
- "room_settings|general|user_url_previews_default_off" ,
61
- { } ,
62
- {
63
- a : ( sub ) => (
64
- < AccessibleButton kind = "link_inline" onClick = { this . onClickUserSettings } >
65
- { sub }
66
- </ AccessibleButton >
67
- ) ,
68
- } ,
69
- ) ;
70
- }
71
-
72
- if ( SettingsStore . canSetValue ( "urlPreviewsEnabled" , roomId , SettingLevel . ROOM ) ) {
73
- previewsForRoom = (
74
- < SettingsFlag
75
- name = "urlPreviewsEnabled"
76
- level = { SettingLevel . ROOM }
77
- roomId = { roomId }
78
- isExplicit = { true }
79
- />
80
- ) ;
81
- } else {
82
- let str = _td ( "room_settings|general|default_url_previews_on" ) ;
83
- if ( ! SettingsStore . getValueAt ( SettingLevel . ROOM , "urlPreviewsEnabled" , roomId , /*explicit=*/ true ) ) {
84
- str = _td ( "room_settings|general|default_url_previews_off" ) ;
85
- }
86
- previewsForRoom = < div > { _t ( str ) } </ div > ;
87
- }
88
- } else {
89
- previewsForAccount = _t ( "room_settings|general|url_preview_encryption_warning" ) ;
90
- }
91
-
92
- const previewsForRoomAccount = // in an e2ee room we use a special key to enforce per-room opt-in
93
- (
94
- < SettingsFlag
95
- name = { isEncrypted ? "urlPreviewsEnabled_e2ee" : "urlPreviewsEnabled" }
96
- level = { SettingLevel . ROOM_DEVICE }
97
- roomId = { roomId }
98
- />
99
- ) ;
100
-
101
- const description = (
102
- < >
103
- < p > { _t ( "room_settings|general|url_preview_explainer" ) } </ p >
104
- < p > { previewsForAccount } </ p >
105
- </ >
37
+ export function UrlPreviewSettings ( { room } : UrlPreviewSettingsProps ) : JSX . Element {
38
+ const { roomId } = room ;
39
+ const matrixClient = useMatrixClientContext ( ) ;
40
+ const isEncrypted = Boolean ( useIsEncrypted ( matrixClient , room ) ) ;
41
+ const previewsForRoomAccount = // in an e2ee room we use a special key to enforce per-room opt-in
42
+ (
43
+ < SettingsFlag
44
+ name = { isEncrypted ? "urlPreviewsEnabled_e2ee" : "urlPreviewsEnabled" }
45
+ level = { SettingLevel . ROOM_DEVICE }
46
+ roomId = { roomId }
47
+ />
106
48
) ;
107
49
108
- return (
109
- < SettingsFieldset legend = { _t ( "room_settings|general|url_previews_section" ) } description = { description } >
110
- { previewsForRoom }
111
- { previewsForRoomAccount }
112
- </ SettingsFieldset >
50
+ return (
51
+ < SettingsFieldset
52
+ legend = { _t ( "room_settings|general|url_previews_section" ) }
53
+ description = { < Description isEncrypted = { isEncrypted } /> }
54
+ >
55
+ < PreviewsForRoom isEncrypted = { isEncrypted } roomId = { roomId } />
56
+ { previewsForRoomAccount }
57
+ </ SettingsFieldset >
58
+ ) ;
59
+ }
60
+
61
+ /**
62
+ * Click handler for the user settings link
63
+ * @param e
64
+ */
65
+ function onClickUserSettings ( e : ButtonEvent ) : void {
66
+ e . preventDefault ( ) ;
67
+ e . stopPropagation ( ) ;
68
+ dis . fire ( Action . ViewUserSettings ) ;
69
+ }
70
+
71
+ /**
72
+ * The description for the URL preview settings
73
+ */
74
+ interface DescriptionProps {
75
+ /**
76
+ * Whether the room is encrypted
77
+ */
78
+ isEncrypted : boolean ;
79
+ }
80
+
81
+ function Description ( { isEncrypted } : DescriptionProps ) : JSX . Element {
82
+ const urlPreviewsEnabled = useSettingValueAt ( SettingLevel . ACCOUNT , "urlPreviewsEnabled" ) ;
83
+
84
+ let previewsForAccount : ReactNode | undefined ;
85
+ if ( isEncrypted ) {
86
+ previewsForAccount = _t ( "room_settings|general|url_preview_encryption_warning" ) ;
87
+ } else {
88
+ const button = {
89
+ a : ( sub : string ) => (
90
+ < AccessibleButton kind = "link_inline" onClick = { onClickUserSettings } >
91
+ { sub }
92
+ </ AccessibleButton >
93
+ ) ,
94
+ } ;
95
+
96
+ previewsForAccount = urlPreviewsEnabled
97
+ ? _t ( "room_settings|general|user_url_previews_default_on" , { } , button )
98
+ : _t ( "room_settings|general|user_url_previews_default_off" , { } , button ) ;
99
+ }
100
+
101
+ return (
102
+ < >
103
+ < p > { _t ( "room_settings|general|url_preview_explainer" ) } </ p >
104
+ < p > { previewsForAccount } </ p >
105
+ </ >
106
+ ) ;
107
+ }
108
+
109
+ /**
110
+ * The description for the URL preview settings
111
+ */
112
+ interface PreviewsForRoomProps {
113
+ /**
114
+ * Whether the room is encrypted
115
+ */
116
+ isEncrypted : boolean ;
117
+ /**
118
+ * The room ID
119
+ */
120
+ roomId : string ;
121
+ }
122
+
123
+ function PreviewsForRoom ( { isEncrypted, roomId } : PreviewsForRoomProps ) : JSX . Element | null {
124
+ const urlPreviewsEnabled = useSettingValueAt (
125
+ SettingLevel . ACCOUNT ,
126
+ "urlPreviewsEnabled" ,
127
+ roomId ,
128
+ /*explicit=*/ true ,
129
+ ) ;
130
+ if ( isEncrypted ) return null ;
131
+
132
+ let previewsForRoom : ReactNode ;
133
+ if ( SettingsStore . canSetValue ( "urlPreviewsEnabled" , roomId , SettingLevel . ROOM ) ) {
134
+ previewsForRoom = (
135
+ < SettingsFlag name = "urlPreviewsEnabled" level = { SettingLevel . ROOM } roomId = { roomId } isExplicit = { true } />
136
+ ) ;
137
+ } else {
138
+ previewsForRoom = (
139
+ < div >
140
+ { urlPreviewsEnabled
141
+ ? _t ( "room_settings|general|default_url_previews_on" )
142
+ : _t ( "room_settings|general|default_url_previews_off" ) }
143
+ </ div >
113
144
) ;
114
145
}
146
+
147
+ return previewsForRoom ;
115
148
}
0 commit comments