| 181 | * Returns a cleanup function to remove the event listener. |
| 182 | */ |
| 183 | export function initSimulateButton(onToggle: SimulateCallback): () => void { |
| 184 | const btn = document.getElementById("simulate-btn") as HTMLButtonElement | null; |
| 185 | if (!btn) { |
| 186 | return () => {}; |
| 187 | } |
| 188 | |
| 189 | let isSimulatingFailure = false; |
| 190 | |
| 191 | function handleClick() { |
| 192 | isSimulatingFailure = !isSimulatingFailure; |
| 193 | |
| 194 | if (isSimulatingFailure) { |
| 195 | btn!.textContent = LABEL_RESTORE_CONNECTION; |
| 196 | btn!.classList.add("simulate-btn--restoring"); |
| 197 | onToggle("connectionLost"); |
| 198 | } else { |
| 199 | btn!.textContent = LABEL_SIMULATE_FAILURE; |
| 200 | btn!.classList.remove("simulate-btn--restoring"); |
| 201 | onToggle("connectionRestored"); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | btn.addEventListener("click", handleClick); |
| 206 | |
| 207 | // Return cleanup so main.ts can remove the listener on dispose |
| 208 | return () => { |
| 209 | btn.removeEventListener("click", handleClick); |
| 210 | }; |
| 211 | } |