summaryrefslogtreecommitdiff
path: root/octosentry/SignInView.swift
blob: 6d791df1cb07f6b83b889baa0a5ab5b6e96ef8c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//  SignInView.swift
//  octosentry
//

import AppKit
import SwiftUI

struct SignInView: View {
    var authStore: AuthStore

    var body: some View {
        VStack(spacing: 16) {
            Spacer()

            switch authStore.state {
            case .signedOut:
                signedOutContent
            case .awaitingAuthorization(let userCode, let verificationURL):
                awaitingAuthorizationContent(userCode: userCode, verificationURL: verificationURL)
            case .signedIn:
                EmptyView()
            }

            if let errorMessage = authStore.errorMessage {
                Text(errorMessage)
                    .font(.caption)
                    .foregroundStyle(.red)
                    .multilineTextAlignment(.center)
                    .padding(.horizontal)
            }

            Spacer()
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding()
    }

    private var signedOutContent: some View {
        VStack(spacing: 16) {
            Image(systemName: "shield.lefthalf.filled")
                .font(.system(size: 40))
                .foregroundStyle(.secondary)
            Text("Sign in with GitHub to see your security alerts.")
                .font(.callout)
                .multilineTextAlignment(.center)
                .foregroundStyle(.secondary)
                .padding(.horizontal)
            Button("Sign in with GitHub") {
                authStore.signIn()
            }
            .buttonStyle(.borderedProminent)
        }
    }

    private func awaitingAuthorizationContent(userCode: String, verificationURL: URL) -> some View {
        VStack(spacing: 12) {
            Text("Enter this code on GitHub")
                .font(.callout)
                .foregroundStyle(.secondary)

            Text(userCode)
                .font(.system(.title, design: .monospaced).weight(.bold))
                .textSelection(.enabled)

            HStack(spacing: 8) {
                Button("Copy Code") {
                    NSPasteboard.general.clearContents()
                    NSPasteboard.general.setString(userCode, forType: .string)
                }
                Button("Open GitHub") {
                    NSWorkspace.shared.open(verificationURL)
                }
                .buttonStyle(.borderedProminent)
            }

            ProgressView()
                .controlSize(.small)
                .padding(.top, 4)
        }
    }
}

#Preview {
    SignInView(authStore: AuthStore())
}