Bot key management

This commit is contained in:
2025-08-23 23:35:37 +08:00
parent 6f4f1216ad
commit 3959f2260b
11 changed files with 938 additions and 6 deletions

View File

@@ -0,0 +1,142 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:island/models/bot.dart';
import 'package:island/screens/developers/bot_keys.dart';
import 'package:island/screens/developers/edit_bot.dart';
import 'package:island/widgets/app_scaffold.dart';
import 'package:island/widgets/content/cloud_files.dart';
import 'package:island/widgets/response.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:go_router/go_router.dart';
import 'package:styled_widget/styled_widget.dart';
class BotDetailScreen extends HookConsumerWidget {
final String publisherName;
final String projectId;
final String botId;
const BotDetailScreen({
super.key,
required this.publisherName,
required this.projectId,
required this.botId,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final tabController = useTabController(initialLength: 2);
final botData = ref.watch(botProvider(publisherName, projectId, botId));
return AppScaffold(
appBar: AppBar(
title: Text(botData.value?.account.nick ?? 'botDetails'.tr()),
actions: [
IconButton(
icon: const Icon(Symbols.edit),
onPressed:
botData.value == null
? null
: () {
context.pushNamed(
'developerBotEdit',
pathParameters: {
'name': publisherName,
'projectId': projectId,
'id': botId,
},
);
},
),
],
bottom: TabBar(
controller: tabController,
tabs: [Tab(text: 'overview'.tr()), Tab(text: 'keys'.tr())],
),
),
body: botData.when(
data: (bot) {
if (bot == null) {
return Center(child: Text('botNotFound'.tr()));
}
return TabBarView(
controller: tabController,
physics: const NeverScrollableScrollPhysics(),
children: [
_BotOverview(bot: bot),
BotKeysScreen(
publisherName: publisherName,
projectId: projectId,
botId: botId,
),
],
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error:
(err, stack) => ResponseErrorWidget(
error: err,
onRetry:
() => ref.invalidate(
botProvider(publisherName, projectId, botId),
),
),
),
);
}
}
class _BotOverview extends StatelessWidget {
final Bot bot;
const _BotOverview({required this.bot});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
AspectRatio(
aspectRatio: 16 / 7,
child: Stack(
clipBehavior: Clip.none,
fit: StackFit.expand,
children: [
Container(
color: Theme.of(context).colorScheme.surfaceContainer,
child:
bot.account.profile.background != null
? CloudFileWidget(
item: bot.account.profile.background!,
fit: BoxFit.cover,
)
: const SizedBox.shrink(),
),
Positioned(
left: 20,
bottom: -32,
child: ProfilePictureWidget(
fileId: bot.account.profile.picture?.id,
radius: 40,
fallbackIcon: Symbols.smart_toy,
),
),
],
),
).padding(bottom: 32),
ListTile(title: Text('name'.tr()), subtitle: Text(bot.account.name)),
ListTile(
title: Text('nickname'.tr()),
subtitle: Text(bot.account.nick),
),
ListTile(title: Text('slug'.tr()), subtitle: Text(bot.slug)),
if (bot.account.profile.bio.isNotEmpty)
ListTile(
title: Text('bio'.tr()),
subtitle: Text(bot.account.profile.bio),
),
],
).padding(bottom: 24),
);
}
}

View File

@@ -0,0 +1,237 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:gap/gap.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:island/models/bot_key.dart';
import 'package:island/pods/network.dart';
import 'package:island/widgets/alert.dart';
import 'package:island/widgets/content/sheet.dart';
import 'package:island/widgets/response.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'bot_keys.g.dart';
@riverpod
Future<List<SnAccountApiKey>> botKeys(
Ref ref,
String publisherName,
String projectId,
String botId,
) async {
final client = ref.watch(apiClientProvider);
final resp = await client.get(
'/develop/developers/$publisherName/projects/$projectId/bots/$botId/keys',
);
return (resp.data as List).map((e) => SnAccountApiKey.fromJson(e)).toList();
}
class BotKeysScreen extends HookConsumerWidget {
final String publisherName;
final String projectId;
final String botId;
const BotKeysScreen({
super.key,
required this.publisherName,
required this.projectId,
required this.botId,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final keys = ref.watch(botKeysProvider(publisherName, projectId, botId));
final keyNameController = useTextEditingController();
void showNewKeySheet(SnAccountApiKey newApiKey) {
final token = newApiKey.key;
if (token == null) return;
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder:
(context) => SheetScaffold(
titleText: 'newKeyGenerated'.tr(),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
Text('copyKeyHint'.tr()),
const SizedBox(height: 16),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainer,
borderRadius: BorderRadius.circular(8),
),
child: SelectableText(token),
),
const SizedBox(height: 20),
FilledButton.icon(
onPressed: () {
Clipboard.setData(ClipboardData(text: token));
},
icon: const Icon(Symbols.copy_all),
label: Text('copy'.tr()),
),
],
),
),
),
).whenComplete(() {
ref.invalidate(botKeysProvider(publisherName, projectId, botId));
});
}
void createKey() {
keyNameController.clear();
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder:
(context) => SheetScaffold(
heightFactor: 0.65,
titleText: 'newBotKey'.tr(),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
controller: keyNameController,
decoration: InputDecoration(labelText: 'keyName'.tr()),
autofocus: true,
),
const SizedBox(height: 20),
FilledButton.icon(
onPressed: () async {
if (keyNameController.text.isEmpty) return;
final keyName = keyNameController.text;
Navigator.pop(context); // Close the sheet
try {
final client = ref.read(apiClientProvider);
final resp = await client.post(
'/develop/developers/$publisherName/projects/$projectId/bots/$botId/keys',
data: {'label': keyName},
);
final newApiKey = SnAccountApiKey.fromJson(resp.data);
showNewKeySheet(newApiKey);
} catch (e) {
showErrorAlert(e.toString());
}
},
icon: const Icon(Symbols.add),
label: Text('create'.tr()),
),
],
),
),
),
);
}
void revokeKey(String keyId) {
showConfirmAlert('revokeBotKeyHint'.tr(), 'revokeBotKey'.tr()).then((
confirm,
) {
if (confirm) {
final client = ref.read(apiClientProvider);
client
.delete(
'/develop/developers/$publisherName/projects/$projectId/bots/$botId/keys/$keyId',
)
.then((_) {
ref.invalidate(
botKeysProvider(publisherName, projectId, botId),
);
})
.catchError((err) {
showErrorAlert(err.toString());
});
}
});
}
return Column(
children: [
ListTile(
leading: const Icon(Symbols.add),
title: Text('newBotKey'.tr()),
trailing: const Icon(Symbols.chevron_right),
onTap: createKey,
),
const Divider(height: 1),
Expanded(
child: keys.when(
data: (data) {
if (data.isEmpty) {
return Center(child: Text('noBotKeys'.tr()));
}
return RefreshIndicator(
onRefresh:
() => ref.refresh(
botKeysProvider(publisherName, projectId, botId).future,
),
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: data.length,
itemBuilder: (context, index) {
final apiKey = data[index];
return ListTile(
title: Text(apiKey.label),
subtitle: Text(
'Created: ${DateFormat.yMMMd().format(apiKey.createdAt)}',
),
contentPadding: EdgeInsets.only(left: 16, right: 12),
trailing: PopupMenuButton(
itemBuilder:
(context) => [
PopupMenuItem(
value: 'revoke',
child: Row(
children: [
const Icon(
Symbols.delete,
color: Colors.red,
),
const Gap(12),
Text(
'revoke'.tr(),
style: TextStyle(color: Colors.red),
),
],
),
),
],
onSelected: (value) {
if (value == 'revoke') {
revokeKey(apiKey.id);
}
},
),
);
},
),
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error:
(err, stack) => ResponseErrorWidget(
error: err,
onRetry:
() => ref.invalidate(
botKeysProvider(publisherName, projectId, botId),
),
),
),
),
],
);
}
}

View File

@@ -0,0 +1,172 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'bot_keys.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$botKeysHash() => r'f7d1121833dc3da0cbd84b6171c2b2539edeb785';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
/// See also [botKeys].
@ProviderFor(botKeys)
const botKeysProvider = BotKeysFamily();
/// See also [botKeys].
class BotKeysFamily extends Family<AsyncValue<List<SnAccountApiKey>>> {
/// See also [botKeys].
const BotKeysFamily();
/// See also [botKeys].
BotKeysProvider call(String publisherName, String projectId, String botId) {
return BotKeysProvider(publisherName, projectId, botId);
}
@override
BotKeysProvider getProviderOverride(covariant BotKeysProvider provider) {
return call(provider.publisherName, provider.projectId, provider.botId);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'botKeysProvider';
}
/// See also [botKeys].
class BotKeysProvider extends AutoDisposeFutureProvider<List<SnAccountApiKey>> {
/// See also [botKeys].
BotKeysProvider(String publisherName, String projectId, String botId)
: this._internal(
(ref) => botKeys(ref as BotKeysRef, publisherName, projectId, botId),
from: botKeysProvider,
name: r'botKeysProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$botKeysHash,
dependencies: BotKeysFamily._dependencies,
allTransitiveDependencies: BotKeysFamily._allTransitiveDependencies,
publisherName: publisherName,
projectId: projectId,
botId: botId,
);
BotKeysProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.publisherName,
required this.projectId,
required this.botId,
}) : super.internal();
final String publisherName;
final String projectId;
final String botId;
@override
Override overrideWith(
FutureOr<List<SnAccountApiKey>> Function(BotKeysRef provider) create,
) {
return ProviderOverride(
origin: this,
override: BotKeysProvider._internal(
(ref) => create(ref as BotKeysRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
publisherName: publisherName,
projectId: projectId,
botId: botId,
),
);
}
@override
AutoDisposeFutureProviderElement<List<SnAccountApiKey>> createElement() {
return _BotKeysProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is BotKeysProvider &&
other.publisherName == publisherName &&
other.projectId == projectId &&
other.botId == botId;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, publisherName.hashCode);
hash = _SystemHash.combine(hash, projectId.hashCode);
hash = _SystemHash.combine(hash, botId.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin BotKeysRef on AutoDisposeFutureProviderRef<List<SnAccountApiKey>> {
/// The parameter `publisherName` of this provider.
String get publisherName;
/// The parameter `projectId` of this provider.
String get projectId;
/// The parameter `botId` of this provider.
String get botId;
}
class _BotKeysProviderElement
extends AutoDisposeFutureProviderElement<List<SnAccountApiKey>>
with BotKeysRef {
_BotKeysProviderElement(super.provider);
@override
String get publisherName => (origin as BotKeysProvider).publisherName;
@override
String get projectId => (origin as BotKeysProvider).projectId;
@override
String get botId => (origin as BotKeysProvider).botId;
}
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package

View File

@@ -140,9 +140,13 @@ class BotsScreen extends HookConsumerWidget {
},
),
onTap: () {
context.goNamed(
'accountProfile',
pathParameters: {'name': bot.account.name},
context.pushNamed(
'developerBotDetail',
pathParameters: {
'name': publisherName,
'projectId': projectId,
'botId': bot.id,
},
);
},
),

View File

@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:island/screens/developers/edit_bot.dart';
class NewBotScreen extends StatelessWidget {
final String publisherName;
final String projectId;
const NewBotScreen({super.key, required this.publisherName, required this.projectId});
@override
Widget build(BuildContext context) {
return EditBotScreen(publisherName: publisherName, projectId: projectId);
}
}