Files
pipelines/app/lib/application/app_route.dart
T

97 lines
3.2 KiB
Dart

/// The destinations the app has a URL for.
///
/// The set mirrors [AppDestination] but stays free of widget imports so the
/// parser and serialiser can be exercised without a Flutter host.
enum AppRouteKind { pipelines, runs, chat, settings }
/// One page route: a destination plus the run it opens, if any.
///
/// Parsing is total, like the web client's route model: an unknown or
/// malformed path resolves to the default Pipelines list instead of throwing.
/// The serialiser is its inverse for every route the app can produce.
class AppRoute {
const AppRoute(this.kind, {this.runId});
static const pipelines = AppRoute(AppRouteKind.pipelines);
static const runs = AppRoute(AppRouteKind.runs);
static const chat = AppRoute(AppRouteKind.chat);
static const settings = AppRoute(AppRouteKind.settings);
static const run = _RunRouteFactory();
final AppRouteKind kind;
/// The run shown by the Runs destination, or null for the list.
final int? runId;
/// The path the app writes into history for this route.
String get path => switch (kind) {
AppRouteKind.pipelines => '/pipelines',
AppRouteKind.runs => runId == null ? '/runs' : '/runs/$runId',
AppRouteKind.chat => '/chat',
AppRouteKind.settings => '/settings',
};
/// Parses a browser pathname (or a full URL) into a route.
///
/// `/runs/abc` and other malformed details are the default route, matching
/// the web parser; `/pipelines/` and other trailing slashes are normalised.
static AppRoute parse(String location) {
var path = location.trim();
if (path.isEmpty) return pipelines;
if (path.contains('://')) {
final uri = Uri.tryParse(path);
if (uri == null) return pipelines;
path = uri.path;
}
final hash = path.indexOf('#');
if (hash != -1) path = path.substring(0, hash);
final query = path.indexOf('?');
if (query != -1) path = path.substring(0, query);
final segments = <String>[];
for (final raw in path.split('/')) {
if (raw.isEmpty) continue;
try {
segments.add(Uri.decodeComponent(raw));
} on ArgumentError {
return pipelines;
}
}
if (segments.isEmpty) return pipelines;
return switch (segments.first) {
'pipelines' when segments.length == 1 => pipelines,
'runs' when segments.length == 1 => runs,
'runs' when segments.length == 2 => _runRoute(segments[1]),
'chat' when segments.length == 1 => chat,
'settings' when segments.length == 1 => settings,
_ => pipelines,
};
}
static AppRoute _runRoute(String value) {
if (!RegExp(r'^[0-9]+$').hasMatch(value)) return pipelines;
final id = int.tryParse(value);
if (id == null || id <= 0) return pipelines;
return AppRoute(AppRouteKind.runs, runId: id);
}
@override
bool operator ==(Object other) =>
other is AppRoute && other.kind == kind && other.runId == runId;
@override
int get hashCode => Object.hash(kind, runId);
@override
String toString() => path;
}
/// `AppRoute.run(3)` reads better than a constructor named after a field.
class _RunRouteFactory {
const _RunRouteFactory();
AppRoute call(int id) => AppRoute(AppRouteKind.runs, runId: id);
}