diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index ba5c5ece7a..62f3bf02c3 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -7,6 +7,7 @@ import 'package:fluffychat/pages/connect/sso_login_state.dart'; import 'package:fluffychat/utils/dialog/twake_dialog.dart'; import 'package:fluffychat/utils/exception/homeserver_exception.dart'; import 'package:fluffychat/utils/platform_infos.dart'; +import 'package:fluffychat/utils/string_extension.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -216,9 +217,9 @@ mixin ConnectPageMixin { String _generatePostLogoutRedirectUrl() { if (kIsWeb) { if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html'; + return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html'; } - return '${getBaseUrlBeforeHash(html.window.location.href)}web/auth.html'; + return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://redirect'; } @@ -230,9 +231,9 @@ mixin ConnectPageMixin { homeserverParam = '?homeserver=$homeserver'; } if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; + return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; } - return '${getBaseUrlBeforeHash(html.window.location.href)}web/auth.html$homeserverParam'; + return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html$homeserverParam'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://login'; } @@ -298,12 +299,7 @@ mixin ConnectPageMixin { html.window.history.replaceState( {}, '', - '${getBaseUrlBeforeHash(html.window.location.href)}#/${route ?? 'rooms'}', + '${html.window.location.href.getBaseUrlBeforeHash()}#/${route ?? 'rooms'}', ); } - - String getBaseUrlBeforeHash(String fullUrl) { - final fragmentIndex = fullUrl.indexOf('#/'); - return fragmentIndex != -1 ? fullUrl.substring(0, fragmentIndex) : fullUrl; - } } diff --git a/lib/utils/string_extension.dart b/lib/utils/string_extension.dart index 02d326a305..2bde62a822 100644 --- a/lib/utils/string_extension.dart +++ b/lib/utils/string_extension.dart @@ -376,4 +376,9 @@ extension StringCasingExtension on String { final match = regex.firstMatch(this); return match?.group(1); } + + String getBaseUrlBeforeHash() { + final fragmentIndex = indexOf('#/'); + return fragmentIndex != -1 ? substring(0, fragmentIndex) : this; + } } diff --git a/test/utils/string_extension_test.dart b/test/utils/string_extension_test.dart index 96fcbda0c9..3c40f60404 100644 --- a/test/utils/string_extension_test.dart +++ b/test/utils/string_extension_test.dart @@ -598,5 +598,41 @@ void main() { expect(result[i].style, expectedSpans[i].style); } }); + + test('getBaseUrlBeforeHash handles URL with hash', () { + const url = 'https://example.com/web/f/#/test'; + const expectedUrl = 'https://example.com/web/f/'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL without hash', () { + const url = 'https://example.com/test'; + const expectedUrl = 'https://example.com/test'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL with multiple hashes', () { + const url = 'https://example.com/#/test#section'; + const expectedUrl = 'https://example.com/'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL with query parameters and hash', () { + const url = 'https://example.com/test?query=1#/section'; + const expectedUrl = 'https://example.com/test?query=1'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); }); }