123 lines
4.2 KiB
Dart
123 lines
4.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, this.chatSessionId});
|
|
|
|
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();
|
|
|
|
/// One chat session, or the chat list when [id] is not a session id.
|
|
static AppRoute chatSession(String id) => isChatSessionId(id)
|
|
? AppRoute(AppRouteKind.chat, chatSessionId: id)
|
|
: chat;
|
|
|
|
final AppRouteKind kind;
|
|
|
|
/// The run shown by the Runs destination, or null for the list.
|
|
final int? runId;
|
|
|
|
/// The chat session shown by the Chat destination, or null for the list.
|
|
final String? chatSessionId;
|
|
|
|
/// 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 =>
|
|
chatSessionId == null || !isChatSessionId(chatSessionId!)
|
|
? '/chat'
|
|
: '/chat/${Uri.encodeComponent(chatSessionId!)}',
|
|
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,
|
|
'chat' when segments.length == 2 => _chatRoute(segments[1]),
|
|
'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);
|
|
}
|
|
|
|
/// A chat session id is nonempty, bounded and URL-safe, matching the web
|
|
/// parser. Anything else is malformed rather than a stale session the
|
|
/// destination has to guess about.
|
|
static bool isChatSessionId(String value) =>
|
|
value.isNotEmpty &&
|
|
value.length <= 200 &&
|
|
RegExp(r'^[A-Za-z0-9_-]+$').hasMatch(value);
|
|
|
|
static AppRoute _chatRoute(String value) =>
|
|
isChatSessionId(value) ? chatSession(value) : pipelines;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is AppRoute &&
|
|
other.kind == kind &&
|
|
other.runId == runId &&
|
|
other.chatSessionId == chatSessionId;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(kind, runId, chatSessionId);
|
|
|
|
@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);
|
|
}
|