4
4
* See License.AGPL.txt in the project root for license information.
5
5
*/
6
6
7
- import { useCallback , useContext , useEffect , useState } from "react" ;
7
+ import { useCallback , useContext , useState } from "react" ;
8
8
import { getGitpodService } from "../service/service" ;
9
9
import { UserContext } from "../user-context" ;
10
10
import { trackEvent } from "../Analytics" ;
@@ -14,39 +14,50 @@ import { ThemeSelector } from "../components/ThemeSelector";
14
14
import Alert from "../components/Alert" ;
15
15
import { Link } from "react-router-dom" ;
16
16
import { Heading2 , Subheading } from "../components/typography/headings" ;
17
+ import { useUserMaySetTimeout } from "../data/current-user/may-set-timeout-query" ;
18
+ import { Button } from "../components/Button" ;
17
19
18
20
export default function Preferences ( ) {
19
- const { user } = useContext ( UserContext ) ;
21
+ const { user, setUser } = useContext ( UserContext ) ;
22
+ const maySetTimeout = useUserMaySetTimeout ( ) ;
20
23
21
24
const [ dotfileRepo , setDotfileRepo ] = useState < string > ( user ?. additionalData ?. dotfileRepo || "" ) ;
22
- const actuallySetDotfileRepo = async ( value : string ) => {
23
- const additionalData = user ?. additionalData || { } ;
24
- const prevDotfileRepo = additionalData . dotfileRepo || "" ;
25
- additionalData . dotfileRepo = value ;
26
- await getGitpodService ( ) . server . updateLoggedInUser ( { additionalData } ) ;
27
- if ( value !== prevDotfileRepo ) {
28
- trackEvent ( "dotfile_repo_changed" , {
29
- previous : prevDotfileRepo ,
30
- current : value ,
31
- } ) ;
32
- }
33
- } ;
34
-
35
25
const [ workspaceTimeout , setWorkspaceTimeout ] = useState < string > ( user ?. additionalData ?. workspaceTimeout ?? "" ) ;
36
- const actuallySetWorkspaceTimeout = useCallback ( async ( value : string ) => {
37
- try {
38
- await getGitpodService ( ) . server . updateWorkspaceTimeoutSetting ( { workspaceTimeout : value } ) ;
39
- } catch ( e ) {
40
- alert ( "Cannot set custom workspace timeout: " + e . message ) ;
41
- }
42
- } , [ ] ) ;
43
26
44
- const [ allowConfigureWorkspaceTimeout , setAllowConfigureWorkspaceTimeout ] = useState < boolean > ( false ) ;
45
- useEffect ( ( ) => {
46
- getGitpodService ( )
47
- . server . maySetTimeout ( )
48
- . then ( ( r ) => setAllowConfigureWorkspaceTimeout ( r ) ) ;
49
- } , [ ] ) ;
27
+ const saveDotfileRepo = useCallback (
28
+ async ( e ) => {
29
+ e . preventDefault ( ) ;
30
+
31
+ const prevDotfileRepo = user ?. additionalData ?. dotfileRepo || "" ;
32
+ const additionalData = {
33
+ ...( user ?. additionalData || { } ) ,
34
+ dotfileRepo,
35
+ } ;
36
+ const updatedUser = await getGitpodService ( ) . server . updateLoggedInUser ( { additionalData } ) ;
37
+ setUser ( updatedUser ) ;
38
+
39
+ if ( dotfileRepo !== prevDotfileRepo ) {
40
+ trackEvent ( "dotfile_repo_changed" , {
41
+ previous : prevDotfileRepo ,
42
+ current : dotfileRepo ,
43
+ } ) ;
44
+ }
45
+ } ,
46
+ [ dotfileRepo , setUser , user ?. additionalData ] ,
47
+ ) ;
48
+
49
+ const saveWorkspaceTimeout = useCallback (
50
+ async ( e ) => {
51
+ e . preventDefault ( ) ;
52
+
53
+ try {
54
+ await getGitpodService ( ) . server . updateWorkspaceTimeoutSetting ( { workspaceTimeout : workspaceTimeout } ) ;
55
+ } catch ( e ) {
56
+ alert ( "Cannot set custom workspace timeout: " + e . message ) ;
57
+ }
58
+ } ,
59
+ [ workspaceTimeout ] ,
60
+ ) ;
50
61
51
62
return (
52
63
< div >
@@ -69,7 +80,7 @@ export default function Preferences() {
69
80
70
81
< Heading2 className = "mt-12" > Dotfiles</ Heading2 >
71
82
< Subheading > Customize workspaces using dotfiles.</ Subheading >
72
- < div className = "mt-4 max-w-xl" >
83
+ < form className = "mt-4 max-w-xl" onSubmit = { saveDotfileRepo } >
73
84
< h4 > Repository URL</ h4 >
74
85
< span className = "flex" >
75
86
< input
@@ -79,9 +90,9 @@ export default function Preferences() {
79
90
placeholder = "e.g. https://github.com/username/dotfiles"
80
91
onChange = { ( e ) => setDotfileRepo ( e . target . value ) }
81
92
/>
82
- < button className = "secondary ml-2" onClick = { ( ) => actuallySetDotfileRepo ( dotfileRepo ) } >
93
+ < Button type = "secondary" className = " ml-2">
83
94
Save Changes
84
- </ button >
95
+ </ Button >
85
96
</ span >
86
97
< div className = "mt-1" >
87
98
< p className = "text-gray-500 dark:text-gray-400" >
@@ -90,14 +101,14 @@ export default function Preferences() {
90
101
clone and install your dotfiles for every new workspace.
91
102
</ p >
92
103
</ div >
93
- </ div >
104
+ </ form >
94
105
95
106
< Heading2 className = "mt-12" > Timeouts</ Heading2 >
96
107
< Subheading > Workspaces will stop after a period of inactivity without any user input.</ Subheading >
97
108
< div className = "mt-4 max-w-xl" >
98
109
< h4 > Default Workspace Timeout</ h4 >
99
110
100
- { ! allowConfigureWorkspaceTimeout && (
111
+ { ! maySetTimeout . isLoading && maySetTimeout . data === false && (
101
112
< Alert type = "message" >
102
113
Upgrade organization{ " " }
103
114
< Link to = "/billing" className = "gp-link" >
@@ -107,32 +118,27 @@ export default function Preferences() {
107
118
</ Alert >
108
119
) }
109
120
110
- { allowConfigureWorkspaceTimeout && (
111
- < >
121
+ { maySetTimeout . data === true && (
122
+ < form onSubmit = { saveWorkspaceTimeout } >
112
123
< span className = "flex" >
113
124
< input
114
125
type = "text"
115
126
className = "w-96 h-9"
116
127
value = { workspaceTimeout }
117
- disabled = { ! allowConfigureWorkspaceTimeout }
118
128
placeholder = "e.g. 30m"
119
129
onChange = { ( e ) => setWorkspaceTimeout ( e . target . value ) }
120
130
/>
121
- < button
122
- className = "secondary ml-2"
123
- disabled = { ! allowConfigureWorkspaceTimeout }
124
- onClick = { ( ) => actuallySetWorkspaceTimeout ( workspaceTimeout ) }
125
- >
131
+ < Button type = "secondary" className = "ml-2" >
126
132
Save Changes
127
- </ button >
133
+ </ Button >
128
134
</ span >
129
135
< div className = "mt-1" >
130
136
< p className = "text-gray-500 dark:text-gray-400" >
131
137
Use minutes or hours, like < span className = "font-semibold" > 30m</ span > or{ " " }
132
138
< span className = "font-semibold" > 2h</ span > .
133
139
</ p >
134
140
</ div >
135
- </ >
141
+ </ form >
136
142
) }
137
143
</ div >
138
144
</ PageWithSettingsSubMenu >
0 commit comments