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.

8.9 KB
Raw Blame
import CodyncKit
import CodyncUI
import SwiftUI

struct AccountSwitcherButton: View {
    @Environment(AccountSession.self) private var account

    var body: some View {
        Button { account.showSwitcher = true } label: {
            HStack(spacing: 4) {
                if account.isBusy || !account.isReady {
                    Spinner(size: 18).frame(width: 30, height: 30)
                } else {
                    AccountAvatar(url: account.avatarURL, email: account.email, size: 30)
                }
                Image(systemName: "chevron.down")
                    .font(.system(size: 10, weight: .semibold))
                    .foregroundStyle(Palette.secondary)
            }
        }
        .accessibilityLabel("Switch account")
        .accessibilityValue(account.email ?? "Local pairing")
    }
}

/// Who you're signed in as, and the other accounts on this iPhone.
struct AccountSwitcherView: View {
    @Environment(AppStore.self) private var app
    @Environment(AccountSession.self) private var account
    @State private var confirmSignOut = false
    @State private var confirmDelete = false
    @State private var pairing = false
    @State private var addingAccount = false

    var body: some View {
        CardForm {
            Button { pairing = true } label: {
                HStack(spacing: 12) {
                    Image(systemName: "qrcode.viewfinder").font(.system(size: 22))
                    Text(L10n.tr("Scan a computer's code")).font(.body.weight(.medium))
                    Spacer(minLength: 8)
                    Image(systemName: "chevron.right").font(.caption.weight(.semibold)).foregroundStyle(Palette.tertiary)
                }
                .foregroundStyle(Palette.text)
                .padding(16)
                .background(Palette.bubbleAgent, in: RoundedRectangle(cornerRadius: 18, style: .continuous))
                .contentShape(Rectangle())
            }
            .buttonStyle(PressScale())

            profile
                .frame(maxWidth: .infinity)
                .padding(.vertical, 8)

            if account.isSignedIn, account.supportsMultipleAccounts || account.accounts.count > 1 {
                switcher
                if addingAccount { SignInButtons().transition(.opacity.combined(with: .move(edge: .top))) }
            }

            if account.isSignedIn {
                CardSection {
                    Button { confirmSignOut = true } label: {
                        RowLabel("Sign out", systemImage: "rectangle.portrait.and.arrow.right", tint: Palette.danger)
                    }
                    .buttonStyle(.plain)
                    .disabled(account.isBusy)
                    Button { confirmDelete = true } label: {
                        RowLabel("Delete account", systemImage: "trash", tint: Palette.danger)
                    }
                    .buttonStyle(.plain)
                    .disabled(account.isBusy)
                }
            }

            if let message = account.errorMessage {
                Label(L10n.tr(message), systemImage: "exclamationmark.triangle.fill")
                    .font(.footnote)
                    .foregroundStyle(Palette.danger)
                    .transition(.opacity)
            }
        }
        .safeAreaInset(edge: .top, spacing: 0) {
            ModalHeader("Account") {
                if account.isBusy {
                    ThinkingOrb(state: .connecting, size: 20)
                        .accessibilityHidden(false).accessibilityLabel(L10n.tr("Signing in"))
                        .transition(.opacity)
                }
            }
            .background(Palette.background)
            .animation(Motion.fade, value: account.isBusy)
        }
        .animation(Motion.layout, value: account.userID)
        .animation(Motion.fade, value: account.errorMessage)
        .tracked(.screenViewed(.account))
        .onChange(of: account.userID) { _, _ in withAnimation(Motion.layout) { addingAccount = false } }
        .codyncSheet(isPresented: $pairing) { PairingView(inModal: true) }
        .codyncDialog("Sign out of \(account.email ?? "this account")?", isPresented: $confirmSignOut,
                      message: "This iPhone stops getting the account's notifications and keeps its computers, so signing back in needs no scan. To take it off them, use Start over in Settings instead.") {
            [DialogAction("Sign out", destructive: true) { Task { await app.signOut() } }]
        }
        .codyncDialog("Delete your Codync account?", isPresented: $confirmDelete,
                      message: "Your account, its list of computers and every device's access through it are deleted. Your computers keep their bots and chats. This can't be undone.") {
            [DialogAction("Delete account", destructive: true) { Task { await app.deleteAccount() } }]
        }
    }

    /// The big picture: your face and address when signed in, both login options when not.
    @ViewBuilder private var profile: some View {
        if account.isSignedIn {
            VStack(spacing: 10) {
                AccountAvatar(url: account.avatarURL, email: account.email, size: 76)
                Text(account.email ?? "")
                    .font(.subheadline.weight(.medium))
                    .foregroundStyle(Palette.text)
                    .lineLimit(1)
                    .truncationMode(.middle)
            }
            .transition(.opacity.combined(with: .scale(scale: 0.96)))
        } else {
            VStack(spacing: 16) {
                ZStack {
                    Circle().fill(Palette.bubbleAgent).frame(width: 76, height: 76)
                    Image(systemName: "person.fill")
                        .font(.system(size: 32))
                        .foregroundStyle(Palette.tertiary)
                }
                .accessibilityHidden(true)
                if account.isConfigured {
                    SignInButtons()
                } else {
                    Label(L10n.tr("QR pairing only in this build"), systemImage: "qrcode")
                        .font(.footnote)
                        .foregroundStyle(Palette.secondary)
                }
            }
            .transition(.opacity.combined(with: .scale(scale: 0.96)))
        }
    }

    /// Every account on this iPhone as a face; tap one to switch, + to add another.
    private var switcher: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 14) {
                ForEach(account.accounts) { item in
                    let current = item.id == account.userID
                    Button { Task { await account.switchAccount(item) } } label: {
                        AccountAvatar(url: item.avatarURL, email: item.email, size: 44)
                            .padding(3)
                            .overlay(Circle().strokeBorder(current ? Palette.accent : .clear, lineWidth: 2))
                    }
                    .buttonStyle(PressScale())
                    .disabled(account.isBusy || current)
                    .accessibilityLabel(item.email)
                    .accessibilityAddTraits(current ? .isSelected : [])
                }
                if account.supportsMultipleAccounts {
                    Button { withAnimation(Motion.layout) { addingAccount.toggle() } } label: {
                        Image(systemName: "plus")
                            .font(.system(size: 18, weight: .medium))
                            .foregroundStyle(Palette.secondary)
                            .frame(width: 50, height: 50)
                            .background(Palette.bubbleAgent, in: Circle())
                    }
                    .buttonStyle(PressScale())
                    .disabled(account.isBusy)
                    .accessibilityLabel("Add account")
                }
            }
            .padding(.horizontal, 4)
            .frame(maxWidth: .infinity)
        }
        .scrollBounceBehavior(.basedOnSize)
    }
}

/// The account's photo; its first letter on a tinted circle until (or unless) the photo loads.
struct AccountAvatar: View {
    let url: URL?
    var email: String?
    let size: CGFloat

    var body: some View {
        AsyncImage(url: url) { phase in
            if let image = phase.image {
                image.resizable().scaledToFill()
            } else {
                ZStack {
                    Circle().fill(Palette.bubbleUser)
                    if let letter = email?.first {
                        Text(String(letter).uppercased())
                            .font(.system(size: size * 0.42, weight: .semibold))
                            .foregroundStyle(Palette.text)
                    } else {
                        Image(systemName: "person.fill")
                            .font(.system(size: size * 0.42))
                            .foregroundStyle(Palette.tertiary)
                    }
                }
            }
        }
        .frame(width: size, height: size)
        .clipShape(Circle())
        .accessibilityHidden(true)
    }
}