[Refactoring] Teact: Stricter typings for useState

This commit is contained in:
Alexander Zinchuk 2023-03-30 18:26:23 -05:00
parent 5dadb3c3c9
commit 2bdc5774ab
8 changed files with 17 additions and 20 deletions

View File

@ -24,7 +24,7 @@ export type OwnProps = {
playSegment?: [number, number];
speed?: number;
noLoop?: boolean;
size: number;
size?: number;
quality?: number;
color?: [number, number, number];
isLowPriority?: boolean;

View File

@ -115,14 +115,13 @@ const StickerView: FC<OwnProps> = ({
const noTransition = isLottie && preloadedPreviewData;
const bounds = useBoundsInSharedCanvas(containerRef, sharedCanvasRef);
const realSize = size || bounds.size;
// Preload preview for Message Input and local message
useMedia(previewMediaHash, !shouldLoad || !shouldPreloadPreview, undefined, cacheBuster);
const randomIdPrefix = useMemo(() => generateIdFor(ID_STORE, true), []);
const renderId = [
(withSharedAnimation ? SHARED_PREFIX : randomIdPrefix), id, realSize, customColor?.join(','),
(withSharedAnimation ? SHARED_PREFIX : randomIdPrefix), id, size, customColor?.join(','),
].filter(Boolean).join('_');
return (
@ -143,7 +142,7 @@ const StickerView: FC<OwnProps> = ({
<AnimatedSticker
key={renderId}
renderId={renderId}
size={realSize}
size={size}
className={buildClassName(
styles.media,
(noTransition || isThumbOpaque) && styles.noTransition,

View File

@ -35,7 +35,7 @@ const useEditing = (
replyingToId?: number,
): [VoidFunction, VoidFunction, boolean] => {
const { editMessage, setEditingDraft, toggleMessageWebPage } = getActions();
const [shouldForceShowEditing, setShouldForceShowEditing] = useState<boolean>();
const [shouldForceShowEditing, setShouldForceShowEditing] = useState(false);
useEffectWithPrevDeps(([prevEditedMessage, prevReplyingToId]) => {
if (!editedMessage) {

View File

@ -94,7 +94,7 @@ const Video: FC<OwnProps> = ({
const { isMobile } = useAppLayout();
const [isLoadAllowed, setIsLoadAllowed] = useState(canAutoLoad);
const shouldLoad = Boolean(isLoadAllowed && isIntersectingForLoading && lastSyncTime);
const [isPlayAllowed, setIsPlayAllowed] = useState(canAutoPlay && !isSpoilerShown);
const [isPlayAllowed, setIsPlayAllowed] = useState(Boolean(canAutoPlay && !isSpoilerShown));
const fullMediaHash = getMessageMediaHash(message, 'inline');
const [isFullMediaPreloaded] = useState(Boolean(fullMediaHash && mediaLoader.getFromMemory(fullMediaHash)));

View File

@ -135,7 +135,7 @@ const ManageChatPrivacyType: FC<OwnProps & StateProps> = ({
if (isPublic && privacyType === 'private') {
openUsernameLostDialog();
} else {
updatePublicLink({ username: privacyType === 'public' ? editableUsername : '' });
updatePublicLink({ username: privacyType === 'public' ? (editableUsername || '') : '' });
}
}, [isPublic, openUsernameLostDialog, privacyType, updatePublicLink, editableUsername]);

View File

@ -61,8 +61,8 @@ const ManageDiscussion: FC<OwnProps & StateProps> = ({
const [linkedGroupId, setLinkedGroupId] = useState<string>();
const [isConfirmUnlinkGroupDialogOpen, openConfirmUnlinkGroupDialog, closeConfirmUnlinkGroupDialog] = useFlag();
const [isConfirmLinkGroupDialogOpen, openConfirmLinkGroupDialog, closeConfirmLinkGroupDialog] = useFlag();
const [isJoinToSend, setIsJoinToSend] = useState(linkedChat?.isJoinToSend);
const [isJoinRequest, setIsJoinRequest] = useState(linkedChat?.isJoinRequest);
const [isJoinToSend, setIsJoinToSend] = useState(Boolean(linkedChat?.isJoinToSend));
const [isJoinRequest, setIsJoinRequest] = useState(Boolean(linkedChat?.isJoinRequest));
const lang = useLang();
const linkedChatId = linkedChat?.id;
@ -92,7 +92,7 @@ const ManageDiscussion: FC<OwnProps & StateProps> = ({
const handleLinkGroupSessions = useCallback(() => {
closeConfirmLinkGroupDialog();
linkDiscussionGroup({ channelId: chatId, chatId: linkedGroupId });
linkDiscussionGroup({ channelId: chatId, chatId: linkedGroupId! });
}, [closeConfirmLinkGroupDialog, linkDiscussionGroup, chatId, linkedGroupId]);
const handleJoinToSendCheck = useCallback((checked: boolean) => {
@ -128,11 +128,9 @@ const ManageDiscussion: FC<OwnProps & StateProps> = ({
}
function renderLinkGroupHeader() {
if (!linkedGroupId) return undefined;
const linkedGroup = chatsByIds[linkedGroupId];
if (!linkedGroup) {
return undefined;
}
if (!linkedGroup) return undefined;
return (
<div className="modal-header">
@ -148,11 +146,9 @@ const ManageDiscussion: FC<OwnProps & StateProps> = ({
}
function renderLinkGroupConfirmText() {
if (!linkedGroupId) return undefined;
const linkedGroup = chatsByIds[linkedGroupId];
if (!linkedGroup) {
return undefined;
}
if (!linkedGroup) return undefined;
if (linkedGroup.hasPrivateLink) {
return renderText(

View File

@ -12,8 +12,8 @@ export function useResize(
cssPropertyName?: string,
) {
const [isActive, markIsActive, unmarkIsActive] = useFlag();
const [initialMouseX, setInitialMouseX] = useState<number>();
const [initialElementWidth, setInitialElementWidth] = useState<number>();
const [initialMouseX, setInitialMouseX] = useState<number>(0);
const [initialElementWidth, setInitialElementWidth] = useState<number>(0);
const setElementStyle = useCallback((width?: number) => {
if (!elementRef.current) {

View File

@ -528,6 +528,8 @@ function forceUpdateComponent(componentInstance: ComponentInstance) {
}
}
export function useState<T>(): [T | undefined, StateHookSetter<T | undefined>];
export function useState<T>(initial: T, debugKey?: string): [T, StateHookSetter<T>];
export function useState<T>(initial?: T, debugKey?: string): [T, StateHookSetter<T>] {
const { cursor, byCursor } = renderingInstance.hooks.state;