Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

18.9 KB
Raw Blame
import CodyncKit
import CodyncUI
import SwiftUI
import WidgetKit

struct RootView: View {
    @Environment(AppStore.self) private var app
    @Environment(AccountStore.self) private var accounts
    @Environment(AccountSession.self) private var account
    @AppStorage("onboardingCompleted") private var onboardingCompleted = false
    /// Chose to go on without signing in (docs/plans/optional-sign-in.md); signing in or starting over ends it.
    @AppStorage("withoutAccount") private var withoutAccount = false
    @Environment(\.accessibilityReduceMotion) private var reduceMotion
    /// First launch only: past the welcome, on to setup.
    @State private var welcomed = false
    /// In the pairing flow by choice: a computer turning up in the account doesn't pull it back to the choice.
    @State private var pairingNew = false
    /// The account computer the choice page is asking for access.
    @State private var asking: CloudComputer?

    /// The open bot, as a NavigationStack path.
    private var path: Binding<[BotReference]> {
        Binding(get: { accounts.selection.map { [$0] } ?? [] }, set: { accounts.selection = $0.last })
    }

    var body: some View {
        Group {
            if mustSignIn {
                // Computers paired signed out wait in the signed-out context.
                WelcomeView(signedOutComputers: accounts.computers.count) {
                    Analytics.shared.capture(.continuedWithoutAccount)
                    animateSetup { withoutAccount = true }
                }
                .transition(.opacity)
            } else if !onboardingCompleted && accounts.computers.isEmpty {
                setup
            } else {
                tabs
            }
        }
        .background(Palette.background)
        .reviewInvitation()
        .onChange(of: account.isSignedIn) { _, signedIn in
            if signedIn { withoutAccount = false }
        }
        .onChange(of: accounts.computers.isEmpty, initial: true) { _, empty in
            // Reaching a computer (paired, or approved through the account) or explicitly skipping completes
            // setup. Keep this device-level milestone across account changes and unpairing.
            if !empty { completeOnboarding(via: accounts.approvedAccess.isEmpty ? .paired : .account) }
        }
        .codyncDialog("Something went wrong", isPresented: errorShown, message: errorMessage, cancel: "OK", inPlace: true) { [] }
        .codyncOverlay(isPresented: Binding(get: { screenTarget.wrappedValue != nil }, set: { if !$0 { screenTarget.wrappedValue = nil } })) { close in
            if let target = screenTarget.wrappedValue, let store = accounts.store(for: target.computerId) {
                ScreenView(watching: target.request.watching, close: close)
                    .environment(store)
                    .id(target.id)
            }
        }
        .codyncSheet(isPresented: Bindable(app).showComputers) { ComputersView() }
        // Here, not on the choice page: approval replaces that page while this sheet says so.
        .codyncSheet(item: $asking) { computer in
            AccessRequestView(computer: computer, pending: accounts.pendingAccess[computer.computerId])
        }
        .codyncSheet(item: Binding(get: { app.marketplace.map(MarketplaceTarget.init) }, set: { app.marketplace = $0?.computerId })) { target in
            // Each computer has its own; it's opened from that computer's heading or row.
            if let store = accounts.store(for: target.computerId) {
                MarketplaceView()
                    .environment(store)
            }
        }
    }

    /// First run, until a computer is reached or setup is skipped.
    @ViewBuilder
    private var setup: some View {
        if !welcomed && !account.isSignedIn && !withoutAccount {
            WelcomeView { animateSetup { welcomed = true } }
                .transition(.move(edge: .leading).combined(with: .opacity))
        } else if account.isSignedIn && !accounts.cloudListLoaded {
            // Whether the account has computers decides the first page: none flashes by before that's known.
            Spinner(size: 28)
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .transition(.opacity)
        } else if !pairingNew && !askableComputers.isEmpty {
            ChooseComputerView(computers: askableComputers, ask: { asking = $0 },
                               pairNew: { animateSetup { pairingNew = true } }, skip: { completeOnboarding(via: .skipped) }) {
                AccountSwitcherButton()
            }
            .transition(.move(edge: .leading).combined(with: .opacity))
        } else {
            PairingView(onSkip: { completeOnboarding(via: .skipped) }, onContinue: { pairingNew = true }) {
                if !askableComputers.isEmpty {
                    BackButton { animateSetup { pairingNew = false } }
                } else if account.isSignedIn {
                    AccountSwitcherButton()
                } else {
                    BackButton {
                        animateSetup {
                            welcomed = false
                            withoutAccount = false
                        }
                    }
                }
            }
            .transition(.move(edge: .trailing).combined(with: .opacity))
        }
    }

    /// The system tab bar; every tab stays alive (scroll position, open thread) and the bar hides while a
    /// thread is open.
    private var tabs: some View {
        TabView(selection: Bindable(app).tab) {
            Tab(L10n.tr("Bots"), systemImage: "bubble.left.and.bubble.right.fill", value: AppTab.bots) {
                NavigationStack(path: path) {
                    BotListView()
                        .safeAreaInset(edge: .bottom, spacing: 0) { AccessBanners() }
                        .navigationDestination(for: BotReference.self) { ref in
                            ChatScreen(ref: ref).toolbar(.hidden, for: .tabBar)
                        }
                }
            }
            Tab(L10n.tr("State"), systemImage: "rectangle.stack.fill", value: AppTab.state) {
                StateTab()
                    .safeAreaInset(edge: .bottom, spacing: 0) { AccessBanners() }
            }
            Tab(L10n.tr("Settings"), systemImage: "gearshape", value: AppTab.settings) {
                SettingsView()
                    .safeAreaInset(edge: .bottom, spacing: 0) { AccessBanners() }
            }
        }
        .tint(Palette.text)
        .tracked(.screenViewed(screen))
        // Opening a bot (notification, widget, link) always lands on the Bots tab.
        .onChange(of: accounts.selection) { _, ref in
            if ref != nil { app.tab = .bots }
        }
    }

    private var screen: AnalyticsEvent.Screen {
        switch app.tab {
        case .bots: .bots
        case .state: .state
        case .settings: .settings
        }
    }

    /// Signed out, a build with accounts offers signing in or going on without one. While Clerk restores, the last
    /// signed-in account's context stays on screen, so an offline launch still reaches its computers.
    private var mustSignIn: Bool {
        account.isConfigured && !account.isSignedIn && !withoutAccount && (account.isReady || accounts.accountId == nil)
    }

    /// What the choice page offers: an older copy of a computer doesn't count as one more.
    private var askableComputers: [CloudComputer] {
        accounts.cloudComputers.filter { !accounts.isOlderCopy($0) }
    }

    private func animateSetup(_ change: () -> Void) {
        withAnimation(Motion.reduced(Motion.layout, reduceMotion), change)
    }

    /// Once per setup (Start over begins a new one): the first computer reached, or Skip.
    private func completeOnboarding(via: AnalyticsEvent.OnboardingVia) {
        guard !onboardingCompleted else { return }
        onboardingCompleted = true
        Analytics.shared.capture(.onboardingCompleted(via: via))
    }

    // MARK: one place for every computer's errors and screen requests

    private var errorMessage: String? {
        accounts.lastError ?? accounts.computers.lazy.compactMap { accounts.store(for: $0.id)?.lastError }.first
    }

    private var errorShown: Binding<Bool> {
        Binding(get: { errorMessage != nil }, set: { shown in
            guard !shown else { return }
            accounts.lastError = nil
            for computer in accounts.computers { accounts.store(for: computer.id)?.lastError = nil }
        })
    }

    private struct ScreenTarget: Identifiable {
        let computerId: ComputerID
        let request: ScreenRequest
        var id: String { "\(computerId)/\(request.id)" }
    }

    /// Any computer's "open the screen" request (thread toolbar, computers list, `codync://screen`).
    private var screenTarget: Binding<ScreenTarget?> {
        Binding(get: {
            accounts.computers.lazy.compactMap { c in
                accounts.store(for: c.id)?.screenRequest.map { ScreenTarget(computerId: c.id, request: $0) }
            }.first
        }, set: { target in
            guard target == nil else { return }
            for computer in accounts.computers { accounts.store(for: computer.id)?.screenRequest = nil }
        })
    }
}

/// A conversation, talking to the computer the bot lives on.
private struct ChatScreen: View {
    let ref: BotReference
    @Environment(AccountStore.self) private var accounts

    var body: some View {
        if let store = accounts.store(for: ref.computerId) {
            ThreadView(botId: ref.botId)
                .environment(store)
        } else {
            VStack(spacing: 0) {
                ScreenHeader { BackButton { accounts.selection = nil } } title: { EmptyView() } trailing: { EmptyView() }
                EmptyState(title: "This computer was removed", systemImage: "desktopcomputer.trianglebadge.exclamationmark")
            }
            .background(Palette.background)
            .hidesSystemNavigationBar()
        }
    }
}

/// Where your bots show up outside the app: Widget, Live Activity and Dynamic Island,
/// each designed and switched on here. Usage limits open from the header.
private struct StateTab: View {
    enum Surface: Hashable { case widget, liveActivity, island }

    @Environment(AppStore.self) private var app
    @Environment(\.accessibilityReduceMotion) private var reduceMotion
    // Screenshots: CODYNC_STATE_SURFACE=widget|liveActivity|island opens the tab on that gallery.
    @State private var surface: Surface = {
        #if DEBUG
            switch ProcessInfo.processInfo.environment["CODYNC_STATE_SURFACE"] {
            case "liveActivity": return .liveActivity
            case "island": return .island
            default: return .widget
            }
        #else
            return .widget
        #endif
    }()

    var body: some View {
        VStack(spacing: 0) {
            ScreenHeader {
                EmptyView()
            } title: {
                Text(L10n.tr("State")).font(.body.weight(.semibold)).foregroundStyle(Palette.text)
            } trailing: {
                IconButton("Usage limits", systemImage: "chart.bar") { app.showUsage = true }
            }
            SegmentedChoice(selection: $surface, options: [
                (.widget, "Widget"), (.liveActivity, "Live Activity"), (.island, "Dynamic Island"),
            ])
            .padding(.horizontal, 16)
            .padding(.bottom, 8)
            ZStack {
                // Holds the gallery's pushes (Lock Screen widgets); no bar shows.
                NavigationStack { WidgetGalleryView() }
                    .environment(\.stateSurfaceActive, app.tab == .state && surface == .widget)
                    .tabLayer(surface == .widget)
                ActivityGalleryView(forms: [.lockScreen])
                    .tabLayer(surface == .liveActivity)
                ActivityGalleryView(forms: [.compact, .minimal, .expanded])
                    .tabLayer(surface == .island)
            }
            .animation(Motion.reduced(Motion.fade, reduceMotion), value: surface)
        }
        .background(Palette.background)
        .codyncSheet(isPresented: Bindable(app).showUsage) { UsageSheet() }
    }
}

/// Usage for one computer at a time: the last active one, which the Usage widget shows too.
private struct UsageSheet: View {
    @Environment(AppStore.self) private var app
    @Environment(AccountStore.self) private var accounts
    @Environment(\.dismissModal) private var dismiss
    /// Picked here; also becomes the computer the Usage widget shows.
    @State private var picked: ComputerID?

    var body: some View {
        let store = picked.flatMap(accounts.store(for:)) ?? app.currentStore
        VStack(spacing: 0) {
            ScreenHeader {
                IconButton("Close", systemImage: "xmark") { dismiss() }
            } title: {
                Text(L10n.tr("Usage")).font(.body.weight(.semibold)).foregroundStyle(Palette.text)
            } trailing: {
                if let store, accounts.computers.count > 1 {
                    DropdownMenu {
                        accounts.computers.map { computer in
                            MenuItem(computer.name, selected: computer.id == store.computer.id) {
                                picked = computer.id
                                accounts.storage.lastComputerId = computer.id
                                WidgetCenter.shared.reloadTimelines(ofKind: "CodyncUsage")
                            }
                        }
                    } label: {
                        ComputerBadge(store.computer, size: 28)
                    }
                    .accessibilityLabel("Computer: \(store.computer.name)")
                }
            }
            if let store {
                UsageView()
                    .environment(store)
                    .id(store.computer.id)
            } else {
                EmptyState(title: "No computer yet", systemImage: "chart.bar",
                           message: "Usage shows up once a computer is connected.")
            }
        }
        .background(Palette.background)
        // What failed here (a switch, a drag) shows over this sheet, not under it at the root,
        // where the same error clears with this one.
        .codyncDialog("Something went wrong", isPresented: Binding(
            get: { store?.lastError != nil }, set: { if !$0 { store?.lastError = nil } }
        ), message: store?.lastError, cancel: "OK", inPlace: true) { [] }
        .tracked(.screenViewed(.usage))
    }
}

/// A centered icon, title and optional line: what a screen shows when it has nothing yet.
struct EmptyState: View {
    let title: String
    let systemImage: String
    var message: String?

    var body: some View {
        VStack(spacing: 8) {
            Image(systemName: systemImage).font(.system(size: 40)).foregroundStyle(Palette.tertiary)
            Text(L10n.tr(title)).font(.title3.weight(.semibold)).foregroundStyle(Palette.text)
            if let message {
                Text(L10n.tr(message)).font(.subheadline).foregroundStyle(Palette.secondary)
            }
        }
        .multilineTextAlignment(.center)
        .padding(24)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

/// A page's title row inside a modal: the close button on the modal's first page,
/// a back button once pushed inside it.
struct PageHeader<Trailing: View>: View {
    let title: String
    var pushed = false
    var icon: String?
    let trailing: Trailing
    @Environment(\.dismiss) private var pop

    init(_ title: String, pushed: Bool = false, icon: String? = nil, @ViewBuilder trailing: () -> Trailing) {
        self.title = title
        self.pushed = pushed
        self.icon = icon
        self.trailing = trailing()
    }

    var body: some View {
        if pushed {
            ScreenHeader {
                BackButton { pop() }
            } title: {
                headerTitle
            } trailing: {
                trailing
            }
        } else {
            ModalHeader { headerTitle } trailing: { trailing }
        }
    }
}

private extension PageHeader {
    var headerTitle: some View {
        HStack(spacing: 8) {
            if let icon {
                Image(systemName: icon).font(.title2).accessibilityHidden(true)
            }
            Text(L10n.tr(title)).font(.body.weight(.semibold)).lineLimit(1)
        }
        .foregroundStyle(Palette.text)
    }
}

extension PageHeader where Trailing == EmptyView {
    init(_ title: String, pushed: Bool = false, icon: String? = nil) { self.init(title, pushed: pushed, icon: icon) { EmptyView() } }
}

extension View {
    /// Tops a screen with its `PageHeader` in place of the system navigation bar.
    func page(_ title: String, pushed: Bool = false, icon: String? = nil) -> some View {
        VStack(spacing: 0) {
            PageHeader(title, pushed: pushed, icon: icon)
            self
        }
        .background(Palette.background)
        .hidesSystemNavigationBar()
    }
}

private extension View {
    /// One tab's stack in the root ZStack: visible and interactive only while selected.
    func tabLayer(_ selected: Bool) -> some View {
        opacity(selected ? 1 : 0)
            .allowsHitTesting(selected)
            .accessibilityHidden(!selected)
    }
}

/// Access requests still waiting after their sheet closed: the code to check, until the computer answers.
private struct AccessBanners: View {
    @Environment(AccountStore.self) private var accounts

    var body: some View {
        VStack(spacing: 8) {
            ForEach(Array(accounts.pendingAccess.values).sorted { $0.requestId < $1.requestId }) { ticket in
                let name = accounts.cloudComputers.first { $0.computerId == ticket.computerId }?.name ?? "Your computer"
                HStack(spacing: 12) {
                    Spinner(size: 16)
                    VStack(alignment: .leading, spacing: 1) {
                        Text(L10n.tr("Approve on \(name)")).font(.subheadline.weight(.medium)).foregroundStyle(Palette.text)
                        Text(verbatim: "\(L10n.tr("Code")) \(ticket.code.prefix(3)) \(ticket.code.suffix(3))")
                            .font(.footnote.monospacedDigit()).foregroundStyle(Palette.secondary)
                    }
                    Spacer(minLength: 8)
                    IconButton("Cancel request", systemImage: "xmark") {
                        Task { await accounts.cancelAccess(ticket.computerId) }
                    }
                }
                .padding(.leading, 14)
                .padding(.trailing, 6)
                .padding(.vertical, 8)
                .background(Palette.bubbleAgent, in: RoundedRectangle(cornerRadius: 16, style: .continuous))
                .transition(.move(edge: .bottom).combined(with: .opacity))
            }
        }
        .padding(.horizontal, 16)
        .padding(.bottom, accounts.pendingAccess.isEmpty ? 0 : 8)
        .animation(Motion.layout, value: accounts.pendingAccess.keys.sorted())
        .accessibilityElement(children: .contain)
    }
}

/// The marketplace sheet's item, so it keeps its computer while sliding away.
private struct MarketplaceTarget: Identifiable {
    let computerId: ComputerID
    var id: String { "marketplace" }
}