Fix bug autofill title (#573)

* Fix bug autofill title

* Remove useless loading
This commit is contained in:
Charles Bochet
2023-07-10 15:20:57 -07:00
committed by GitHub
parent 25eeada92c
commit 5d071187f5
3 changed files with 22 additions and 12 deletions

View File

@ -85,11 +85,13 @@ const StyledTopActionsContainer = styled.div`
type OwnProps = {
commentThreadId: string;
showComment?: boolean;
autoFillTitle?: boolean;
};
export function CommentThread({
commentThreadId,
showComment = true,
autoFillTitle = false,
}: OwnProps) {
const { data } = useGetCommentThreadQuery({
variables: {
@ -100,7 +102,8 @@ export function CommentThread({
const commentThread = data?.findManyCommentThreads[0];
const [title, setTitle] = useState<string | null | undefined>(undefined);
const [isLoading, setIsLoading] = useState(true);
const [hasUserManuallySetTitle, setHasUserManuallySetTitle] =
useState<boolean>(false);
const [updateCommentThreadTitleMutation] =
useUpdateCommentThreadTitleMutation();
@ -121,21 +124,18 @@ export function CommentThread({
}, [commentThreadId, updateCommentThreadTitleMutation]);
function updateTitleFromBody(body: string) {
const title = JSON.parse(body)[0]?.content[0]?.text;
if (!commentThread?.title || commentThread?.title === '') {
setTitle(title);
debounceUpdateTitle(title);
const parsedTitle = JSON.parse(body)[0]?.content[0]?.text;
if (!hasUserManuallySetTitle && autoFillTitle) {
setTitle(parsedTitle);
debounceUpdateTitle(parsedTitle);
}
}
useEffect(() => {
if (commentThread) {
setIsLoading(false);
}
if (isLoading) {
setTitle(commentThread?.title ?? '');
}
}, [commentThread, isLoading]);
}, [commentThread]);
if (!commentThread) {
return <></>;
@ -152,6 +152,7 @@ export function CommentThread({
<StyledEditableTitleInput
placeholder="Note title (optional)"
onChange={(event) => {
setHasUserManuallySetTitle(true);
setTitle(event.target.value);
debounceUpdateTitle(event.target.value);
}}