import SwiftUI import Combine @MainActor @Observable final class AppState { let auth: AuthStore var wsStatus: WebSocketStatus = .idle var webSocket: TaskWebSocket? var pendingDeepLink: DeepLink? init(auth: AuthStore = AuthStore()) { self.auth = auth if ScreenshotMode.isEnabled { auth.seedScreenshotData() } } func ensureWebSocket() { guard !ScreenshotMode.isEnabled else { wsStatus = .online return } guard webSocket == nil, let client = auth.client else { return } let ws = TaskWebSocket(profile: client.profile, apiKey: client.apiKey) ws.onStatusChange = { [weak self] s in Task { @MainActor in self?.wsStatus = s } } ws.connect() webSocket = ws } func teardownWebSocket() { webSocket?.disconnect() webSocket = nil wsStatus = .idle } } enum DeepLink: Hashable { case task(id: String) case directive(id: String) case contract(id: String) init?(url: URL) { guard url.scheme == "makima", let host = url.host else { return nil } let id = url.pathComponents.dropFirst().first ?? "" guard !id.isEmpty else { return nil } switch host { case "task": self = .task(id: id) case "directive": self = .directive(id: id) case "contract": self = .contract(id: id) default: return nil } } }