Skip to content

Commit

Permalink
Add explicit types in useCallback calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Dec 24, 2024
1 parent b47e77f commit b5333fd
Show file tree
Hide file tree
Showing 28 changed files with 53 additions and 46 deletions.
4 changes: 2 additions & 2 deletions apps/meteor/client/hooks/useDownloadFromServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export const useDownloadFromServiceWorker = (href: string, title?: string) => {

return {
disabled: !controller,
onContextMenu: useCallback((e) => e.preventDefault(), []),
onContextMenu: useCallback((e: MouseEvent) => e.preventDefault(), []),
onClick: useCallback(
(e: MouseEvent<HTMLElement>) => {
(e: MouseEvent) => {
e.preventDefault();

forAttachmentDownload(uid, href, controller);
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/hooks/useFormatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSetting } from '@rocket.chat/ui-contexts';
import moment from 'moment';
import { useCallback } from 'react';

export const useFormatDate = (): ((time: string | Date | number) => string) => {
export const useFormatDate = () => {
const format = useSetting('Message_DateFormat');
return useCallback((time) => moment(time).format(String(format)), [format]);
return useCallback((time: string | Date | number) => moment(time).format(String(format)), [format]);
};
2 changes: 1 addition & 1 deletion apps/meteor/client/hooks/useSingleFileInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { useRef, useEffect } from 'react';

export const useSingleFileInput = (
onSetFile: (file: FileList[number], formData: FormData) => void,
onSetFile: (file: File, formData: FormData) => void,
fileType = 'image/*',
fileField = 'image',
): [onClick: () => void, reset: () => void] => {
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/hooks/useTimeFromNow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import moment from 'moment';
import { useCallback } from 'react';

export const useTimeFromNow = (withSuffix: boolean): ((date?: Date | string) => string) =>
useCallback((date) => moment(date).fromNow(!withSuffix), [withSuffix]);
export const useTimeFromNow = (withSuffix: boolean) =>
useCallback((date?: Date | string) => moment(date).fromNow(!withSuffix), [withSuffix]);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useDebouncedValue, useLocalStorage, useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { useSetModal, useRouter } from '@rocket.chat/ui-contexts';
import type { MouseEvent } from 'react';
import type { ChangeEvent, MouseEvent } from 'react';
import { memo, useCallback, useMemo, useState } from 'react';

import CannedResponseList from './CannedResponseList';
Expand All @@ -27,7 +27,7 @@ export const WrapCannedResponseList = () => {

const isRoomOverMacLimit = useIsRoomOverMacLimit(room);

const handleTextChange = useCallback((event) => {
const handleTextChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
setText(event.currentTarget.value);
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr
const uploadCustomSound = useMethod('uploadCustomSound');
const insertOrUpdateSound = useMethod('insertOrUpdateSound');

const handleChangeFile = useCallback((soundFile) => {
const handleChangeFile = useCallback((soundFile: File) => {
setSound(soundFile);
}, []);

Expand Down
11 changes: 9 additions & 2 deletions apps/meteor/client/views/admin/customSounds/EditSound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactEl
const previousSound = useMemo(() => data || {}, [data]);

const [name, setName] = useState(() => data?.name ?? '');
const [sound, setSound] = useState(() => data);
const [sound, setSound] = useState<
| {
_id: string;
name: string;
extension?: string;
}
| File
>(() => data);

useEffect(() => {
setName(previousName || '');
Expand All @@ -39,7 +46,7 @@ function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactEl
const uploadCustomSound = useMethod('uploadCustomSound');
const insertOrUpdateSound = useMethod('insertOrUpdateSound');

const handleChangeFile = useCallback((soundFile) => {
const handleChangeFile = useCallback((soundFile: File) => {
setSound(soundFile);
}, []);

Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/views/admin/rooms/RoomsTableFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box, Icon, TextInput } from '@rocket.chat/fuselage';
import type { OptionProp } from '@rocket.chat/ui-client';
import { MultiSelectCustom } from '@rocket.chat/ui-client';
import { useCallback, useMemo, useState } from 'react';
import type { Dispatch, ReactElement, SetStateAction } from 'react';
import type { Dispatch, FormEvent, ReactElement, SetStateAction } from 'react';
import { useTranslation } from 'react-i18next';

const initialRoomTypeFilterStructure = [
Expand Down Expand Up @@ -76,7 +76,7 @@ const RoomsTableFilters = ({ setFilters }: { setFilters: Dispatch<SetStateAction
return (
<Box
is='form'
onSubmit={useCallback((e) => e.preventDefault(), [])}
onSubmit={useCallback((e: FormEvent) => e.preventDefault(), [])}
mb='x8'
display='flex'
flexWrap='wrap'
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/views/admin/settings/SettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Icon, SearchInput, Skeleton, CardGrid } from '@rocket.chat/fuselage';
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import type { TranslationKey } from '@rocket.chat/ui-contexts';
import { useIsSettingsContextLoading } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import type { ChangeEvent, ReactElement } from 'react';
import { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

Expand All @@ -15,7 +15,7 @@ import PageBlockWithBorder from '../../../components/Page/PageBlockWithBorder';
const SettingsPage = (): ReactElement => {
const { t } = useTranslation();
const [filter, setFilter] = useState('');
const handleChange = useCallback((e) => setFilter(e.currentTarget.value), []);
const handleChange = useCallback((e: ChangeEvent<HTMLInputElement>) => setFilter(e.currentTarget.value), []);

const groups = useSettingsGroups(useDebouncedValue(filter, 400));
const isLoadingGroups = useIsSettingsContextLoading();
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/views/admin/users/AdminInviteUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { FormSkeleton } from '../../../components/Skeleton';
const AdminInviteUsers = () => {
const t = useTranslation();
const [text, setText] = useState('');
const getEmails = useCallback((text) => text.split(/[\ ,;]+/i).filter((val: string) => validateEmail(val)), []);
const getEmails = useCallback((text: string) => text.split(/[\ ,;]+/i).filter((val: string) => validateEmail(val)), []);
const adminRouter = useRoute('admin-settings');
const sendInvitationMutation = useSendInvitationEmailMutation();
const { data, isLoading } = useSmtpQuery();
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/views/admin/viewLogs/ServerLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ServerLogs = (): ReactElement => {

const [newLogsVisible, setNewLogsVisible] = useState(false);

const isAtBottom = useCallback((scrollThreshold = 0) => {
const isAtBottom = useCallback<(scrollThreshold?: number) => boolean>((scrollThreshold = 0) => {
const wrapper = wrapperRef.current;

if (!wrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const OmnichannelDirectoryPage = () => {
[router],
);

const handleTabClick = useCallback((tab) => router.navigate({ name: 'omnichannel-directory', params: { tab } }), [router]);
const handleTabClick = useCallback((tab: string) => router.navigate({ name: 'omnichannel-directory', params: { tab } }), [router]);

return (
<ChatsProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useListIsAtBottom = () => {
}
}, [atBottomRef, sendToBottom]);

const isAtBottom = useCallback((threshold = 0) => {
const isAtBottom = useCallback<(threshold?: number) => boolean>((threshold = 0) => {
if (!innerBoxRef.current) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IDiscussionMessage } from '@rocket.chat/core-typings';
import { Box, Icon, TextInput, Callout, Throbber } from '@rocket.chat/fuselage';
import { useResizeObserver, useAutoFocus } from '@rocket.chat/fuselage-hooks';
import { useSetting } from '@rocket.chat/ui-contexts';
import type { RefObject } from 'react';
import type { ChangeEvent, MouseEvent, RefObject } from 'react';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { Virtuoso } from 'react-virtuoso';
Expand All @@ -28,7 +28,7 @@ type DiscussionsListProps = {
onClose: () => void;
error: unknown;
text: string;
onChangeFilter: (e: unknown) => void;
onChangeFilter: (e: ChangeEvent<HTMLInputElement>) => void;
};

function DiscussionsList({
Expand All @@ -45,9 +45,9 @@ function DiscussionsList({
const showRealNames = useSetting('UI_Use_Real_Name', false);
const inputRef = useAutoFocus(true);

const onClick = useCallback((e) => {
const onClick = useCallback((e: MouseEvent<HTMLElement>) => {
const { drid } = e.currentTarget.dataset;
goToRoomById(drid);
if (drid) goToRoomById(drid);
}, []);

const { ref, contentBoxSize: { inlineSize = 378, blockSize = 1 } = {} } = useResizeObserver<HTMLElement>({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IDiscussionMessage } from '@rocket.chat/core-typings';
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import { useUserId } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import type { ChangeEvent, ReactElement } from 'react';
import { useCallback, useMemo, useState } from 'react';

import DiscussionsList from './DiscussionsList';
Expand Down Expand Up @@ -30,7 +30,7 @@ const DiscussionListContextBar = (): ReactElement | null => {
const { discussionsList, loadMoreItems } = useDiscussionsList(options, userId);
const { phase, error, items: discussions, itemCount: totalItemCount } = useRecordList<IDiscussionMessage>(discussionsList);

const handleTextChange = useCallback((e) => {
const handleTextChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {
setText(e.currentTarget.value);
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { normalizeThreadMessage } from '../../../../lib/normalizeThreadMessage';
type DiscussionListRowProps = {
discussion: IDiscussionMessage;
showRealNames: boolean;
onClick: (e: MouseEvent) => void;
onClick: (e: MouseEvent<HTMLElement>) => void;
};

function DiscussionListRow({ discussion, showRealNames, onClick }: DiscussionListRowProps) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IUpload, IUploadWithUser } from '@rocket.chat/core-typings';
import type { SelectOption } from '@rocket.chat/fuselage';
import { Box, Icon, TextInput, Select, Throbber, ContextualbarSection } from '@rocket.chat/fuselage';
import type { FormEvent } from 'react';
import type { ChangeEvent } from 'react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { Virtuoso } from 'react-virtuoso';
Expand All @@ -24,7 +24,7 @@ type RoomFilesProps = {
filesItems: IUploadWithUser[];
loadMoreItems: (start: number, end: number) => void;
setType: (value: any) => void;
setText: (e: FormEvent<HTMLElement>) => void;
setText: (e: ChangeEvent<HTMLInputElement>) => void;
total: number;
onClickClose: () => void;
onClickDelete: (id: IUpload['_id']) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useLocalStorage } from '@rocket.chat/fuselage-hooks';
import type { ChangeEvent } from 'react';
import { useState, useCallback, useMemo } from 'react';

import RoomFiles from './RoomFiles';
Expand All @@ -15,7 +16,7 @@ const RoomFilesWithData = () => {
const [text, setText] = useState('');
const [type, setType] = useLocalStorage('file-list-type', 'all');

const handleTextChange = useCallback((event) => {
const handleTextChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
setText(event.currentTarget.value);
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IRoom, IUser } from '@rocket.chat/core-typings';
import { isRoomFederated, isDirectMessageRoom, isTeamRoom } from '@rocket.chat/core-typings';
import { useMutableCallback, useDebouncedValue, useLocalStorage } from '@rocket.chat/fuselage-hooks';
import { useUserRoom, useAtLeastOnePermission, useUser, usePermission, useUserSubscription } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import type { ChangeEvent, ReactElement } from 'react';
import { useCallback, useMemo, useState } from 'react';

import * as Federation from '../../../../lib/federation/Federation';
Expand Down Expand Up @@ -56,7 +56,7 @@ const RoomMembersWithData = ({ rid }: { rid: IRoom['_id'] }): ReactElement => {

const canAddUsers = room && user && isFederated ? Federation.isEditableByTheUser(user, room, subscription) : hasPermissionToAddUsers;

const handleTextChange = useCallback((event) => {
const handleTextChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
setText(event.currentTarget.value);
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const SetupWizardProvider = ({ children }: { children: ReactElement }): ReactEle

const goToPreviousStep = useCallback(() => setCurrentStep((currentStep) => currentStep - 1), [setCurrentStep]);
const goToNextStep = useCallback(() => setCurrentStep((currentStep) => currentStep + 1), [setCurrentStep]);
const goToStep = useCallback((step) => setCurrentStep(() => step), [setCurrentStep]);
const goToStep = useCallback((step: number) => setCurrentStep(() => step), [setCurrentStep]);

const _validateEmail = useCallback(
(email: string): true | string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const BaseConvertToChannelModal = ({

const eligibleRooms = rooms;

const onChangeRoomSelection = useCallback((room) => {
const onChangeRoomSelection = useCallback((room: Serialized<IRoom>) => {
setSelectedRooms((selectedRooms) => {
if (selectedRooms[room._id]) {
delete selectedRooms[room._id];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IRoom } from '@rocket.chat/core-typings';
import { useLocalStorage, useDebouncedValue, useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { useSetModal, usePermission, useAtLeastOnePermission } from '@rocket.chat/ui-contexts';
import type { ChangeEvent } from 'react';
import { useCallback, useMemo, useState } from 'react';

import AddExistingModal from './AddExistingModal';
Expand Down Expand Up @@ -36,7 +37,7 @@ const TeamsChannelsWithData = () => {

const { phase, items, itemCount: total } = useRecordList(teamsChannelList);

const handleTextChange = useCallback((event) => {
const handleTextChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
setText(event.currentTarget.value);
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const LeaveTeamModal = ({ rooms, onCancel, onConfirm }: LeaveTeamModalProps): Re
const handleContinue = useCallback(() => setStep(LEAVE_TEAM_STEPS.CONFIRM_LEAVE), []);
const handleReturn = useCallback(() => setStep(LEAVE_TEAM_STEPS.LIST_ROOMS), []);

const onChangeRoomSelection = useCallback((room) => {
const onChangeRoomSelection = useCallback((room: Serialized<IRoom>) => {
setSelectedRooms((selectedRooms) => {
if (selectedRooms[room._id]) {
delete selectedRooms[room._id];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const BaseRemoveUsersModal = ({

const eligibleRooms = rooms?.filter(({ isLastOwner }) => !isLastOwner);

const onChangeRoomSelection = useCallback((room) => {
const onChangeRoomSelection = useCallback((room: Serialized<IRoom>) => {
setSelectedRooms((selectedRooms) => {
if (selectedRooms[room._id]) {
delete selectedRooms[room._id];
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/voip/modal/DialPad/hooks/useDialPad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const useDialPad = ({ initialValue, initialErrorMessage }: DialPadProps):
closeDialModal();
}, [outboundClient, setError, t, value, closeDialModal]);

const handleOnChange = useCallback((e) => onChange(e), [onChange]);
const handleOnChange = useCallback((e: ChangeEvent) => onChange(e), [onChange]);

useEffect(() => {
setDisabled(!value);
Expand Down
4 changes: 2 additions & 2 deletions packages/ui-client/src/hooks/useDocumentTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const titles = new Set<{

const useReactiveDocumentTitle = (): string =>
useSyncExternalStore(
useCallback((callback) => ee.on('change', callback), []),
useCallback((callback: () => void) => ee.on('change', callback), []),
(): string =>
Array.from(titles)
.reverse()
Expand All @@ -23,7 +23,7 @@ const useReactiveDocumentTitle = (): string =>

const useReactiveDocumentTitleKey = (): string =>
useSyncExternalStore(
useCallback((callback) => ee.on('change', callback), []),
useCallback((callback: () => void) => ee.on('change', callback), []),
(): string =>
Array.from(titles)
.filter(({ refocus }) => refocus)
Expand Down
6 changes: 2 additions & 4 deletions packages/ui-contexts/src/hooks/useRolesDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSyncExternalStore } from 'use-sync-external-store/shim';
import type { IRoles } from '../AuthorizationContext';
import { AuthorizationContext } from '../AuthorizationContext';

export const useRolesDescription = (): ((ids: Array<string>) => [string]) => {
export const useRolesDescription = () => {
const { roleStore } = useContext(AuthorizationContext);

const [subscribe, getSnapshot] = useMemo(
Expand All @@ -22,7 +22,5 @@ export const useRolesDescription = (): ((ids: Array<string>) => [string]) => {

const roles = useSyncExternalStore(subscribe, getSnapshot);

return useCallback((values) => values.map((role) => roles[role]?.description || roles[role]?.name || role), [roles]) as (
ids: Array<string>,
) => [string];
return useCallback((ids: string[]) => ids.map((role) => roles[role]?.description || roles[role]?.name || role), [roles]);
};
4 changes: 2 additions & 2 deletions packages/ui-contexts/src/hooks/useSessionDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useContext } from 'react';

import { SessionContext } from '../SessionContext';

export const useSessionDispatch = (name: string): ((value: unknown) => void) => {
export const useSessionDispatch = (name: string) => {
const { dispatch } = useContext(SessionContext);
return useCallback((value) => dispatch(name, value), [dispatch, name]);
return useCallback((value: unknown) => dispatch(name, value), [dispatch, name]);
};

0 comments on commit b5333fd

Please sign in to comment.