Skip to content

refactor: remove startup from Meteor #35915

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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
66 changes: 66 additions & 0 deletions apps/meteor/client/hooks/useStartup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { UserStatus } from '@rocket.chat/core-typings';
import { useUserId } from '@rocket.chat/ui-contexts';
import { Meteor } from 'meteor/meteor';
import { UserPresence } from 'meteor/rocketchat:user-presence';
import { Session } from 'meteor/session';
import moment from 'moment';
import { useState, useEffect } from 'react';

import { getUserPreference } from '../../app/utils/client';
import { sdk } from '../../app/utils/client/lib/SDKClient';
import { synchronizeUserData, removeLocalUserData } from '../lib/userData';
import { fireGlobalEvent } from '../lib/utils/fireGlobalEvent';

export const useStartup = () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Documentation and Comments high

/**
 * @description Hook to handle user-specific initialization after login.
 * Synchronizes user data, sets UTC offset, configures presence (auto-away),
 * updates status, starts presence tracking, and fires 'startup' and 'status-changed' global events.
 */
export const useStartup = () => {

The useStartup hook lacks proper documentation, making it difficult to understand its purpose and usage.

This issue appears in multiple locations:

  • apps/meteor/client/hooks/useStartup.ts: Lines 14-14
    Please add JSDoc comments to the useStartup hook to clearly explain its functionality and any side effects.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

const [status, setStatus] = useState<UserStatus | undefined>(undefined);
const uid = useUserId();

useEffect(() => {
if (!uid) {
removeLocalUserData();
return;
}

if (!Meteor.status().connected) {
return;
}

if (Meteor.loggingIn()) {
return;
}

const getUser = async () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Error Handling high

const getUser = async () => {
			try {
				const utcOffset = moment().utcOffset() / 60;
				const user = await synchronizeUserData(uid);
				if (!user) {
					console.error('User data synchronization failed.'); // Or handle more gracefully
					return;
				}
				if (user.utcOffset !== utcOffset) {
					await sdk.call('userSetUtcOffset', utcOffset);
				}

				if (getUserPreference(user, 'enableAutoAway')) {
					const idleTimeLimit = (getUserPreference(user, 'idleTimeLimit') as number | null | undefined) || 300;
					UserPresence.awayTime = idleTimeLimit * 1000;
				} else {
					delete UserPresence.awayTime;
					UserPresence.stopTimer();
				}

				setStatus(user.status);
				UserPresence.start();
			} catch (error) {
				console.error('Error during user startup sequence:', error);
				// Potentially set an error state or notify the user
			}
		};

The useStartup hook lacks proper documentation, making it difficult to understand its purpose and usage.

This issue appears in multiple locations:

  • apps/meteor/client/hooks/useStartup.ts: Lines 32-52
  • apps/meteor/client/hooks/useStartup.ts: Lines 14-14
  • apps/meteor/client/views/root/AppLayout.tsx: Lines 27-27
    Please add JSDoc comments to the useStartup hook to clearly explain its functionality and any side effects.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

const utcOffset = moment().utcOffset() / 60;
const user = await synchronizeUserData(uid);
if (!user) {
return;
}
if (user.utcOffset !== utcOffset) {
sdk.call('userSetUtcOffset', utcOffset);
}

if (getUserPreference(user, 'enableAutoAway')) {
const idleTimeLimit = (getUserPreference(user, 'idleTimeLimit') as number | null | undefined) || 300;
UserPresence.awayTime = idleTimeLimit * 1000;
} else {
delete UserPresence.awayTime;
UserPresence.stopTimer();
}

setStatus(user.status);
UserPresence.start();
};
getUser();
}, [uid]);

useEffect(() => {
fireGlobalEvent('startup', true);
Session.setDefault('AvatarRandom', 0);
}, []);

useEffect(() => {
if (status) {
fireGlobalEvent('status-changed', status);
}
}, [status]);
};
1 change: 0 additions & 1 deletion apps/meteor/client/startup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ import './roles';
import './rootUrlChange';
import './routes';
import './slashCommands';
import './startup';
import './streamMessage';
import './unread';
63 changes: 0 additions & 63 deletions apps/meteor/client/startup/startup.ts

This file was deleted.

2 changes: 2 additions & 0 deletions apps/meteor/client/views/root/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { useAnalytics } from '../../hooks/useAnalytics';
import { useAnalyticsEventTracking } from '../../hooks/useAnalyticsEventTracking';
import { useAutoupdate } from '../../hooks/useAutoupdate';
import { useLoadRoomForAllowedAnonymousRead } from '../../hooks/useLoadRoomForAllowedAnonymousRead';
import { useStartup } from '../../hooks/useStartup';
import { appLayout } from '../../lib/appLayout';
import { useCustomOAuth } from '../../sidebar/hooks/useCustomOAuth';
import { useRedirectToSetupWizard } from '../../startup/useRedirectToSetupWizard';
Expand Down Expand Up @@ -61,6 +62,7 @@ const AppLayout = () => {
useCodeHighlight();
useLoginViaQuery();
useLoadMissedMessages();
useStartup();

const layout = useSyncExternalStore(appLayout.subscribe, appLayout.getSnapshot);

Expand Down
Loading