Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: move required external auth buttons to the submit side #18586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions site/src/hooks/useExternalAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ export const useExternalAuth = (versionId: string | undefined) => {
externalAuthPollingState,
isLoadingExternalAuth,
externalAuthError: error,
isPollingExternalAuth: externalAuthPollingState === "polling",
};
};
165 changes: 108 additions & 57 deletions site/src/pages/TasksPage/TasksPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import Skeleton from "@mui/material/Skeleton";
import { API } from "api/api";
import { getErrorDetail, getErrorMessage } from "api/errors";
import { disabledRefetchOptions } from "api/queries/util";
import type { Template } from "api/typesGenerated";
import type { Template, TemplateVersionExternalAuth } from "api/typesGenerated";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { Avatar } from "components/Avatar/Avatar";
import { AvatarData } from "components/Avatar/AvatarData";
import { AvatarDataSkeleton } from "components/Avatar/AvatarDataSkeleton";
import { Button } from "components/Button/Button";
import { Form, FormFields, FormSection } from "components/Form/Form";
import { displayError } from "components/GlobalSnackbar/utils";
import { Margins } from "components/Margins/Margins";
import {
Expand Down Expand Up @@ -37,9 +36,16 @@ import {
TableRowSkeleton,
} from "components/TableLoader/TableLoader";

import { ExternalImage } from "components/ExternalImage/ExternalImage";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "components/Tooltip/Tooltip";
import { useAuthenticated } from "hooks";
import { useExternalAuth } from "hooks/useExternalAuth";
import { RotateCcwIcon, SendIcon } from "lucide-react";
import { RedoIcon, RotateCcwIcon, SendIcon } from "lucide-react";
import { AI_PROMPT_PARAMETER_NAME, type Task } from "modules/tasks/tasks";
import { WorkspaceAppStatus } from "modules/workspaces/WorkspaceAppStatus/WorkspaceAppStatus";
import { generateWorkspaceName } from "modules/workspaces/generateWorkspaceName";
Expand All @@ -50,12 +56,12 @@ import { Link as RouterLink } from "react-router-dom";
import TextareaAutosize from "react-textarea-autosize";
import { pageTitle } from "utils/page";
import { relativeTime } from "utils/time";
import { ExternalAuthButton } from "../CreateWorkspacePage/ExternalAuthButton";
import { type UserOption, UsersCombobox } from "./UsersCombobox";

type TasksFilter = {
user: UserOption | undefined;
};

const TasksPage: FC = () => {
const { user, permissions } = useAuthenticated();
const [filter, setFilter] = useState<TasksFilter>({
Expand Down Expand Up @@ -201,21 +207,20 @@ type TaskFormProps = {
const TaskForm: FC<TaskFormProps> = ({ templates }) => {
const { user } = useAuthenticated();
const queryClient = useQueryClient();

const [templateId, setTemplateId] = useState<string>(templates[0].id);
const {
externalAuth,
externalAuthPollingState,
startPollingExternalAuth,
isLoadingExternalAuth,
externalAuthError,
} = useExternalAuth(
templates.find((t) => t.id === templateId)?.active_version_id,
const [selectedTemplateId, setSelectedTemplateId] = useState<string>(
templates[0].id,
);

const hasAllRequiredExternalAuth = externalAuth?.every(
(auth) => auth.optional || auth.authenticated,
const selectedTemplate = templates.find(
(t) => t.id === selectedTemplateId,
) as Template;
const { externalAuth, externalAuthError, isPollingExternalAuth } =
useExternalAuth(selectedTemplate.active_version_id);
const missedExternalAuth = externalAuth?.filter(
(auth) => !auth.optional && !auth.authenticated,
);
const isMissingExternalAuth = missedExternalAuth
? missedExternalAuth.length > 0
: true;

const createTaskMutation = useMutation({
mutationFn: async ({ prompt, templateId }: CreateTaskMutationFnProps) =>
Expand All @@ -235,10 +240,6 @@ const TaskForm: FC<TaskFormProps> = ({ templates }) => {
const prompt = formData.get("prompt") as string;
const templateID = formData.get("templateID") as string;

if (!prompt || !templateID) {
return;
}

try {
await createTaskMutation.mutateAsync({
prompt,
Expand All @@ -253,8 +254,12 @@ const TaskForm: FC<TaskFormProps> = ({ templates }) => {
};

return (
<Form onSubmit={onSubmit} aria-label="Create AI task">
{Boolean(externalAuthError) && <ErrorAlert error={externalAuthError} />}
<form
onSubmit={onSubmit}
aria-label="Create AI task"
className="flex flex-col gap-4"
>
{externalAuthError && <ErrorAlert error={externalAuthError} />}

<fieldset
className="border border-border border-solid rounded-lg p-4"
Expand All @@ -274,7 +279,7 @@ const TaskForm: FC<TaskFormProps> = ({ templates }) => {
<div className="flex items-center justify-between pt-2">
<Select
name="templateID"
onValueChange={(value) => setTemplateId(value)}
onValueChange={(value) => setSelectedTemplateId(value)}
defaultValue={templates[0].id}
required
>
Expand All @@ -294,43 +299,89 @@ const TaskForm: FC<TaskFormProps> = ({ templates }) => {
</SelectContent>
</Select>

<Button
size="sm"
type="submit"
disabled={!hasAllRequiredExternalAuth}
>
<Spinner
loading={createTaskMutation.isPending || isLoadingExternalAuth}
>
<SendIcon />
</Spinner>
Run task
</Button>
<div className="flex items-center gap-2">
{missedExternalAuth && (
<ExternalAuthButtons
template={selectedTemplate}
missedExternalAuth={missedExternalAuth}
/>
)}

<Button size="sm" type="submit" disabled={isMissingExternalAuth}>
<Spinner
loading={createTaskMutation.isPending || isPollingExternalAuth}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one should be createTaskMutation.isPending || isLoadingExternalAuth || isPollingExternalAuth so it spins while waiting for auth to load as well right? (or we could use !missedExternalAuth).

>
<SendIcon />
</Spinner>
Run task
</Button>
</div>
</div>
</fieldset>
</form>
);
};

{!hasAllRequiredExternalAuth &&
externalAuth &&
externalAuth.length > 0 && (
<FormSection
title="External Authentication"
description="This template uses external services for authentication."
>
<FormFields>
{externalAuth.map((auth) => (
<ExternalAuthButton
key={auth.id}
auth={auth}
isLoading={externalAuthPollingState === "polling"}
onStartPolling={startPollingExternalAuth}
displayRetry={externalAuthPollingState === "abandoned"}
/>
))}
</FormFields>
</FormSection>
type ExternalAuthButtonProps = {
template: Template;
missedExternalAuth: TemplateVersionExternalAuth[];
};

const ExternalAuthButtons: FC<ExternalAuthButtonProps> = ({
template,
missedExternalAuth,
}) => {
const {
startPollingExternalAuth,
isPollingExternalAuth,
externalAuthPollingState,
} = useExternalAuth(template.active_version_id);
const shouldRetry = externalAuthPollingState === "abandoned";

return missedExternalAuth.map((auth) => {
return (
<div className="flex items-center gap-2" key={auth.id}>
<Button
variant="outline"
size="sm"
disabled={isPollingExternalAuth || auth.authenticated}
onClick={() => {
window.open(
auth.authenticate_url,
"_blank",
"width=900,height=600",
);
startPollingExternalAuth();
}}
>
<Spinner loading={isPollingExternalAuth}>
<ExternalImage src={auth.display_icon} />
</Spinner>
Connect to {auth.display_name}
</Button>

{shouldRetry && !auth.authenticated && (
<TooltipProvider>
<Tooltip delayDuration={100}>
<TooltipTrigger asChild>
<Button
variant="outline"
size="icon"
onClick={startPollingExternalAuth}
>
<RedoIcon />
<span className="sr-only">Refresh external auth</span>
</Button>
</TooltipTrigger>
<TooltipContent>
Retry connect to {auth.display_name}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ~ing sounds more natural: Retry connecting to

</TooltipContent>
</Tooltip>
</TooltipProvider>
)}
</Form>
);
</div>
);
});
};

type TasksFilterProps = {
Expand Down
Loading