Add types for TaskType to TaskHook

Pre-commit

Apply better naming to task api hooks

Lint
This commit is contained in:
rjmacarthy
2023-01-27 10:51:32 +00:00
parent e6009933db
commit bf77d4dc60
3 changed files with 31 additions and 4 deletions
+3 -3
View File
@@ -4,7 +4,7 @@ import { TaskEmptyState } from "src/components/EmptyState";
import { LoadingScreen } from "src/components/Loading/LoadingScreen";
import { Task } from "src/components/Tasks/Task";
import { TaskInfos } from "src/components/Tasks/TaskTypes";
import { apiHooksByType, ERROR_CODES } from "src/lib/constants";
import { ERROR_CODES, taskApiHooks } from "src/lib/constants";
import { getTypeSafei18nKey } from "src/lib/i18n";
import { TaskType } from "src/types/Task";
@@ -14,8 +14,8 @@ type TaskPageProps = {
export const TaskPage = ({ type }: TaskPageProps) => {
const { t } = useTranslation(["tasks", "common"]);
const apiHook = apiHooksByType[type];
const { tasks, isLoading, reset, trigger, error } = apiHook(type);
const taskApiHook = taskApiHooks[type];
const { tasks, isLoading, reset, trigger, error } = taskApiHook(type);
const taskInfo = TaskInfos.find((taskType) => taskType.type === type);
if (isLoading) {
+2 -1
View File
@@ -14,6 +14,7 @@ import {
useRankInitialPromptsTask,
useRankPrompterRepliesTask,
} from "src/hooks/tasks/useRankReplies";
import { TaskApiHooks } from "src/types/Hooks";
import { TaskType } from "src/types/Task";
export const ERROR_CODES = {
@@ -28,7 +29,7 @@ export const ERROR_CODES = {
TASK_MESSAGE_TOO_LONG: 1008,
};
export const apiHooksByType = {
export const taskApiHooks: TaskApiHooks = {
[TaskType.random]: useGenericTaskAPI,
[TaskType.assistant_reply]: useCreateAssistantReply,
[TaskType.initial_prompt]: useCreateInitialPrompt,
+26
View File
@@ -0,0 +1,26 @@
import { MutatorCallback, MutatorOptions } from "swr";
import { BaseTask, TaskResponse, TaskType } from "./Task";
type ConcreteTaskResponse = TaskResponse<BaseTask>;
type TaskError = { errorCode: number; message: string };
type Trigger = (
extraArgument?: unknown,
options?: MutatorOptions<ConcreteTaskResponse>
) => Promise<ConcreteTaskResponse>;
type Reset = (
data?: ConcreteTaskResponse | Promise<ConcreteTaskResponse> | MutatorCallback<ConcreteTaskResponse>,
opts?: boolean | MutatorOptions<ConcreteTaskResponse>
) => Promise<ConcreteTaskResponse>;
type TaskAPIHook = {
tasks: TaskResponse<BaseTask>[];
isLoading: boolean;
error: TaskError;
trigger: Trigger;
reset: Reset;
};
export type TaskApiHooks = Record<TaskType, (args: TaskType) => TaskAPIHook>;