@@ -9,107 +9,144 @@ 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
+ import { InlineSpinner } from "@vector-im/compound-web" ;
14
15
15
- import { _t , _td } from "../../../languageHandler" ;
16
+ import { _t } from "../../../languageHandler" ;
16
17
import SettingsStore from "../../../settings/SettingsStore" ;
17
18
import dis from "../../../dispatcher/dispatcher" ;
18
- import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
19
19
import { Action } from "../../../dispatcher/actions" ;
20
20
import { SettingLevel } from "../../../settings/SettingLevel" ;
21
21
import SettingsFlag from "../elements/SettingsFlag" ;
22
22
import SettingsFieldset from "../settings/SettingsFieldset" ;
23
23
import AccessibleButton , { ButtonEvent } from "../elements/AccessibleButton" ;
24
+ import { useIsEncrypted } from "../../../hooks/useIsEncrypted.ts" ;
25
+ import { useMatrixClientContext } from "../../../contexts/MatrixClientContext.tsx" ;
26
+ import { useSettingValueAt } from "../../../hooks/useSettings.ts" ;
24
27
25
- interface IProps {
28
+ /**
29
+ * The URL preview settings for a room
30
+ */
31
+ interface UrlPreviewSettingsProps {
32
+ /**
33
+ * The room.
34
+ */
26
35
room : Room ;
27
36
}
28
37
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 = (
38
+ export function UrlPreviewSettings ( { room } : UrlPreviewSettingsProps ) : JSX . Element {
39
+ const { roomId } = room ;
40
+ const matrixClient = useMatrixClientContext ( ) ;
41
+ const isEncrypted = useIsEncrypted ( matrixClient , room ) ;
42
+ const isLoading = isEncrypted === null ;
43
+
44
+ return (
45
+ < SettingsFieldset
46
+ legend = { _t ( "room_settings|general|url_previews_section" ) }
47
+ description = { ! isLoading && < Description isEncrypted = { isEncrypted } /> }
48
+ >
49
+ { isLoading ? (
50
+ < InlineSpinner />
51
+ ) : (
52
+ < >
53
+ < PreviewsForRoom isEncrypted = { isEncrypted } roomId = { roomId } />
74
54
< SettingsFlag
75
- name = " urlPreviewsEnabled"
76
- level = { SettingLevel . ROOM }
55
+ name = { isEncrypted ? "urlPreviewsEnabled_e2ee" : " urlPreviewsEnabled"}
56
+ level = { SettingLevel . ROOM_DEVICE }
77
57
roomId = { roomId }
78
- isExplicit = { true }
79
58
/>
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
- </ >
106
- ) ;
59
+ </ >
60
+ ) }
61
+ </ SettingsFieldset >
62
+ ) ;
63
+ }
64
+
65
+ /**
66
+ * Click handler for the user settings link
67
+ * @param e
68
+ */
69
+ function onClickUserSettings ( e : ButtonEvent ) : void {
70
+ e . preventDefault ( ) ;
71
+ e . stopPropagation ( ) ;
72
+ dis . fire ( Action . ViewUserSettings ) ;
73
+ }
74
+
75
+ /**
76
+ * The description for the URL preview settings
77
+ */
78
+ interface DescriptionProps {
79
+ /**
80
+ * Whether the room is encrypted
81
+ */
82
+ isEncrypted : boolean ;
83
+ }
84
+
85
+ function Description ( { isEncrypted } : DescriptionProps ) : JSX . Element {
86
+ const urlPreviewsEnabled = useSettingValueAt ( SettingLevel . ACCOUNT , "urlPreviewsEnabled" ) ;
87
+
88
+ let previewsForAccount : ReactNode | undefined ;
89
+ if ( isEncrypted ) {
90
+ previewsForAccount = _t ( "room_settings|general|url_preview_encryption_warning" ) ;
91
+ } else {
92
+ const button = {
93
+ a : ( sub : string ) => (
94
+ < AccessibleButton kind = "link_inline" onClick = { onClickUserSettings } >
95
+ { sub }
96
+ </ AccessibleButton >
97
+ ) ,
98
+ } ;
107
99
108
- return (
109
- < SettingsFieldset legend = { _t ( "room_settings|general|url_previews_section" ) } description = { description } >
110
- { previewsForRoom }
111
- { previewsForRoomAccount }
112
- </ SettingsFieldset >
100
+ previewsForAccount = urlPreviewsEnabled
101
+ ? _t ( "room_settings|general|user_url_previews_default_on" , { } , button )
102
+ : _t ( "room_settings|general|user_url_previews_default_off" , { } , button ) ;
103
+ }
104
+
105
+ return (
106
+ < >
107
+ < p > { _t ( "room_settings|general|url_preview_explainer" ) } </ p >
108
+ < p > { previewsForAccount } </ p >
109
+ </ >
110
+ ) ;
111
+ }
112
+
113
+ /**
114
+ * The description for the URL preview settings
115
+ */
116
+ interface PreviewsForRoomProps {
117
+ /**
118
+ * Whether the room is encrypted
119
+ */
120
+ isEncrypted : boolean ;
121
+ /**
122
+ * The room ID
123
+ */
124
+ roomId : string ;
125
+ }
126
+
127
+ function PreviewsForRoom ( { isEncrypted, roomId } : PreviewsForRoomProps ) : JSX . Element | null {
128
+ const urlPreviewsEnabled = useSettingValueAt (
129
+ SettingLevel . ACCOUNT ,
130
+ "urlPreviewsEnabled" ,
131
+ roomId ,
132
+ /*explicit=*/ true ,
133
+ ) ;
134
+ if ( isEncrypted ) return null ;
135
+
136
+ let previewsForRoom : ReactNode ;
137
+ if ( SettingsStore . canSetValue ( "urlPreviewsEnabled" , roomId , SettingLevel . ROOM ) ) {
138
+ previewsForRoom = (
139
+ < SettingsFlag name = "urlPreviewsEnabled" level = { SettingLevel . ROOM } roomId = { roomId } isExplicit = { true } />
140
+ ) ;
141
+ } else {
142
+ previewsForRoom = (
143
+ < div >
144
+ { urlPreviewsEnabled
145
+ ? _t ( "room_settings|general|default_url_previews_on" )
146
+ : _t ( "room_settings|general|default_url_previews_off" ) }
147
+ </ div >
113
148
) ;
114
149
}
150
+
151
+ return previewsForRoom ;
115
152
}
0 commit comments