forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseHandleExceedMaxCommentLength.ts
28 lines (23 loc) · 1.06 KB
/
useHandleExceedMaxCommentLength.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import _ from 'lodash';
import {useCallback, useMemo, useState} from 'react';
import * as ReportUtils from '@libs/ReportUtils';
import type {ParsingDetails} from '@libs/ReportUtils';
import CONST from '@src/CONST';
const useHandleExceedMaxCommentLength = () => {
const [hasExceededMaxCommentLength, setHasExceededMaxCommentLength] = useState(false);
const handleValueChange = useCallback(
(value: string, parsingDetails?: ParsingDetails) => {
if (ReportUtils.getCommentLength(value, parsingDetails) <= CONST.MAX_COMMENT_LENGTH) {
if (hasExceededMaxCommentLength) {
setHasExceededMaxCommentLength(false);
}
return;
}
setHasExceededMaxCommentLength(true);
},
[hasExceededMaxCommentLength],
);
const validateCommentMaxLength = useMemo(() => _.debounce(handleValueChange, 1500, {leading: true}), [handleValueChange]);
return {hasExceededMaxCommentLength, validateCommentMaxLength};
};
export default useHandleExceedMaxCommentLength;