Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Sources/CodeCaps/CodeCapsAlertChannels.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation
import QuotaCore

/// One alert occurrence on its way to a delivery channel.
///
/// Every event carries its stable `UsageAlertKind` identifier, so channels
/// select by kind — never by which producer emitted the event — and a
/// future per-kind channel opt-in needs no producer changes.
public struct CodeCapsAlertEvent: Equatable, Sendable {
public let kind: UsageAlertKind
public let title: String
public let body: String
public let identifier: String
public let occurredAt: Date

public init(kind: UsageAlertKind, title: String, body: String,
identifier: String, occurredAt: Date) {
self.kind = kind
self.title = title
self.body = body
self.identifier = identifier
self.occurredAt = occurredAt
}
}

/// A delivery channel for app-level alert events.
///
/// The seam is deliberately at the app level, not inside any plugin: a new
/// channel registers once with the dispatcher and receives every producer's
/// events. The planned Twilio SMS channel is the expected second channel —
/// app-level, selecting a deliberately small set of kinds — and it slots in
/// here without reworking any monitor.
@MainActor
public protocol CodeCapsAlertChannel: AnyObject {
func deliver(_ event: CodeCapsAlertEvent)
}

/// App-level fan-out for alert events. Producers emit here, never to a
/// channel directly.
@MainActor
public final class CodeCapsAlertDispatcher {
private var channels: [CodeCapsAlertChannel] = []

public init() {}

public func register(_ channel: CodeCapsAlertChannel) {
channels.append(channel)
}

public func emit(_ event: CodeCapsAlertEvent) {
for channel in channels { channel.deliver(event) }
}
}

/// The only channel today: CodeCaps' own alert surface, through the same
/// per-kind preferences and delivery path as every other usage alert.
@MainActor
public final class LocalAlertChannel: CodeCapsAlertChannel {
private weak var manager: ResetAlarmManager?

public init(manager: ResetAlarmManager) {
self.manager = manager
}

public func deliver(_ event: CodeCapsAlertEvent) {
manager?.deliverUsageAlert(kind: event.kind, title: event.title,
body: event.body, identifier: event.identifier)
}
}
47 changes: 46 additions & 1 deletion Sources/CodeCaps/ConsoleViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum ConsolePage: Hashable {
case allPlatforms
case platform(String)
case runawayAlerts
case plugin(String)
case settingsMenuBar
case settingsPlatforms
case settingsLogoStyle
Expand All @@ -19,7 +20,7 @@ enum ConsolePage: Hashable {

var isSettings: Bool {
switch self {
case .allPlatforms, .platform, .runawayAlerts: return false
case .allPlatforms, .platform, .runawayAlerts, .plugin: return false
default: return true
}
}
Expand All @@ -29,6 +30,7 @@ enum ConsolePage: Hashable {
case .allPlatforms: return "allPlatforms"
case .platform(let providerKey): return "platform:" + providerKey
case .runawayAlerts: return "runawayAlerts"
case .plugin(let pluginId): return "plugin:" + pluginId
case .settingsMenuBar: return "settingsMenuBar"
case .settingsPlatforms: return "settingsPlatforms"
case .settingsLogoStyle: return "settingsLogoStyle"
Expand All @@ -53,6 +55,9 @@ enum ConsolePage: Hashable {
case "settingsInfisical": return .settingsInfisical
case "settingsAbout": return .settingsAbout
default:
if value.hasPrefix("plugin:") {
return .plugin(String(value.dropFirst(7)))
}
guard value.hasPrefix("platform:") else { return nil }
return .platform(String(value.dropFirst(9)))
}
Expand All @@ -63,6 +68,7 @@ enum ConsolePage: Hashable {
switch self {
case .allPlatforms: return "All Platforms"
case .runawayAlerts: return "Runaway Alerts"
case .plugin(let pluginId): return CodeCapsPlugin.named(pluginId)?.title ?? "Plugin"
case .settingsMenuBar: return "Menu Bar & PiP"
case .settingsPlatforms: return "Platforms"
case .settingsLogoStyle: return "Logo Style"
Expand All @@ -79,6 +85,7 @@ enum ConsolePage: Hashable {
switch self {
case .allPlatforms: return "square.grid.2x2"
case .runawayAlerts: return "flame.fill"
case .plugin(let pluginId): return CodeCapsPlugin.named(pluginId)?.symbol ?? "puzzlepiece.extension"
case .settingsMenuBar: return "menubar.rectangle"
case .settingsPlatforms: return "square.grid.2x2"
case .settingsLogoStyle: return "photo.on.rectangle.angled"
Expand All @@ -94,6 +101,8 @@ enum ConsolePage: Hashable {
static let settingsPages: [ConsolePage] = [
.settingsMenuBar, .settingsPlatforms, .settingsLogoStyle, .settingsSourcesFleet, .settingsNotifications, .settingsAppearance, .settingsInfisical, .settingsAbout,
]

static let pluginPages: [ConsolePage] = CodeCapsPlugin.allCases.map { .plugin($0.id) }
}

struct AlertNavigation: Equatable {
Expand Down Expand Up @@ -192,6 +201,13 @@ final class ConsoleState: ObservableObject {
awaitingInitialSelection = false
return
}
if case .plugin = page {
// A plugin page is local: it never waits on a quota read, and no
// source disappearing can invalidate it.
pendingStoredKey = nil
awaitingInitialSelection = false
return
}
if page.isSettings && !awaitingInitialSelection { return }
if sections.isEmpty && !readCompleted { return }
isReconciling = true
Expand Down Expand Up @@ -335,6 +351,9 @@ struct ConsoleView: View {
case .runawayAlerts:
RunawayAlertsPage(model: model, state: state)
.padding(Metrics.pagePadding)
case .plugin(let pluginId):
MuseCodePluginPage(model: model, pluginId: pluginId)
.padding(Metrics.pagePadding)
case .platform(let key):
PlatformDetailPage(model: model, state: state, providerKey: key)
.padding(Metrics.pagePadding)
Expand Down Expand Up @@ -482,6 +501,7 @@ struct ConsoleSidebar: View {
private var allPages: [ConsolePage] {
var pages: [ConsolePage] = [.allPlatforms, .runawayAlerts]
pages.append(contentsOf: model.displaySections.map { .platform($0.id) })
pages.append(contentsOf: ConsolePage.pluginPages)
pages.append(contentsOf: ConsolePage.settingsPages)
return pages
}
Expand Down Expand Up @@ -539,6 +559,31 @@ struct ConsoleSidebar: View {
} header: {
Eyebrow("QUOTAS")
}
Section {
ForEach(ConsolePage.pluginPages, id: \.self) { page in
sidebarRow(page: page) {
HStack(spacing: 8) {
Image(systemName: page.symbol)
.font(.system(size: 13))
.foregroundStyle(model.museCodeBurnAlert == nil ? Theme.ink : Theme.warning)
.frame(width: 18)
Text(page.settingsTitle)
.font(.system(size: 13, weight: .medium))
Spacer(minLength: 4)
if model.museCodeBurnAlert != nil {
Text("1")
.font(.system(size: 10, weight: .bold))
.foregroundStyle(.white)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background(Theme.warning, in: Capsule())
}
}
}
}
} header: {
Eyebrow("PLUGINS")
}
Section {
ForEach(ConsolePage.settingsPages, id: \.self) { page in
sidebarRow(page: page) {
Expand Down
Loading