💡 (hooks): add TypeScript types to hook parameters for better type safety

💡 (use-dropdown-options.tsx): add TypeScript types to function parameters

💡 (use-api-keys.tsx): add TypeScript types to function parameters

💡 (use-handle-delete-key.tsx): add TypeScript types to function parameters

💡 (use-preload-images.tsx): add TypeScript types to function parameters

💡 (use-patch-password.tsx): add TypeScript types to function parameters

💡 (use-patch-profile-picture.tsx): add TypeScript types to function parameters

💡 (use-save-key.tsx): add TypeScript types to function parameters

💡 (use-scroll-to-element.tsx): add TypeScript types to function parameters

💡 (use-messages-table.tsx): add TypeScript types to function parameters

💡 (use-remove-messages.tsx): add TypeScript types to function parameters

💡 (use-updateMessage.tsx): add type annotations to setSuccessData and setErrorData for better type safety and code clarity
This commit is contained in:
cristhianzl 2024-06-21 18:35:52 -03:00
commit a0117f5794
20 changed files with 140 additions and 71 deletions

View file

@ -1,15 +1,15 @@
import { useCallback } from "react"; import { useCallback } from "react";
const useDeleteMultipleFlows = ( const useDeleteMultipleFlows = (
selectedFlowsComponentsCards, selectedFlowsComponentsCards: string[],
removeFlow, removeFlow: (selectedFlowsComponentsCards: string[]) => Promise<void>,
resetFilter, resetFilter: () => void,
getFoldersApi, getFoldersApi: (refetch?: boolean) => Promise<void>,
folderId, folderId: string | undefined,
myCollectionId, myCollectionId: string,
getFolderById, getFolderById: (id: string) => void,
setSuccessData, setSuccessData: (data: { title: string }) => void,
setErrorData, setErrorData: (data: { title: string; list: string[] }) => void,
) => { ) => {
const handleDeleteMultiple = useCallback(() => { const handleDeleteMultiple = useCallback(() => {
removeFlow(selectedFlowsComponentsCards) removeFlow(selectedFlowsComponentsCards)

View file

@ -1,6 +1,9 @@
import { useMemo } from "react"; import { useMemo } from "react";
const useDescriptionModal = (selectedFlowsComponentsCards, type) => { const useDescriptionModal = (
selectedFlowsComponentsCards: string[] | undefined,
type: string | undefined,
) => {
const getDescriptionModal = useMemo(() => { const getDescriptionModal = useMemo(() => {
const getTypeLabel = (type) => { const getTypeLabel = (type) => {
const labels = { const labels = {

View file

@ -1,18 +1,31 @@
import { useCallback } from "react"; import { useCallback } from "react";
import { XYPosition } from "reactflow";
import { FlowType } from "../../../../../types/flow";
const useDuplicateFlows = ( const useDuplicateFlows = (
selectedFlowsComponentsCards, selectedFlowsComponentsCards: string[],
addFlow, addFlow: (
allFlows, newProject: boolean,
resetFilter, flow?: FlowType,
getFoldersApi, override?: boolean,
folderId, position?: XYPosition,
myCollectionId, fromDragAndDrop?: boolean,
getFolderById, ) => Promise<string | undefined>,
setSuccessData, allFlows: any[],
setSelectedFlowsComponentsCards, resetFilter: () => void,
handleSelectAll, getFoldersApi: (
cardTypes, refetch?: boolean,
startupApplication?: boolean,
) => Promise<void>,
folderId: string,
myCollectionId: string,
getFolderById: (id: string) => void,
setSuccessData: (data: { title: string }) => void,
setSelectedFlowsComponentsCards: (
selectedFlowsComponentsCards: string[],
) => void,
handleSelectAll: (select: boolean) => void,
cardTypes: string,
) => { ) => {
const handleDuplicate = useCallback(() => { const handleDuplicate = useCallback(() => {
Promise.all( Promise.all(

View file

@ -1,15 +1,18 @@
import { useCallback } from "react"; import { useCallback } from "react";
import { FlowType } from "../../../../../types/flow";
const useExportFlows = ( const useExportFlows = (
selectedFlowsComponentsCards, selectedFlowsComponentsCards: string[],
allFlows, allFlows: Array<FlowType>,
downloadFlow, downloadFlow: (flow: any, name: string, description: string) => void,
removeApiKeys, removeApiKeys: (flow: any) => any,
version, version: string,
setSuccessData, setSuccessData: (data: { title: string }) => void,
setSelectedFlowsComponentsCards, setSelectedFlowsComponentsCards: (
handleSelectAll, selectedFlowsComponentsCards: string[],
cardTypes, ) => void,
handleSelectAll: (select: boolean) => void,
cardTypes: string,
) => { ) => {
const handleExport = useCallback(() => { const handleExport = useCallback(() => {
selectedFlowsComponentsCards.forEach((selectedFlowId) => { selectedFlowsComponentsCards.forEach((selectedFlowId) => {

View file

@ -1,6 +1,11 @@
import { useCallback } from "react"; import { useCallback } from "react";
import { FlowType } from "../../../../../types/flow";
const useSelectAll = (flowsFromFolder, getValues, setValue) => { const useSelectAll = (
flowsFromFolder: FlowType[],
getValues: () => Record<string, boolean>,
setValue: (key: string, value: boolean) => void,
) => {
const handleSelectAll = useCallback( const handleSelectAll = useCallback(
(select) => { (select) => {
const flowsFromFolderIds = flowsFromFolder?.map((f) => f.id); const flowsFromFolderIds = flowsFromFolder?.map((f) => f.id);

View file

@ -1,15 +1,15 @@
import { useCallback } from "react"; import { useCallback } from "react";
const useSelectOptionsChange = ( const useSelectOptionsChange = (
selectedFlowsComponentsCards, selectedFlowsComponentsCards: string[] | undefined,
setErrorData, setErrorData: (data: { title: string; list: string[] }) => void,
setOpenDelete, setOpenDelete: (value: boolean) => void,
handleDuplicate, handleDuplicate: () => void,
handleExport, handleExport: () => void,
) => { ) => {
const handleSelectOptionsChange = useCallback( const handleSelectOptionsChange = useCallback(
(action) => { (action) => {
const hasSelected = selectedFlowsComponentsCards?.length > 0; const hasSelected = selectedFlowsComponentsCards?.length! > 0;
if (!hasSelected) { if (!hasSelected) {
setErrorData({ setErrorData({
title: "No items selected", title: "No items selected",

View file

@ -1,8 +1,10 @@
import { useEffect } from "react"; import { useEffect } from "react";
const useSelectedFlows = ( const useSelectedFlows = (
entireFormValues, entireFormValues: Record<string, boolean> | undefined,
setSelectedFlowsComponentsCards, setSelectedFlowsComponentsCards: (
selectedFlowsComponentsCards: string[],
) => void,
) => { ) => {
useEffect(() => { useEffect(() => {
if (!entireFormValues || Object.keys(entireFormValues).length === 0) return; if (!entireFormValues || Object.keys(entireFormValues).length === 0) return;

View file

@ -107,7 +107,7 @@ export default function ComponentsComponent({
const methods = useForm(); const methods = useForm();
const { handleSelectAll } = useSelectAll( const { handleSelectAll } = useSelectAll(
flowsFromFolder, flowsFromFolder!,
getValues, getValues,
setValue, setValue,
); );
@ -119,7 +119,7 @@ export default function ComponentsComponent({
resetFilter, resetFilter,
getFoldersApi, getFoldersApi,
folderId, folderId,
myCollectionId, myCollectionId!,
getFolderById, getFolderById,
setSuccessData, setSuccessData,
setSelectedFlowsComponentsCards, setSelectedFlowsComponentsCards,
@ -155,7 +155,7 @@ export default function ComponentsComponent({
resetFilter, resetFilter,
getFoldersApi, getFoldersApi,
folderId, folderId,
myCollectionId, myCollectionId!,
getFolderById, getFolderById,
setSuccessData, setSuccessData,
setErrorData, setErrorData,

View file

@ -2,7 +2,7 @@ import useAlertStore from "../../../stores/alertStore";
import { useFolderStore } from "../../../stores/foldersStore"; import { useFolderStore } from "../../../stores/foldersStore";
import { deleteFolder, getFolderById } from "../services"; import { deleteFolder, getFolderById } from "../services";
const useDeleteFolder = ({ navigate }) => { const useDeleteFolder = ({ navigate }: { navigate: (url: string) => void }) => {
const setSuccessData = useAlertStore((state) => state.setSuccessData); const setSuccessData = useAlertStore((state) => state.setSuccessData);
const setErrorData = useAlertStore((state) => state.setErrorData); const setErrorData = useAlertStore((state) => state.setErrorData);
const folderToEdit = useFolderStore((state) => state.folderToEdit); const folderToEdit = useFolderStore((state) => state.folderToEdit);

View file

@ -1,7 +1,26 @@
import { XYPosition } from "reactflow";
import { CONSOLE_ERROR_MSG } from "../../../constants/alerts_constants"; import { CONSOLE_ERROR_MSG } from "../../../constants/alerts_constants";
import useAlertStore from "../../../stores/alertStore"; import useAlertStore from "../../../stores/alertStore";
const useDropdownOptions = ({ uploadFlow, navigate, is_component }) => { const useDropdownOptions = ({
uploadFlow,
navigate,
is_component,
}: {
uploadFlow: ({
newProject,
file,
isComponent,
position,
}: {
newProject: boolean;
file?: File;
isComponent: boolean | null;
position?: XYPosition;
}) => Promise<string | never>;
navigate: (url: string) => void;
is_component: boolean;
}) => {
const setSuccessData = useAlertStore((state) => state.setSuccessData); const setSuccessData = useAlertStore((state) => state.setSuccessData);
const setErrorData = useAlertStore((state) => state.setErrorData); const setErrorData = useAlertStore((state) => state.setErrorData);
const handleImportFromJSON = () => { const handleImportFromJSON = () => {

View file

@ -1,6 +1,12 @@
import { getApiKey } from "../../../../../controllers/API"; import { getApiKey } from "../../../../../controllers/API";
import { Users } from "../../../../../types/api";
const useApiKeys = (userData, setLoadingKeys, keysList, setUserId) => { const useApiKeys = (
userData: Users | null,
setLoadingKeys: (load: boolean) => void,
keysList: React.MutableRefObject<never[]>,
setUserId: (userId: string) => void,
) => {
const fetchApiKeys = () => { const fetchApiKeys = () => {
setLoadingKeys(true); setLoadingKeys(true);
getApiKey() getApiKey()

View file

@ -7,10 +7,10 @@ import {
import { deleteApiKey } from "../../../../../controllers/API"; import { deleteApiKey } from "../../../../../controllers/API";
const useDeleteApiKeys = ( const useDeleteApiKeys = (
selectedRows, selectedRows: string[],
resetFilter, resetFilter: () => void,
setSuccessData, setSuccessData: (data: { title: string }) => void,
setErrorData, setErrorData: (data: { title: string; list: string[] }) => void,
) => { ) => {
const handleDeleteKey = () => { const handleDeleteKey = () => {
Promise.all(selectedRows.map((selectedRow) => deleteApiKey(selectedRow))) Promise.all(selectedRows.map((selectedRow) => deleteApiKey(selectedRow)))

View file

@ -4,7 +4,10 @@ import {
BASE_URL_API, BASE_URL_API,
} from "../../../../../../../../../constants/constants"; } from "../../../../../../../../../constants/constants";
const usePreloadImages = (profilePictures, setImagesLoaded) => { const usePreloadImages = (
profilePictures: { [key: string]: string[] },
setImagesLoaded: (value: boolean) => void,
) => {
const preloadImages = async (imageUrls) => { const preloadImages = async (imageUrls) => {
return Promise.all( return Promise.all(
imageUrls.map( imageUrls.map(

View file

@ -5,8 +5,13 @@ import {
SAVE_SUCCESS_ALERT, SAVE_SUCCESS_ALERT,
} from "../../../../constants/alerts_constants"; } from "../../../../constants/alerts_constants";
import { resetPassword } from "../../../../controllers/API"; import { resetPassword } from "../../../../controllers/API";
import { Users } from "../../../../types/api";
const usePatchPassword = (userData, setSuccessData, setErrorData) => { const usePatchPassword = (
userData: Users | null,
setSuccessData: (data: { title: string; list?: string[] }) => void,
setErrorData: (data: { title: string; list: string[] }) => void,
) => {
const handlePatchPassword = async (password, cnfPassword, handleInput) => { const handlePatchPassword = async (password, cnfPassword, handleInput) => {
if (password !== cnfPassword) { if (password !== cnfPassword) {
setErrorData({ setErrorData({
@ -16,7 +21,7 @@ const usePatchPassword = (userData, setSuccessData, setErrorData) => {
return; return;
} }
try { try {
if (password !== "") await resetPassword(userData.id, { password }); if (password !== "") await resetPassword(userData!.id, { password });
handleInput({ target: { name: "password", value: "" } }); handleInput({ target: { name: "password", value: "" } });
handleInput({ target: { name: "cnfPassword", value: "" } }); handleInput({ target: { name: "cnfPassword", value: "" } });
setSuccessData({ title: SAVE_SUCCESS_ALERT }); setSuccessData({ title: SAVE_SUCCESS_ALERT });

View file

@ -4,21 +4,22 @@ import {
SAVE_SUCCESS_ALERT, SAVE_SUCCESS_ALERT,
} from "../../../../constants/alerts_constants"; } from "../../../../constants/alerts_constants";
import { updateUser } from "../../../../controllers/API"; import { updateUser } from "../../../../controllers/API";
import { Users } from "../../../../types/api";
const usePatchProfilePicture = ( const usePatchProfilePicture = (
setSuccessData, setSuccessData: (data: { title: string; list?: string[] }) => void,
setErrorData, setErrorData: (data: { title: string; list: string[] }) => void,
currentUserData, currentUserData: Users | null,
setUserData, setUserData: (data: any) => void,
) => { ) => {
const handlePatchProfilePicture = async (profile_picture) => { const handlePatchProfilePicture = async (profile_picture) => {
try { try {
if (profile_picture !== "") { if (profile_picture !== "") {
await updateUser(currentUserData.id, { await updateUser(currentUserData!.id, {
profile_image: profile_picture, profile_image: profile_picture,
}); });
let newUserData = cloneDeep(currentUserData); let newUserData = cloneDeep(currentUserData);
newUserData.profile_image = profile_picture; newUserData!.profile_image = profile_picture;
setUserData(newUserData); setUserData(newUserData);
} }
setSuccessData({ title: SAVE_SUCCESS_ALERT }); setSuccessData({ title: SAVE_SUCCESS_ALERT });

View file

@ -7,11 +7,11 @@ import { AuthContext } from "../../../../contexts/authContext";
import { addApiKeyStore } from "../../../../controllers/API"; import { addApiKeyStore } from "../../../../controllers/API";
const useSaveKey = ( const useSaveKey = (
setSuccessData, setSuccessData: (data: { title: string }) => void,
setErrorData, setErrorData: (data: { title: string; list: string[] }) => void,
setHasApiKey, setHasApiKey: (hasApiKey: boolean) => void,
setValidApiKey, setValidApiKey: (validApiKey: boolean) => void,
setLoadingApiKey, setLoadingApiKey: (loadingApiKey: boolean) => void,
) => { ) => {
const { storeApiKey } = useContext(AuthContext); const { storeApiKey } = useContext(AuthContext);

View file

@ -1,6 +1,9 @@
import { useEffect } from "react"; import { useEffect } from "react";
const useScrollToElement = (scrollId, setCurrentFlowId) => { const useScrollToElement = (
scrollId: string | null | undefined,
setCurrentFlowId: (currentFlowId: string) => void,
) => {
useEffect(() => { useEffect(() => {
const element = document.getElementById(scrollId ?? "null"); const element = document.getElementById(scrollId ?? "null");
if (element) { if (element) {

View file

@ -1,8 +1,11 @@
import { ColDef, ColGroupDef } from "ag-grid-community";
import { useEffect } from "react"; import { useEffect } from "react";
import { getMessagesTable } from "../../../../../controllers/API"; import { getMessagesTable } from "../../../../../controllers/API";
import { useMessagesStore } from "../../../../../stores/messagesStore"; import { useMessagesStore } from "../../../../../stores/messagesStore";
const useMessagesTable = (setColumns) => { const useMessagesTable = (
setColumns: (data: Array<ColDef | ColGroupDef>) => void,
) => {
const setMessages = useMessagesStore((state) => state.setMessages); const setMessages = useMessagesStore((state) => state.setMessages);
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchData = async () => {

View file

@ -2,10 +2,10 @@ import { deleteMessagesFn } from "../../../../../controllers/API";
import { useMessagesStore } from "../../../../../stores/messagesStore"; import { useMessagesStore } from "../../../../../stores/messagesStore";
const useRemoveMessages = ( const useRemoveMessages = (
setSelectedRows, setSelectedRows: (data: number[]) => void,
setSuccessData, setSuccessData: (data: { title: string }) => void,
setErrorData, setErrorData: (data: { title: string }) => void,
selectedRows, selectedRows: number[],
) => { ) => {
const deleteMessages = useMessagesStore((state) => state.removeMessages); const deleteMessages = useMessagesStore((state) => state.removeMessages);

View file

@ -2,7 +2,10 @@ import { updateMessageApi } from "../../../../../controllers/API";
import { useMessagesStore } from "../../../../../stores/messagesStore"; import { useMessagesStore } from "../../../../../stores/messagesStore";
import { Message } from "../../../../../types/messages"; import { Message } from "../../../../../types/messages";
const useUpdateMessage = (setSuccessData, setErrorData) => { const useUpdateMessage = (
setSuccessData: (data: { title: string; list?: string[] }) => void,
setErrorData: (data: { title: string; list?: string[] }) => void,
) => {
const updateMessage = useMessagesStore((state) => state.updateMessage); const updateMessage = useMessagesStore((state) => state.updateMessage);
const handleUpdate = async (data: Message) => { const handleUpdate = async (data: Message) => {