import SwiftUI import Observation #if os(iOS) import SafariServices #else import AppKit #endif /// Fixed task/settings routes and checked forge links share the same opener. enum SettingsNavigation { static func openChanges(origin: URL, url: URL, opener: (URL) -> Void) { guard url.user == nil, url.password == nil else { return } let sameOrigin = url.scheme == origin.scheme && url.host == origin.host && url.port == origin.port let forge = url.scheme == "https" && url.host == "amadan.net" && (url.port == nil || url.port == 443) guard sameOrigin || forge, ["http", "https"].contains(url.scheme) else { return } opener(url) } static func open(origin: URL, path: String, opener: (URL) -> Void) { let fixedSettings = ["/conversation/settings/machines", "/conversation/settings/connections", "/conversation/settings/connections/add"].contains(path) let taskPage = path.range(of: #"^/tasks/[A-Za-z0-9_-]+(?:/move|/preview)?\z"#, options: .regularExpression) != nil guard fixedSettings || taskPage, var url = URLComponents(url: origin, resolvingAgainstBaseURL: false) else { return } url.path = path; url.query = nil; url.fragment = nil if let destination = url.url { opener(destination) } } } @MainActor @Observable final class SettingsPresentation { struct Page: Identifiable { let url: URL; var id: URL { url } } var page: Page? func open(origin: URL, path: String) { SettingsNavigation.open(origin: origin, path: path, opener: present) } func openChanges(origin: URL, url: URL) { SettingsNavigation.openChanges(origin: origin, url: url, opener: present) } private func present(_ url: URL) { #if os(macOS) NSWorkspace.shared.open(url) #else page = Page(url: url) #endif } } /// Owned by the signed-in tab container, which survives changing task pauses. struct SettingsSheetPresenter: ViewModifier { let session: EquipoSession func body(content: Content) -> some View { #if os(iOS) @Bindable var presentation = session.settings content.sheet(item: $presentation.page, onDismiss: { Task { await session.refresh() } }) { page in SafariSettingsView(url: page.url).ignoresSafeArea() } #else content #endif } } #if os(iOS) private struct SafariSettingsView: UIViewControllerRepresentable { let url: URL func makeUIViewController(context: Context) -> SFSafariViewController { SFSafariViewController(url: url) } func updateUIViewController(_ controller: SFSafariViewController, context: Context) {} } #endif