mattermost-mobile/app/components/server_version/index.tsx
Elias Nahum 784b05fe97
Upgrade Dependencies (#7299)
* upgrade reanimated

* update devDependencies

* upgrade react-intl

* update react-native and some dependencies

* update react-native-permissions

* update RN

* use Share sheet for Report a problem

* update Sentry

* remove step to downloadWebRTC

* update detox deps

* feedback review
2023-04-21 12:16:54 -04:00

60 lines
2.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import {useEffect} from 'react';
import {type IntlShape, useIntl} from 'react-intl';
import {distinctUntilChanged, map} from 'rxjs/operators';
import {setLastServerVersionCheck} from '@actions/local/systems';
import {useServerUrl} from '@context/server';
import {getServer} from '@queries/app/servers';
import {observeConfigValue, observeLastServerVersionCheck} from '@queries/servers/system';
import {observeCurrentUser} from '@queries/servers/user';
import {isSupportedServer, unsupportedServer} from '@utils/server';
import {isSystemAdmin} from '@utils/user';
import type {WithDatabaseArgs} from '@typings/database/database';
type ServerVersionProps = {
isAdmin: boolean;
lastChecked: number;
version?: string;
};
const VALIDATE_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
const handleUnsupportedServer = async (serverUrl: string, isAdmin: boolean, intl: IntlShape) => {
const serverModel = await getServer(serverUrl);
unsupportedServer(serverModel?.displayName || '', isAdmin, intl);
setLastServerVersionCheck(serverUrl);
};
const ServerVersion = ({isAdmin, lastChecked, version}: ServerVersionProps) => {
const intl = useIntl();
const serverUrl = useServerUrl();
useEffect(() => {
const serverVersion = version || '';
const shouldValidate = (Date.now() - lastChecked) >= VALIDATE_INTERVAL || !lastChecked;
if (serverVersion && shouldValidate && !isSupportedServer(serverVersion)) {
// Only display the Alert if the TOS does not need to show first
handleUnsupportedServer(serverUrl, isAdmin, intl);
}
}, [version, isAdmin, lastChecked, serverUrl]);
return null;
};
const enahanced = withObservables([], ({database}: WithDatabaseArgs) => ({
lastChecked: observeLastServerVersionCheck(database),
version: observeConfigValue(database, 'Version'),
isAdmin: observeCurrentUser(database).pipe(
map((user) => isSystemAdmin(user?.roles || '')),
distinctUntilChanged(),
),
}));
export default withDatabase(enahanced(ServerVersion));