Merge branch 'optional-chaining' into 'develop'
Rewrite code with optional chaining (?.) See merge request soapbox-pub/soapbox-fe!1041
This commit is contained in:
commit
0fdb2b1c4b
|
@ -24,7 +24,7 @@ const fetchExternalInstance = baseURL => {
|
|||
.get('/api/v1/instance')
|
||||
.then(({ data: instance }) => fromJS(instance))
|
||||
.catch(error => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
if (error.response?.status === 401) {
|
||||
// Authenticated fetch is enabled.
|
||||
// Continue with a limited featureset.
|
||||
return ImmutableMap({ version: '0.0.0' });
|
||||
|
|
|
@ -65,20 +65,20 @@ export function importFetchedStatus(status, idempotencyKey) {
|
|||
|
||||
const normalizedStatus = normalizeStatus(status, normalOldStatus, expandSpoilers);
|
||||
|
||||
if (status.reblog && status.reblog.id) {
|
||||
if (status.reblog?.id) {
|
||||
dispatch(importFetchedStatus(status.reblog));
|
||||
}
|
||||
|
||||
// Fedibird quotes
|
||||
if (status.quote && status.quote.id) {
|
||||
if (status.quote?.id) {
|
||||
dispatch(importFetchedStatus(status.quote));
|
||||
}
|
||||
|
||||
if (status.pleroma && status.pleroma.quote && status.pleroma.quote.id) {
|
||||
if (status.pleroma?.quote?.id) {
|
||||
dispatch(importFetchedStatus(status.pleroma.quote));
|
||||
}
|
||||
|
||||
if (status.poll && status.poll.id) {
|
||||
if (status.poll?.id) {
|
||||
dispatch(importFetchedPoll(status.poll));
|
||||
}
|
||||
|
||||
|
@ -119,20 +119,20 @@ export function importFetchedStatuses(statuses) {
|
|||
normalStatuses.push(normalizeStatus(status, normalOldStatus, expandSpoilers));
|
||||
accounts.push(status.account);
|
||||
|
||||
if (status.reblog && status.reblog.id) {
|
||||
if (status.reblog?.id) {
|
||||
processStatus(status.reblog);
|
||||
}
|
||||
|
||||
// Fedibird quotes
|
||||
if (status.quote && status.quote.id) {
|
||||
if (status.quote?.id) {
|
||||
processStatus(status.quote);
|
||||
}
|
||||
|
||||
if (status.pleroma && status.pleroma.quote && status.pleroma.quote.id) {
|
||||
if (status.pleroma?.quote?.id) {
|
||||
processStatus(status.pleroma.quote);
|
||||
}
|
||||
|
||||
if (status.poll && status.poll.id) {
|
||||
if (status.poll?.id) {
|
||||
polls.push(normalizePoll(status.poll));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,19 +59,19 @@ export function normalizeStatus(status, normalOldStatus, expandSpoilers) {
|
|||
|
||||
normalStatus.account = status.account.id;
|
||||
|
||||
if (status.reblog && status.reblog.id) {
|
||||
if (status.reblog?.id) {
|
||||
normalStatus.reblog = status.reblog.id;
|
||||
}
|
||||
|
||||
if (status.poll && status.poll.id) {
|
||||
if (status.poll?.id) {
|
||||
normalStatus.poll = status.poll.id;
|
||||
}
|
||||
|
||||
if (status.pleroma && status.pleroma.quote && status.pleroma.quote.id) {
|
||||
if (status.pleroma?.quote?.id) {
|
||||
// Normalize quote to the top-level, so delete the original for performance
|
||||
normalStatus.quote = status.pleroma.quote.id;
|
||||
delete normalStatus.pleroma.quote;
|
||||
} else if (status.quote && status.quote.id) {
|
||||
} else if (status.quote?.id) {
|
||||
// Fedibird compatibility, because why not
|
||||
normalStatus.quote = status.quote.id;
|
||||
} else if (status.quote_id) {
|
||||
|
@ -88,7 +88,7 @@ export function normalizeStatus(status, normalOldStatus, expandSpoilers) {
|
|||
normalStatus.hidden = normalOldStatus.get('hidden');
|
||||
} else {
|
||||
const spoilerText = normalStatus.spoiler_text || '';
|
||||
const searchContent = ([spoilerText, status.content].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
|
||||
const searchContent = ([spoilerText, status.content].concat((status.poll?.options) ? status.poll.options.map(option => option.title) : [])).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
|
||||
const emojiMap = makeEmojiMap(normalStatus);
|
||||
|
||||
normalStatus.search_index = domParser.parseFromString(searchContent, 'text/html').documentElement.textContent;
|
||||
|
@ -107,7 +107,7 @@ export function normalizePoll(poll) {
|
|||
|
||||
normalPoll.options = poll.options.map((option, index) => ({
|
||||
...option,
|
||||
voted: poll.own_votes && poll.own_votes.includes(index),
|
||||
voted: Boolean(poll.own_votes?.includes(index)),
|
||||
title_emojified: emojify(escapeTextContentForBrowser(option.title), emojiMap),
|
||||
}));
|
||||
|
||||
|
|
|
@ -206,16 +206,16 @@ export function expandNotifications({ maxId } = {}, done = noOp) {
|
|||
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
||||
|
||||
const entries = response.data.reduce((acc, item) => {
|
||||
if (item.account && item.account.id) {
|
||||
if (item.account?.id) {
|
||||
acc.accounts[item.account.id] = item.account;
|
||||
}
|
||||
|
||||
// Used by Move notification
|
||||
if (item.target && item.target.id) {
|
||||
if (item.target?.id) {
|
||||
acc.accounts[item.target.id] = item.target;
|
||||
}
|
||||
|
||||
if (item.status && item.status.id) {
|
||||
if (item.status?.id) {
|
||||
acc.statuses[item.status.id] = item.status;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ export function createStatus(params, idempotencyKey) {
|
|||
|
||||
const poll = (retries = 5) => {
|
||||
api(getState).get(`/api/v1/statuses/${status.id}`).then(response => {
|
||||
if (response.data && response.data.card) {
|
||||
if (response.data?.card) {
|
||||
dispatch(importFetchedStatus(response.data));
|
||||
} else if (retries > 0 && response.status === 200) {
|
||||
setTimeout(() => poll(retries - 1), delay);
|
||||
|
@ -157,7 +157,7 @@ export function fetchContext(id) {
|
|||
}
|
||||
return context;
|
||||
}).catch(error => {
|
||||
if (error.response && error.response.status === 404) {
|
||||
if (error.response?.status === 404) {
|
||||
dispatch(deleteFromTimelines(id));
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ export const MAX_QUEUED_ITEMS = 40;
|
|||
export function processTimelineUpdate(timeline, status, accept) {
|
||||
return (dispatch, getState) => {
|
||||
const me = getState().get('me');
|
||||
const ownStatus = status.account && status.account.id === me;
|
||||
const ownStatus = status.account?.id === me;
|
||||
const hasPendingStatuses = !getState().get('pending_statuses').isEmpty();
|
||||
|
||||
const columnSettings = getSettings(getState()).get(timeline, ImmutableMap());
|
||||
|
|
|
@ -19,7 +19,7 @@ const mapStateToProps = (state, props) => {
|
|||
|
||||
const birthdays = state.getIn(['user_lists', 'birthday_reminders', me]);
|
||||
|
||||
if (birthdays && birthdays.size > 0) {
|
||||
if (birthdays?.size > 0) {
|
||||
return {
|
||||
birthdays,
|
||||
account: getAccount(state, birthdays.first()),
|
||||
|
@ -151,4 +151,4 @@ class BirthdayReminders extends ImmutablePureComponent {
|
|||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ export default class ColumnBackButton extends React.PureComponent {
|
|||
handleClick = () => {
|
||||
const { to } = this.props;
|
||||
|
||||
if (window.history && window.history.length === 1) {
|
||||
if (window.history?.length === 1) {
|
||||
this.context.router.history.push(to ? to : '/');
|
||||
} else {
|
||||
this.context.router.history.goBack();
|
||||
|
|
|
@ -34,7 +34,7 @@ export default class ColumnHeader extends React.PureComponent {
|
|||
};
|
||||
|
||||
historyBack = () => {
|
||||
if (window.history && window.history.length === 1) {
|
||||
if (window.history?.length === 1) {
|
||||
this.context.router.history.push('/');
|
||||
} else {
|
||||
this.context.router.history.goBack();
|
||||
|
|
|
@ -49,7 +49,7 @@ class DisplayName extends React.PureComponent {
|
|||
</div>
|
||||
) : null;
|
||||
|
||||
if (others && others.size > 1) {
|
||||
if (others?.size > 1) {
|
||||
displayName = others.take(2).map(a => (
|
||||
<span className='display-name__name' key={a.get('id')}>
|
||||
<bdi><strong className='display-name__html' dangerouslySetInnerHTML={{ __html: a.get('display_name_html') }} /></bdi>
|
||||
|
|
|
@ -52,8 +52,8 @@ class LoginPage extends ImmutablePureComponent {
|
|||
dispatch(switchAccount(account.id));
|
||||
}
|
||||
}).catch(error => {
|
||||
const data = error.response && error.response.data;
|
||||
if (data && data.error === 'mfa_required') {
|
||||
const data = error.response?.data;
|
||||
if (data?.error === 'mfa_required') {
|
||||
this.setState({ mfa_auth_needed: true, mfa_token: data.mfa_token });
|
||||
}
|
||||
this.setState({ isLoading: false });
|
||||
|
|
|
@ -197,7 +197,7 @@ class RegistrationForm extends ImmutablePureComponent {
|
|||
this.setState({ usernameUnavailable: !!account });
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.response && error.response.status === 404) {
|
||||
if (error.response?.status === 404) {
|
||||
this.setState({ usernameUnavailable: false });
|
||||
}
|
||||
});
|
||||
|
|
|
@ -124,7 +124,7 @@ export default class ComposeForm extends ImmutablePureComponent {
|
|||
document.querySelector('.privacy-dropdown__dropdown'),
|
||||
document.querySelector('.emoji-picker-dropdown__menu'),
|
||||
document.querySelector('.modal-root__overlay'),
|
||||
].some(element => element && element.contains(e.target));
|
||||
].some(element => element?.contains(e.target));
|
||||
}
|
||||
|
||||
handleClick = (e) => {
|
||||
|
|
|
@ -264,7 +264,7 @@ const persistAuthAccount = account => {
|
|||
};
|
||||
|
||||
const deleteForbiddenToken = (state, error, token) => {
|
||||
if (error.response && [401, 403].includes(error.response.status)) {
|
||||
if ([401, 403].includes(error.response?.status)) {
|
||||
return deleteToken(state, token);
|
||||
} else {
|
||||
return state;
|
||||
|
|
|
@ -163,7 +163,7 @@ const persistInstance = instance => {
|
|||
};
|
||||
|
||||
const handleInstanceFetchFail = (state, error) => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
if (error.response?.status === 401) {
|
||||
return handleAuthFetch(state);
|
||||
} else {
|
||||
return state;
|
||||
|
|
|
@ -13,7 +13,7 @@ import {
|
|||
const initialState = null;
|
||||
|
||||
const handleForbidden = (state, error) => {
|
||||
if (error.response && [401, 403].includes(error.response.status)) {
|
||||
if ([401, 403].includes(error.response?.status)) {
|
||||
return false;
|
||||
} else {
|
||||
return state;
|
||||
|
|
|
@ -91,7 +91,7 @@ const handlePush = (event) => {
|
|||
options.image = notification.status && notification.status.media_attachments.length > 0 && notification.status.media_attachments[0].preview_url || undefined;
|
||||
options.data = { access_token, preferred_locale, id: notification.status ? notification.status.id : notification.account.id, url: notification.status ? `/@${notification.account.username}/posts/${notification.status.id}` : `/@${notification.account.username}` };
|
||||
|
||||
if (notification.status && notification.status.spoiler_text || notification.status.sensitive) {
|
||||
if (notification.status?.spoiler_text || notification.status.sensitive) {
|
||||
options.data.hiddenBody = htmlToPlainText(notification.status.content);
|
||||
options.data.hiddenImage = notification.status.media_attachments.length > 0 && notification.status.media_attachments[0].preview_url;
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ const toSimplePolicy = configs => {
|
|||
return acc.set(trimStart(key, ':'), ImmutableSet(hosts));
|
||||
};
|
||||
|
||||
if (config && config.get) {
|
||||
if (config?.get) {
|
||||
const value = config.get('value', ImmutableList());
|
||||
return value.reduce(reducer, ImmutableMap());
|
||||
} else {
|
||||
|
|
|
@ -80,7 +80,7 @@ const dropOrientationIfNeeded = (orientation) => new Promise(resolve => {
|
|||
// });
|
||||
|
||||
const getImageUrl = inputFile => new Promise((resolve, reject) => {
|
||||
if (window.URL && URL.createObjectURL) {
|
||||
if (window.URL?.createObjectURL) {
|
||||
try {
|
||||
resolve(URL.createObjectURL(inputFile));
|
||||
} catch (error) {
|
||||
|
|
Loading…
Reference in New Issue