[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]; playSegment?: [number, number];
speed?: number; speed?: number;
noLoop?: boolean; noLoop?: boolean;
size: number; size?: number;
quality?: number; quality?: number;
color?: [number, number, number]; color?: [number, number, number];
isLowPriority?: boolean; isLowPriority?: boolean;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,8 +12,8 @@ export function useResize(
cssPropertyName?: string, cssPropertyName?: string,
) { ) {
const [isActive, markIsActive, unmarkIsActive] = useFlag(); const [isActive, markIsActive, unmarkIsActive] = useFlag();
const [initialMouseX, setInitialMouseX] = useState<number>(); const [initialMouseX, setInitialMouseX] = useState<number>(0);
const [initialElementWidth, setInitialElementWidth] = useState<number>(); const [initialElementWidth, setInitialElementWidth] = useState<number>(0);
const setElementStyle = useCallback((width?: number) => { const setElementStyle = useCallback((width?: number) => {
if (!elementRef.current) { 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>] { export function useState<T>(initial?: T, debugKey?: string): [T, StateHookSetter<T>] {
const { cursor, byCursor } = renderingInstance.hooks.state; const { cursor, byCursor } = renderingInstance.hooks.state;