Skip to content

Commit 80b0f14

Browse files
authored
refactor(client): inline UserProvider.queryPreference subscribe/getSnapshot (#40491)
1 parent f61c7bd commit 80b0f14

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

apps/meteor/client/providers/UserProvider/UserProvider.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { FindOptions, SubscriptionWithRoom } from '@rocket.chat/ui-contexts
66
import { UserContext, useRouteParameter, useSearchParameter } from '@rocket.chat/ui-contexts';
77
import { useQueryClient } from '@tanstack/react-query';
88
import { Meteor } from 'meteor/meteor';
9-
import type { Filter } from 'mongodb';
9+
import type { Filter, ObjectId } from 'mongodb';
1010
import type { ContextType, ReactElement, ReactNode } from 'react';
1111
import { useEffect, useMemo, useRef } from 'react';
1212
import type { StoreApi, UseBoundStore } from 'zustand';
@@ -16,13 +16,12 @@ import { useDeleteUser } from './hooks/useDeleteUser';
1616
import { useEmailVerificationWarning } from './hooks/useEmailVerificationWarning';
1717
import { useReloadAfterLogin } from './hooks/useReloadAfterLogin';
1818
import { useUpdateAvatar } from './hooks/useUpdateAvatar';
19-
import { getUserPreference } from '../../../app/utils/client';
2019
import { sdk } from '../../../app/utils/client/lib/SDKClient';
2120
import { useIdleConnection } from '../../hooks/useIdleConnection';
2221
import type { IDocumentMapStore } from '../../lib/cachedStores/DocumentMapStore';
2322
import { applyQueryOptions } from '../../lib/cachedStores/applyQueryOptions';
24-
import { createReactiveSubscriptionFactory } from '../../lib/createReactiveSubscriptionFactory';
2523
import { getDdpSdk } from '../../lib/sdk/ddpSdk';
24+
import { settings } from '../../lib/settings';
2625
import { userIdStore } from '../../lib/user';
2726
import { Users, Rooms, Subscriptions } from '../../stores';
2827
import { useSamlInviteToken } from '../../views/invite/hooks/useSamlInviteToken';
@@ -138,9 +137,30 @@ const UserProvider = ({ children }: UserProviderProps): ReactElement => {
138137
(): ContextType<typeof UserContext> => ({
139138
userId,
140139
user,
141-
queryPreference: createReactiveSubscriptionFactory(
142-
<T,>(key: string, defaultValue?: T) => getUserPreference(userId, key, defaultValue) as T,
143-
),
140+
queryPreference: <T,>(
141+
key: string | ObjectId,
142+
defaultValue?: T,
143+
): [subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => T | undefined] => {
144+
const effectiveKey = String(key);
145+
146+
const subscribe = (onStoreChange: () => void): (() => void) => {
147+
const unsubUsers = Users.use.subscribe(onStoreChange);
148+
const unsubSettings = settings.observe(`Accounts_Default_User_Preferences_${effectiveKey}`, onStoreChange);
149+
return () => {
150+
unsubUsers();
151+
unsubSettings();
152+
};
153+
};
154+
155+
const getSnapshot = (): T | undefined => {
156+
return (
157+
(user?.settings?.preferences?.[effectiveKey] as T | undefined) ??
158+
defaultValue ??
159+
settings.peek(`Accounts_Default_User_Preferences_${effectiveKey}`)
160+
);
161+
};
162+
return [subscribe, getSnapshot];
163+
},
144164
querySubscription,
145165
queryRoom,
146166
querySubscriptions,

0 commit comments

Comments
 (0)