Skip to content

chore: patch 2.13.1 #13927

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

Merged
merged 3 commits into from
Jul 18, 2024
Merged
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
7 changes: 5 additions & 2 deletions site/src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,18 @@ export const getValidationErrorMessage = (error: unknown): string => {
return validationErrors.map((error) => error.detail).join("\n");
};

export const getErrorDetail = (error: unknown): string | undefined | null => {
export const getErrorDetail = (error: unknown): string | undefined => {
if (error instanceof Error) {
return "Please check the developer console for more details.";
}

if (isApiError(error)) {
return error.response.data.detail;
}

if (isApiErrorResponse(error)) {
return error.detail;
}
return null;

return undefined;
};
2 changes: 0 additions & 2 deletions site/src/contexts/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export type AuthContextValue = {
isUpdatingProfile: boolean;
user: User | undefined;
permissions: Permissions | undefined;
organizationIds: readonly string[] | undefined;
signInError: unknown;
updateProfileError: unknown;
signOut: () => void;
Expand Down Expand Up @@ -119,7 +118,6 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
permissions: permissionsQuery.data as Permissions | undefined,
signInError: loginMutation.error,
updateProfileError: updateProfileMutation.error,
organizationIds: userQuery.data?.organization_ids,
}}
>
{children}
Expand Down
1 change: 0 additions & 1 deletion site/src/contexts/auth/RequireAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe("useAuthenticated", () => {
wrapper: createAuthWrapper({
user: MockUser,
permissions: MockPermissions,
organizationIds: [],
}),
});
}).not.toThrow();
Expand Down
6 changes: 1 addition & 5 deletions site/src/contexts/auth/RequireAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type RequireKeys<T, R extends keyof T> = Omit<T, R> & {
// values are not undefined when authenticated
type AuthenticatedAuthContextValue = RequireKeys<
AuthContextValue,
"user" | "permissions" | "organizationIds"
"user" | "permissions"
>;

export const useAuthenticated = (): AuthenticatedAuthContextValue => {
Expand All @@ -88,9 +88,5 @@ export const useAuthenticated = (): AuthenticatedAuthContextValue => {
throw new Error("Permissions are not available.");
}

if (!auth.organizationIds) {
throw new Error("Organization ID is not available.");
}

return auth as AuthenticatedAuthContextValue;
};
35 changes: 7 additions & 28 deletions site/src/modules/dashboard/DashboardProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
createContext,
type FC,
type PropsWithChildren,
useState,
} from "react";
import { createContext, type FC, type PropsWithChildren } from "react";
import { useQuery } from "react-query";
import { appearance } from "api/queries/appearance";
import { entitlements } from "api/queries/entitlements";
Expand All @@ -15,12 +10,14 @@ import type {
} from "api/typesGenerated";
import { Loader } from "components/Loader/Loader";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";

export interface DashboardValue {
/**
* @deprecated Do not add new usage of this value. It is being removed as part
* of the multi-org work.
*/
organizationId: string;
setOrganizationId: (id: string) => void;
entitlements: Entitlements;
experiments: Experiments;
appearance: AppearanceConfig;
Expand All @@ -32,40 +29,22 @@ export const DashboardContext = createContext<DashboardValue | undefined>(

export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
const { metadata } = useEmbeddedMetadata();
const { user, organizationIds } = useAuthenticated();
const { user } = useAuthenticated();
const entitlementsQuery = useQuery(entitlements(metadata.entitlements));
const experimentsQuery = useQuery(experiments(metadata.experiments));
const appearanceQuery = useQuery(appearance(metadata.appearance));

const isLoading =
!entitlementsQuery.data || !appearanceQuery.data || !experimentsQuery.data;

const lastUsedOrganizationId = localStorage.getItem(
`user:${user.id}.lastUsedOrganizationId`,
);
const [activeOrganizationId, setActiveOrganizationId] = useState(() =>
lastUsedOrganizationId && organizationIds.includes(lastUsedOrganizationId)
? lastUsedOrganizationId
: organizationIds[0],
);

const setOrganizationId = useEffectEvent((id: string) => {
if (!organizationIds.includes(id)) {
throw new ReferenceError("Invalid organization ID");
}
localStorage.setItem(`user:${user.id}.lastUsedOrganizationId`, id);
setActiveOrganizationId(id);
});

if (isLoading) {
return <Loader fullscreen />;
}

return (
<DashboardContext.Provider
value={{
organizationId: activeOrganizationId,
setOrganizationId: setOrganizationId,
organizationId: user.organization_ids[0] ?? "default",
entitlements: entitlementsQuery.data,
experiments: experimentsQuery.data,
appearance: appearanceQuery.data,
Expand Down
31 changes: 22 additions & 9 deletions site/src/modules/resources/AgentLogs/useAgentLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,35 @@ export type UseAgentLogsOptions = Readonly<{
enabled?: boolean;
}>;

/**
* Defines a custom hook that gives you all workspace agent logs for a given
* workspace.
*
* Depending on the status of the workspace, all logs may or may not be
* available.
*/
export function useAgentLogs(
options: UseAgentLogsOptions,
): readonly WorkspaceAgentLog[] | undefined {
const { workspaceId, agentId, agentLifeCycleState, enabled = true } = options;

const queryClient = useQueryClient();
const queryOptions = agentLogs(workspaceId, agentId);
const query = useQuery({
...queryOptions,
enabled,
});
const logs = query.data;
const { data: logs, isFetched } = useQuery({ ...queryOptions, enabled });

// Track the ID of the last log received when the initial logs response comes
// back. If the logs are not complete, the ID will mark the start point of the
// Web sockets response so that the remaining logs can be received over time
const lastQueriedLogId = useRef(0);
useEffect(() => {
if (logs && lastQueriedLogId.current === 0) {
lastQueriedLogId.current = logs[logs.length - 1].id;
const isAlreadyTracking = lastQueriedLogId.current !== 0;
if (isAlreadyTracking) {
return;
}

const lastLog = logs?.at(-1);
if (lastLog !== undefined) {
lastQueriedLogId.current = lastLog.id;
}
}, [logs]);

Expand All @@ -42,7 +55,7 @@ export function useAgentLogs(
});

useEffect(() => {
if (agentLifeCycleState !== "starting" || !query.isFetched) {
if (agentLifeCycleState !== "starting" || !isFetched) {
return;
}

Expand All @@ -69,7 +82,7 @@ export function useAgentLogs(
return () => {
socket.close();
};
}, [addLogs, agentId, agentLifeCycleState, query.isFetched]);
}, [addLogs, agentId, agentLifeCycleState, isFetched]);

return logs;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, type FC, Suspense, useContext } from "react";
import { useQuery } from "react-query";
import { Outlet, useParams } from "react-router-dom";
import { Outlet, useLocation, useParams } from "react-router-dom";
import { myOrganizations } from "api/queries/users";
import type { Organization } from "api/typesGenerated";
import { Loader } from "components/Loader/Loader";
Expand All @@ -13,7 +13,7 @@ import NotFoundPage from "pages/404Page/404Page";
import { Sidebar } from "./Sidebar";

type OrganizationSettingsContextValue = {
currentOrganizationId: string;
currentOrganizationId?: string;
organizations: Organization[];
};

Expand All @@ -32,13 +32,18 @@ export const useOrganizationSettings = (): OrganizationSettingsContextValue => {
};

export const OrganizationSettingsLayout: FC = () => {
const { permissions, organizationIds } = useAuthenticated();
const location = useLocation();
const { permissions } = useAuthenticated();
const { experiments } = useDashboard();
const { organization } = useParams() as { organization: string };
const organizationsQuery = useQuery(myOrganizations());

const multiOrgExperimentEnabled = experiments.includes("multi-organization");

const inOrganizationSettings =
location.pathname.startsWith("/organizations") &&
location.pathname !== "/organizations/new";

if (!multiOrgExperimentEnabled) {
return <NotFoundPage />;
}
Expand All @@ -50,10 +55,13 @@ export const OrganizationSettingsLayout: FC = () => {
{organizationsQuery.data ? (
<OrganizationSettingsContext.Provider
value={{
currentOrganizationId:
organizationsQuery.data.find(
(org) => org.name === organization,
)?.id ?? organizationIds[0],
currentOrganizationId: !inOrganizationSettings
? undefined
: !organization
? organizationsQuery.data[0]?.id
: organizationsQuery.data.find(
(org) => org.name === organization,
)?.id,
organizations: organizationsQuery.data,
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const OrganizationSettingsPage: FC = () => {
css={styles.dangerButton}
variant="contained"
onClick={() =>
deleteOrganizationMutation.mutate(currentOrganizationId)
deleteOrganizationMutation.mutate(currentOrganizationId!)
}
>
Delete this organization
Expand Down
Loading
Loading