PromucFlow_constructor/app/client/src/utils/hooks/useNameEditor.ts

71 lines
1.9 KiB
TypeScript
Raw Normal View History

feat: editable ide tabs (#36665) ## Description Adds editable tab behavior for queries and JS objects. Fixes #32440 /ok-to-test tags="@tag.All" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/11288430138> > Commit: dc89acbd51afc6b238283836c6305ab68337575d > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11288430138&attempt=3" target="_blank">Cypress dashboard</a>. > Tags: `@tag.All` > Spec: > <hr>Fri, 11 Oct 2024 09:55:27 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced a new dependency for improved React hooks functionality. - Added new constants for data source operations and user interactions, enhancing feedback and clarity. - **Improvements** - Enhanced messaging related to data source actions, providing clearer user prompts and error handling messages. - Renamed constants for better readability and consistency across the application. - **Bug Fixes** - Corrected naming conventions for constants to improve consistency across the application. - **Chores** - Removed deprecated `FileTabs` component and related tests to streamline the codebase. - Added unit tests for the `FileTab` component to ensure expected functionality. - Updated the `Text` component to improve ref handling and styling options. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-10-14 08:05:38 +00:00
import { useSelector, useDispatch, shallowEqual } from "react-redux";
import { isNameValid, removeSpecialChars } from "utils/helpers";
import type { AppState } from "ee/reducers";
import { getUsedActionNames } from "selectors/actionSelectors";
import {
ACTION_INVALID_NAME_ERROR,
ACTION_NAME_CONFLICT_ERROR,
createMessage,
} from "ee/constants/messages";
import type { ReduxAction } from "ee/constants/ReduxActionConstants";
import { useEventCallback } from "usehooks-ts";
interface NameSaveActionParams {
name: string;
id: string;
}
interface UseNameEditorProps {
entityId: string;
entityName: string;
nameSaveAction: (
params: NameSaveActionParams,
) => ReduxAction<NameSaveActionParams>;
nameErrorMessage?: (name: string) => string;
}
/**
* Provides a unified way to validate and save entity names.
*/
export function useNameEditor(props: UseNameEditorProps) {
const dispatch = useDispatch();
const {
entityId,
entityName,
nameErrorMessage = ACTION_NAME_CONFLICT_ERROR,
nameSaveAction,
} = props;
const isNew =
new URLSearchParams(window.location.search).get("editName") === "true";
const usedEntityNames = useSelector(
(state: AppState) => getUsedActionNames(state, ""),
shallowEqual,
);
const validateName = useEventCallback((name: string): string | null => {
if (!name || name.trim().length === 0) {
return createMessage(ACTION_INVALID_NAME_ERROR);
} else if (name !== entityName && !isNameValid(name, usedEntityNames)) {
return createMessage(nameErrorMessage, name);
}
return null;
});
const handleNameSave = useEventCallback((name: string) => {
if (name !== entityName && validateName(name) === null) {
dispatch(nameSaveAction({ id: entityId, name }));
}
});
return {
isNew,
validateName,
handleNameSave,
normalizeName: removeSpecialChars,
};
}