Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix location path on web #2067

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
36 changes: 36 additions & 0 deletions test/utils/string_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
hoangdat marked this conversation as resolved.
Show resolved Hide resolved
});
}