diff --git a/apps/spu-ui/src/app/_components/on-demand/single-acquisition.tsx b/apps/spu-ui/src/app/_components/on-demand/single-acquisition.tsx index 00166cb..da159ed 100644 --- a/apps/spu-ui/src/app/_components/on-demand/single-acquisition.tsx +++ b/apps/spu-ui/src/app/_components/on-demand/single-acquisition.tsx @@ -6,6 +6,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { CameraIcon } from "lucide-react"; import { useForm } from "react-hook-form"; import { useQueue } from "@sophys-web/api-client/hooks"; +import { api } from "@sophys-web/api-client/react"; import { cn } from "@sophys-web/ui"; import { Button } from "@sophys-web/ui/button"; import { Checkbox } from "@sophys-web/ui/checkbox"; @@ -46,6 +47,7 @@ export function SingleAcquitision({ className?: string; }) { const [open, setOpen] = useState(false); + const { data } = api.auth.getUser.useQuery(); return ( @@ -78,6 +80,7 @@ export function SingleAcquitision({ {lastSampleParams !== undefined && ( { setOpen(false); @@ -90,11 +93,13 @@ export function SingleAcquitision({ } function SingleAcquisitionForm({ - className, lastSampleParams, + proposal, + className, onSubmitSuccess, }: { lastSampleParams: LastSampleParams; + proposal?: string; className?: string; onSubmitSuccess: () => void; }) { @@ -103,6 +108,7 @@ function SingleAcquisitionForm({ const form = useForm>({ resolver: zodResolver(schema), defaultValues: { + proposal: proposal, isRef: false, metadata: { row: lastSampleParams.row, diff --git a/apps/spu-ui/src/app/_components/queue/upload.tsx b/apps/spu-ui/src/app/_components/queue/upload.tsx index e553acc..f8500c2 100644 --- a/apps/spu-ui/src/app/_components/queue/upload.tsx +++ b/apps/spu-ui/src/app/_components/queue/upload.tsx @@ -1,11 +1,10 @@ "use client"; -import React, { useCallback, useState } from "react"; +import React, { useState } from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { CheckIcon, MoveRightIcon, UploadIcon } from "lucide-react"; import { useForm } from "react-hook-form"; import { z } from "zod"; -import type { Session } from "@sophys-web/auth"; import { useQueue } from "@sophys-web/api-client/hooks"; import { api } from "@sophys-web/api-client/react"; import { cn } from "@sophys-web/ui"; @@ -228,7 +227,9 @@ const stepsMap = [ ] as const; function StepByStepForm({ onSubmitSuccess }: { onSubmitSuccess?: () => void }) { - const { data: session, isLoading } = api.auth.getSession.useQuery(); + const { data: user } = api.auth.getUser.useQuery(); + + const { addBatch } = useQueue(); const [cleaningParams, setCleaningParams] = useState>(); @@ -236,24 +237,14 @@ function StepByStepForm({ onSubmitSuccess }: { onSubmitSuccess?: () => void }) { useState[]>(); const [capillaryParams, setCapillaryParams] = useState>(); - const [proposal, setProposal] = useState(""); + const [formProposal, setFormProposal] = useState(); const [useCapillary, setUseCapillary] = useState(false); - const { addBatch } = useQueue(); - - const getProposal = useCallback(() => { - if (!proposal && session) { - const user = session.user as unknown as Session["user"]; - return user.proposal; - } - return proposal; - }, [proposal, session]); - const { step, currentStepIdx, next, goTo } = useStepForm([ void }) { void }) { ,
Step 1: Proposal
-

Proposal: {proposal}

+

Proposal: {formProposal}

Use Capillary: {useCapillary ? "Yes" : "No"}

{!!useCapillary && ( <> @@ -342,15 +333,8 @@ function StepByStepForm({ onSubmitSuccess }: { onSubmitSuccess?: () => void }) {
, ]); - if (isLoading) { - return
Loading...
; - } - if (!session) { - return
Not authenticated
; - } - function onSubmitProposal(data: z.infer) { - setProposal(data.proposal); + setFormProposal(data.proposal); setUseCapillary(data.useCapillary); if (data.useCapillary) { next(); @@ -386,7 +370,7 @@ function StepByStepForm({ onSubmitSuccess }: { onSubmitSuccess?: () => void }) { args: [], kwargs: { ...params, - proposal, + proposal: formProposal, ...cleaningParams, }, itemType: "plan", @@ -399,7 +383,7 @@ function StepByStepForm({ onSubmitSuccess }: { onSubmitSuccess?: () => void }) { args: [], kwargs: { ...capillaryParams, - proposal, + proposal: formProposal, }, itemType: "plan", }, @@ -454,22 +438,23 @@ function StepByStepForm({ onSubmitSuccess }: { onSubmitSuccess?: () => void }) { { label: "Capillary", onClick: () => goTo(1), - isDisabled: !useCapillary || !proposal || currentStepIdx < 1, + isDisabled: !useCapillary || !formProposal || currentStepIdx < 1, }, { label: "Cleaning", onClick: () => goTo(2), - isDisabled: !proposal || currentStepIdx < 2, + isDisabled: !formProposal || currentStepIdx < 2, }, { label: "Acquisition", onClick: () => goTo(3), - isDisabled: !proposal || currentStepIdx < 3, + isDisabled: !formProposal || currentStepIdx < 3, }, { label: "Check", onClick: () => goTo(4), - isDisabled: !proposal || !acquisitionParams || currentStepIdx < 4, + isDisabled: + !formProposal || !acquisitionParams || currentStepIdx < 4, }, ]} currentStep={currentStepIdx} diff --git a/packages/api/src/router/auth.ts b/packages/api/src/router/auth.ts index fe55dfd..1e978e4 100644 --- a/packages/api/src/router/auth.ts +++ b/packages/api/src/router/auth.ts @@ -35,10 +35,29 @@ const principalSchema = z.object({ apiKeyScopes: z.array(z.string()).nullable(), }); +const userSchema = z.object({ + name: z.string(), + roles: z.array(z.string()).optional().nullable(), + scopes: z.array(z.string()).optional().nullable(), + proposal: z.string(), +}); + export const authRouter = { getSession: protectedProcedure.query(({ ctx }) => { return ctx.session; }), + getUser: protectedProcedure.query(({ ctx }) => { + const user = userSchema.safeParse({ + name: ctx.session.user.name, + roles: ctx.session.user.roles, + scopes: ctx.session.user.scopes, + proposal: ctx.session.user.proposal, + }); + if (!user.success) { + throw new Error("Invalid user session:", user.error); + } + return user.data; + }), whoAmI: protectedProcedure.query(async ({ ctx }) => { const fetchURL = `${env.BLUESKY_HTTPSERVER_URL}/api/auth/whoami`; const blueskyAccessToken = ctx.session.user.blueskyAccessToken;