Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useLazyRef hook usage #18003

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions client/fuselage-hooks.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
declare module '@rocket.chat/fuselage-hooks' {
import { RefObject } from 'react';

export const useDebouncedCallback: (fn: (...args: any[]) => any, ms: number, deps: any[]) => (...args: any[]) => any;
export const useLazyRef: <T>(initializer: () => T) => RefObject<T>;
export const useMutableCallback: (fn: (...args: any[]) => any) => (...args: any[]) => any;
}
54 changes: 31 additions & 23 deletions client/providers/EditableSettingsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useLazyRef, useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { Mongo } from 'meteor/mongo';
import { Tracker } from 'meteor/tracker';
import React, { useEffect, useMemo, FunctionComponent, useCallback } from 'react';
import React, { useEffect, useMemo, FunctionComponent, useCallback, useRef, MutableRefObject } from 'react';

import { SettingId, GroupId } from '../../definition/ISetting';
import { EditableSettingsContext, IEditableSetting, EditableSettingsContextValue } from '../contexts/EditableSettingsContext';
Expand All @@ -18,28 +18,36 @@ const EditableSettingsProvider: FunctionComponent<EditableSettingsProviderProps>
children,
query = defaultQuery,
}) => {
const settingsCollectionRef = useLazyRef(() => new Mongo.Collection<any>(null));
const settingsCollectionRef = useRef<Mongo.Collection<IEditableSetting>>(null) as MutableRefObject<Mongo.Collection<IEditableSetting>>;
const persistedSettings = useSettings(query);

useEffect(() => {
const getSettingsCollection = useMutableCallback(() => {
if (!settingsCollectionRef.current) {
return;
settingsCollectionRef.current = new Mongo.Collection<any>(null);
}

settingsCollectionRef.current.remove({ _id: { $nin: persistedSettings.map(({ _id }) => _id) } });
for (const setting of persistedSettings) {
settingsCollectionRef.current.upsert(setting._id, { ...setting });
return settingsCollectionRef.current;
}) as () => Mongo.Collection<IEditableSetting>;

useEffect(() => {
const settingsCollection = getSettingsCollection();

settingsCollection.remove({ _id: { $nin: persistedSettings.map(({ _id }) => _id) } });
for (const { _id, ...fields } of persistedSettings) {
settingsCollection.upsert(_id, { $set: { ...fields } });
}
}, [persistedSettings, settingsCollectionRef]);
}, [getSettingsCollection, persistedSettings]);

const queryEditableSetting = useReactiveSubscriptionFactory(
useCallback(
(_id: SettingId): IEditableSetting | undefined => {
if (!settingsCollectionRef.current) {
return;
}
const settingsCollection = getSettingsCollection();

const editableSetting = settingsCollection.findOne(_id);

const editableSetting = settingsCollectionRef.current.findOne(_id);
if (!editableSetting) {
return undefined;
}

if (editableSetting.blocked) {
return { ...editableSetting, disabled: true };
Expand All @@ -54,16 +62,16 @@ const EditableSettingsProvider: FunctionComponent<EditableSettingsProviderProps>
: editableSetting.enableQuery);
return {
...editableSetting,
disabled: !queries.every((query) => (settingsCollectionRef.current?.find(query)?.count() ?? 0) > 0),
disabled: !queries.every((query) => settingsCollection.find(query).count() > 0),
};
},
[settingsCollectionRef],
[getSettingsCollection],
),
);

const queryEditableSettings = useReactiveSubscriptionFactory(
useCallback(
(query = {}) => settingsCollectionRef.current?.find({
(query = {}) => getSettingsCollection().find({
...('_id' in query) && { _id: { $in: query._id } },
...('group' in query) && { group: query.group },
...('section' in query) && (
Expand All @@ -72,7 +80,7 @@ const EditableSettingsProvider: FunctionComponent<EditableSettingsProviderProps>
: {
$or: [
{ section: { $exists: false } },
{ section: null },
{ section: '' },
],
}
),
Expand All @@ -83,15 +91,15 @@ const EditableSettingsProvider: FunctionComponent<EditableSettingsProviderProps>
sorter: 1,
i18nLabel: 1,
},
}).fetch() ?? [],
[settingsCollectionRef],
}).fetch(),
[getSettingsCollection],
),
);

const queryGroupSections = useReactiveSubscriptionFactory(
useCallback(
(_id: GroupId) => Array.from(new Set(
(settingsCollectionRef.current?.find({
getSettingsCollection().find({
group: _id,
}, {
fields: {
Expand All @@ -102,9 +110,9 @@ const EditableSettingsProvider: FunctionComponent<EditableSettingsProviderProps>
sorter: 1,
i18nLabel: 1,
},
}).fetch() ?? []).map(({ section }) => section),
}).fetch().map(({ section }) => section || ''),
)),
[settingsCollectionRef],
[getSettingsCollection],
),
);

Expand All @@ -114,7 +122,7 @@ const EditableSettingsProvider: FunctionComponent<EditableSettingsProviderProps>
continue;
}

settingsCollectionRef.current?.update(_id, { $set: data });
getSettingsCollection().update(_id, { $set: data });
}
Tracker.flush();
});
Expand Down