From 1a736799feda0e5ae99890c32fca91b0db95a6fb Mon Sep 17 00:00:00 2001 From: Jamey Huffnagle Date: Fri, 4 Oct 2024 11:29:28 -0400 Subject: [PATCH] feat(api-client, react-api-client): Wire up `/runs/:runId/currentState` (#16417) Works towards RSQ-161 --- api-client/src/runs/getRunCurrentState.ts | 17 +++++++++++ api-client/src/runs/index.ts | 1 + api-client/src/runs/types.ts | 28 +++++++++++++++++++ react-api-client/src/runs/index.ts | 1 + .../src/runs/useRunCurrentState.ts | 28 +++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 api-client/src/runs/getRunCurrentState.ts create mode 100644 react-api-client/src/runs/useRunCurrentState.ts diff --git a/api-client/src/runs/getRunCurrentState.ts b/api-client/src/runs/getRunCurrentState.ts new file mode 100644 index 00000000000..6a64d9b9e0f --- /dev/null +++ b/api-client/src/runs/getRunCurrentState.ts @@ -0,0 +1,17 @@ +import { GET, request } from '../request' + +import type { ResponsePromise } from '../request' +import type { HostConfig } from '../types' +import type { RunCurrentState } from './types' + +export function getRunCurrentState( + config: HostConfig, + runId: string +): ResponsePromise { + return request( + GET, + `/runs/${runId}/currentState`, + null, + config + ) +} diff --git a/api-client/src/runs/index.ts b/api-client/src/runs/index.ts index 9f314f4b025..183b8f7e4d4 100644 --- a/api-client/src/runs/index.ts +++ b/api-client/src/runs/index.ts @@ -10,6 +10,7 @@ export { getCommands } from './commands/getCommands' export { getCommandsAsPreSerializedList } from './commands/getCommandsAsPreSerializedList' export { createRunAction } from './createRunAction' export { getRunCommandErrors } from './commands/getRunCommandErrors' +export { getRunCurrentState } from './getRunCurrentState' export * from './createLabwareOffset' export * from './createLabwareDefinition' export * from './constants' diff --git a/api-client/src/runs/types.ts b/api-client/src/runs/types.ts index 5998259ae50..241a3892622 100644 --- a/api-client/src/runs/types.ts +++ b/api-client/src/runs/types.ts @@ -87,10 +87,24 @@ export interface Run { data: RunData } +export interface RunCurrentState { + data: RunCurrentStateData + links: RunCommandLink +} + export interface RunsLinks { current?: ResourceLink } +export interface RunCommandLink { + current: CommandLinkNoMeta +} + +export interface CommandLinkNoMeta { + id: string + href: string +} + export interface GetRunsParams { pageLength?: number // the number of items to include } @@ -100,6 +114,10 @@ export interface Runs { links: RunsLinks } +export interface RunCurrentStateData { + activeNozzleLayouts: Record // keyed by pipetteId +} + export const RUN_ACTION_TYPE_PLAY: 'play' = 'play' export const RUN_ACTION_TYPE_PAUSE: 'pause' = 'pause' export const RUN_ACTION_TYPE_STOP: 'stop' = 'stop' @@ -173,3 +191,13 @@ export interface UpdateErrorRecoveryPolicyRequest { } export type UpdateErrorRecoveryPolicyResponse = Record + +/** + * Current Run State Data + */ +export interface NozzleLayoutValues { + startingNozzle: string + activeNozzles: string[] + config: NozzleLayoutConfig +} +export type NozzleLayoutConfig = 'column' | 'row' | 'full' | 'subrect' diff --git a/react-api-client/src/runs/index.ts b/react-api-client/src/runs/index.ts index 4b04d913487..71e3360a5f9 100644 --- a/react-api-client/src/runs/index.ts +++ b/react-api-client/src/runs/index.ts @@ -1,5 +1,6 @@ export { useAllRunsQuery } from './useAllRunsQuery' export { useRunQuery } from './useRunQuery' +export { useRunCurrentState } from './useRunCurrentState' export { useCreateRunMutation } from './useCreateRunMutation' export { useDeleteRunMutation } from './useDeleteRunMutation' export { useCreateCommandMutation } from './useCreateCommandMutation' diff --git a/react-api-client/src/runs/useRunCurrentState.ts b/react-api-client/src/runs/useRunCurrentState.ts new file mode 100644 index 00000000000..6eb46589e87 --- /dev/null +++ b/react-api-client/src/runs/useRunCurrentState.ts @@ -0,0 +1,28 @@ +import { useQuery } from 'react-query' +import type { AxiosError } from 'axios' +import type { RunCurrentState, HostConfig } from '@opentrons/api-client' +import type { UseQueryOptions, UseQueryResult } from 'react-query' +import { useHost } from '../api' +import { getRunCurrentState } from '@opentrons/api-client' + +export function useRunCurrentState( + runId: string | null, + options: UseQueryOptions = {}, + hostOverride?: HostConfig +): UseQueryResult { + const contextHost = useHost() + const host = + hostOverride != null ? { ...contextHost, ...hostOverride } : contextHost + + return useQuery( + [host, 'runs', runId, 'currentState'], + () => + getRunCurrentState(host as HostConfig, runId as string).then( + response => response.data + ), + { + enabled: host != null && runId != null && options.enabled !== false, + ...options, + } + ) +}