From 8c57b1bee45307c513608eee88b394900d38902e Mon Sep 17 00:00:00 2001 From: hmydgz Date: Tue, 29 Oct 2024 15:48:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E9=9B=86=E7=BC=96=E8=BE=91=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8E=92=E5=BA=8F=E5=8A=9F=E8=83=BD=20#298?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/apis/operation-set.ts | 8 +- src/apis/operation.ts | 9 +- .../operation-set/AddToOperationSet.tsx | 4 +- .../operation-set/OperationSetEditor.tsx | 152 ++++++++++++++---- yarn.lock | 6 +- 6 files changed, 137 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index e0de93e..b9f89f9 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "linkify-react": "^3.0.4", "linkifyjs": "^3.0.5", "lodash-es": "^4.17.21", - "maa-copilot-client": "https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#0.1.0-SNAPSHOT.758.dbc2eb1", + "maa-copilot-client": "https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#0.1.0-SNAPSHOT.784.58f96e5", "normalize.css": "^8.0.1", "prettier": "^3.2.5", "react": "^18.0.0", diff --git a/src/apis/operation-set.ts b/src/apis/operation-set.ts index 573ae81..59ffaa8 100644 --- a/src/apis/operation-set.ts +++ b/src/apis/operation-set.ts @@ -4,6 +4,7 @@ import { CopilotSetPageRes, CopilotSetQuery, CopilotSetStatus, + CopilotSetUpdateReq, } from 'maa-copilot-client' import useSWR from 'swr' import useSWRInfinite from 'swr/infinite' @@ -184,12 +185,7 @@ export async function createOperationSet(req: { }) } -export async function updateOperationSet(req: { - id: number - name: string - description: string - status: CopilotSetStatus -}) { +export async function updateOperationSet(req: CopilotSetUpdateReq) { await new OperationSetApi().updateCopilotSet({ copilotSetUpdateReq: req }) } diff --git a/src/apis/operation.ts b/src/apis/operation.ts index 2e127cf..2e81602 100644 --- a/src/apis/operation.ts +++ b/src/apis/operation.ts @@ -114,7 +114,14 @@ export function useOperations({ const isReachingEnd = !!pages?.some((page) => !page.hasNext) - const operations = pages?.map((page) => page.data).flat() + const _operations = pages?.map((page) => page.data).flat() ?? [] + + // 按 operationIds 的顺序排序 + const operations = operationIds?.length + ? operationIds + ?.map((id) => _operations?.find((v) => v.id === id)) + .filter((v) => !!v) + : _operations return { error, diff --git a/src/components/operation-set/AddToOperationSet.tsx b/src/components/operation-set/AddToOperationSet.tsx index 0eeb202..4a2a42d 100644 --- a/src/components/operation-set/AddToOperationSet.tsx +++ b/src/components/operation-set/AddToOperationSet.tsx @@ -142,7 +142,7 @@ export function AddToOperationSet({ return ( <> -
+
{error && ( {formatError(error)} @@ -165,7 +165,7 @@ export function AddToOperationSet({
{ const updateInfo = async () => { if (isEdit) { - await updateOperationSet({ + const params: UpdateCopilotSetRequest['copilotSetUpdateReq'] = { id: operationSet!.id, name, description, status, - }) + } + + if (sortIds?.length) params.copilotIds = sortIds + + await updateOperationSet(params) AppToaster.show({ intent: 'success', @@ -168,6 +195,7 @@ interface FormValues { idsToAdd?: number[] idsToRemove?: number[] + sortIds?: number[] } function OperationSetForm({ operationSet, onSubmit }: FormProps) { @@ -207,7 +235,7 @@ function OperationSetForm({ operationSet, onSubmit }: FormProps) { return (
@@ -330,7 +359,11 @@ interface OperationSelectorProps { } interface OperationSelectorRef { - getValues(): { idsToAdd: number[]; idsToRemove: number[] } + getValues(): { + idsToAdd: number[] + idsToRemove: number[] + sortIds?: number[] + } } function OperationSelector({ @@ -341,6 +374,14 @@ function OperationSelector({ operationIds: operationSet.copilotIds, }) + const [isSorting, setIsSorting] = useState(false) + + const [renderedOperations, setRenderedOperations] = useState([]) + useEffect(() => { + setRenderedOperations([...(operations ?? [])]) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [operations.length]) + const [checkboxOverrides, setCheckboxOverrides] = useState( {} as Record, ) @@ -356,7 +397,7 @@ function OperationSelector({ getValues() { const idsToAdd: number[] = [] const idsToRemove: number[] = [] - + const sortIds: number[] = [] Object.entries(checkboxOverrides).forEach(([idKey, checked]) => { const id = +idKey if (isNaN(id)) return @@ -367,13 +408,37 @@ function OperationSelector({ idsToRemove.push(id) } }) - - return { idsToAdd, idsToRemove } + if (isSorting) { + sortIds.push( + ...renderedOperations + .map(({ id }) => (checkboxOverrides[id] === false ? 0 : id)) + .filter((id) => !!id), + ) + } + + return { idsToAdd, idsToRemove, sortIds } }, }), - [checkboxOverrides, alreadyAdded], + [checkboxOverrides, isSorting, alreadyAdded, renderedOperations], ) + const sensors = useSensors(useSensor(PointerSensor)) + + const handleDragEnd = (event: DragEndEvent) => { + const { active, over } = event + + if (active.id !== over?.id) { + setRenderedOperations((items) => { + const oldIndex = items.findIndex((v) => v.id === active.id) + const newIndex = items.findIndex((v) => v.id === over?.id) + + return arrayMove(items, oldIndex, newIndex) + }) + + setIsSorting(true) + } + } + return (
{error && ( @@ -382,28 +447,53 @@ function OperationSelector({ )} - {operations?.map(({ id, parsedContent }) => ( -
- { - const checked = (e.target as HTMLInputElement).checked - setCheckboxOverrides((prev) => ({ ...prev, [id]: checked })) - }} - > -
{id}: 
-
- {parsedContent.doc.title} -
-
-
- ))} + + id)} + strategy={verticalListSortingStrategy} + > + {renderedOperations?.map(({ id, parsedContent }) => ( + + {({ listeners, attributes }) => ( +
+ + { + const checked = (e.target as HTMLInputElement).checked + setCheckboxOverrides((prev) => ({ + ...prev, + [id]: checked, + })) + }} + > +
+ {id}:  +
+
+ {parsedContent.doc.title} +
+
+
+ )} +
+ ))} +
+
) } diff --git a/yarn.lock b/yarn.lock index 5986c87..e046387 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3642,9 +3642,9 @@ lru-cache@^6.0.0: resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.0.tgz#0bd445ca57363465900f4d1f9bd8db343a4d95c3" integrity sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q== -"maa-copilot-client@https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#0.1.0-SNAPSHOT.758.dbc2eb1": - version "0.1.0-SNAPSHOT.758.dbc2eb1" - resolved "https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#d6750070b90fec2cac02d3f3d718a3165fd5d55a" +"maa-copilot-client@https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#0.1.0-SNAPSHOT.784.58f96e5": + version "0.1.0-SNAPSHOT.784.58f96e5" + resolved "https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#cecb29af32fa36d6f11ed46469765abe56caafaf" make-dir@^2.1.0: version "2.1.0" From 7644f430ad5b688120860bc4cff812e0627ad2fa Mon Sep 17 00:00:00 2001 From: hmydgz Date: Tue, 26 Nov 2024 12:01:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20feat:=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operation-set/OperationSetEditor.tsx | 81 ++++--------------- 1 file changed, 14 insertions(+), 67 deletions(-) diff --git a/src/components/operation-set/OperationSetEditor.tsx b/src/components/operation-set/OperationSetEditor.tsx index a102c97..c112808 100644 --- a/src/components/operation-set/OperationSetEditor.tsx +++ b/src/components/operation-set/OperationSetEditor.tsx @@ -24,9 +24,7 @@ import { import { useOperations } from 'apis/operation' import { - addToOperationSet, createOperationSet, - removeFromOperationSet, updateOperationSet, useRefreshOperationSet, useRefreshOperationSets, @@ -87,9 +85,7 @@ export function OperationSetEditorDialog({ name, description, status, - idsToAdd, - idsToRemove, - sortIds, + copilotIds, }) => { const updateInfo = async () => { if (isEdit) { @@ -98,10 +94,9 @@ export function OperationSetEditorDialog({ name, description, status, + copilotIds, } - if (sortIds?.length) params.copilotIds = sortIds - await updateOperationSet(params) AppToaster.show({ @@ -129,35 +124,7 @@ export function OperationSetEditorDialog({ } } - const addOperations = async () => { - if (operationSet && idsToAdd?.length) { - await addToOperationSet({ - operationSetId: operationSet.id, - operationIds: idsToAdd, - }) - - AppToaster.show({ - intent: 'success', - message: `添加作业成功`, - }) - } - } - - const removeOperations = async () => { - if (operationSet && idsToRemove?.length) { - await removeFromOperationSet({ - operationSetId: operationSet.id, - operationIds: idsToRemove, - }) - - AppToaster.show({ - intent: 'success', - message: `移除作业成功`, - }) - } - } - - await Promise.all([updateInfo(), addOperations(), removeOperations()]) + await updateInfo() onClose() } @@ -195,7 +162,7 @@ interface FormValues { idsToAdd?: number[] idsToRemove?: number[] - sortIds?: number[] + copilotIds?: number[] } function OperationSetForm({ operationSet, onSubmit }: FormProps) { @@ -360,9 +327,7 @@ interface OperationSelectorProps { interface OperationSelectorRef { getValues(): { - idsToAdd: number[] - idsToRemove: number[] - sortIds?: number[] + copilotIds?: number[] } } @@ -374,8 +339,6 @@ function OperationSelector({ operationIds: operationSet.copilotIds, }) - const [isSorting, setIsSorting] = useState(false) - const [renderedOperations, setRenderedOperations] = useState([]) useEffect(() => { setRenderedOperations([...(operations ?? [])]) @@ -395,31 +358,17 @@ function OperationSelector({ selectorRef, () => ({ getValues() { - const idsToAdd: number[] = [] - const idsToRemove: number[] = [] - const sortIds: number[] = [] - Object.entries(checkboxOverrides).forEach(([idKey, checked]) => { - const id = +idKey - if (isNaN(id)) return - - if (checked && !alreadyAdded(id)) { - idsToAdd.push(id) - } else if (!checked && alreadyAdded(id)) { - idsToRemove.push(id) - } - }) - if (isSorting) { - sortIds.push( - ...renderedOperations - .map(({ id }) => (checkboxOverrides[id] === false ? 0 : id)) - .filter((id) => !!id), - ) - } - - return { idsToAdd, idsToRemove, sortIds } + const copilotIds: number[] = [] + copilotIds.push( + ...renderedOperations + .map(({ id }) => (checkboxOverrides[id] === false ? 0 : id)) + .filter((id) => !!id), + ) + + return { copilotIds } }, }), - [checkboxOverrides, isSorting, alreadyAdded, renderedOperations], + [checkboxOverrides, renderedOperations], ) const sensors = useSensors(useSensor(PointerSensor)) @@ -434,8 +383,6 @@ function OperationSelector({ return arrayMove(items, oldIndex, newIndex) }) - - setIsSorting(true) } }