Open History to see the last commit
1import Foundation
2import UIKit
3
4enum ThreadCopyActionKind: Equatable, Sendable {
5 case path
6 case branch
7 case threadID
8 case project
9 case environment
10 case url
11
12 var title: String {
13 switch self {
14 case .path: "Path"
15 case .branch: "Branch"
16 case .threadID: "Thread ID"
17 case .project: "Project"
18 case .environment: "Environment"
19 case .url: "URL"
20 }
21 }
22
23 var systemImage: String {
24 switch self {
25 case .path: "folder"
26 case .branch: "arrow.triangle.branch"
27 case .threadID: "number"
28 case .project: "folder.badge.gearshape"
29 case .environment: "desktopcomputer"
30 case .url: "link"
31 }
32 }
33
34 var copyAnnouncement: String { "\(title) copied" }
35}
36
37struct ThreadCopyContext: Equatable, Sendable {
38 let projectName: String?
39 let projectWorkspaceRoot: String?
40 let environmentName: String?
41 let environmentID: String?
42}
43
44struct ThreadCopyAction: Equatable, Sendable {
45 let kind: ThreadCopyActionKind
46 let value: String?
47
48 var isAvailable: Bool { value != nil }
49
50 var announcement: String {
51 isAvailable ? kind.copyAnnouncement : "\(kind.title) unavailable"
52 }
53}
54
55enum ThreadCopyModel {
56 /// The long-press row menu mirrors the Electron thread menu.
57 static func menuActions(
58 for thread: FeatureThread,
59 context: ThreadCopyContext
60 ) -> [ThreadCopyAction] {
61 actions(for: thread, context: context).filter { action in
62 switch action.kind {
63 case .path, .branch, .threadID:
64 true
65 case .project, .environment, .url:
66 false
67 }
68 }
69 }
70
71 static func actions(
72 for thread: FeatureThread,
73 context: ThreadCopyContext
74 ) -> [ThreadCopyAction] {
75 var actions: [ThreadCopyAction] = []
76
77 let path = nonBlank(thread.worktreePath) ?? nonBlank(context.projectWorkspaceRoot)
78 actions.append(ThreadCopyAction(kind: .path, value: path))
79 if let branch = nonBlank(thread.branch) {
80 actions.append(ThreadCopyAction(kind: .branch, value: branch))
81 }
82 if let threadID = nonBlank(thread.wireID) ?? nonBlank(thread.id) {
83 actions.append(ThreadCopyAction(kind: .threadID, value: threadID))
84 }
85 if let project = nonBlank(context.projectName) {
86 actions.append(ThreadCopyAction(kind: .project, value: project))
87 }
88 if let environment = nonBlank(context.environmentName) {
89 actions.append(ThreadCopyAction(kind: .environment, value: environment))
90 }
91
92 let environmentID = nonBlank(thread.environmentID) ?? nonBlank(context.environmentID)
93 if let url = threadURL(environmentID: environmentID, threadID: nonBlank(thread.wireID)) {
94 actions.append(ThreadCopyAction(kind: .url, value: url))
95 }
96
97 return actions
98 }
99
100 private static func threadURL(environmentID: String?, threadID: String?) -> String? {
101 guard let environmentID,
102 let threadID,
103 let encodedEnvironmentID = pathSegment(environmentID),
104 let encodedThreadID = pathSegment(threadID) else {
105 return nil
106 }
107
108 var components = URLComponents()
109 components.scheme = "https"
110 components.host = "app.t3.codes"
111 components.percentEncodedPath = "/\(encodedEnvironmentID)/\(encodedThreadID)"
112 return components.url?.absoluteString
113 }
114
115 private static func pathSegment(_ value: String) -> String? {
116 let allowed = CharacterSet(
117 charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
118 )
119 return value.addingPercentEncoding(withAllowedCharacters: allowed)
120 }
121
122 /// Availability ignores surrounding whitespace, while the copied value remains byte-for-byte
123 /// identical to the value received from the server.
124 private static func nonBlank(_ value: String?) -> String? {
125 guard let value,
126 !value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
127 return nil
128 }
129 return value
130 }
131}
132
133@MainActor
134enum ThreadCopyClipboard {
135 static func copy(_ action: ThreadCopyAction) {
136 if let value = action.value {
137 UIPasteboard.general.string = value
138 }
139 UIAccessibility.post(
140 notification: .announcement,
141 argument: action.announcement
142 )
143 }
144}