Skip to content

Commit

Permalink
fix(topic): fix jumping reply topic issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lcandy2 committed Mar 16, 2024
1 parent 4c9e89a commit 11cb2ef
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chaoxing-auto",
"private": true,
"version": "1.0.1",
"version": "1.0.4",
"type": "module",
"homepage": "https://github.com/lcandy2/user.js",
"author": "lcandy2 (甜檸Cirtron)",
Expand Down
11 changes: 8 additions & 3 deletions src/topic/lib/compoments/panel-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useLogStore, useStatusStore } from "@topic/lib/store";
import {
AppendHashSuccess,
GetHashAction,
GetHashIndex,
GetHashStart,
GetHashSuccess,
} from "@topic/lib/hash";
Expand Down Expand Up @@ -33,10 +34,12 @@ export default function PanelActions() {
const actionFrameStatusStatus = actionFrameStatus.status;

useEffect(() => {
const hashSuccess = GetHashSuccess();
if (hashSuccess) {
if (GetHashSuccess()) {
setCurrentStatus("success");
}
if (GetHashAction()) {
setIsInActionFrame(true);
}
if (DetailMatch()) {
setCurrentPage("detail");
} else if (ListMatch()) {
Expand All @@ -50,6 +53,8 @@ export default function PanelActions() {
if (topicDetail) {
if (
topicDetail.replies &&
topicDetail.replies[0] &&
topicDetail.replies[0].content &&
topicDetail.replies[0].content === "NEED_OBSERVE"
) {
console.debug("NEED_OBSERVE");
Expand Down Expand Up @@ -120,7 +125,7 @@ export default function PanelActions() {
useEffect(() => {
if (isInActionFrame && currentStatus === "success") {
console.debug("PostMessageSuccess");
PostMessageSuccess();
PostMessageSuccess(GetHashIndex() || 0);
}
}, [isInActionFrame, currentStatus]);

Expand Down
18 changes: 18 additions & 0 deletions src/topic/lib/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export const GetHashAction = () => {
return hash.includes(text);
};

export const GetHashIndex = () => {
const text = "cxauto_index_";
const index = hash.match(/cxauto_index_(\d+)/);
if (index) {
return parseInt(index[1]);
}
return 0;
};

export const AppendHashStart = (url?: string) => {
const text = "cxauto_start";
if (url) {
Expand Down Expand Up @@ -50,3 +59,12 @@ export const AppendHashAction = (url?: string) => {
appendHash(text);
return true;
};

export const AppendHashActionIndex = (index: number, url?: string) => {
const text = `#cxauto_index_${index}`;
if (url) {
return `${url}#${text}`;
}
appendHash(text);
return true;
};
3 changes: 0 additions & 3 deletions src/topic/lib/hooks/use-current-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export default function useCurrentStatus({
setIsButtonDisabled(false);
const hashAction = GetHashAction();
const hashStart = GetHashStart() || hashAction;
if (hashAction) {
setIsInActionFrame(true);
}
if (hashStart) {
setCurrentStatus("triggered");
}
Expand Down
4 changes: 2 additions & 2 deletions src/topic/lib/running-topic-detail-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ const ActionButtonToSubmit = async ({
event.preventDefault();
});
}
await Standby(2);
await Standby(1);
element.click();
textarea.value = "";
if (i === contextToReply.length - 1) {
continue;
}
await Standby(1);
addLogItem(`Reply submitted, waiting to continue...`);
await Standby(1);
}
return true;
} catch (error) {
Expand Down
21 changes: 13 additions & 8 deletions src/topic/lib/running-topic-list-reply.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useLogStore, useStatusStore } from "@topic/lib/store";
import { AppendHashAction, AppendHashStart } from "@topic/lib/hash";
import {
AppendHashAction,
AppendHashActionIndex,
AppendHashStart,
} from "@topic/lib/hash";
import MakeError from "@topic/lib/make-error";
import { ReceiveMessage } from "@topic/lib/window-message";
import { ClearMessage, ReceiveMessage } from "@topic/lib/window-message";
import Standby from "@topic/lib/standby";
import { ActionFrameStatusStatus } from "@topic/lib/types";

Expand All @@ -26,7 +30,7 @@ export default async function RunningTopicListReply(
const init = InitRunningTopicListReply();
if (init) {
addLogItem("开始:批量回复讨论");
Standby(0.1);
await Standby(0.3);
setActionFrameStatusStatus("waitingToStart");
}
}
Expand All @@ -36,11 +40,11 @@ export default async function RunningTopicListReply(
const src = topicList[index].url;
if (src) {
const srcHashed = AppendHashAction(src).toString();
setActionFrameStatusSrc(srcHashed);
const srcIndexed = AppendHashActionIndex(index, srcHashed).toString();
setActionFrameStatusSrc(srcIndexed);
addLogItem(`正在回复讨论:${topicList[index].title}`);
Standby(0.1);
await Standby(0.5);
ReceiveMessage();
Standby(0.3);
setActionFrameStatusStatus("running");
} else {
MakeError("未找到讨论链接");
Expand All @@ -53,7 +57,7 @@ export default async function RunningTopicListReply(
if (status === "success") {
const title = topicList[index].title;
addLogItem(`已回复讨论:${title}`);
Standby(0.4);
await Standby(1);
setActionFrameStatusStatus("waitingToNext");
}
const total = actionFrameStatusTotal || 0;
Expand Down Expand Up @@ -81,12 +85,13 @@ export const PrepareNextRunningTopicListReply = async (
index: number,
total: number,
) => {
addLogItem("等待继续...");
setActionFrameStatusSrc("");
await Standby(0.5);
if (index === total - 1) {
return true;
} else {
setActionFrameStatusIndex(index + 1);
Standby(0.5);
setActionFrameStatusStatus("waitingToStart");
return false;
}
Expand Down
26 changes: 19 additions & 7 deletions src/topic/lib/window-message.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { useStatusStore } from "@topic/lib/store";

export const PostMessageSuccess = () => {
console.debug("PostMessageSuccess");
window.parent.postMessage(
{ type: "cxauto_action_frame", status: "success" },
"*",
);
export const PostMessageSuccess = (index: number) => {
const message = {
type: "cxauto_action_frame",
status: "success",
index: index,
};
console.debug("PostMessageSuccess", message);
window.parent.postMessage(message, "*");
};

export const ReceiveMessage = () => {
window.addEventListener("message", (event) => {
console.debug("ReceiveMessage", event.data);
if (event.data.type === "cxauto_action_frame") {
const status = useStatusStore.getState().actionFrameStatus.status;
if (event.data.status === "success" && status === "running") {
const index = useStatusStore.getState().actionFrameStatus.index;
if (
event.data.status === "success" &&
status === "running" &&
index === event.data.index
) {
useStatusStore.getState().setActionFrameStatusStatus("success");
}
}
});
};

export const ClearMessage = () => {
window.removeEventListener("message", () => {});
};

0 comments on commit 11cb2ef

Please sign in to comment.