~cytrogen/masto-fe

86fa133c5091f8ed0a50ab6a0ece5c6e30cc6059 — julia 1 year, 3 months ago 873838e
[bugfix] Store /api/web/settings state locally for persistence (#2)

This also prevents the 'Can't verify CSRF token authenticity' errors that popup whenever you modify a 'web setting', such as the column layout.

~~NB: There is something funky going on. This works 99% of the time and sometimes doesn't?~~ Fixed now, there was two bits of stuff in verify_state doing the same thing, removed the duplicate.

Reviewed-on: https://codeberg.org/superseriousbusiness/masto-fe-standalone/pulls/2
Co-authored-by: julia <midnight@trainwit.ch>
Co-committed-by: julia <midnight@trainwit.ch>
2 files changed, 14 insertions(+), 5 deletions(-)

M app/javascript/flavours/glitch/actions/settings.js
M public/verify-state.js
M app/javascript/flavours/glitch/actions/settings.js => app/javascript/flavours/glitch/actions/settings.js +2 -3
@@ 26,9 26,8 @@ const debouncedSave = debounce((dispatch, getState) => {

  const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS();

  api(getState).put('/api/web/settings', { data })
    .then(() => dispatch({ type: SETTING_SAVE }))
    .catch(error => dispatch(showAlertForError(error)));
  localStorage.setItem('web_settings', JSON.stringify(data));
  dispatch({ type: SETTING_SAVE });
}, 5000, { trailing: true });

export function saveSettings() {

M public/verify-state.js => public/verify-state.js +12 -2
@@ 6,14 6,20 @@ async function loadState() {
  const domain = localStorage.getItem('domain');
  const access_token = localStorage.getItem('access_token');
  const storedState = localStorage.getItem('initial_state');
  const webSettings = localStorage.getItem('web_settings');

  if (!domain || !access_token) {
    window.location.href = '/login.html';
    return;
  }

  /* We try to load the initial state now to prevent a race between us and mastodon */
  if (storedState && window.location.pathname !== '/prepare.html') {
    document.getElementById('initial-state').textContent = storedState;
    const state = JSON.parse(storedState);
    if (webSettings) {
      state.settings = JSON.parse(webSettings);
    }
    document.getElementById('initial-state').textContent = JSON.stringify(state);
  }

  const apiUrl = `${protocol}${domain}/api`;


@@ 1092,8 1098,12 @@ async function loadState() {
    ],
  };

  if (webSettings) {
    state.settings = JSON.parse(webSettings);
  }

  const json = JSON.stringify(state);
  if (window.location.pathname !== '/prepare.html') document.getElementById('initial-state').textContent = json;
  localStorage.setItem("initial_state", json);
  if (window.location.pathname === '/prepare.html') window.location.href = '/';
}
\ No newline at end of file
}