1- const WORKER_URL = '' ; // Relative to the same domain in CF Pages
2-
3- document . addEventListener ( 'DOMContentLoaded' , loadIncidentList ) ;
1+ // During local development (static server on localhost), point API calls to the local Worker.
2+ const WORKER_URL = ( typeof window !== 'undefined' && ( window . location . hostname === 'localhost' || window . location . hostname === '127.0.0.1' ) )
3+ ? 'http://127.0.0.1:8787'
4+ : '' ;
45
56async function updateServiceStatus ( ) {
67 const serviceId = document . getElementById ( 'service-id' ) . value ;
@@ -53,7 +54,6 @@ async function postIncident() {
5354 setFeedback ( feedback , 'Incident posted successfully!' , 'success' ) ;
5455 document . getElementById ( 'inc-title' ) . value = '' ;
5556 document . getElementById ( 'inc-desc' ) . value = '' ;
56- await loadIncidentList ( ) ;
5757 } else {
5858 setFeedback ( feedback , `Failed: ${ res . statusText } ` , 'error' ) ;
5959 }
@@ -62,90 +62,6 @@ async function postIncident() {
6262 }
6363}
6464
65- async function loadIncidentList ( ) {
66- const listEl = document . getElementById ( 'admin-incidents-list' ) ;
67- if ( ! listEl ) return ;
68-
69- listEl . innerHTML = '<p class="loading-text">Loading investigation entries...</p>' ;
70-
71- try {
72- const res = await fetch ( `${ WORKER_URL } /api/status` ) ;
73- if ( ! res . ok ) {
74- listEl . innerHTML = '<p class="loading-text">Unable to load entries right now.</p>' ;
75- return ;
76- }
77-
78- const data = await res . json ( ) ;
79- const incidents = Array . isArray ( data . incidents ) ? data . incidents : [ ] ;
80-
81- if ( incidents . length === 0 ) {
82- listEl . innerHTML = '<p class="loading-text">No entries in the investigation list.</p>' ;
83- return ;
84- }
85-
86- listEl . innerHTML = incidents . map ( ( incident ) => {
87- const createdLabel = incident . createdAt ? new Date ( incident . createdAt ) . toLocaleString ( ) : 'Unknown time' ;
88- return `
89- <article class="admin-incident-item">
90- <div class="admin-incident-meta">
91- <p class="admin-incident-title">${ escapeHtml ( incident . title || 'Untitled' ) } </p>
92- <p class="admin-incident-subtitle">${ escapeHtml ( incident . status || 'Unknown' ) } • ${ escapeHtml ( createdLabel ) } </p>
93- </div>
94- <button class="btn btn-danger" onclick="removeIncident('${ encodeURIComponent ( incident . id || '' ) } ')">Remove</button>
95- </article>
96- ` ;
97- } ) . join ( '' ) ;
98- } catch ( e ) {
99- listEl . innerHTML = '<p class="loading-text">Unable to load entries right now.</p>' ;
100- }
101- }
102-
103- async function removeIncident ( encodedIncidentId ) {
104- const incidentId = decodeURIComponent ( encodedIncidentId || '' ) ;
105- const feedback = document . getElementById ( 'remove-incident-feedback' ) ;
106- if ( ! incidentId ) {
107- setFeedback ( feedback , 'Missing incident ID.' , 'error' ) ;
108- return ;
109- }
110-
111- setFeedback ( feedback , '' , null ) ;
112-
113- try {
114- const res = await fetch ( `${ WORKER_URL } /api/admin/remove_incident` , {
115- method : 'POST' ,
116- headers : {
117- 'Content-Type' : 'application/json'
118- } ,
119- body : JSON . stringify ( { incidentId } )
120- } ) ;
121-
122- if ( res . ok ) {
123- setFeedback ( feedback , 'Investigation entry removed.' , 'success' ) ;
124- await loadIncidentList ( ) ;
125- } else {
126- let errorMessage = `Failed: ${ res . statusText } ` ;
127- try {
128- const body = await res . json ( ) ;
129- if ( body ?. error ) errorMessage = `Failed: ${ body . error } ` ;
130- } catch {
131- // keep fallback message
132- }
133- setFeedback ( feedback , errorMessage , 'error' ) ;
134- }
135- } catch ( e ) {
136- setFeedback ( feedback , `Error: ${ e . message } ` , 'error' ) ;
137- }
138- }
139-
140- function escapeHtml ( value ) {
141- return String ( value )
142- . replaceAll ( '&' , '&' )
143- . replaceAll ( '<' , '<' )
144- . replaceAll ( '>' , '>' )
145- . replaceAll ( '"' , '"' )
146- . replaceAll ( "'" , ''' ) ;
147- }
148-
14965function setFeedback ( element , message , type ) {
15066 element . textContent = message ;
15167 element . classList . remove ( 'feedback-success' , 'feedback-error' ) ;
0 commit comments