refactor: add typescript definition
This commit is contained in:
@@ -17,7 +17,7 @@ let originalValue: null | object = null;
|
||||
|
||||
export default function useConfig(config = "smtp") {
|
||||
const [changed, setChanged] = useState(false);
|
||||
const [values, setValues] = useState({});
|
||||
const [values, setValues] = useState<object>({});
|
||||
const [updateLoginConfig, { isSuccess: LoginUpdated }] = useUpdateLoginConfigMutation();
|
||||
const [updateSMTPConfig, { isSuccess: SMTPUpdated }] = useUpdateSMTPConfigMutation();
|
||||
const [getAgoraConfig, { data: agoraConfig }] = useLazyGetAgoraConfigQuery();
|
||||
|
||||
@@ -4,6 +4,10 @@ import {
|
||||
useUpdateGithubAuthConfigMutation
|
||||
} from "../../app/services/server";
|
||||
|
||||
interface GithubAuthConfig {
|
||||
client_id: string;
|
||||
}
|
||||
|
||||
export default function useGithubAuthConfig() {
|
||||
const [changed, setChanged] = useState(false);
|
||||
const [config, setConfig] = useState({});
|
||||
|
||||
@@ -19,7 +19,6 @@ export default function useInviteLink(cid: string | null = "") {
|
||||
|
||||
useEffect(() => {
|
||||
const _link = channelInviteLink;
|
||||
console.log("fetching", channelInviteLink);
|
||||
if (_link && smtpStatusFetchSuccess) {
|
||||
// const tmpURL = new URL(_link);
|
||||
// tmpURL.searchParams.set("code", SMTPEnabled);
|
||||
@@ -29,6 +28,7 @@ export default function useInviteLink(cid: string | null = "") {
|
||||
const genServerLink = () => {
|
||||
generateChannelInviteLink();
|
||||
};
|
||||
|
||||
return {
|
||||
enableSMTP: SMTPEnabled,
|
||||
generating: generatingChannelLink,
|
||||
|
||||
@@ -6,8 +6,10 @@ import { addFileMessage, removeFileMessage } from "../../../app/slices/message.f
|
||||
import { addUserMsg, removeUserMsg } from "../../../app/slices/message.user";
|
||||
import { updateAfterMid } from "../../../app/slices/footprint";
|
||||
import { ContentTypes } from "../../../app/config";
|
||||
import { ChatEvent } from "../../../types/sse";
|
||||
import { AppDispatch, RootState } from "../../../app/store";
|
||||
|
||||
const handler = (data, dispatch, currState) => {
|
||||
const handler = (data: ChatEvent, dispatch: AppDispatch, currState: RootState) => {
|
||||
const {
|
||||
mid,
|
||||
from_uid,
|
||||
@@ -45,37 +47,30 @@ const handler = (data, dispatch, currState) => {
|
||||
// 此处有点绕
|
||||
const id = to == "user" ? (self ? target.uid : from_uid) : target.gid;
|
||||
const readIndex = (to == "user" ? readUsers[id] : readChannels[id]) || 0;
|
||||
const read = self ? true : mid < readIndex ? true : false;
|
||||
const read = self ? true : mid < readIndex;
|
||||
switch (type) {
|
||||
case "normal":
|
||||
{
|
||||
batch(() => {
|
||||
dispatch(
|
||||
addMessage({
|
||||
mid,
|
||||
// 如果是自己发的消息,就是已读
|
||||
read,
|
||||
...common
|
||||
})
|
||||
);
|
||||
// 未推送完 or 不是自己发的消息
|
||||
console.log("curr state", ready, loginUid, common.from_uid);
|
||||
// if (!ready || loginUid != common.from_uid) {
|
||||
dispatch(
|
||||
appendMessage({
|
||||
id,
|
||||
mid,
|
||||
local_id: properties ? properties.local_id : null
|
||||
})
|
||||
);
|
||||
// 加到file message 列表
|
||||
if (content_type == ContentTypes.file) {
|
||||
dispatch(addFileMessage(mid));
|
||||
}
|
||||
// }
|
||||
});
|
||||
}
|
||||
case "normal": {
|
||||
batch(() => {
|
||||
// 如果是自己发的消息,就是已读
|
||||
dispatch(addMessage({ mid, read, ...common }));
|
||||
// 未推送完 or 不是自己发的消息
|
||||
console.log("curr state", ready, loginUid, common.from_uid);
|
||||
// if (!ready || loginUid != common.from_uid) {
|
||||
dispatch(
|
||||
appendMessage({
|
||||
id,
|
||||
mid,
|
||||
local_id: properties ? properties.local_id : null
|
||||
})
|
||||
);
|
||||
// 加到file message 列表
|
||||
if (content_type == ContentTypes.file) {
|
||||
dispatch(addFileMessage(mid));
|
||||
}
|
||||
// }
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "reply":
|
||||
{
|
||||
batch(() => {
|
||||
|
||||
@@ -22,6 +22,7 @@ import { updateUsersByLogs, updateUsersStatus } from "../../../app/slices/contac
|
||||
import { resetAuthData } from "../../../app/slices/auth.data";
|
||||
import chatMessageHandler from "./chat.handler";
|
||||
import store, { useAppDispatch, useAppSelector } from "../../../app/store";
|
||||
import { ServerEvent } from "../../../types/sse";
|
||||
|
||||
class RetriableError extends Error {}
|
||||
|
||||
@@ -115,7 +116,7 @@ export default function useStreaming() {
|
||||
console.log("sse debug: error message fatal");
|
||||
throw new FatalError(evt.data);
|
||||
}
|
||||
const data = JSON.parse(evt.data);
|
||||
const data: ServerEvent = JSON.parse(evt.data);
|
||||
const { type } = data;
|
||||
switch (type) {
|
||||
case "heartbeat":
|
||||
|
||||
@@ -5,7 +5,8 @@ import BASE_URL, { FILE_SLICE_SIZE } from "../../app/config";
|
||||
import { usePrepareUploadFileMutation, useUploadFileMutation } from "../../app/services/message";
|
||||
import { useAppDispatch, useAppSelector } from "../../app/store";
|
||||
|
||||
export default function useUploadFile(props = {}) {
|
||||
// todo: check props type
|
||||
export default function useUploadFile(props: { context: string; id: string } | object = {}) {
|
||||
const { context = "", id = "" } = props;
|
||||
const dispatch = useAppDispatch();
|
||||
const { stageFiles, replying } = useAppSelector((store) => {
|
||||
@@ -16,7 +17,7 @@ export default function useUploadFile(props = {}) {
|
||||
});
|
||||
const [data, setData] = useState(null);
|
||||
// const [uploadingFile, setUploadingFile] = useState(false);
|
||||
const canneledRef = useRef(false);
|
||||
const canceledRef = useRef(false);
|
||||
const sliceUploadedCountRef = useRef(0);
|
||||
const totalSliceCountRef = useRef(1);
|
||||
const [prepareUploadFile, { isLoading: isPreparing, isSuccess: isPrepared }] =
|
||||
@@ -26,16 +27,18 @@ export default function useUploadFile(props = {}) {
|
||||
{ isLoading: isUploading, isSuccess: isUploaded, isError: uploadFileError }
|
||||
] = useUploadFileMutation();
|
||||
|
||||
const uploadChunk = (data) => {
|
||||
const uploadChunk = (data: { file_id: string; chunk: File; is_last: boolean }) => {
|
||||
const { file_id, chunk, is_last } = data;
|
||||
const formData = new FormData();
|
||||
formData.append("file_id", file_id);
|
||||
formData.append("chunk_data", chunk);
|
||||
formData.append("chunk_is_last", is_last);
|
||||
formData.append("chunk_is_last", `${is_last}`);
|
||||
// old code
|
||||
// formData.append("chunk_is_last", is_last);
|
||||
return uploadFileFn(formData);
|
||||
};
|
||||
|
||||
const uploadFile = async (file) => {
|
||||
const uploadFile = async (file?: File) => {
|
||||
if (!file) return;
|
||||
setData(null);
|
||||
const {
|
||||
@@ -51,7 +54,7 @@ export default function useUploadFile(props = {}) {
|
||||
console.log("file id", file_id);
|
||||
|
||||
let uploadResult = null;
|
||||
canneledRef.current = false;
|
||||
canceledRef.current = false;
|
||||
totalSliceCountRef.current = 1;
|
||||
sliceUploadedCountRef.current = 0;
|
||||
// setUploadingFile(true);
|
||||
@@ -69,7 +72,7 @@ export default function useUploadFile(props = {}) {
|
||||
|
||||
for await (const [idx] of _arr.entries()) {
|
||||
// 退出循环
|
||||
if (canneledRef.current) break;
|
||||
if (canceledRef.current) break;
|
||||
try {
|
||||
const chunk = file.slice(FILE_SLICE_SIZE * idx, FILE_SLICE_SIZE * (idx + 1), file_type);
|
||||
|
||||
@@ -109,7 +112,7 @@ export default function useUploadFile(props = {}) {
|
||||
};
|
||||
|
||||
const stopUploading = () => {
|
||||
canneledRef.current = true;
|
||||
canceledRef.current = true;
|
||||
};
|
||||
|
||||
const removeStageFile = (idx) => {
|
||||
|
||||
Reference in New Issue
Block a user