Skip to content

Commit

Permalink
Fix location path on web (#2067)
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored Sep 30, 2024
1 parent a828354 commit 637644f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/presentation/mixins/connect_page_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -216,9 +217,9 @@ mixin ConnectPageMixin {
String _generatePostLogoutRedirectUrl() {
if (kIsWeb) {
if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) {
return '${html.window.origin!}/twake-on-matrix/${AppConfig.issueId}/auth.html';
return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html';
}
return '${html.window.origin!}/web/auth.html';
return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html';
}
return '${AppConfig.appOpenUrlScheme.toLowerCase()}://redirect';
}
Expand All @@ -230,9 +231,9 @@ mixin ConnectPageMixin {
homeserverParam = '?homeserver=$homeserver';
}
if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) {
return '${html.window.origin!}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam';
return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam';
}
return '${html.window.origin!}/web/auth.html$homeserverParam';
return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html$homeserverParam';
}
return '${AppConfig.appOpenUrlScheme.toLowerCase()}://login';
}
Expand Down Expand Up @@ -295,6 +296,10 @@ mixin ConnectPageMixin {
}) {
final loginTokenExisted = getQueryParameter('loginToken') != null;
if (!loginTokenExisted) return;
html.window.history.replaceState({}, '', '/#/${route ?? 'rooms'}');
html.window.history.replaceState(
{},
'',
'${html.window.location.href.getBaseUrlBeforeHash()}#/${route ?? 'rooms'}',
);
}
}
5 changes: 5 additions & 0 deletions lib/utils/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
38 changes: 38 additions & 0 deletions test/utils/string_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,42 @@ void main() {
}
});
});

group('getBaseUrlBeforeHash test', () {
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));
});
});
}

0 comments on commit 637644f

Please sign in to comment.