Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 31 additions & 12 deletions site/lib/src/client/global_scripts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ void _setUpTabs() {
// If the tab wrapper and this tab have a save key and ID defined,
// switch other tabs to the tab with the same ID.
_findAndActivateTabsWithSaveId(currentSaveKey, currentSaveId);
web.window.localStorage.setItem(
'tab-save-$currentSaveKey',
currentSaveId,
);
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @parlough I added the changes in the rest of the files. For now I have added some print statements in debug mode. Please feel free to direct me if we need to perform other actions there.

Also, it'd be great if you can give tips on testing this.

web.window.localStorage.setItem(
'tab-save-$currentSaveKey',
currentSaveId,
);
} catch (e) {
if (kDebugMode) {
print('Error accessing localStorage $e');
}
}
} else {
_clearActiveTabs(tabs);
_setActiveTab(tabElement);
Expand All @@ -129,12 +135,19 @@ void _setUpTabs() {

tabElement.addEventListener('click', handleClick.toJS);

// If a tab was previously specified as selected in local storage,
// save a reference to it that can be switched to later.
if (saveId.isNotEmpty &&
localStorageKey != null &&
web.window.localStorage.getItem(localStorageKey) == saveId) {
tabToChangeTo = tabElement;
try {
// If a tab was previously specified as selected in local storage,
// save a reference to it that can be switched to later.
final tabSaveKey = localStorageKey != null
? web.window.localStorage.getItem(localStorageKey)
: null;
if (saveId.isNotEmpty && tabSaveKey != null && tabSaveKey == saveId) {
tabToChangeTo = tabElement;
}
} catch (e) {
if (kDebugMode) {
print('Error accessing localStorage $e');
}
}
}

Expand Down Expand Up @@ -165,8 +178,14 @@ void _updateTabsFromQueryParameters() {

for (final MapEntry(:key, :value) in originalQueryParameters.entries) {
if (key.startsWith('tab-save-')) {
web.window.localStorage.setItem(key, value);
updatedQueryParameters.remove(key);
try {
web.window.localStorage.setItem(key, value);
updatedQueryParameters.remove(key);
} catch (e) {
if (kDebugMode) {
print('Error accessing localStorage $e');
}
}
}
}

Expand Down
44 changes: 30 additions & 14 deletions site/lib/src/components/common/client/cookie_notice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@ final class _CookieNoticeState extends State<CookieNotice> {
void initState() {
if (kIsWeb) {
var shouldShowNotice = true;
if (web.window.localStorage.getItem(_cookieStorageKey)
case final lastConsentedMs?) {
if (int.tryParse(lastConsentedMs) case final msFromEpoch?) {
final consentedDateTime = DateTime.fromMillisecondsSinceEpoch(
msFromEpoch,
);
final difference = consentedDateTime.difference(DateTime.now());
if (difference.inDays < 180) {
// If consented less than 180 days ago, don't show the notice.
shouldShowNotice = false;
try {
final storedConsent = web.window.localStorage.getItem(
_cookieStorageKey,
);
if (storedConsent case final lastConsentedMs?) {
if (int.tryParse(lastConsentedMs) case final msFromEpoch?) {
final consentedDateTime = DateTime.fromMillisecondsSinceEpoch(
msFromEpoch,
);
final difference = consentedDateTime.difference(DateTime.now());
if (difference.inDays < 180) {
// If consented less than 180 days ago, don't show the notice.
shouldShowNotice = false;
}
}
}
} catch (e) {
// If localStorage is unavailable or throws an error,
// keep the `shouldShowNotice` to true.
if (kDebugMode) {
print('Failed to get stored content $e');
}
}

showNotice = shouldShowNotice;
Expand Down Expand Up @@ -69,10 +79,16 @@ final class _CookieNoticeState extends State<CookieNotice> {
content: 'OK, got it',
style: ButtonStyle.filled,
onClick: () {
web.window.localStorage.setItem(
_cookieStorageKey,
DateTime.now().millisecondsSinceEpoch.toString(),
);
try {
web.window.localStorage.setItem(
_cookieStorageKey,
DateTime.now().millisecondsSinceEpoch.toString(),
);
} catch (e) {
if (kDebugMode) {
print('Failed to set stored consent: $e');
}
}
setState(() {
showNotice = false;
});
Expand Down
8 changes: 7 additions & 1 deletion site/lib/src/components/layout/theme_switcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ final class _ThemeSwitcherState extends State<ThemeSwitcher> {
);
}

web.window.localStorage.setItem('theme', newTheme.id);
try {
web.window.localStorage.setItem('theme', newTheme.id);
} catch (e) {
if (kDebugMode) {
print('Failed to save theme preference: $e');
}
}

setState(() {
_currentTheme = newTheme;
Expand Down
22 changes: 13 additions & 9 deletions site/lib/src/layouts/dash_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,20 @@ ga('send', 'pageview');
// avoid a flash of the initial theme on load.
raw('''
<script>
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
try {
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');

const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
}
} catch (e) {
// localStorage is not available, do nothing and fallback to default.
}
</script>
'''),
Expand Down