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

Add custom hook types #601

Merged
merged 6 commits into from
Nov 4, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"stylelint-config-recommended-scss": "^4.2.0",
"stylelint-config-standard": "^20.0.0",
"stylelint-scss": "^3.18.0",
"typescript": "^4.0.3",
"typescript": "4.1.1-rc",
"url-loader": "^4.1.1",
"webpack": "4.44.2",
"webpack-cli": "^3.3.12",
Expand Down
5 changes: 1 addition & 4 deletions src/components/Channel/hooks/useEditMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { useContext } from 'react';
import { ChatContext } from '../../../context';

/**
* @typedef {import('stream-chat').Message} Message
* @typedef {ReturnType<import('stream-chat').StreamChat['updateMessage']>} UpdateMessagePromise
* @param {((cid: string, updatedMessage: Message) => UpdateMessagePromise) | undefined} doUpdateMessageRequest
* @returns {(updatedMessage: Message) => UpdateMessagePromise}
* @type {import('types').useEditMessageHandler}
*/
const useEditMessageHandler = (doUpdateMessageRequest) => {
const { channel, client } = useContext(ChatContext);
Expand Down
6 changes: 1 addition & 5 deletions src/components/Channel/hooks/useMentionsHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
import { useCallback } from 'react';

/**
* @typedef {import('stream-chat').UserResponse} UserResponse
* @typedef {(e: React.MouseEvent, user?: UserResponse) => void} MentionsHandler
* @param {MentionsHandler=} onMentionsHover
* @param {MentionsHandler=} onMentionsClick
* @type {import('types').useMentionsHandlers}
*/
const useMentionsHandlers = (onMentionsHover, onMentionsClick) => {
return useCallback(
/** @type {(e: React.MouseEvent<HTMLSpanElement>, mentioned_users: UserResponse[]) => void} */
(e, mentioned_users) => {
if (!onMentionsHover && !onMentionsClick) return;
// eslint-disable-next-line prefer-destructuring
Expand Down
2 changes: 1 addition & 1 deletion src/components/Message/hooks/useActionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ChannelContext } from '../../../context';
export const handleActionWarning = `Action handler was called, but it is missing one of its required arguments.
Make sure the ChannelContext was properly set and that this hook was initialized with a valid message.`;
/**
* @type {(message: import('stream-chat').MessageResponse | undefined) => (name: string, value: string, event: React.MouseEvent<HTMLElement>) => Promise<void>}
* @type {import('types').useActionHandler}
*/
export const useActionHandler = (message) => {
const { channel, updateMessage, removeMessage } = useContext(ChannelContext);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Message/hooks/useDeleteHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useContext } from 'react';
import { ChannelContext } from '../../../context';

/**
* @type {(message: import('stream-chat').MessageResponse | undefined) => (event: React.MouseEvent<HTMLElement>) => Promise<void>}
* @type {import('types').useDeleteHandler}
*/
export const useDeleteHandler = (message) => {
const { updateMessage, client } = useContext(ChannelContext);
Expand Down
7 changes: 1 addition & 6 deletions src/components/Message/hooks/useFlagHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ export const missingUseFlagHandlerParameterWarning =
'useFlagHandler was called but it is missing one or more necessary parameters.';

/**
* @typedef {{
* notify?: import('types').MessageComponentProps['addNotification'],
* getSuccessNotification?: import('types').MessageComponentProps['getMuteUserSuccessNotification'],
* getErrorNotification?: import('types').MessageComponentProps['getMuteUserErrorNotification'],
* }} NotificationArg
* @type {(message: import('stream-chat').MessageResponse | undefined, notification: NotificationArg) => (event: React.MouseEvent<HTMLElement>) => Promise<void>}
* @type {import('types').useFlagHandler}
*/
export const useFlagHandler = (message, notifications = {}) => {
const { client } = useContext(ChannelContext);
Expand Down
21 changes: 3 additions & 18 deletions src/components/Message/hooks/useMentionsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useContext } from 'react';
import { ChannelContext } from '../../../context';

/** @type {(fn: Function | undefined, message: import('stream-chat').MessageResponse | undefined) => Handler} * */
/** @type {(fn: Function | undefined, message: import('stream-chat').MessageResponse | undefined) => React.EventHandler<React.SyntheticEvent>} * */
function createEventHandler(fn, message) {
return (e) => {
if (typeof fn !== 'function' || !message?.mentioned_users) {
Expand All @@ -13,16 +13,7 @@ function createEventHandler(fn, message) {
}

/**
* @typedef {React.EventHandler<React.SyntheticEvent>} Handler
* @typedef { import('stream-chat').MessageResponse | undefined } Message
* @typedef { (event: React.MouseEvent, user: import('stream-chat').UserResponse[] ) => void } CustomMentionHandler
* @type {(
* message: Message,
* customMentionHandler?: {
* onMentionsClick?: CustomMentionHandler,
* onMentionsHover?: CustomMentionHandler
* }
* ) => { onMentionsClick: Handler, onMentionsHover: Handler }}
* @type {import('types').useMentionsHandler}
*/
export const useMentionsHandler = (message, customMentionHandler) => {
/**
Expand Down Expand Up @@ -50,13 +41,7 @@ export const useMentionsHandler = (message, customMentionHandler) => {
};

/**
* @type {(
* message: Message,
* eventHandlers?: {
* onMentionsClick?: Handler,
* onMentionsHover?: Handler,
* },
* ) => { onMentionsClick: Handler, onMentionsHover: Handler }}
* @type {import('types').useMentionsUIHandler}
*/
export const useMentionsUIHandler = (message, eventHandlers) => {
/**
Expand Down
7 changes: 1 addition & 6 deletions src/components/Message/hooks/useMuteHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ export const missingUseMuteHandlerParamsWarning =
'useMuteHandler was called but it is missing one or more necessary parameter.';

/**
* @typedef {{
* notify?: import('types').MessageComponentProps['addNotification'],
* getSuccessNotification?: import('types').MessageComponentProps['getMuteUserSuccessNotification'],
* getErrorNotification?: import('types').MessageComponentProps['getMuteUserErrorNotification'],
* }} NotificationArg
* @type {(message: import('stream-chat').MessageResponse | undefined, notification: NotificationArg) => (event: React.MouseEvent<HTMLElement>) => Promise<void>}
* @type {import('types').useMuteHandler}
*/
export const useMuteHandler = (message, notifications = {}) => {
const { client, mutes } = useContext(ChannelContext);
Expand Down
5 changes: 1 addition & 4 deletions src/components/Message/hooks/useOpenThreadHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { useContext } from 'react';
import { ChannelContext } from '../../../context';

/**
* @type {(
* message: import('stream-chat').MessageResponse | undefined,
* customOpenThread?: (message: import('stream-chat').MessageResponse, event: React.SyntheticEvent) => void
* ) => (event: React.SyntheticEvent) => void}
* @type {import('types').useOpenThreadHandler}
*/
export const useOpenThreadHandler = (message, customOpenThread) => {
/**
Expand Down
9 changes: 2 additions & 7 deletions src/components/Message/hooks/useReactionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ChannelContext } from '../../../context';
export const reactionHandlerWarning = `Reaction handler was called, but it is missing one of its required arguments.
Make sure the ChannelContext was properly set and that this hook was initialized with a valid message.`;
/**
* @type {(message: import('stream-chat').MessageResponse | undefined) => (reactionType: string, event: React.MouseEvent) => Promise<void>}
* @type {import('types').useReactionHandler}
*/
export const useReactionHandler = (message) => {
const { client, channel, updateMessage } = useContext(ChannelContext);
Expand Down Expand Up @@ -76,12 +76,7 @@ export const useReactionHandler = (message) => {
};

/**
* @typedef {{ onReactionListClick: () => void, showDetailedReactions: boolean, isReactionEnabled: boolean }} ReactionClickHandler
* @type {(
* message: import('stream-chat').MessageResponse | undefined,
* reactionSelectorRef: React.RefObject<HTMLDivElement | null>,
* messageWrapperRef?: React.RefObject<HTMLElement | null>
* ) => ReactionClickHandler}
* @type {import('types').useReactionClick}
*/
export const useReactionClick = (
message,
Expand Down
4 changes: 1 addition & 3 deletions src/components/Message/hooks/useRetryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { useContext } from 'react';
import { ChannelContext } from '../../../context';

/**
* @type {(
* customRetrySendMessage?: (message: import('stream-chat').Message) => Promise<void>
* ) => (message: import('stream-chat').Message | undefined) => Promise<void>}
* @type {import('types').useRetryHandler}
*/
export const useRetryHandler = (customRetrySendMessage) => {
/**
Expand Down
4 changes: 1 addition & 3 deletions src/components/Message/hooks/useUserHandler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check
/**
* @typedef {React.EventHandler<React.SyntheticEvent>} Handler
* @typedef {(e: React.MouseEvent, user: import('stream-chat').User) => void} UserEventHandler
* @type {(message: import('stream-chat').MessageResponse | undefined, eventHandlers: {onUserClickHandler?: UserEventHandler, onUserHoverHandler?: UserEventHandler}) => { onUserClick: Handler, onUserHover: Handler }}
* @type {import('types').useUserHandler}
*/
export const useUserHandler = (message, eventHandlers) => {
return {
Expand Down
10 changes: 1 addition & 9 deletions src/components/Message/hooks/useUserRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ import { useContext } from 'react';
import { ChannelContext } from '../../../context';

/**
* @typedef {{
* isMyMessage: boolean;
* isAdmin: boolean;
* isModerator: boolean;
* isOwner: boolean;
* canEditMessage: boolean;
* canDeleteMessage: boolean;
* }} UserRoles
* @type {(message: import('stream-chat').MessageResponse | undefined) => UserRoles} Typescript syntax
* @type {import('types').useUserRole}
*/
export const useUserRole = (message) => {
const { client, channel } = useContext(ChannelContext);
Expand Down
5 changes: 3 additions & 2 deletions src/components/MessageInput/hooks/messageInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ function messageInputReducer(state, action) {
}
/**
* hook for MessageInput state
* @param {Props} props
* @type{import('types').useMessageInput}
mahboubii marked this conversation as resolved.
Show resolved Hide resolved
*/
export default function useMessageInputState(props) {
export default function useMessageInput(props) {
const {
doImageUploadRequest,
doFileUploadRequest,
Expand Down Expand Up @@ -665,6 +665,7 @@ export default function useMessageInputState(props) {
);

const onPaste = useCallback(
/** (e: React.ClipboardEvent) */
(e) => {
(async (event) => {
// TODO: Move this handler to package with ImageDropzone
Expand Down
Loading