93 lines
3.0 KiB
Dart
93 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/l10n/app_localizations.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../app_context.dart';
|
|
import '../Models/managerContext.dart';
|
|
|
|
/// Affiche la consommation IA du mois sous le bouton "Traduire via IA", pour que
|
|
/// l'utilisateur sache où il en est avant de lancer une traduction (le quota est
|
|
/// partagé avec l'assistant visiteur).
|
|
///
|
|
/// Pour forcer un rafraîchissement après une traduction, passer une `key` dont la
|
|
/// valeur change (ex. `ValueKey(compteur)`) : le State est recréé et refetch.
|
|
class AiQuotaHint extends StatefulWidget {
|
|
const AiQuotaHint({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<AiQuotaHint> createState() => _AiQuotaHintState();
|
|
}
|
|
|
|
class _AiQuotaHintState extends State<AiQuotaHint> {
|
|
InstanceQuotaDTO? _quota;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _fetch());
|
|
}
|
|
|
|
Future<void> _fetch() async {
|
|
final ctx = Provider.of<AppContext>(context, listen: false).getContext() as ManagerAppContext;
|
|
final instanceId = ctx.instanceId;
|
|
final client = ctx.clientAPI;
|
|
if (instanceId == null || client == null) return;
|
|
|
|
try {
|
|
final quota = await client.instanceApi!.instanceGetQuota(instanceId);
|
|
if (mounted) setState(() => _quota = quota);
|
|
} catch (_) {
|
|
// Silencieux : l'indication de quota ne doit jamais bloquer la traduction
|
|
}
|
|
}
|
|
|
|
static String _formatTokens(int tokens) {
|
|
if (tokens < 1000) return '$tokens';
|
|
if (tokens < 1000000) return '${(tokens / 1000).toStringAsFixed(0)}K';
|
|
return '${(tokens / 1000000).toStringAsFixed(1)}M';
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_quota == null) return const SizedBox.shrink();
|
|
|
|
final used = _quota!.aiTokensUsed ?? 0;
|
|
final quota = _quota!.aiTokensPerMonth ?? 0;
|
|
|
|
// 0 = pas de plafond côté backend (le test est `quota > 0`)
|
|
if (quota == 0) {
|
|
return Text(
|
|
AppLocalizations.of(context)!.aiQuotaUnlimited,
|
|
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
|
|
textAlign: TextAlign.center,
|
|
);
|
|
}
|
|
|
|
final ratio = used / quota;
|
|
final color = ratio >= 0.95
|
|
? Colors.red
|
|
: ratio >= 0.80
|
|
? Colors.orange
|
|
: Colors.grey[600];
|
|
|
|
return Column(
|
|
children: [
|
|
Text(
|
|
AppLocalizations.of(context)!.aiQuotaUsed(_formatTokens(used), _formatTokens(quota)),
|
|
style: TextStyle(fontSize: 12, color: color, fontWeight: ratio >= 0.80 ? FontWeight.w600 : FontWeight.normal),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
if (ratio >= 0.80)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 2),
|
|
child: Text(
|
|
AppLocalizations.of(context)!.aiQuotaSharedWithGuide,
|
|
style: TextStyle(fontSize: 11, color: Colors.grey[500]),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|