mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
Refactor DashBot
This commit is contained in:
15
lib/dashbot/utils/dashbot_icons.dart
Normal file
15
lib/dashbot/utils/dashbot_icons.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class DashbotIcons {
|
||||
DashbotIcons._();
|
||||
static String get dashbotIcon1 => 'assets/dashbot/dashbot_icon_1.png';
|
||||
static String get dashbotIcon2 => 'assets/dashbot/dashbot_icon_2.png';
|
||||
|
||||
static Image getDashbotIcon1({double? width, double? height, BoxFit? fit}) {
|
||||
return Image.asset(dashbotIcon1, width: width, height: height, fit: fit);
|
||||
}
|
||||
|
||||
static Image getDashbotIcon2({double? width, double? height, BoxFit? fit}) {
|
||||
return Image.asset(dashbotIcon2, width: width, height: height, fit: fit);
|
||||
}
|
||||
}
|
||||
22
lib/dashbot/utils/dashbot_route_utils.dart
Normal file
22
lib/dashbot/utils/dashbot_route_utils.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:apidash/models/models.dart';
|
||||
|
||||
import '../routes/dashbot_routes.dart';
|
||||
|
||||
/// Computes the base Dashbot route for a given request based on whether a
|
||||
/// response exists.
|
||||
/// - Returns [DashbotRoutes.dashbotHome] if the request has a response (either
|
||||
/// statusCode or responseStatus present).
|
||||
/// - Otherwise returns [DashbotRoutes.dashbotDefault].
|
||||
String computeDashbotBaseRoute(RequestModel? req) {
|
||||
final hasResponse = (req?.httpResponseModel?.statusCode != null) ||
|
||||
(req?.responseStatus != null);
|
||||
return hasResponse ? DashbotRoutes.dashbotHome : DashbotRoutes.dashbotDefault;
|
||||
}
|
||||
|
||||
/// Returns true if the route that should be shown for [req] differs from the
|
||||
/// currently active [currentRoute].
|
||||
/// This helper is pure and does not perform any side effects.
|
||||
bool needsDashbotRouteChange(RequestModel? req, String currentRoute) {
|
||||
final target = computeDashbotBaseRoute(req);
|
||||
return target != currentRoute;
|
||||
}
|
||||
30
lib/dashbot/utils/safe_parse_json_message.dart
Normal file
30
lib/dashbot/utils/safe_parse_json_message.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'dart:convert';
|
||||
|
||||
/// Lightweight JSON parser helper to avoid adding dependencies.
|
||||
/// Intended for parsing AI agent structured outputs that may be wrapped
|
||||
/// in markdown code fences or include extra prose.
|
||||
class MessageJson {
|
||||
static Map<String, dynamic> safeParse(String input) {
|
||||
// Try strict JSON first
|
||||
try {
|
||||
return _parseJson(input);
|
||||
} catch (_) {
|
||||
// If input looks like markdown fenced block containing JSON, try to extract
|
||||
final start = input.indexOf('{');
|
||||
final end = input.lastIndexOf('}');
|
||||
if (start != -1 && end != -1 && end > start) {
|
||||
final slice = input.substring(start, end + 1);
|
||||
return _parseJson(slice);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
static Map<String, dynamic> _parseJson(String s) {
|
||||
final decoded = jsonDecode(s);
|
||||
if (decoded is Map<String, dynamic>) {
|
||||
return decoded;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
28
lib/dashbot/utils/show_dashbot.dart
Normal file
28
lib/dashbot/utils/show_dashbot.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../dashbot_dashboard.dart';
|
||||
import '../providers/providers.dart';
|
||||
|
||||
void showDashbotWindow(BuildContext context, WidgetRef ref) {
|
||||
final isDashbotActive = ref.read(dashbotWindowNotifierProvider).isActive;
|
||||
final isDashbotPopped = ref.read(dashbotWindowNotifierProvider).isPopped;
|
||||
final windowNotifier = ref.read(dashbotWindowNotifierProvider.notifier);
|
||||
if (isDashbotActive) return;
|
||||
if (!isDashbotPopped) return;
|
||||
final overlay = Overlay.of(context);
|
||||
OverlayEntry? entry;
|
||||
|
||||
entry = OverlayEntry(
|
||||
builder: (context) => ProviderScope(
|
||||
child: DashbotWindow(
|
||||
onClose: () {
|
||||
entry?.remove();
|
||||
windowNotifier.toggleActive();
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
// Mark active and insert overlay
|
||||
windowNotifier.toggleActive();
|
||||
overlay.insert(entry);
|
||||
}
|
||||
4
lib/dashbot/utils/utils.dart
Normal file
4
lib/dashbot/utils/utils.dart
Normal file
@@ -0,0 +1,4 @@
|
||||
export 'dashbot_icons.dart';
|
||||
export 'dashbot_route_utils.dart';
|
||||
export 'safe_parse_json_message.dart';
|
||||
export 'show_dashbot.dart';
|
||||
Reference in New Issue
Block a user