Graphley / app · graphley.ai / Apps / Mac / AgentsView.swift
| 1 | import SwiftUI |
| 2 | import AnalyticsKit |
| 3 | import AnalyticsUI |
| 4 | |
| 5 | struct AgentsView: View { |
| 6 | @Environment(AppModel.self) private var model |
| 7 | @State private var showAll = false |
| 8 | |
| 9 | var executable: String { Bundle.main.executablePath ?? "/Applications/Graphley.app/Contents/MacOS/Graphley" } |
| 10 | |
| 11 | var body: some View { |
| 12 | VStack(alignment: .leading, spacing: 16) { |
| 13 | ScreenHeading("Agents", subtitle: "Claude Code, Codex or any MCP client can set up Graphley and read your numbers. It all runs on this Mac.") |
| 14 | ViewThatFits(in: .horizontal) { |
| 15 | HStack(alignment: .top, spacing: 16) { |
| 16 | activity.frame(minWidth: 420).layoutPriority(1.4) |
| 17 | side.frame(width: 380) |
| 18 | } |
| 19 | VStack(alignment: .leading, spacing: 16) { side; activity } |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | var activity: some View { |
| 25 | TideCard("Recent agent activity", subtitle: "Every change an agent makes is listed here.") { |
| 26 | let log = model.workspace.agentLog |
| 27 | if log.isEmpty { |
| 28 | Text("No agent changes yet. When an agent adds a site or builds a dashboard, it shows up here.") |
| 29 | .font(Tide.secondary).foregroundStyle(Tide.muted) |
| 30 | } |
| 31 | VStack(alignment: .leading, spacing: 0) { |
| 32 | ForEach(Array(log.prefix(showAll ? 200 : 20).enumerated()), id: \.element.id) { i, a in |
| 33 | if i > 0 { RowDivider() } |
| 34 | HStack(alignment: .firstTextBaseline, spacing: 10) { |
| 35 | Text(Self.when(a.at)).font(Tide.figure(11.5)).foregroundStyle(Tide.muted).frame(width: 72, alignment: .leading) |
| 36 | (Text(a.client).font(Tide.sans(13.5, .semibold)) + Text(" " + a.summary.prefix(1).lowercased() + a.summary.dropFirst()).font(Tide.sans(13.5))) |
| 37 | .foregroundStyle(Tide.ink) |
| 38 | .frame(maxWidth: .infinity, alignment: .leading) |
| 39 | Text(a.tool).font(Tide.figure(11.5)).foregroundStyle(Tide.muted) |
| 40 | .padding(.vertical, 1).padding(.horizontal, 5).background(Tide.sunk, in: RoundedRectangle(cornerRadius: 4)) |
| 41 | } |
| 42 | .padding(.vertical, 8) |
| 43 | } |
| 44 | } |
| 45 | if log.count > 20 && !showAll { |
| 46 | Button("Show All") { showAll = true }.buttonStyle(.tide(.quiet, size: .small)) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | var side: some View { |
| 52 | VStack(alignment: .leading, spacing: 16) { |
| 53 | TideCard("Connect an agent") { |
| 54 | Text("Claude Code").font(Tide.fieldLabel).foregroundStyle(Tide.ink) |
| 55 | CopyBlock("claude mcp add graphley -- \"\(executable)\" --mcp") |
| 56 | Text("Other MCP clients").font(Tide.fieldLabel).foregroundStyle(Tide.ink).padding(.top, 4) |
| 57 | CopyBlock("{\"mcpServers\": {\"graphley\": {\"command\": \"\(executable)\", \"args\": [\"--mcp\"]}}}", wraps: true) |
| 58 | Text("Paste the command in Terminal. Your agent starts Graphley's MCP server whenever it needs it.") |
| 59 | .font(Tide.caption).foregroundStyle(Tide.muted) |
| 60 | } |
| 61 | TideCard("What agents can do") { |
| 62 | VStack(spacing: 0) { |
| 63 | ToggleRow("Let agents make changes", detail: "Add sites and apps, build dashboards and refresh.", isOn: setting(\.agentsCanWrite)) |
| 64 | .padding(.bottom, 12) |
| 65 | RowDivider() |
| 66 | ToggleRow("Let agents set keys", detail: "Save an App Store Connect key for you. Turn it off again afterwards.", isOn: setting(\.agentsCanSetKeys)) |
| 67 | .padding(.top, 12) |
| 68 | } |
| 69 | } |
| 70 | TideCard("Try asking") { |
| 71 | VStack(alignment: .leading, spacing: 8) { |
| 72 | PromptChip("Set up Graphley with all my Google Analytics sites.") |
| 73 | PromptChip("Add my App Store apps to Graphley and refresh.") |
| 74 | PromptChip("Make a dashboard comparing my top two sites this month.") |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func setting(_ key: WritableKeyPath<AnalyticsKit.Settings, Bool>) -> Binding<Bool> { |
| 81 | Binding(get: { model.workspace.settings[keyPath: key] }, set: { v in model.updateSettings { $0[keyPath: key] = v } }) |
| 82 | } |
| 83 | |
| 84 | static func when(_ date: Date) -> String { |
| 85 | let cal = Calendar.current |
| 86 | if cal.isDateInToday(date) { return date.formatted(date: .omitted, time: .shortened) } |
| 87 | if cal.isDateInYesterday(date) { return "Yesterday" } |
| 88 | return date.formatted(.dateTime.month(.abbreviated).day()) |
| 89 | } |
| 90 | } |