refactor: add more TS constraints

This commit is contained in:
Tristan Yang
2022-07-04 18:15:24 +08:00
parent 8330e45674
commit ff7fe05b58
18 changed files with 48 additions and 202 deletions
+6 -1
View File
@@ -4,7 +4,12 @@ import { addMessage } from "../../app/slices/message";
import { addChannelMsg } from "../../app/slices/message.channel";
import { addUserMsg } from "../../app/slices/message.user";
export default function useAddLocalFileMessage({ context, to }) {
interface IProps {
context: "channel" | "uesr";
to: number;
}
export default function useAddLocalFileMessage({ context, to }: IProps) {
const dispatch = useDispatch();
const addContextMessage = context == "channel" ? addChannelMsg : addUserMsg;
const addLocalFileMesage = (data) => {
+8 -7
View File
@@ -2,7 +2,7 @@ import { useState, useEffect } from "react";
import { copyImageToClipboard } from "copy-image-clipboard";
import toast from "react-hot-toast";
const useCopy = (config) => {
const useCopy = (config: { enableToast: boolean } | void) => {
const { enableToast = true } = config || {};
const [copied, setCopied] = useState(false);
@@ -12,26 +12,27 @@ const useCopy = (config) => {
}
}, [copied]);
const copyToClipboard = (str) => {
const copyToClipboard = (str: string) => {
const el = document.createElement("textarea");
el.value = str;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
const selection = document.getSelection();
if (!selection) return false;
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
el.select();
const success = document.execCommand("copy");
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
selection.removeAllRanges();
selection.addRange(selected);
}
return success;
};
const copy = (text, isImage = false) => {
const copy = (text: string, isImage = false) => {
let inter = 0;
console.log("copy", text, isImage);
if (!copied) {
+12 -10
View File
@@ -8,12 +8,12 @@ import { useLazyDeleteUserQuery } from "../../app/services/user";
import useConfig from "./useConfig";
import useCopy from "./useCopy";
import { useAppSelector } from "../../app/store";
interface Props {
interface IProps {
uid?: number;
cid?: number;
}
const useUserOperation: FC<Props> = ({ uid, cid }) => {
const [passedUid, setPassedUid] = useState(undefined);
const useUserOperation = ({ uid, cid }: IProps) => {
const [passedUid, setPassedUid] = useState<number | undefined>(undefined);
const { values: agoraConfig } = useConfig("agora");
const isUserDetailPath = useMatch(`/users/${uid}`);
const [removeUser, { isSuccess: removeUserSuccess }] = useLazyDeleteUserQuery();
@@ -29,7 +29,7 @@ const useUserOperation: FC<Props> = ({ uid, cid }) => {
});
useEffect(() => {
setPassedUid(uid ?? loginUser.uid);
setPassedUid(uid ?? loginUser?.uid);
}, [uid, loginUser]);
useEffect(() => {
@@ -41,21 +41,23 @@ const useUserOperation: FC<Props> = ({ uid, cid }) => {
}
}, [removeSuccess, removeUserSuccess, isUserDetailPath]);
const handleRemoveFromChannel = (id) => {
const handleRemoveFromChannel = (id: number) => {
if (!cid) return;
const isNumber = !Number.isNaN(+id);
const finalId = isNumber ? id || passedUid : passedUid;
if (!finalId) return;
removeInChannel({ id: +cid, members: [+finalId] });
hideAll();
};
const handleRemove = (id) => {
const handleRemove = (id: number) => {
const isNumber = !Number.isNaN(+id);
const finalId = isNumber ? id || passedUid : passedUid;
removeUser(finalId);
hideAll();
};
const copyEmail = (email) => {
const copyEmail = (email: string) => {
const isString = typeof email == "string";
const finalEmail = isString ? email || user?.email : user?.email;
copy(finalEmail);
@@ -67,15 +69,15 @@ const useUserOperation: FC<Props> = ({ uid, cid }) => {
};
const call = () => {
toast.success("Cooming Soon...");
toast.success("Coming Soon...");
hideAll();
};
const isAdmin = loginUser?.is_admin;
const loginUid = loginUser?.uid;
const canRemoveFromChannel =
cid && !channel?.is_public && (isAdmin || channel?.owner == loginUid);
const canCall = agoraConfig.enabled && loginUid != uid;
const canRemove = isAdmin && loginUid != uid && !cid;
const canCall: boolean = agoraConfig.enabled && loginUid != uid;
const canRemove: boolean = isAdmin && loginUid != uid && !cid;
return {
canRemove,