mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-05 10:28:54 +00:00
* implements auto cache cleanup when zero persistence mode is enabled (0 value) * uses memory flag with sqlite instead of loki when running in zero persistence mode * renames ephemeral mode manager and improves readability * zero persistence refactors and restore values during transitions * fixes ephemeral mode tests * use initializeClient instead of just initialize to wait for doReconnect to finish that way teams loading is awaited while WS reconnects during ZPM transitions * adds inital sync as a mount hook to prevent undesired load error states during app cold start in ZPM mode, we need a way to load team and channels without showing loading error during the transition. The nre is initial sync hook allows for it * reset started sync subject on wipe * removes extra log lines * resets tracked server during ephemeral mode transitions * improvements to apply persistence mode changes when something fails * address coderabbit PR suggestions * add batching time logging * throws ephemeral mode change error as is * make deleteServerDatabaseFiles private * inline derive persistence flag function * return current user id when refetching current user * support zero persistence mode server for push notifications * adds support for zero persistence mode when sharing to mattermost * adds clarifying comment * catch and log error deleting files * reorder translations to correct position * adds some new tests to improve coverage * ios push notification native fix * logout when fetchMe fails with 401 status code during reconnection * properly surface errors * return proper false value when checking for zero persistence flag * change comments and properly await on some operations * prevent not set persistence mode to report as an error Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * include servers with no persistence flag set in share list * disable button for accessibility Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * improves when to store and remove signing key from global area * review push notification reception running in zero persistence mode * add server to ephemeral mode manager right after login --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
87 lines
3.2 KiB
TypeScript
87 lines
3.2 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {DatabaseProvider} from '@nozbe/watermelondb/react';
|
|
import React, {type ComponentType, useEffect, useState} from 'react';
|
|
|
|
import DeviceInfoProvider from '@context/device';
|
|
import ServerProvider from '@context/server';
|
|
import ThemeProvider from '@context/theme';
|
|
import UserLocaleProvider from '@context/user_locale';
|
|
import DatabaseManager from '@database/manager';
|
|
import {subscribeActiveServers} from '@database/subscription/servers';
|
|
import {secureGetFromRecord} from '@utils/types';
|
|
|
|
import type {Database} from '@nozbe/watermelondb';
|
|
import type {PersistenceFlag} from '@typings/database/models/app/servers';
|
|
import type ServersModel from '@typings/database/models/app/servers';
|
|
|
|
type State = {
|
|
database: Database;
|
|
serverUrl: string;
|
|
serverDisplayName: string;
|
|
persistenceFlag: PersistenceFlag;
|
|
};
|
|
|
|
export function withServerDatabase<T extends React.JSX.IntrinsicAttributes>(Component: ComponentType<T>): ComponentType<T> {
|
|
return function ServerDatabaseComponent(props: T) {
|
|
const [state, setState] = useState<State | undefined>();
|
|
|
|
const observer = (servers: ServersModel[]) => {
|
|
const server = servers?.length ? servers.reduce((a, b) =>
|
|
(b.lastActiveAt > a.lastActiveAt ? b : a),
|
|
) : undefined;
|
|
|
|
if (server) {
|
|
const database =
|
|
secureGetFromRecord(DatabaseManager.serverDatabases, server.url)?.database;
|
|
|
|
if (database) {
|
|
setState({
|
|
database,
|
|
serverUrl: server.url,
|
|
serverDisplayName: server.displayName,
|
|
persistenceFlag: server.persistenceFlag,
|
|
});
|
|
}
|
|
} else {
|
|
setState(undefined);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const subscription = subscribeActiveServers(observer);
|
|
|
|
return () => {
|
|
subscription?.unsubscribe();
|
|
};
|
|
}, []);
|
|
|
|
if (!state?.database) {
|
|
return null;
|
|
}
|
|
|
|
// Flip the key when the adapter changes (on-disk ↔ in-memory) so React unmounts and
|
|
// remounts the subtree; withObservables uses empty triggerProps and only binds to the
|
|
// database at mount, so without a remount it would stay subscribed to the dropped instance.
|
|
const dbKey = state.persistenceFlag === 'zero-persistence' ? `${state.serverUrl}.mem` : state.serverUrl;
|
|
|
|
return (
|
|
<DatabaseProvider
|
|
database={state.database}
|
|
key={dbKey}
|
|
>
|
|
<DeviceInfoProvider>
|
|
<UserLocaleProvider database={state.database}>
|
|
<ServerProvider server={{displayName: state.serverDisplayName, url: state.serverUrl}}>
|
|
<ThemeProvider database={state.database}>
|
|
<Component {...props}/>
|
|
</ThemeProvider>
|
|
</ServerProvider>
|
|
</UserLocaleProvider>
|
|
</DeviceInfoProvider>
|
|
</DatabaseProvider>
|
|
);
|
|
};
|
|
}
|