Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PlatformRouteResolver.swift

90 lines3.3 KB

1import Foundation
2​
3enum PlatformRouteResolver {
4 static func thread(
5 in snapshot: FeatureSnapshot,
6 environmentID: String?,
7 id: String
8 ) -> FeatureThread? {
9 let matches = snapshot.threads.filter { thread in
10 (environmentID == nil || thread.environmentID == environmentID)
11 && (thread.id == id || thread.wireID == id)
12 }
13 guard environmentID != nil || matches.count == 1 else { return nil }
14 return matches.max { $0.updatedAt < $1.updatedAt }
15 }
16​
17 static func project(
18 in snapshot: FeatureSnapshot,
19 environmentID: String?,
20 id: String
21 ) -> FeatureProject? {
22 let matches = snapshot.projects.filter { project in
23 (environmentID == nil || project.environmentID == environmentID)
24 && (project.id == id || project.wireID == id)
25 }
26 guard environmentID != nil || matches.count == 1 else { return nil }
27 return matches.first
28 }
30​
31/// Decides which links tapped inside the app navigate in place instead of
32/// being handed to the system.
33///
34/// A T3 link only stays in the app when it names a destination this device can
35/// already show, so unknown web links keep opening on the web instead of
36/// failing with an in-app error. Pairing links are always left to the system so
37/// onboarding keeps owning connection confirmation.
38enum PlatformInAppLinkRouter {
39 static func route(for url: URL, in snapshot: FeatureSnapshot) -> PlatformRoute? {
40 guard let route = try? PlatformDeepLinkParser.parse(url) else { return nil }
41​
42 switch route {
43 case .usageLimits:
44 return route
45 case .connection:
46 return nil
47 case let .thread(environmentID, threadID):
48 guard isSavedEnvironment(environmentID, in: snapshot),
49 PlatformRouteResolver.thread(
50 in: snapshot,
51 environmentID: environmentID,
52 id: threadID
53 ) != nil
54 else {
55 return nil
56 }
57 return route
58 case let .project(environmentID, projectID):
59 guard isSavedEnvironment(environmentID, in: snapshot),
60 PlatformRouteResolver.project(
61 in: snapshot,
62 environmentID: environmentID,
63 id: projectID
64 ) != nil
65 else {
66 return nil
67 }
68 return route
69 case let .environment(id):
70 guard isSavedEnvironment(id, in: snapshot) else { return nil }
71 return route
72 case let .newTask(environmentID, projectID):
73 guard isSavedEnvironment(environmentID, in: snapshot) else { return nil }
74 guard let projectID else { return route }
75 guard PlatformRouteResolver.project(
76 in: snapshot,
77 environmentID: environmentID,
78 id: projectID
79 ) != nil else {
80 return nil
81 }
82 return route
83 }
84 }
85​
86 private static func isSavedEnvironment(_ id: String?, in snapshot: FeatureSnapshot) -> Bool {
87 guard let id else { return true }
88 return snapshot.environments.contains { $0.id == id }
89 }