sforkowany z mirror/soapbox
Preload: refactor actions, add MASTODON_PRELOAD_IMPORT action
rodzic
1763934c3e
commit
06ed4e9096
|
@ -1,25 +1,57 @@
|
|||
import { mapValues } from 'lodash';
|
||||
|
||||
export const PLEROMA_PRELOAD_IMPORT = 'PLEROMA_PRELOAD_IMPORT';
|
||||
export const PLEROMA_PRELOAD_IMPORT = 'PLEROMA_PRELOAD_IMPORT';
|
||||
export const MASTODON_PRELOAD_IMPORT = 'MASTODON_PRELOAD_IMPORT';
|
||||
|
||||
// https://git.pleroma.social/pleroma/pleroma-fe/-/merge_requests/1176/diffs
|
||||
const decodeUTF8Base64 = (data) => {
|
||||
const decodeUTF8Base64 = data => {
|
||||
const rawData = atob(data);
|
||||
const array = Uint8Array.from(rawData.split('').map((char) => char.charCodeAt(0)));
|
||||
const text = new TextDecoder().decode(array);
|
||||
return text;
|
||||
};
|
||||
|
||||
const decodeData = data =>
|
||||
mapValues(data, base64string =>
|
||||
JSON.parse(decodeUTF8Base64(base64string)));
|
||||
const decodePleromaData = data => {
|
||||
return mapValues(data, base64string => JSON.parse(decodeUTF8Base64(base64string)));
|
||||
};
|
||||
|
||||
export function preload() {
|
||||
const element = document.getElementById('initial-results');
|
||||
const data = element ? JSON.parse(element.textContent) : {};
|
||||
const pleromaDecoder = json => decodePleromaData(JSON.parse(json));
|
||||
|
||||
return {
|
||||
type: PLEROMA_PRELOAD_IMPORT,
|
||||
data: decodeData(data),
|
||||
// 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
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function preload() {
|
||||
return (dispatch, getState) => {
|
||||
dispatch(preloadFromMarkup('initial-results', pleromaDecoder, preloadPleroma));
|
||||
dispatch(preloadFromMarkup('initial-state', JSON.parse, preloadMastodon));
|
||||
};
|
||||
}
|
||||
|
||||
export function preloadPleroma(data) {
|
||||
return {
|
||||
type: PLEROMA_PRELOAD_IMPORT,
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
export function preloadMastodon(data) {
|
||||
return {
|
||||
type: MASTODON_PRELOAD_IMPORT,
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue