Skip to content

feat: preserve user settings when visiting random instance #454

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

S8M1T
Copy link

@S8M1T S8M1T commented Jul 17, 2025

Fixes user experience issue where custom settings were lost when switching instances during errors. Users now seamlessly retain their personalized experience across instances.

src/settings.rs Outdated

// Get current user settings as JSON for API consumption
pub async fn get_json(req: Request<Body>) -> Result<Response<Body>, String> {
// Create preferences - this should be safe but we wrap it for completeness
Copy link
Member

Choose a reason for hiding this comment

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

What are we wrapping, exactly? 🤔

Copy link
Author

Choose a reason for hiding this comment

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

Fixed! Removed the confusing comment entirely.

src/settings.rs Outdated
Comment on lines 306 to 313
// Try to encode preferences, but provide fallback if it fails
let url_encoded = match prefs.to_urlencoded() {
Ok(encoded) => Some(encoded),
Err(e) => {
eprintln!("Warning: Failed to encode preferences for settings transfer: {e}");
None
}
};
Copy link
Member

Choose a reason for hiding this comment

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

Everything but the "url_encoded field" isn't even accessed in the javascript at all. Remove everything after here, just return the urlencoded value as a string here. We can test for an error via the HTTP code.

Copy link
Author

Choose a reason for hiding this comment

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

Done! Simplified the endpoint to return just the URL-encoded string directly with HTTP 200, or a plain text error message with HTTP 500. The javascript now reads the response as text instead of JSON.

Comment on lines 42 to 45
// Fetch list of available instances
const instancesResponse = await fetch('/instances.json');
const instancesData = await instancesResponse.json();
const randomInstance = instancesData.instances[Math.floor(Math.random() * instancesData.instances.length)];
Copy link
Member

Choose a reason for hiding this comment

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

What is the purpose of renaming these variables? No need for diff churn.

Copy link
Author

Choose a reason for hiding this comment

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

You're absolutely right! Reverted the variable names back to response and data to avoid unnecessary diff churn.


// Fetch current user settings to transfer them to the new instance
let targetUrl = instanceUrl + window.location.pathname;

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let text = "Visit Random Instance"

Copy link
Author

Choose a reason for hiding this comment

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

Good suggestion! Implemented this approach - much cleaner than my original variable naming.

const settingsData = await settingsResponse.json();
// Check if settings were successfully encoded and are available for transfer
if (settingsData.success && settingsData.url_encoded) {
targetUrl = instanceUrl + '/settings/restore/?' + settingsData.url_encoded + '&redirect=' + encodeURIComponent(window.location.pathname.substring(1));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
targetUrl = instanceUrl + '/settings/restore/?' + settingsData.url_encoded + '&redirect=' + encodeURIComponent(window.location.pathname.substring(1));
targetUrl = instanceUrl + '/settings/restore/?' + settingsData.url_encoded + '&redirect=' + encodeURIComponent(window.location.pathname.substring(1));
text += " (bringing preferences)"

Copy link
Author

@S8M1T S8M1T Jul 21, 2025

Choose a reason for hiding this comment

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

Done!

}

// Set the href of the <a> tag to the instance URL with path and settings included
document.getElementById('random-instance').href = targetUrl;
document.getElementById('random-instance').innerText = "Visit Random Instance";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
document.getElementById('random-instance').innerText = "Visit Random Instance";
document.getElementById('random-instance').innerText = text;

Copy link
Author

Choose a reason for hiding this comment

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

Done!

S8M1T added 2 commits July 21, 2025 09:36
- Add /settings.json API endpoint to export current preferences
- Enhance random instance redirect to include settings transfer
- Implement comprehensive error handling with graceful fallbacks
- Maintain full backward compatibility with existing functionality

Fixes user experience issue where custom settings were lost when
switching instances during errors. Users now seamlessly retain
their personalized experience across instances.
- Simplify settings.json endpoint to return URL-encoded string directly
- Use HTTP status codes for error handling (200/500)
- Update button text to show '(bringing preferences)' when transferring
- Revert unnecessary variable renaming (response, data)
- Remove confusing comments per maintainer feedback
@S8M1T S8M1T force-pushed the feat/preserve-settings-random-instance branch from f82197c to a49a94b Compare July 21, 2025 04:14
Cleanup unused import after simplifying settings endpoint to return plain text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants