Skip to content

Commit

Permalink
Fix Play/Pause for NI Task (#778)
Browse files Browse the repository at this point in the history
* [console] - adjusted task state initial fetch

* [console] - removed unnecessary deep copy

* [console] - minor docs explanation
  • Loading branch information
emilbon99 authored Aug 18, 2024
1 parent 7488207 commit c72cf34
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
10 changes: 6 additions & 4 deletions console/src/hardware/device/ontology.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ const TreeContextMenu: Ontology.TreeContextMenu = (props) => {
{singleResource && (
<>
<Menu.RenameItem />
<PMenu.Divider />
{first.data?.configured !== true && (
<PMenu.Item itemKey="configure" startIcon={<Icon.Hardware />}>
Configure
</PMenu.Item>
<>
<PMenu.Divider />
<PMenu.Item itemKey="configure" startIcon={<Icon.Hardware />}>
Configure
</PMenu.Item>
</>
)}
</>
)}
Expand Down
11 changes: 10 additions & 1 deletion console/src/hardware/opc/task/ReadTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import {
Input,
List,
Menu,
Status,
Synnax,
Text,
useAsyncEffect,
useSyncedRef,
} from "@synnaxlabs/pluto";
import { caseconv, primitiveIsZero } from "@synnaxlabs/x";
import { caseconv, deep, primitiveIsZero } from "@synnaxlabs/x";
import { useMutation } from "@tanstack/react-query";
import { type ReactElement, useCallback, useState } from "react";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -93,6 +94,7 @@ const Wrapped = ({
task,
}: WrappedTaskLayoutProps<Read, ReadPayload>): ReactElement => {
const client = Synnax.use();
const addStatus = Status.useAggregator();
const [device, setDevice] = useState<device.Device<Device.Properties> | undefined>(
undefined,
);
Expand Down Expand Up @@ -206,6 +208,13 @@ const Wrapped = ({

createTask({ key: task?.key, name, type: READ_TYPE, config });
},
onError: (e) => {
addStatus({
variant: "error",
message: "Failed to configure task",
description: e.message,
});
},
});

const start = useMutation({
Expand Down
4 changes: 3 additions & 1 deletion console/src/hardware/task/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ const Content = (): ReactElement => {
menu={({ keys }) => {
const selected = keys.map((k) => tasks.find((t) => t.key === k));
const canStart = selected.some((t) => t?.state?.details?.running !== true);
const canStop = selected.some((t) => t?.state?.details?.running === false);
const canStop = selected.some((t) => t?.state?.details?.running === true);
return (
<Menu.Menu
level="small"
iconSpacing="small"
onChange={{
delete: () => del.mutate(keys),
start: () => selected.forEach((t) => t?.executeCommand("start")),
stop: () => selected.forEach((t) => t?.executeCommand("stop")),
}}
>
{canStart && (
Expand Down
9 changes: 6 additions & 3 deletions console/src/hardware/task/common/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "@synnaxlabs/pluto";
import { caseconv, deep, Key, Keyed, Optional, UnknownRecord } from "@synnaxlabs/x";
import { useQuery } from "@tanstack/react-query";
import { FC, ReactElement, useCallback, useState } from "react";
import { FC, ReactElement, useCallback, useId, useState } from "react";
import { useDispatch } from "react-redux";
import { z } from "zod";

Expand Down Expand Up @@ -323,8 +323,11 @@ export const wrapTaskLayout = <T extends task.Task, P extends task.Payload>(
const client = Synnax.use();
const args = Layout.useSelectArgs<{ create: boolean }>(layoutKey);
const altKey = Layout.useSelectAltKey(layoutKey);
const id = useId();
// The query can't take into account state changes, so we need to use a unique
// key for every query.
const fetchTask = useQuery<WrappedTaskLayoutProps<T, P>>({
queryKey: [layoutKey, client?.key, altKey],
queryKey: [layoutKey, client?.key, altKey, id],
queryFn: async () => {
if (client == null || args.create)
return { initialValues: deep.copy(zeroPayload), layoutKey };
Expand Down Expand Up @@ -352,7 +355,7 @@ export const wrapTaskLayout = <T extends task.Task, P extends task.Payload>(
</Status.Text.Centered>
</Align.Space>
);
else if (!fetchTask.isPending)
else if (fetchTask.isSuccess)
content = <Wrapped {...fetchTask.data} layoutKey={layoutKey} />;
return <Eraser.Eraser>{content}</Eraser.Eraser>;
};
Expand Down

0 comments on commit c72cf34

Please sign in to comment.