bigbuffet-rw/app/soapbox/actions/preload.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-08-24 13:23:05 -07:00
import { mapValues } from 'lodash';
export const PLEROMA_PRELOAD_IMPORT = 'PLEROMA_PRELOAD_IMPORT';
export const MASTODON_PRELOAD_IMPORT = 'MASTODON_PRELOAD_IMPORT';
2020-08-24 13:23:05 -07:00
// https://git.pleroma.social/pleroma/pleroma-fe/-/merge_requests/1176/diffs
const decodeUTF8Base64 = data => {
2020-08-24 13:23:05 -07:00
const rawData = atob(data);
const array = Uint8Array.from(rawData.split('').map((char) => char.charCodeAt(0)));
const text = new TextDecoder().decode(array);
return text;
};
const decodePleromaData = data => {
return mapValues(data, base64string => JSON.parse(decodeUTF8Base64(base64string)));
};
const pleromaDecoder = json => decodePleromaData(JSON.parse(json));
// This will throw if it fails.
// Should be called inside a try-catch.
const decodeFromMarkup = (elementId, decoder) => {
const { textContent } = document.getElementById(elementId);
return decoder(textContent);
};
function preloadFromMarkup(elementId, decoder, action) {
return (dispatch, getState) => {
try {
const data = decodeFromMarkup(elementId, decoder);
dispatch(action(data));
} catch {
// Do nothing
}
};
}
2020-08-24 13:23:05 -07:00
export function preload() {
return (dispatch, getState) => {
dispatch(preloadFromMarkup('initial-results', pleromaDecoder, preloadPleroma));
dispatch(preloadFromMarkup('initial-state', JSON.parse, preloadMastodon));
};
}
2020-08-24 13:23:05 -07:00
export function preloadPleroma(data) {
2020-08-24 13:23:05 -07:00
return {
type: PLEROMA_PRELOAD_IMPORT,
data,
};
}
export function preloadMastodon(data) {
return {
type: MASTODON_PRELOAD_IMPORT,
data,
2020-08-24 13:23:05 -07:00
};
}