@@ -12,29 +12,28 @@ import { getCounter, postCounter } from "./utilities/requests";
1212import Menu from "./components/Animated/Menu/Menu" ;
1313
1414function App ( ) {
15- const countapiKey = "f3dceeba-1841-42cf-b76c-26e9026dc0cf" ;
15+ const countapiKey = "f3dceeba-1841-42cf-b76c-26e9026dc0cf" ; // Yes, it's an API key but it's not a secret. It's used to identify the counter.
1616 const countapiNamespace = "spectrocloud.com" ;
1717 const [ isLogoVisible , setIsLogoVisible ] = useState ( false ) ;
1818 const [ clickCount , setClickCount ] = useState ( 0 ) ;
1919 const [ connected , setConnected ] = useState ( false ) ;
2020 const [ firstLoad , setFirstLoad ] = useState ( true ) ;
2121 let API_URI = env . REACT_APP_API_URI ;
2222 let API_VERSION = env . REACT_APP_API_VERSION ;
23-
24- // This is the default value for the API_URI env variable
25- // In this scenario, either the global API is used OR local storage is used
26- if ( API_URI === "/" ) {
23+ let REVERSE_PROXY = env . REACT_APP_REVERSE_PROXY ;
24+
25+ if ( API_URI == "undefined" ) {
2726 API_URI = ""
2827 }
2928
30- // If no API_URI is provided, then assume the user is running the API locally
31- if ( API_URI === "" ) {
32- API_URI = "http://localhost:3000"
33- }
29+ if ( REVERSE_PROXY ) {
30+ API_URI = "http://0.0.0.0:3000"
31+ }
3432
3533 if ( API_VERSION === "" || API_VERSION == "undefined" ) {
3634 API_VERSION = 1
3735 }
36+
3837 useEffect ( ( ) => {
3938 // Checks if internet is connected by attempting to load an image
4039 const image = new Image ( ) ;
@@ -47,18 +46,8 @@ function App() {
4746 } , [ connected ] )
4847
4948 async function loadCount ( ) {
50- // If not connected to the internet, fallback to using local storage for count
51- // Otherwise, use Count API
52- if ( connected && ! API_URI || API_URI === "undefined" ) {
53- try {
54- const count = await countapi . get ( countapiNamespace , countapiKey ) ;
55- setClickCount ( count ?. value || clickCount ) ;
56- } catch ( error ) {
57- setClickCount ( clickCount + 1 ) ;
58- localStorage . setItem ( "clickCount" , clickCount ) ;
59- }
60- }
61-
49+
50+ // If an API URI is provided, use that to get the count
6251 if ( API_URI ) {
6352 try {
6453 const databaseCount = await getCounter ( API_URI , API_VERSION ) ;
@@ -69,6 +58,20 @@ function App() {
6958 }
7059 }
7160
61+
62+ // If connected to the internet, and no API URI is provided then use the Global Counter API.
63+ // Fallback to using local storage for count if the Global Counter API is unavailable.
64+ if ( connected && ! API_URI || API_URI === "undefined" ) {
65+ try {
66+ const count = await countapi . get ( countapiNamespace , countapiKey ) ;
67+ setClickCount ( count ?. value || clickCount ) ;
68+ } catch ( error ) {
69+ setClickCount ( clickCount + 1 ) ;
70+ localStorage . setItem ( "clickCount" , clickCount ) ;
71+ }
72+ }
73+
74+ // If not connected to the internet, and no API URI is provided then use local storage for count.
7275 if ( ! connected && ! API_URI || API_URI === "undefined" ) {
7376 const count = localStorage . getItem ( "clickCount" ) ;
7477 setClickCount ( parseInt ( count ) || 0 ) ;
@@ -77,25 +80,28 @@ function App() {
7780 }
7881
7982 async function countUp ( ) {
80- if ( connected && ! API_URI || API_URI === "undefined" ) {
81- try {
82- await countapi . update ( countapiNamespace , countapiKey , + 1 ) ;
83- await loadCount ( ) ;
84- } catch ( error ) {
85- console . log ( error ) ;
86- }
87- }
8883
84+ // If an API URI is provided, use that to update the count
8985 if ( API_URI ) {
9086 try {
9187 const databaseCount = await postCounter ( API_URI , API_VERSION ) ;
9288 setClickCount ( databaseCount ) ;
9389 } catch ( error ) {
9490 alert ( `Error: Unable to connect to database on ${ API_URI } . Please try again later. 😢` )
9591 }
96-
9792 }
98-
93+
94+ // If connected to the internet, and no API URI is provided then use the Global Counter API.
95+ if ( connected && ! API_URI || API_URI === "undefined" ) {
96+ try {
97+ await countapi . update ( countapiNamespace , countapiKey , + 1 ) ;
98+ await loadCount ( ) ;
99+ } catch ( error ) {
100+ console . log ( error ) ;
101+ }
102+ }
103+
104+ // If not connected to the internet, and no API URI is provided then use local storage for count.
99105 if ( ! connected && ! API_URI || API_URI === "undefined" ) {
100106 setClickCount ( clickCount + 1 ) ;
101107 localStorage . setItem ( "clickCount" , clickCount ) ;
0 commit comments