summaryrefslogtreecommitdiff
path: root/Hutch/App/HutchIntents.swift
blob: 947ec042e8c40b12f9c27f7495f8ed547cfebdf0 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import AppIntents
import Foundation

// MARK: - Navigation Intents

struct OpenWorkQueueIntent: AppIntent {
    static var title: LocalizedStringResource = "Open Work Queue"
    static var description = IntentDescription("Opens Hutch to your Work Queue.")
    static var openAppWhenRun = true

    var route: HutchRoute { .workQueue(scope: .all) }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

struct OpenRecentActivityIntent: AppIntent {
    static var title: LocalizedStringResource = "Open Recent Activity"
    static var description = IntentDescription("Opens Hutch to recent activity.")
    static var openAppWhenRun = true

    var route: HutchRoute { .recentActivity }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

struct OpenSystemStatusIntent: AppIntent {
    static var title: LocalizedStringResource = "Open System Status"
    static var description = IntentDescription("Opens Hutch to SourceHut system status.")
    static var openAppWhenRun = true

    var route: HutchRoute { .systemStatus }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

struct OpenPinnedResourceIntent: AppIntent {
    static var title: LocalizedStringResource = "Open Pinned Resource"
    static var description = IntentDescription("Opens a pinned Hutch resource.")
    static var openAppWhenRun = true

    @Parameter(title: "Pinned Resource")
    var pinnedResource: PinnedResourceEntity

    var route: HutchRoute { pinnedResource.route }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

struct OpenProjectDashboardIntent: AppIntent {
    static var title: LocalizedStringResource = "Open Project Dashboard"
    static var description = IntentDescription("Opens a pinned project dashboard in Hutch.")
    static var openAppWhenRun = true

    @Parameter(title: "Project")
    var project: ProjectEntity

    var route: HutchRoute {
        .projectDashboard(id: project.id, title: project.name)
    }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

enum HutchShortcutScope: String, AppEnum {
    case all

    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Scope")
    static var caseDisplayRepresentations: [HutchShortcutScope: DisplayRepresentation] = [
        .all: "All"
    ]
}

struct OpenFailedBuildsIntent: AppIntent {
    static var title: LocalizedStringResource = "Open Failed Builds"
    static var description = IntentDescription("Opens Hutch to failed builds.")
    static var openAppWhenRun = true

    @Parameter(title: "Scope", default: .all)
    var scope: HutchShortcutScope

    var route: HutchRoute { .failedBuilds }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

struct OpenAssignedTicketsIntent: AppIntent {
    static var title: LocalizedStringResource = "Open Assigned Tickets"
    static var description = IntentDescription("Opens Hutch to tickets assigned to you.")
    static var openAppWhenRun = true

    @Parameter(title: "Scope", default: .all)
    var scope: HutchShortcutScope

    var route: HutchRoute { .workQueue(scope: .assigned) }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

struct SearchHutchIntent: AppIntent {
    static var title: LocalizedStringResource = "Search Hutch"
    static var description = IntentDescription("Opens Hutch lookup with a search query.")
    static var openAppWhenRun = true

    @Parameter(title: "Query")
    var query: String

    var route: HutchRoute {
        let normalized = query.trimmingCharacters(in: .whitespacesAndNewlines)
        // Routes to Lookup for now; repoint at a global content search when Hutch
        // gains one — tracked in ROADMAP.md § "App Intent gaps".
        return normalized.isEmpty ? .lookup : .search(query: normalized)
    }

    @MainActor
    func perform() async throws -> some IntentResult {
        HutchIntentNavigator.shared.open(route)
        return .result()
    }
}

// An OpenSavedSearchIntent belongs here once Hutch has global saved-search
// persistence — tracked in ROADMAP.md § "App Intent gaps".

// MARK: - App Entities

struct PinnedResourceEntity: AppEntity, Identifiable {
    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Pinned Resource")
    static var defaultQuery = PinnedResourceQuery()

    let id: String
    let name: String
    let subtitle: String
    let route: HutchRoute

    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(name)", subtitle: "\(subtitle)")
    }
}

struct PinnedResourceQuery: EntityQuery {
    @MainActor
    func entities(for identifiers: [PinnedResourceEntity.ID]) async throws -> [PinnedResourceEntity] {
        HutchIntentEntityStore.pinnedResources().filter { identifiers.contains($0.id) }
    }

    @MainActor
    func suggestedEntities() async throws -> [PinnedResourceEntity] {
        HutchIntentEntityStore.pinnedResources()
    }
}

struct ProjectEntity: AppEntity, Identifiable {
    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Project")
    static var defaultQuery = ProjectEntityQuery()

    let id: String
    let name: String

    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(name)")
    }
}

struct ProjectEntityQuery: EntityQuery {
    @MainActor
    func entities(for identifiers: [ProjectEntity.ID]) async throws -> [ProjectEntity] {
        HutchIntentEntityStore.projects().filter { identifiers.contains($0.id) }
    }

    @MainActor
    func suggestedEntities() async throws -> [ProjectEntity] {
        HutchIntentEntityStore.projects()
    }
}

private enum HutchIntentEntityStore {
    static func pinnedResources() -> [PinnedResourceEntity] {
        pins().compactMap { makePinnedResource(from: $0) }
    }

    static func projects() -> [ProjectEntity] {
        pins().compactMap { pin in
            guard pin.kind == .project else { return nil }
            return ProjectEntity(id: pin.value, name: pin.title)
        }
    }

    private static func pins() -> [HomePinRecord] {
        guard let userKey = ContributionWidgetContextStore.loadActor(),
              !userKey.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
        else {
            return []
        }

        return HomePinStore.loadPins(for: userKey, defaults: activeAccountDefaults)
    }

    private static var activeAccountDefaults: UserDefaults {
        let activeID = UserDefaults.standard.string(forKey: AppStorageKeys.activeAccountID) ?? ""
        guard !activeID.isEmpty else { return .standard }
        return AccountDefaultsStore.userDefaults(for: activeID)
    }

    private static func makePinnedResource(from pin: HomePinRecord) -> PinnedResourceEntity? {
        guard let route = route(for: pin) else { return nil }
        return PinnedResourceEntity(
            id: pin.id,
            name: pin.title,
            subtitle: pin.subtitle,
            route: route
        )
    }

    private static func route(for pin: HomePinRecord) -> HutchRoute? {
        switch pin.kind {
        case .project:
            return .projectDashboard(id: pin.value, title: pin.title)
        case .repository:
            guard let owner = pin.ownerUsername else { return nil }
            return .repository(
                service: pin.service ?? .git,
                owner: formattedOwner(owner),
                repo: pin.value
            )
        case .tracker:
            guard let owner = pin.ownerUsername else { return nil }
            return .tracker(owner: formattedOwner(owner), tracker: pin.value)
        case .mailingList:
            guard let owner = pin.ownerUsername else { return nil }
            return .mailingList(owner: formattedOwner(owner), list: pin.value)
        case .user:
            guard let owner = pin.ownerUsername else { return nil }
            return .userProfile(owner: formattedOwner(owner))
        }
    }

    private static func formattedOwner(_ owner: String) -> String {
        owner.hasPrefix("~") ? owner : "~\(owner)"
    }
}

// MARK: - Existing Read-Only Summary Intents

struct CheckSystemStatusIntent: AppIntent {
    static var title: LocalizedStringResource = "Check SourceHut Status"
    static var description = IntentDescription("Returns the current SourceHut system status.")

    @MainActor
    func perform() async throws -> some IntentResult & ReturnsValue<String> {
        guard let snapshot = SystemStatusWidgetSnapshotStore.load() else {
            return .result(value: "System status is unavailable. Open Hutch to refresh.")
        }

        if snapshot.hasDisruption {
            let disrupted = snapshot.services
                .filter { $0.requiresAttention }
                .map { "\($0.name): \($0.status)" }
                .joined(separator: ", ")
            return .result(value: "SourceHut disruption detected: \(disrupted)")
        }

        return .result(value: "All SourceHut services operational.")
    }
}

struct CheckBuildsIntent: AppIntent {
    static var title: LocalizedStringResource = "Check Hutch Builds"
    static var description = IntentDescription("Returns a summary of your recent build status.")

    @MainActor
    func perform() async throws -> some IntentResult & ReturnsValue<String> {
        guard let snapshot = NeedsAttentionSnapshotStore.load() else {
            return .result(value: "Build status unavailable. Open Hutch to refresh.")
        }

        var parts: [String] = []

        if let failed = snapshot.failedBuilds {
            if failed > 0 {
                parts.append("\(failed) failed build\(failed == 1 ? "" : "s")")
            } else {
                parts.append("No failed builds")
            }
        }

        if let unread = snapshot.unreadInboxThreads, unread > 0 {
            parts.append("\(unread) unread thread\(unread == 1 ? "" : "s")")
        }

        if let assigned = snapshot.assignedOpenTickets, assigned > 0 {
            parts.append("\(assigned) assigned ticket\(assigned == 1 ? "" : "s")")
        }

        if parts.isEmpty {
            return .result(value: "No recent data. Open Hutch to refresh.")
        }

        return .result(value: parts.joined(separator: ". ") + ".")
    }
}

// MARK: - Shortcuts Provider

struct HutchShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: OpenWorkQueueIntent(),
            phrases: [
                "Open my work queue in \(.applicationName)",
                "Show work in \(.applicationName)"
            ],
            shortTitle: "Work Queue",
            systemImageName: "tray.full"
        )

        AppShortcut(
            intent: OpenRecentActivityIntent(),
            phrases: [
                "Open recent activity in \(.applicationName)",
                "Show activity in \(.applicationName)"
            ],
            shortTitle: "Recent Activity",
            systemImageName: "clock.arrow.circlepath"
        )

        AppShortcut(
            intent: OpenSystemStatusIntent(),
            phrases: [
                "Open system status in \(.applicationName)",
                "Show SourceHut status in \(.applicationName)"
            ],
            shortTitle: "System Status",
            systemImageName: "server.rack"
        )

        AppShortcut(
            intent: OpenPinnedResourceIntent(),
            phrases: [
                "Open \(\.$pinnedResource) in \(.applicationName)",
                "Show my pinned \(\.$pinnedResource) in \(.applicationName)"
            ],
            shortTitle: "Pinned Resource",
            systemImageName: "pin"
        )

        AppShortcut(
            intent: OpenProjectDashboardIntent(),
            phrases: [
                "Open \(\.$project) dashboard in \(.applicationName)",
                "Show project \(\.$project) in \(.applicationName)"
            ],
            shortTitle: "Project Dashboard",
            systemImageName: "square.stack.3d.up"
        )

        AppShortcut(
            intent: OpenFailedBuildsIntent(),
            phrases: [
                "Open failed builds in \(.applicationName)",
                "Show failed builds in \(.applicationName)"
            ],
            shortTitle: "Failed Builds",
            systemImageName: "exclamationmark.triangle"
        )

        AppShortcut(
            intent: OpenAssignedTicketsIntent(),
            phrases: [
                "Open assigned tickets in \(.applicationName)",
                "Show my assigned tickets in \(.applicationName)"
            ],
            shortTitle: "Assigned Tickets",
            systemImageName: "person.crop.circle.badge.checkmark"
        )

        AppShortcut(
            intent: SearchHutchIntent(),
            phrases: [
                "Search \(.applicationName)",
                "Look up something in \(.applicationName)"
            ],
            shortTitle: "Search Hutch",
            systemImageName: "magnifyingglass"
        )

        AppShortcut(
            intent: CheckSystemStatusIntent(),
            phrases: [
                "Check \(.applicationName) status",
                "Is SourceHut up in \(.applicationName)"
            ],
            shortTitle: "Check Status",
            systemImageName: "server.rack"
        )

        AppShortcut(
            intent: CheckBuildsIntent(),
            phrases: [
                "Check my \(.applicationName) builds",
                "Build status in \(.applicationName)"
            ],
            shortTitle: "Check Builds",
            systemImageName: "hammer"
        )
    }
}

// MARK: - Intent Navigator

@MainActor
@Observable
final class HutchIntentNavigator {
    static let shared = HutchIntentNavigator()
    var pendingRoute: HutchRoute?

    private init() {
        /* Singleton; external code uses `shared`. */
    }

    func open(_ route: HutchRoute) {
        pendingRoute = route
    }
}