1- import React , { useContext , useLayoutEffect , useState } from "react" ;
1+ import { useCallback , useContext , useLayoutEffect , useState } from "react" ;
22import Card from "../shared/Card" ;
33import Input from "../shared/Input" ;
44import Toggle from "../shared/Toggle" ;
@@ -14,38 +14,20 @@ interface Profile {
1414}
1515
1616interface ProfileSettingsProps {
17- // eslint-disable-next-line no-unused-vars
1817 stateHasChanged : ( isActive : boolean , streamKey : string ) => void ;
1918}
2019
2120export default function ProfileSettings ( props : ProfileSettingsProps ) {
2221 const { locale } = useContext ( LocaleContext )
22+ const { stateHasChanged } = props
2323 const streamKey = location . pathname . split ( '/' ) . pop ( )
2424
2525 const [ profileType , setProfileType ] = useState < "Public" | "Reserved" > ( "Public" )
2626 const [ isPublic , setIsPublic ] = useState < "Public" | "Private" > ( "Public" )
2727 const [ motd , setMotd ] = useState < string > ( "" )
2828
29- const updateSettings = ( ) => {
30- fetch ( `/api/whip/profile` , {
31- method : "POST" ,
32- headers : {
33- Authorization : `Bearer ${ toBase64Utf8 ( streamKey ) } ` ,
34- } ,
35- body : JSON . stringify ( {
36- motd : motd ,
37- isPublic : isPublic === "Public"
38- } ) ,
39- } ) . then ( ( result ) => {
40- if ( result . status > 400 && result . status < 500 ) {
41- return ;
42- }
43-
44- getSettings ( ) ;
45- } ) ;
46- } ;
47- const getSettings = ( ) => {
48- fetch ( `/api/whip/profile` , {
29+ const getSettings = useCallback ( ( ) => {
30+ return fetch ( `/api/whip/profile` , {
4931 method : "GET" ,
5032 headers : {
5133 Authorization : `Bearer ${ toBase64Utf8 ( streamKey ) } ` ,
@@ -56,7 +38,7 @@ export default function ProfileSettings(props: ProfileSettingsProps) {
5638 }
5739
5840 return result . json ( )
59- } ) . then ( ( result : Profile ) => {
41+ } ) . then ( ( result : Profile | undefined ) => {
6042
6143 if ( result === undefined ) {
6244 setProfileType ( ( ) => "Public" )
@@ -66,14 +48,32 @@ export default function ProfileSettings(props: ProfileSettingsProps) {
6648 setProfileType ( ( ) => "Reserved" )
6749 setIsPublic ( ( ) => result . isPublic ? "Public" : "Private" )
6850 setMotd ( ( ) => result . motd )
69- props . stateHasChanged ?.( result . isActive , result . streamKey )
51+ stateHasChanged ?.( result . isActive , result . streamKey )
52+ } ) ;
53+ } , [ stateHasChanged , streamKey ] )
54+
55+ const updateSettings = useCallback ( ( ) => {
56+ fetch ( `/api/whip/profile` , {
57+ method : "POST" ,
58+ headers : {
59+ Authorization : `Bearer ${ toBase64Utf8 ( streamKey ) } ` ,
60+ } ,
61+ body : JSON . stringify ( {
62+ motd : motd ,
63+ isPublic : isPublic === "Public"
64+ } ) ,
65+ } ) . then ( ( result ) => {
66+ if ( result . status > 400 && result . status < 500 ) {
67+ return ;
68+ }
69+
70+ void getSettings ( ) ;
7071 } ) ;
71- } ;
72+ } , [ getSettings , isPublic , motd , streamKey ] ) ;
7273
7374 useLayoutEffect ( ( ) => {
74- getSettings ( )
75- // eslint-disable-next-line react-hooks/exhaustive-deps
76- } , [ ] )
75+ void getSettings ( )
76+ } , [ getSettings ] )
7777
7878 if ( profileType === "Public" ) {
7979 return < > </ >
0 commit comments