Files
social-app/apps/lib/features/chat/data/tools/route_navigation_tool.dart
T

79 lines
1.7 KiB
Dart
Raw Normal View History

typedef RouteNavigator = void Function(String target, {bool replace});
const Set<String> _allowedRoutes = {
'/settings',
'/todo',
'/calendar/dayweek',
'/messages/invites',
};
const List<String> _allowedRoutePrefixes = [
'/calendar/events/',
];
class RouteNavigationTool {
RouteNavigationTool._();
static final RouteNavigationTool instance = RouteNavigationTool._();
RouteNavigator? _navigator;
void bindNavigator(RouteNavigator navigator) {
_navigator = navigator;
}
void clearNavigator() {
_navigator = null;
}
Map<String, dynamic> execute(Map<String, dynamic> args) {
final target = args['target'];
if (target is! String || target.isEmpty) {
return {
'ok': false,
'error': 'target is required',
};
}
if (!_isAllowedTarget(target)) {
return {
'ok': false,
'target': target,
'error': 'target is not allowed',
};
}
final replace = args['replace'] == true;
final navigator = _navigator;
if (navigator == null) {
return {
'ok': false,
'target': target,
'replace': replace,
'error': 'navigator not bound',
};
}
navigator(target, replace: replace);
return {
'ok': true,
'target': target,
'replace': replace,
'applied': true,
};
}
bool _isAllowedTarget(String target) {
if (!target.startsWith('/')) {
return false;
}
final normalized = target.split('?').first;
if (_allowedRoutes.contains(normalized)) {
return true;
}
for (final prefix in _allowedRoutePrefixes) {
if (normalized.startsWith(prefix)) {
return true;
}
}
return false;
}
}