refactor: more TS code

This commit is contained in:
Tristan Yang
2022-07-28 23:25:47 +08:00
parent e26b6f0fcc
commit 875f74c410
17 changed files with 67 additions and 47 deletions
+2 -2
View File
@@ -20,8 +20,8 @@ export default function useFavMessage({
const addFavorite = async (mid = []) => {
const mids = Array.isArray(mid) ? mid.map((i) => +i) : [+mid];
if (mids.length == 0) return;
const { error = null } = await addFav(mids);
return !error;
const resp = await addFav(mids);
return "error" in resp;
};
const removeFavorite = (id: number) => {
+2 -1
View File
@@ -5,7 +5,7 @@ import { Channel } from "../../types/channel";
export default function useFilteredChannels() {
const [input, setInput] = useState("");
const channels = useAppSelector((store) => Object.values(store.channels.byId));
const [filteredChannels, setFilteredChannels] = useState<Channel[]>([]);
const [filteredChannels, setFilteredChannels] = useState<(Channel | undefined)[]>([]);
useEffect(() => {
if (!input) {
@@ -15,6 +15,7 @@ export default function useFilteredChannels() {
let reg = new RegExp(str);
setFilteredChannels(
channels.filter((c) => {
if (!c) return false;
return reg.test(c.name.toLowerCase());
})
);
+3 -1
View File
@@ -1,10 +1,11 @@
import { useState, useEffect } from "react";
import { StoredUser } from "../../app/slices/users";
import { useAppSelector } from "../../app/store";
export default function useFilteredUsers() {
const [input, setInput] = useState("");
const users = useAppSelector((store) => Object.values(store.users.byId));
const [filteredUsers, setFilteredUsers] = useState([]);
const [filteredUsers, setFilteredUsers] = useState<(StoredUser | undefined)[]>([]);
useEffect(() => {
if (!input) {
setFilteredUsers(users);
@@ -13,6 +14,7 @@ export default function useFilteredUsers() {
let reg = new RegExp(str);
setFilteredUsers(
users.filter((c) => {
if (!c) return false;
return reg.test(c.name.toLowerCase());
})
);
+5 -1
View File
@@ -25,7 +25,11 @@ export default function useForwardMessage() {
channels: number[];
}) => {
setForwarding(true);
const { data: archive_id } = await createArchive(mids);
const resp = await createArchive(mids);
if ("error" in resp) {
return;
}
const archive_id = resp.data;
if (users.length) {
for await (const uid of users) {
await sendUserMsg({
+2 -5
View File
@@ -25,7 +25,7 @@ interface SendMessageDTO {
}
const useSendMessage = (props?: Props) => {
const { context = "user", from = 0, to = null } = props || {};
const { context = "user", from = 0, to = 0 } = props || {};
const dispatch = useAppDispatch();
const stageFiles = useAppSelector((store) => store.ui.uploadFiles[`${context}_${to}`] || []);
const [replyMessage, { isError: replyErr, isLoading: replying, isSuccess: replySuccess }] =
@@ -72,12 +72,9 @@ const useSendMessage = (props?: Props) => {
if (reply_mid) {
removeReplying();
await replyMessage({
id: to,
reply_mid,
type,
content,
context,
from_uid: from
content
});
} else {
await sendFn({
+13 -6
View File
@@ -1,10 +1,11 @@
import { useState, useRef, FC } from "react";
import { useState, useRef } from "react";
import toast from "react-hot-toast";
import { updateUploadFiles } from "../../app/slices/ui";
import BASE_URL, { FILE_SLICE_SIZE } from "../../app/config";
import { usePrepareUploadFileMutation, useUploadFileMutation } from "../../app/services/message";
import { useAppDispatch, useAppSelector } from "../../app/store";
import { Message } from "../../types/channel";
import { UploadFileResponse } from "../../types/message";
// todo: check props type
interface IProps {
@@ -28,7 +29,7 @@ const useUploadFile = (props?: IProps) => {
const [uploadFileFn, { isLoading: isUploading, isError: uploadFileError }] =
useUploadFileMutation();
const uploadChunk = (data: { file_id: string; chunk: File; is_last: boolean }) => {
const uploadChunk = (data: { file_id: string; chunk: Blob; is_last: boolean }) => {
const { file_id, chunk, is_last } = data;
const formData = new FormData();
formData.append("file_id", file_id);
@@ -48,10 +49,15 @@ const useUploadFile = (props?: IProps) => {
size: file_size
} = file;
// 拿file id
const { data: file_id } = await prepareUploadFile({
const resp = await prepareUploadFile({
content_type: file_type,
filename: name
});
if ("error" in resp) {
toast.error("Prepare Upload File Error");
return;
}
const file_id = resp.data;
console.log("file id", file_id);
let uploadResult = null;
@@ -92,9 +98,10 @@ const useUploadFile = (props?: IProps) => {
console.log("wtfff", uploadResult);
}
// setUploadingFile(false);
const {
data: { path, size, hash }
} = uploadResult;
if (!uploadResult || "error" in uploadResult) {
return;
}
const { path, size, hash } = uploadResult.data as UploadFileResponse;
const encodedPath = encodeURIComponent(path);
const res = {
name,