import { useEffect, useState } from "react"; import { useNavigate, useSearch } from "@tanstack/react-router"; import { Button, Card, CardTitle, FormControl, Input, Select, SelectItem } from "@app/components/v2"; import { ROUTE_PATHS } from "@app/const/routes"; import { useWorkspace } from "@app/context"; import { useCreateIntegration } from "@app/hooks/api"; import { useGetIntegrationAuthApps, useGetIntegrationAuthById, useGetIntegrationAuthNorthflankSecretGroups } from "@app/hooks/api/integrationAuth"; export const NorthflankConfigurePage = () => { const navigate = useNavigate(); const { mutateAsync } = useCreateIntegration(); const { currentWorkspace } = useWorkspace(); const [selectedSourceEnvironment, setSelectedSourceEnvironment] = useState( currentWorkspace.environments[0].slug ); const [secretPath, setSecretPath] = useState("/"); const [targetAppId, setTargetAppId] = useState(""); const [targetSecretGroupId, setTargetSecretGroupId] = useState(""); const [isLoading, setIsLoading] = useState(false); const integrationAuthId = useSearch({ from: ROUTE_PATHS.SecretManager.Integratons.NorthflankConfigurePage.id, select: (el) => el.integrationAuthId }); const { data: integrationAuth } = useGetIntegrationAuthById((integrationAuthId as string) ?? ""); const { data: integrationAuthApps } = useGetIntegrationAuthApps({ integrationAuthId: (integrationAuthId as string) ?? "" }); const { data: integrationAuthSecretGroups } = useGetIntegrationAuthNorthflankSecretGroups({ integrationAuthId: (integrationAuthId as string) ?? "", appId: targetAppId }); useEffect(() => { if (integrationAuthApps) { if (integrationAuthApps.length > 0) { // setTargetApp(integrationAuthApps[0].name); setTargetAppId(integrationAuthApps[0].appId as string); } else { // setTargetApp("none"); setTargetAppId("none"); } } }, [integrationAuthApps]); useEffect(() => { if (integrationAuthSecretGroups) { if (integrationAuthSecretGroups.length > 0) { // case: project has at least 1 secret group in Northflank setTargetSecretGroupId(integrationAuthSecretGroups[0].groupId); } else { // case: project has no secret groups in Northflank setTargetSecretGroupId("none"); } } }, [integrationAuthSecretGroups]); const handleButtonClick = async () => { try { if (!integrationAuth?.id) return; setIsLoading(true); await mutateAsync({ integrationAuthId: integrationAuth?.id, isActive: true, app: integrationAuthApps?.find( (integrationAuthApp) => integrationAuthApp.appId === targetAppId )?.name, appId: targetAppId, sourceEnvironment: selectedSourceEnvironment, targetServiceId: targetSecretGroupId, secretPath }); setIsLoading(false); navigate({ to: "/secret-manager/$projectId/integrations", params: { projectId: currentWorkspace.id } }); } catch (err) { console.error(err); } }; return integrationAuth && selectedSourceEnvironment && integrationAuthApps && targetAppId ? (
Northflank Integration setSecretPath(evt.target.value)} placeholder="Provide a path, default is /" /> {targetSecretGroupId !== "" && integrationAuthSecretGroups && ( )}
) : (
); };