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

fix(fuselage-hooks): Stricter generics for useEffectEvent #1521

Merged
merged 1 commit into from
Jan 8, 2025
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
6 changes: 6 additions & 0 deletions .changeset/eight-birds-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/fuselage-hooks': patch
'@rocket.chat/fuselage': patch
---

fix(fuselage-hooks): Stricter generics for `useEffectEvent`
9 changes: 6 additions & 3 deletions packages/fuselage-hooks/src/useEffectEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
* @returns a stable callback
* @public
*/
export const useEffectEvent = <P extends any[], T>(
fn: (...args: P) => T,
): ((...args: P) => T) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export const useEffectEvent = <TFunction extends Function>(fn: TFunction) => {
const fnRef = useRef(fn);

type P = TFunction extends (...args: infer P) => any ? P : never;
type T = TFunction extends (...args: any) => infer T ? T : never;

const stableFnRef = useRef(
(...args: P): T => fnRef.current.call(undefined, ...args),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffectEvent, useResizeObserver } from '@rocket.chat/fuselage-hooks';
import { type ElementType, useState, useRef, useMemo } from 'react';

import { type SelectProps } from '..';
import type { OptionType, SelectOption, SelectProps } from '..';
import { prevent } from '../../helpers/prevent';
import AnimatedVisibility from '../AnimatedVisibility';
import Box from '../Box';
Expand Down Expand Up @@ -52,9 +52,9 @@ export const PaginatedSelect = ({

const [visible, hide, show] = useVisible();

const internalChangedByClick = useEffectEvent(([value]) => {
const internalChangedByClick = useEffectEvent(([value]: OptionType) => {
setInternalValue(value);
onChange(value);
onChange(value as SelectOption[0]); // FIXME
hide();
});

Expand Down
8 changes: 4 additions & 4 deletions packages/fuselage/src/components/Select/SelectLegacy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
fn();
}
didMount.current = true;
}, deps || []);

Check warning on line 50 in packages/fuselage/src/components/Select/SelectLegacy.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

React Hook useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies

Check warning on line 50 in packages/fuselage/src/components/Select/SelectLegacy.tsx

View workflow job for this annotation

GitHub Actions / Build and Test

React Hook useEffect has a missing dependency: 'fn'. Either include it or remove the dependency array
};

export type SelectProps = Omit<ComponentProps<typeof Box>, 'onChange'> & {
Expand Down Expand Up @@ -89,9 +89,9 @@
) => {
const [internalValue, setInternalValue] = useState(value || '');

const internalChangedByKeyboard = useEffectEvent(([value]) => {
const internalChangedByKeyboard = useEffectEvent(([value]: OptionType) => {
setInternalValue(value);
onChange(value);
onChange(value as SelectOption[0]); // FIXME
});

const option = options.find(
Expand Down Expand Up @@ -123,9 +123,9 @@
const removeFocusClass = () =>
innerRef.current?.classList.remove('focus-visible');

const internalChangedByClick = useEffectEvent(([value]) => {
const internalChangedByClick = useEffectEvent(([value]: OptionType) => {
setInternalValue(value);
onChange(value);
onChange(value as SelectOption[0]); // FIXME
removeFocusClass();
hide();
});
Expand Down
Loading