1- import React , { useEffect } from 'react' ;
2- import { Form , Input , Select , Tooltip } from 'antd' ;
1+ import React , { useEffect , useState } from 'react' ;
2+ import { Form , Input , Select , Tooltip , message } from 'antd' ;
33import {
44 QuestionCircleOutlined ,
55 InfoCircleOutlined ,
@@ -11,62 +11,121 @@ import useGenericSettings from '@app/hooks/useGenericSettings';
1111import { SettingsGroupType } from '@app/types/settings.types' ;
1212import BaseSettingsForm from './BaseSettingsForm' ;
1313import * as S from './Settings.styles' ;
14+ import { readToken } from '@app/services/localStorage.service' ;
15+ import config from '@app/config/config' ;
1416const { Option } = Select ;
1517const { TextArea } = Input ;
1618
1719const RelayInfoSettings : React . FC = ( ) => {
1820 const { settings, loading, error, fetchSettings, updateSettings, saveSettings } = useGenericSettings ( 'relay_info' ) ;
1921 const [ form ] = Form . useForm ( ) ;
22+ const [ isUploadingDefaultIcon , setIsUploadingDefaultIcon ] = useState ( false ) ;
2023
21- // Update form values when settings change
22- useEffect ( ( ) => {
23- if ( settings ) {
24- const currentOrigin = window . location . origin ;
25- const expectedDefaultIcon = `${ currentOrigin } /logo-dark-192.png` ;
26-
27- console . log ( 'RelayInfoSettings - Current settings:' , settings ) ;
28- console . log ( 'RelayInfoSettings - Expected default icon:' , expectedDefaultIcon ) ;
29- console . log ( 'RelayInfoSettings - Current relayicon:' , settings . relayicon ) ;
24+ // Function to automatically upload the bee logo
25+ const uploadDefaultIcon = async ( ) : Promise < string | null > => {
26+ try {
27+ setIsUploadingDefaultIcon ( true ) ;
3028
31- // Check if relay icon is empty and set default bee logo
32- const updatedSettings = { ...settings } ;
33- let shouldUpdateBackend = false ;
29+ // Get JWT token for authentication
30+ const token = readToken ( ) ;
31+ if ( ! token ) {
32+ console . error ( 'No authentication token available' ) ;
33+ return null ;
34+ }
35+
36+ // Fetch the bee logo from the public folder
37+ const response = await fetch ( '/logo-dark-192.png' ) ;
38+ if ( ! response . ok ) {
39+ throw new Error ( 'Failed to fetch bee logo' ) ;
40+ }
3441
35- if ( ! updatedSettings . relayicon || updatedSettings . relayicon . trim ( ) === '' ) {
36- console . log ( 'RelayInfoSettings - Setting default icon for empty field' ) ;
37- updatedSettings . relayicon = expectedDefaultIcon ;
38- shouldUpdateBackend = true ;
39- } else if ( updatedSettings . relayicon . endsWith ( '/logo-dark-192.png' ) ) {
40- // Check if the domain has changed from what's stored
41- const storedIcon = updatedSettings . relayicon ;
42- if ( storedIcon !== expectedDefaultIcon ) {
43- console . log ( 'RelayInfoSettings - Updating icon for domain change' ) ;
44- updatedSettings . relayicon = expectedDefaultIcon ;
45- shouldUpdateBackend = true ;
46- }
42+ const blob = await response . blob ( ) ;
43+ const file = new File ( [ blob ] , 'logo-dark-192.png' , { type : 'image/png' } ) ;
44+
45+ // Create form data for the API
46+ const formData = new FormData ( ) ;
47+ formData . append ( 'image' , file ) ;
48+ formData . append ( 'panel_url' , window . location . origin ) ;
49+
50+ // Upload using panel API
51+ const uploadResponse = await fetch ( `${ config . baseURL } /api/relay/icon` , {
52+ method : 'POST' ,
53+ headers : {
54+ 'Authorization' : `Bearer ${ token } ` ,
55+ } ,
56+ body : formData
57+ } ) ;
58+
59+ if ( ! uploadResponse . ok ) {
60+ const errorData = await uploadResponse . json ( ) . catch ( ( ) => ( { error : 'Upload failed' } ) ) ;
61+ throw new Error ( errorData . error || `HTTP error! status: ${ uploadResponse . status } ` ) ;
4762 }
63+
64+ const result = await uploadResponse . json ( ) ;
65+ console . log ( 'Default icon uploaded successfully:' , result . url ) ;
66+ return result . url ;
67+
68+ } catch ( error ) {
69+ console . error ( 'Failed to upload default icon:' , error ) ;
70+ return null ;
71+ } finally {
72+ setIsUploadingDefaultIcon ( false ) ;
73+ }
74+ } ;
75+
76+ // Update form values when settings change
77+ useEffect ( ( ) => {
78+ if ( settings ) {
79+ form . setFieldsValue ( settings ) ;
4880
49- form . setFieldsValue ( updatedSettings ) ;
81+ const currentOrigin = window . location . origin ;
82+ const isEmptyIcon = ! settings . relayicon || settings . relayicon . trim ( ) === '' ;
83+ const isLocalhostIcon = settings . relayicon && settings . relayicon . includes ( 'localhost' ) ;
84+ const isProductionDomain = ! currentOrigin . includes ( 'localhost' ) ;
5085
51- // Automatically update backend if domain changed
52- if ( shouldUpdateBackend ) {
53- console . log ( 'RelayInfoSettings - Updating backend with:' , expectedDefaultIcon ) ;
54- updateSettings ( { relayicon : expectedDefaultIcon } ) ;
55- // Auto-save the change to persist it
56- setTimeout ( ( ) => {
57- console . log ( 'RelayInfoSettings - Saving settings to backend' ) ;
58- saveSettings ( ) ;
59- } , 100 ) ;
86+ // Auto-upload default icon if:
87+ // 1. Icon is empty, OR
88+ // 2. We're on production domain but icon still points to localhost
89+ if ( isEmptyIcon || ( isLocalhostIcon && isProductionDomain ) ) {
90+ console . log ( 'Auto-uploading default icon...' , {
91+ isEmptyIcon,
92+ isLocalhostIcon,
93+ isProductionDomain,
94+ currentOrigin,
95+ currentIcon : settings . relayicon
96+ } ) ;
97+
98+ uploadDefaultIcon ( ) . then ( uploadedUrl => {
99+ if ( uploadedUrl ) {
100+ // Update the settings with the uploaded URL
101+ updateSettings ( { relayicon : uploadedUrl } ) ;
102+ form . setFieldsValue ( { relayicon : uploadedUrl } ) ;
103+
104+ if ( isEmptyIcon ) {
105+ message . success ( 'Default bee logo set automatically' ) ;
106+ } else {
107+ message . success ( 'Icon updated for new domain' ) ;
108+ }
109+ }
110+ } ) ;
60111 }
61112 }
62- } , [ settings , form , updateSettings , saveSettings ] ) ;
113+ } , [ settings , form , updateSettings ] ) ;
63114
64115 // Handle form value changes
65116 const handleValuesChange = ( changedValues : Partial < SettingsGroupType < 'relay_info' > > ) => {
66- // If relay icon is being cleared, set it to default
117+ // If relay icon is being cleared, automatically upload default
67118 if ( changedValues . relayicon !== undefined && ( ! changedValues . relayicon || changedValues . relayicon . trim ( ) === '' ) ) {
68- changedValues . relayicon = `${ window . location . origin } /logo-dark-192.png` ;
119+ uploadDefaultIcon ( ) . then ( uploadedUrl => {
120+ if ( uploadedUrl ) {
121+ updateSettings ( { relayicon : uploadedUrl } ) ;
122+ form . setFieldsValue ( { relayicon : uploadedUrl } ) ;
123+ message . success ( 'Default bee logo set automatically' ) ;
124+ }
125+ } ) ;
126+ return ; // Don't update with empty value
69127 }
128+
70129 updateSettings ( changedValues ) ;
71130 } ;
72131
0 commit comments