Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: add url validation in cell_editor (#2287)

Shubham Rawat2af2621b4954
diff --git a/frontend/appflowy_flutter/lib/core/helpers/helpers.dart b/frontend/appflowy_flutter/lib/core/helpers/helpers.dart
index e325b1a7bb..fd832306e8 100644
--- a/frontend/appflowy_flutter/lib/core/helpers/helpers.dart
+++ b/frontend/appflowy_flutter/lib/core/helpers/helpers.dart
@@ -1 +1,2 @@
export 'target_platform.dart';
+export 'url_validator.dart';
diff --git a/frontend/appflowy_flutter/lib/core/helpers/url_validator.dart b/frontend/appflowy_flutter/lib/core/helpers/url_validator.dart
new file mode 100644
index 0000000000..bfead5619d
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/core/helpers/url_validator.dart
@@ -0,0 +1,21 @@
+import 'package:dartz/dartz.dart';
+import 'package:freezed_annotation/freezed_annotation.dart';
+part 'url_validator.freezed.dart';
+
+Either<UriFailure, Uri> parseValidUrl(String url) {
+ try {
+ final uri = Uri.parse(url);
+ if (uri.scheme.isEmpty || uri.host.isEmpty) {
+ return left(const UriFailure.invalidSchemeHost());
+ }
+ return right(uri);
+ } on FormatException {
+ return left(const UriFailure.invalidUriFormat());
+ }
+}
+
+@freezed
+class UriFailure with _$UriFailure {
+ const factory UriFailure.invalidSchemeHost() = _InvalidSchemeHost;
+ const factory UriFailure.invalidUriFormat() = _InvalidUriFormat;
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/url_cell/cell_editor.dart b/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/url_cell/cell_editor.dart
index a6270ab8d7..89249fc3bf 100644
--- a/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/url_cell/cell_editor.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/url_cell/cell_editor.dart
@@ -1,4 +1,6 @@
+import 'package:appflowy/core/helpers/helpers.dart';
import 'package:appflowy/plugins/database_view/application/cell/cell_controller_builder.dart';
+import 'package:flowy_infra_ui/style_widget/snap_bar.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -73,7 +75,18 @@ class _URLCellEditorState extends State<URLCellEditor> {
if (mounted) {
if (_cellBloc.isClosed == false &&
_controller.text != _cellBloc.state.content) {
- _cellBloc.add(URLCellEditorEvent.updateText(_controller.text));
+ final parseResult = parseValidUrl(_controller.text);
+
+ parseResult.fold(
+ (_) {
+ showSnapBar(
+ context,
+ "Enter a valid URL",
+ Theme.of(context).colorScheme.error,
+ );
+ },
+ (_) => _cellBloc.add(URLCellEditorEvent.updateText(_controller.text)),
+ );
}
}
}
diff --git a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/snap_bar.dart b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/snap_bar.dart
index 8c63a330ca..dfa12313a3 100644
--- a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/snap_bar.dart
+++ b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/style_widget/snap_bar.dart
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
-void showSnapBar(BuildContext context, String title) {
+void showSnapBar(BuildContext context, String title, [Color? backgroundColor]) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context)
@@ -18,6 +18,7 @@ void showSnapBar(BuildContext context, String title) {
),
),
),
+ backgroundColor: backgroundColor,
),
)
.closed