forked from Ivasoft/mattermost-mobile
98 lines
2.5 KiB
JavaScript
98 lines
2.5 KiB
JavaScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {Alert, Platform} from 'react-native';
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
|
|
|
import {Posts} from '@mm-redux/constants';
|
|
|
|
const INVALID_VERSIONS = ['1.29.0'];
|
|
|
|
export function fromAutoResponder(post) {
|
|
return Boolean(post.type && (post.type === Posts.SYSTEM_AUTO_RESPONDER));
|
|
}
|
|
|
|
export function toTitleCase(str) {
|
|
function doTitleCase(txt) {
|
|
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
|
}
|
|
return str.replace(/\w\S*/g, doTitleCase);
|
|
}
|
|
|
|
export function alertErrorWithFallback(intl, error, fallback, values, buttons) {
|
|
let msg = error.message;
|
|
if (!msg || msg === 'Network request failed') {
|
|
msg = intl.formatMessage(fallback, values);
|
|
}
|
|
Alert.alert('', msg, buttons);
|
|
}
|
|
|
|
export function alertErrorIfInvalidPermissions(result) {
|
|
function isForbidden(data) {
|
|
const {error} = data;
|
|
return error && error.status_code === 403;
|
|
}
|
|
|
|
let error = null;
|
|
if (Array.isArray(result)) {
|
|
const item = result.find((r) => isForbidden(r));
|
|
if (item) {
|
|
error = item.error;
|
|
}
|
|
} else if (isForbidden(result)) {
|
|
error = result.error;
|
|
}
|
|
|
|
if (error) {
|
|
Alert.alert(error.message);
|
|
}
|
|
}
|
|
|
|
export function emptyFunction() { // eslint-disable-line no-empty-function
|
|
|
|
}
|
|
|
|
export function hapticFeedback(method = 'impactLight') {
|
|
ReactNativeHapticFeedback.trigger(method, {
|
|
enableVibrateFallback: false,
|
|
ignoreAndroidSystemSettings: false,
|
|
});
|
|
}
|
|
|
|
export function throttle(fn, limit, ...args) {
|
|
let inThrottle;
|
|
let lastFunc;
|
|
let lastRan;
|
|
|
|
return () => {
|
|
if (inThrottle) {
|
|
clearTimeout(lastFunc);
|
|
lastFunc = setTimeout(() => {
|
|
if ((Date.now() - lastRan) >= limit) {
|
|
Reflect.apply(fn, this, args);
|
|
lastRan = Date.now();
|
|
}
|
|
}, limit - (Date.now() - lastRan));
|
|
} else {
|
|
Reflect.apply(fn, this, args);
|
|
lastRan = Date.now();
|
|
inThrottle = true;
|
|
}
|
|
};
|
|
}
|
|
|
|
export function isPendingPost(postId, userId) {
|
|
return postId.startsWith(userId);
|
|
}
|
|
|
|
export function validatePreviousVersion(previousVersion) {
|
|
if (Platform.OS === 'ios') {
|
|
INVALID_VERSIONS.push('1.31.0', '1.31.1');
|
|
}
|
|
|
|
if (!previousVersion || INVALID_VERSIONS.includes(previousVersion)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} |