perf(studio): defer optional panels and trim validator imports
This commit is contained in:
1 parent
a45d792563
commit
3b76d063a6
7 files changed
+401
-118
No files matched your search
@@ -1,34 +1,53 @@
|
||||
import { QueryClient, queryOptions } from "@tanstack/react-query";
|
||||
import { z } from "zod/mini";
|
||||
import {
|
||||
array as zArray,
|
||||
boolean as zBoolean,
|
||||
enum as zEnum,
|
||||
int as zInt,
|
||||
iso as zIso,
|
||||
literal as zLiteral,
|
||||
looseObject as zLooseObject,
|
||||
maxLength as zMaxLength,
|
||||
nonnegative as zNonnegative,
|
||||
nullable as zNullable,
|
||||
number as zNumber,
|
||||
object as zObject,
|
||||
optional as zOptional,
|
||||
positive as zPositive,
|
||||
refine as zRefine,
|
||||
regex as zRegex,
|
||||
string as zString,
|
||||
uuid as zUuid,
|
||||
} from "zod/mini";
|
||||
import { adminFetch } from "@/lib/admin-fetch";
|
||||
import type { ImportJob } from "@/lib/furni/import-job";
|
||||
|
||||
const endpoint = "/api/admin/studio/import-jobs";
|
||||
const historyTimeoutMs = 15_000;
|
||||
const jobSchema = z.looseObject({
|
||||
id: z.uuid(),
|
||||
userId: z.int().check(z.positive()),
|
||||
createdAt: z.iso.datetime({ offset: true }),
|
||||
updatedAt: z.iso.datetime({ offset: true }),
|
||||
state: z.enum(["queued", "running", "completed", "interrupted", "cancelled"]),
|
||||
translate: z.boolean(),
|
||||
mode: z.optional(z.literal("repair")),
|
||||
syncKind: z.optional(z.enum(["official", "clone"])),
|
||||
operationId: z.optional(z.string()),
|
||||
retryOf: z.optional(z.string()),
|
||||
sourceId: z.optional(z.string()),
|
||||
cancelRequested: z.optional(z.boolean()),
|
||||
langs: z.optional(z.array(z.string())),
|
||||
items: z.array(
|
||||
z.looseObject({
|
||||
id: z.int().check(z.nonnegative()),
|
||||
classname: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
type: z.string(),
|
||||
revision: z.number(),
|
||||
category: z.string(),
|
||||
state: z.enum([
|
||||
const jobSchema = zLooseObject({
|
||||
id: zUuid(),
|
||||
userId: zInt().check(zPositive()),
|
||||
createdAt: zIso.datetime({ offset: true }),
|
||||
updatedAt: zIso.datetime({ offset: true }),
|
||||
state: zEnum(["queued", "running", "completed", "interrupted", "cancelled"]),
|
||||
translate: zBoolean(),
|
||||
mode: zOptional(zLiteral("repair")),
|
||||
syncKind: zOptional(zEnum(["official", "clone"])),
|
||||
operationId: zOptional(zString()),
|
||||
retryOf: zOptional(zString()),
|
||||
sourceId: zOptional(zString()),
|
||||
cancelRequested: zOptional(zBoolean()),
|
||||
langs: zOptional(zArray(zString())),
|
||||
items: zArray(
|
||||
zLooseObject({
|
||||
id: zInt().check(zNonnegative()),
|
||||
classname: zString(),
|
||||
name: zString(),
|
||||
description: zString(),
|
||||
type: zString(),
|
||||
revision: zNumber(),
|
||||
category: zString(),
|
||||
state: zEnum([
|
||||
"pending",
|
||||
"running",
|
||||
"done",
|
||||
@@ -36,26 +55,24 @@ const jobSchema = z.looseObject({
|
||||
"cancelled",
|
||||
"interrupted",
|
||||
]),
|
||||
error: z.optional(z.string()),
|
||||
warnings: z.optional(z.array(z.string())),
|
||||
itemId: z.optional(z.number()),
|
||||
error: zOptional(zString()),
|
||||
warnings: zOptional(zArray(zString())),
|
||||
itemId: zOptional(zNumber()),
|
||||
}),
|
||||
),
|
||||
});
|
||||
const historySchema = z.object({
|
||||
ok: z.literal(true),
|
||||
jobs: z.array(jobSchema).check(z.maxLength(30)),
|
||||
nextCursor: z.optional(
|
||||
z.nullable(
|
||||
z.string().check(
|
||||
z.regex(
|
||||
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\|[0-9a-f-]{36}$/i,
|
||||
),
|
||||
z.refine((value) => {
|
||||
const historySchema = zObject({
|
||||
ok: zLiteral(true),
|
||||
jobs: zArray(jobSchema).check(zMaxLength(30)),
|
||||
nextCursor: zOptional(
|
||||
zNullable(
|
||||
zString().check(
|
||||
zRegex(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\|[0-9a-f-]{36}$/i),
|
||||
zRefine((value) => {
|
||||
const [timestamp, id] = value.split("|");
|
||||
return (
|
||||
z.iso.datetime({ precision: 3 }).safeParse(timestamp).success &&
|
||||
z.uuid().safeParse(id).success
|
||||
zIso.datetime({ precision: 3 }).safeParse(timestamp).success &&
|
||||
zUuid().safeParse(id).success
|
||||
);
|
||||
}),
|
||||
),
|
||||
@@ -96,9 +113,9 @@ export async function readFurnitureJobResponse(
|
||||
fallback: string,
|
||||
): Promise<ImportJob> {
|
||||
const data = await responseData(response, fallback);
|
||||
const parsed = z
|
||||
.object({ ok: z.literal(true), job: jobSchema })
|
||||
.safeParse(data);
|
||||
const parsed = zObject({ ok: zLiteral(true), job: jobSchema }).safeParse(
|
||||
data,
|
||||
);
|
||||
if (!parsed.success) throw Error("Invalid import job response");
|
||||
return parsed.data.job as ImportJob;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user