// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useEffect, useState} from 'react';
import Autocomplete from '@components/autocomplete';
import {useServerUrl} from '@context/server';
import {useAutocompleteDefaultAnimatedValues} from '@hooks/autocomplete';
import {useDefaultHeaderHeight} from '@hooks/header';
import Archived from './archived';
import DraftHandler from './draft_handler';
import ReadOnly from './read_only';
import type {AvailableScreens} from '@typings/screens/navigation';
const AUTOCOMPLETE_ADJUST = -5;
type Props = {
testID?: string;
canPost: boolean;
channelId: string;
channelIsArchived?: boolean;
channelIsReadOnly: boolean;
deactivatedChannel: boolean;
files?: FileInfo[];
isSearch?: boolean;
message?: string;
rootId?: string;
containerHeight: number;
isChannelScreen: boolean;
canShowPostPriority?: boolean;
location: AvailableScreens;
onPostCreated?: (postId: string) => void;
}
function PostDraft({
testID,
canPost,
channelId,
channelIsArchived,
channelIsReadOnly,
deactivatedChannel,
files,
isSearch,
message = '',
rootId = '',
containerHeight,
isChannelScreen,
canShowPostPriority,
location,
onPostCreated,
}: Props) {
const [value, setValue] = useState(message);
const [cursorPosition, setCursorPosition] = useState(message.length);
const [postInputTop, setPostInputTop] = useState(0);
const [isFocused, setIsFocused] = useState(false);
const headerHeight = useDefaultHeaderHeight();
const serverUrl = useServerUrl();
// Update draft in case we switch channels or threads
useEffect(() => {
setValue(message);
setCursorPosition(message.length);
}, [channelId, message, rootId]);
const autocompletePosition = postInputTop + AUTOCOMPLETE_ADJUST;
const autocompleteAvailableSpace = containerHeight - autocompletePosition - (isChannelScreen ? headerHeight : 0);
const [animatedAutocompletePosition, animatedAutocompleteAvailableSpace] = useAutocompleteDefaultAnimatedValues(autocompletePosition, autocompleteAvailableSpace);
if (channelIsArchived || deactivatedChannel) {
const archivedTestID = `${testID}.archived`;
return (
);
}
if (channelIsReadOnly || !canPost) {
const readOnlyTestID = `${testID}.read_only`;
return (
);
}
const draftHandler = (
);
const autoComplete = isFocused ? (
) : null;
return (
<>
{draftHandler}
{autoComplete}
>
);
}
export default PostDraft;