Cotton code
148 lines · 6.0 KB · SwiftRaw
import SwiftUI/// Linking WhatsApp on a source page: the number and the code to type in WhatsApp, or a QR to scan, and/// WhatsApp's passkey step. Moved from the card the panel had before (22 Nov 2026).struct WhatsappPairing: View {    @Bindable var model: AppModel    let panel: CoreStatus.Panel    let need: SourceStatus.Need?    let signer: String?    /// Code first: the number, then the code WhatsApp asks for under Linked    /// devices › Link with phone number; a QR to scan instead.    var body: some View {        VStack(alignment: .leading, spacing: 10) {            if let step = need?.passkey {                Text(passkeyLine(step)).font(Faces.small).foregroundStyle(Palette.ink)                passkey(step, confirm: need?.confirm, signer: signer.flatMap { $0.isEmpty ? nil : $0 })            } else {                pairingByCodeOrQr(need)            }        }    }    private func passkeyLine(_ step: String) -> String {        switch step {        case "asking", "retry": "WhatsApp asks for your passkey"        case "sent": "Sending the passkey to WhatsApp…"        case "confirm": "Does this match your phone?"        default: "Linking WhatsApp…"        }    }    /// WhatsApp's passkey step: Cotton Keys signs it on this Mac when the vault    /// is open, else it is given in the Chrome window the importer opened at    /// web.whatsapp.com (Touch ID, or the phone by its QR); `signer` is the    /// core's line for which ("Signed by Cotton Keys on this Mac." / "Given in    /// Chrome (why)."). Then the code both screens show is checked before    /// WhatsApp links.    @ViewBuilder    private func passkey(_ step: String, confirm: String?, signer: String?) -> some View {        switch step {        case "confirm":            if let code = confirm {                Text(code)                    .font(Faces.code)                    .tracking(6)                    .foregroundStyle(Palette.ink)                    .frame(maxWidth: .infinity)                    .accessibilityLabel("Code to match \(code)")            }            pairErrorLine            Button("Confirm") { model.passkeyConfirm() }                .buttonStyle(PrimaryButtonStyle(enabled: !model.pairBusy))                .disabled(model.pairBusy)        case "retry":            pairErrorLine            Button("Use passkey") { model.passkeyUse() }                .buttonStyle(PrimaryButtonStyle(enabled: !model.pairBusy))                .disabled(model.pairBusy)        case "asking", "sent":            if let signer {                Text(signer)                    .font(Faces.small)                    .foregroundStyle(Palette.quiet)                    .fixedSize(horizontal: false, vertical: true)            }            if step == "asking", signer?.hasPrefix("Signed by") != true {                Text("Give it in the Chrome window that opened: Touch ID, or scan its QR with your phone.")                    .font(Faces.small)                    .foregroundStyle(Palette.quiet)                    .fixedSize(horizontal: false, vertical: true)            }        default:            EmptyView()        }        if step != "linking" {            pairSwitch("Cancel") { model.pairCancel() }        }    }    @ViewBuilder    private func pairingByCodeOrQr(_ need: SourceStatus.Need?) -> some View {        switch need?.by {        case "code":            Text(panel.word("whatsapp_code_line")).font(Faces.small).foregroundStyle(Palette.quiet).fixedSize(horizontal: false, vertical: true)            if let code = need?.code {                Text(code)                    .font(Faces.code)                    .tracking(6)                    .foregroundStyle(Palette.ink)                    .textSelection(.enabled)                    .frame(maxWidth: .infinity)                    .accessibilityLabel("WhatsApp link code \(code)")            }            pairSwitch(panel.word("whatsapp_use_qr")) { model.pairByQr() }        case "qr":            Text(panel.word("whatsapp_qr_line")).font(Faces.small).foregroundStyle(Palette.quiet).fixedSize(horizontal: false, vertical: true)            if let qr = need?.qr, let image = QRCode.image(qr) {                Image(nsImage: image)                    .interpolation(.none)                    .resizable()                    .frame(width: 176, height: 176)                    .padding(8)                    .background(Color.white)                    .frame(maxWidth: .infinity)                    .accessibilityLabel("WhatsApp pairing QR")            }            pairSwitch(panel.word("whatsapp_use_code")) { model.whatsappNumber = model.whatsappNumber ?? model.status?.phone; model.pairByCode() }        default:            TextField(panel.word("whatsapp_number"), text: whatsappNumber)                .textFieldStyle(.roundedBorder)                .onSubmit { model.pairByCode() }                .accessibilityIdentifier("whatsapp-number")            pairErrorLine            Button(panel.word("whatsapp_get_code")) { model.pairByCode() }                .buttonStyle(PrimaryButtonStyle(enabled: !model.pairBusy))                .disabled(model.pairBusy)            pairSwitch(panel.word("whatsapp_use_qr")) { model.pairByQr() }        }    }    private var whatsappNumber: Binding<String> {        Binding(            get: { model.whatsappNumber ?? model.status?.phone ?? "" },            set: { model.whatsappNumber = $0 }        )    }    @ViewBuilder    private var pairErrorLine: some View {        if let error = model.pairError {            Text(error)                .font(Faces.small)                .foregroundStyle(Palette.warnText)                .fixedSize(horizontal: false, vertical: true)        }    }    private func pairSwitch(_ title: String, _ action: @escaping () -> Void) -> some View {        Button(title, action: action)            .buttonStyle(.plain)            .font(Faces.small)            .foregroundStyle(Palette.link)            .disabled(model.pairBusy)            .frame(maxWidth: .infinity)    }}