✨ Thinking billing check
This commit is contained in:
@@ -6,6 +6,7 @@ import "package:riverpod_annotation/riverpod_annotation.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:island/models/thought.dart";
|
||||
import "package:island/pods/network.dart";
|
||||
import "package:island/widgets/alert.dart";
|
||||
import "package:island/widgets/app_scaffold.dart";
|
||||
import "package:island/widgets/response.dart";
|
||||
import "package:island/widgets/thought/thought_sequence_list.dart";
|
||||
@@ -14,6 +15,13 @@ import "package:material_symbols_icons/material_symbols_icons.dart";
|
||||
|
||||
part 'think.g.dart';
|
||||
|
||||
@riverpod
|
||||
Future<bool> thoughtAvailableStaus(Ref ref) async {
|
||||
final apiClient = ref.watch(apiClientProvider);
|
||||
final response = await apiClient.get('/insight/billing/status');
|
||||
return response.data['status'] == 'ok';
|
||||
}
|
||||
|
||||
@riverpod
|
||||
Future<List<SnThinkingThought>> thoughtSequence(
|
||||
Ref ref,
|
||||
@@ -47,6 +55,8 @@ class ThoughtScreen extends HookConsumerWidget {
|
||||
? initialThoughts.first.sequence!.topic
|
||||
: 'aiThought'.tr();
|
||||
|
||||
final statusAsync = ref.watch(thoughtAvailableStausProvider);
|
||||
|
||||
return AppScaffold(
|
||||
isNoBackground: false,
|
||||
appBar: AppBar(
|
||||
@@ -71,23 +81,91 @@ class ThoughtScreen extends HookConsumerWidget {
|
||||
const Gap(8),
|
||||
],
|
||||
),
|
||||
body: thoughts.when(
|
||||
data:
|
||||
(thoughtList) => ThoughtChatInterface(
|
||||
initialThoughts: thoughtList,
|
||||
initialTopic: initialTopic,
|
||||
),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error:
|
||||
(error, _) => ResponseErrorWidget(
|
||||
error: error,
|
||||
onRetry:
|
||||
() =>
|
||||
selectedSequenceId.value != null
|
||||
? ref.invalidate(
|
||||
thoughtSequenceProvider(selectedSequenceId.value!),
|
||||
)
|
||||
: null,
|
||||
body: statusAsync.maybeWhen(
|
||||
data: (status) {
|
||||
final retry = useMemoized(
|
||||
() => () async {
|
||||
showLoadingModal(context);
|
||||
try {
|
||||
await ref
|
||||
.read(apiClientProvider)
|
||||
.post('/insight/billing/retry');
|
||||
showSnackBar('Retried billing process');
|
||||
ref.invalidate(thoughtAvailableStausProvider);
|
||||
} catch (e) {
|
||||
showSnackBar('Failed to retry billing');
|
||||
}
|
||||
hideLoadingModal(context);
|
||||
},
|
||||
[context, ref],
|
||||
);
|
||||
|
||||
final thoughtsBody = thoughts.when(
|
||||
data:
|
||||
(thoughtList) => ThoughtChatInterface(
|
||||
initialThoughts: thoughtList,
|
||||
initialTopic: initialTopic,
|
||||
isDisabled: !status,
|
||||
),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error:
|
||||
(error, _) => ResponseErrorWidget(
|
||||
error: error,
|
||||
onRetry:
|
||||
() =>
|
||||
selectedSequenceId.value != null
|
||||
? ref.invalidate(
|
||||
thoughtSequenceProvider(
|
||||
selectedSequenceId.value!,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
return status
|
||||
? thoughtsBody
|
||||
: Column(
|
||||
children: [
|
||||
MaterialBanner(
|
||||
leading: const Icon(Symbols.error),
|
||||
content: const Text(
|
||||
'You have unpaid orders. Please settle your payment to continue using the service.',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
retry();
|
||||
},
|
||||
child: Text('retry'.tr()),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(child: thoughtsBody),
|
||||
],
|
||||
);
|
||||
},
|
||||
orElse:
|
||||
() => thoughts.when(
|
||||
data:
|
||||
(thoughtList) => ThoughtChatInterface(
|
||||
initialThoughts: thoughtList,
|
||||
initialTopic: initialTopic,
|
||||
),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error:
|
||||
(error, _) => ResponseErrorWidget(
|
||||
error: error,
|
||||
onRetry:
|
||||
() =>
|
||||
selectedSequenceId.value != null
|
||||
? ref.invalidate(
|
||||
thoughtSequenceProvider(
|
||||
selectedSequenceId.value!,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user