Skip to content

Commit

Permalink
update engine API routes to include team slug
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Mar 4, 2025
1 parent 35ccf8f commit 4aacef1
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 44 deletions.
48 changes: 33 additions & 15 deletions apps/dashboard/src/@3rdweb-sdk/react/hooks/useEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,15 @@ export function useEngineUpdateDeployment() {

export type RemoveEngineFromDashboardIParams = {
instanceId: string;
teamIdOrSlug: string;
};

export async function removeEngineFromDashboard({
instanceId,
teamIdOrSlug,
}: RemoveEngineFromDashboardIParams) {
const res = await apiServerProxy({
pathname: `/v1/engine/${instanceId}`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${instanceId}`,
method: "DELETE",
});

Expand Down Expand Up @@ -273,18 +275,20 @@ export async function deleteCloudHostedEngine({
}

export type EditEngineInstanceParams = {
teamIdOrSlug: string;
instanceId: string;
name: string;
url: string;
};

export async function editEngineInstance({
teamIdOrSlug,
instanceId,
name,
url,
}: EditEngineInstanceParams) {
const res = await apiServerProxy({
pathname: `/v1/engine/${instanceId}`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${instanceId}`,
method: "PUT",
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -1628,7 +1632,7 @@ export function useEngineSubscriptionsLastBlock(params: {

interface EngineResourceMetrics {
error: string;
data: {
result: {
cpu: number;
memory: number;
errorRate: ResultItem[];
Expand All @@ -1637,15 +1641,15 @@ interface EngineResourceMetrics {
};
}

export function useEngineSystemMetrics(engineId: string) {
export function useEngineSystemMetrics(engineId: string, teamIdOrSlug: string) {
const [enabled, setEnabled] = useState(true);

return useQuery({
queryKey: engineKeys.systemMetrics(engineId),
queryFn: async () => {
const res = await apiServerProxy({
method: "GET",
pathname: `/v1/engine/${engineId}/metrics`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${engineId}/metrics`,
});

if (!res.ok) {
Expand Down Expand Up @@ -1674,14 +1678,14 @@ export interface EngineAlertRule {
pausedAt: Date | null;
}

export function useEngineAlertRules(engineId: string) {
export function useEngineAlertRules(engineId: string, teamIdOrSlug: string) {
return useQuery({
queryKey: engineKeys.alertRules(engineId),
queryFn: async () => {
const res = await apiServerProxy<{
data: EngineAlertRule[];
}>({
pathname: `/v1/engine/${engineId}/alert-rules`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${engineId}/alert-rules`,
method: "GET",
});

Expand All @@ -1703,14 +1707,19 @@ export interface EngineAlert {
endsAt: Date | null;
}

export function useEngineAlerts(engineId: string, limit: number, offset = 0) {
export function useEngineAlerts(
engineId: string,
teamIdOrSlug: string,
limit: number,
offset = 0,
) {
return useQuery({
queryKey: engineKeys.alerts(engineId),
queryFn: async () => {
const res = await apiServerProxy<{
data: EngineAlert[];
}>({
pathname: `/v1/engine/${engineId}/alerts`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${engineId}/alerts`,
searchParams: {
limit: `${limit}`,
offset: `${offset}`,
Expand Down Expand Up @@ -1754,14 +1763,17 @@ export type EngineNotificationChannel = {
subscriptionRoutes: string[];
};

export function useEngineNotificationChannels(engineId: string) {
export function useEngineNotificationChannels(
engineId: string,
teamIdOrSlug: string,
) {
return useQuery({
queryKey: engineKeys.notificationChannels(engineId),
queryFn: async () => {
const res = await apiServerProxy<{
data: EngineNotificationChannel[];
}>({
pathname: `/v1/engine/${engineId}/notification-channels`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${engineId}/notification-channels`,
method: "GET",
});

Expand All @@ -1781,15 +1793,18 @@ export interface CreateNotificationChannelInput {
value: string;
}

export function useEngineCreateNotificationChannel(engineId: string) {
export function useEngineCreateNotificationChannel(
engineId: string,
teamIdOrSlug: string,
) {
const queryClient = useQueryClient();

return useMutation({
mutationFn: async (input: CreateNotificationChannelInput) => {
const res = await apiServerProxy<{
data: EngineNotificationChannel;
}>({
pathname: `/v1/engine/${engineId}/notification-channels`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${engineId}/notification-channels`,
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -1812,13 +1827,16 @@ export function useEngineCreateNotificationChannel(engineId: string) {
});
}

export function useEngineDeleteNotificationChannel(engineId: string) {
export function useEngineDeleteNotificationChannel(
engineId: string,
teamIdOrSlug: string,
) {
const queryClient = useQueryClient();

return useMutation({
mutationFn: async (notificationChannelId: string) => {
const res = await apiServerProxy({
pathname: `/v1/engine/${engineId}/notification-channels/${notificationChannelId}`,
pathname: `/v1/teams/${teamIdOrSlug}/engine/${engineId}/notification-channels/${notificationChannelId}`,
method: "DELETE",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ const formSchema = z.object({

type ImportEngineParams = z.infer<typeof formSchema>;

async function importEngine(data: ImportEngineParams) {
async function importEngine({
teamIdOrSlug,
...data
}: ImportEngineParams & { teamIdOrSlug: string }) {
// Instance URLs should end with a /.
const url = data.url.endsWith("/") ? data.url : `${data.url}/`;

const res = await apiServerProxy({
pathname: "/v1/engine",
pathname: `/v1/teams/${teamIdOrSlug}/engine`,
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -59,7 +62,7 @@ export function EngineImportCard(props: {
<EngineImportCardUI
prefillImportUrl={props.prefillImportUrl}
importEngine={async (params) => {
await importEngine(params);
await importEngine({ ...params, teamIdOrSlug: props.teamSlug });
router.push(`/team/${props.teamSlug}/~/engine`);
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ type RemovedEngineFromDashboard = (
) => Promise<void>;

export function EngineInstancesTable(props: {
teamIdOrSlug: string;
instances: EngineInstance[];
engineLinkPrefix: string;
}) {
const router = useDashboardRouter();

return (
<EngineInstancesTableUI
teamIdOrSlug={props.teamIdOrSlug}
instances={props.instances}
engineLinkPrefix={props.engineLinkPrefix}
deleteCloudHostedEngine={async (params) => {
Expand All @@ -100,6 +102,7 @@ export function EngineInstancesTable(props: {
}

export function EngineInstancesTableUI(props: {
teamIdOrSlug: string;
instances: EngineInstance[];
engineLinkPrefix: string;
deleteCloudHostedEngine: DeletedCloudHostedEngine;
Expand All @@ -124,6 +127,7 @@ export function EngineInstancesTableUI(props: {
{props.instances.map((instance) => (
<EngineInstanceRow
key={instance.id}
teamIdOrSlug={props.teamIdOrSlug}
instance={instance}
engineLinkPrefix={props.engineLinkPrefix}
deleteCloudHostedEngine={props.deleteCloudHostedEngine}
Expand All @@ -145,6 +149,7 @@ export function EngineInstancesTableUI(props: {
}

function EngineInstanceRow(props: {
teamIdOrSlug: string;
instance: EngineInstance;
engineLinkPrefix: string;
deleteCloudHostedEngine: DeletedCloudHostedEngine;
Expand Down Expand Up @@ -187,13 +192,15 @@ function EngineInstanceRow(props: {
</TableRow>

<EditModal
teamIdOrSlug={props.teamIdOrSlug}
instance={instance}
open={isEditModalOpen}
onOpenChange={setIsEditModalOpen}
editEngineInstance={props.editEngineInstance}
/>

<RemoveModal
teamIdOrSlug={props.teamIdOrSlug}
instance={instance}
onOpenChange={setIsRemoveModalOpen}
open={isRemoveModalOpen}
Expand Down Expand Up @@ -349,6 +356,7 @@ function EngineActionsDropdown(props: {

function EditModal(props: {
open: boolean;
teamIdOrSlug: string;
onOpenChange: (open: boolean) => void;
instance: EngineInstance;
editEngineInstance: EditedEngineInstance;
Expand All @@ -357,6 +365,7 @@ function EditModal(props: {
<Dialog open={props.open} onOpenChange={props.onOpenChange}>
<DialogContent className="overflow-hidden p-0">
<EditModalContent
teamIdOrSlug={props.teamIdOrSlug}
instance={props.instance}
editEngineInstance={props.editEngineInstance}
closeModal={() => props.onOpenChange(false)}
Expand All @@ -372,6 +381,7 @@ const editEngineFormSchema = z.object({
});

function EditModalContent(props: {
teamIdOrSlug: string;
instance: EngineInstance;
editEngineInstance: EditedEngineInstance;
closeModal: () => void;
Expand All @@ -396,6 +406,7 @@ function EditModalContent(props: {
onSubmit={form.handleSubmit((data) =>
editInstance.mutate(
{
teamIdOrSlug: props.teamIdOrSlug,
instanceId: props.instance.id,
name: data.name,
url: data.url,
Expand Down Expand Up @@ -475,6 +486,7 @@ function EditModalContent(props: {
}

function RemoveModal(props: {
teamIdOrSlug: string;
instance: EngineInstance;
open: boolean;
onOpenChange: (open: boolean) => void;
Expand All @@ -491,6 +503,7 @@ function RemoveModal(props: {
(instance.status === "active" && !instance.deploymentId) ? (
<RemoveEngineFromDashboardModalContent
instance={instance}
teamIdOrSlug={props.teamIdOrSlug}
close={() => onOpenChange(false)}
removeEngineFromDashboard={props.removeEngineFromDashboard}
/>
Expand All @@ -507,6 +520,7 @@ function RemoveModal(props: {
}

function RemoveEngineFromDashboardModalContent(props: {
teamIdOrSlug: string;
instance: EngineInstance;
close: () => void;
removeEngineFromDashboard: RemovedEngineFromDashboard;
Expand Down Expand Up @@ -552,6 +566,7 @@ function RemoveEngineFromDashboardModalContent(props: {
removeFromDashboard.mutate(
{
instanceId: instance.id,
teamIdOrSlug: props.teamIdOrSlug,
},
{
onSuccess: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const EngineInstancesList = (props: {
return (
<div className="flex grow flex-col">
<EngineInstancesTable
teamIdOrSlug={props.team_slug}
instances={props.instances}
engineLinkPrefix={engineLinkPrefix}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export default async function Page(props: {
loginRedirect(`/team/${params.team_slug}/~/engine`);
}

const res = await getEngineInstances({ authToken });
const res = await getEngineInstances({
authToken,
teamIdOrSlug: params.team_slug,
});

return (
<EngineInstancesList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import { RecentEngineAlertsSection } from "./RecentEngineAlerts";

export function EngineAlertsPage(props: {
instance: EngineInstance;
teamIdOrSlug: string;
}) {
const alertRulesQuery = useEngineAlertRules(props.instance.id);
const alertRulesQuery = useEngineAlertRules(
props.instance.id,
props.teamIdOrSlug,
);
const alertRules = alertRulesQuery.data ?? [];

return (
Expand All @@ -19,12 +23,14 @@ export function EngineAlertsPage(props: {
alertRules={alertRules}
engineId={props.instance.id}
alertRulesIsLoading={alertRulesQuery.isLoading}
teamIdOrSlug={props.teamIdOrSlug}
/>
<div className="h-8" />
<RecentEngineAlertsSection
alertRules={alertRules}
engineId={props.instance.id}
alertRulesIsLoading={alertRulesQuery.isLoading}
teamIdOrSlug={props.teamIdOrSlug}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ type CreateAlertMutation = UseMutationResult<
>;

export function ManageEngineAlertsSection(props: {
teamIdOrSlug: string;
alertRules: EngineAlertRule[];
alertRulesIsLoading: boolean;
engineId: string;
}) {
const notificationChannelsQuery = useEngineNotificationChannels(
props.engineId,
props.teamIdOrSlug,
);
const deleteAlertMutation = useEngineDeleteNotificationChannel(
props.engineId,
props.teamIdOrSlug,
);

const createAlertMutation = useEngineCreateNotificationChannel(
props.engineId,
props.teamIdOrSlug,
);

// not passing the mutation to avoid multiple rows sharing the same mutation state, we create the new mutation for each row instead in each component instead
Expand Down
Loading

0 comments on commit 4aacef1

Please sign in to comment.