Skip to content

Commit

Permalink
fix: abort task route, improvements on UI and more
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo committed Apr 26, 2023
1 parent a9cda03 commit 1165112
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 77 deletions.
3 changes: 2 additions & 1 deletion apps/client/public/locales/en-US/common.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"app-name": "VisualDynamics",
"app-name": "Visual Dynamics",
"abort": "Abort run",
"errors": {
"404": {
"title": "This place... isn't a place at all",
Expand Down
1 change: 0 additions & 1 deletion apps/client/public/locales/en-US/running.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"title": "Your Dynamic is Running",
"taskId": "Task ID",
"abort": "Abort run",
"description": "Info",
"type": "Model",
"molecule": "Molecule",
Expand Down
49 changes: 32 additions & 17 deletions apps/client/src/components/AdminRunningDynamicsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { Server } from "lucide-react";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";

import { DynamicRunningAbortButton } from "@app/components/DynamicRunningAbortButton";
import { GetAdminRunningDynamicsListResult } from "@app/queries/useAdminRunningDynamicsList";

interface AdminRunningDynamicsListProps {
runningDynamics: GetAdminRunningDynamicsListResult;
refetch: () => void;
}

export function AdminRunningDynamicsList({
refetch,
runningDynamics
}: AdminRunningDynamicsListProps) {
const router = useRouter();
Expand All @@ -31,24 +34,36 @@ export function AdminRunningDynamicsList({
{dynamics && dynamics.length > 0 ? (
<div className="flex flex-col gap-y-1 rounded-lg p-4 odd:bg-zinc-500/20 even:bg-zinc-500/10">
{dynamics.map((dynamic) => (
<div key={`${worker}_${dynamic.id}`}>
<div className="flex gap-x-1">
<p>{t("admin-running:dynamic.path")}:</p>
<p className="font-semibold">{dynamic.args[0]}</p>
</div>
<div className="flex gap-x-1">
<p>{t("admin-running:dynamic.started-at")}:</p>
<p className="font-semibold">
{Intl.DateTimeFormat(router.locale, {
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
}).format(new Date(dynamic.time_start * 1000))}
</p>
<div
className="flex flex-col gap-y-2"
key={`${worker}_${dynamic.id}`}
>
<div className="flex flex-col gap-y-1">
<div className="flex gap-x-1">
<p className="whitespace-nowrap">
{t("admin-running:dynamic.path")}:
</p>
<p className="font-semibold">{dynamic.args[0]}</p>
</div>
<div className="flex gap-x-1">
<p>{t("admin-running:dynamic.started-at")}:</p>
<p className="font-semibold">
{Intl.DateTimeFormat(router.locale, {
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
}).format(new Date(dynamic.time_start * 1000))}
</p>
</div>
</div>
<DynamicRunningAbortButton
refetch={refetch}
celeryId={dynamic.id}
folder={dynamic.args[0]}
/>
</div>
))}
</div>
Expand Down
54 changes: 54 additions & 0 deletions apps/client/src/components/DynamicRunningAbortButton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Slash } from "lucide-react";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";

import { Button } from "@app/components/Button";
import { api } from "@app/lib/api";

interface DynamicRunningAbortButtonProps {
celeryId: string;
disableAbortButton?: boolean;
folder: string;
refetch: () => void;
}

export function DynamicRunningAbortButton({
celeryId,
disableAbortButton,
folder,
refetch
}: DynamicRunningAbortButtonProps) {
const { t } = useTranslation(["common"]);
const router = useRouter();

async function abortTask() {
const formData = new FormData();

formData.append("taskId", celeryId);
formData.append("folder", folder);
api
.post("/run/abort", formData, {
headers: {
"Content-Type": "multipart/form-data"
}
})
.then(() => {
if (refetch) {
refetch();
} else {
router.push("/my-dynamics");
}
});
}

return (
<Button
className="w-fit bg-red-700 enabled:hover:bg-red-800"
LeftIcon={Slash}
disabled={disableAbortButton}
onClick={abortTask}
>
{t("common:abort")}
</Button>
);
}
9 changes: 6 additions & 3 deletions apps/client/src/hocs/withSSRAuth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { GetServerSidePropsContext, GetServerSidePropsResult } from "next";
import { getServerSession } from "next-auth";
import { getServerSession, Session } from "next-auth";

import { authOptions } from "@app/pages/api/auth/[...nextauth]";

type IncomingGSSP<P> = (ctx: GetServerSidePropsContext) => Promise<P>;
type IncomingGSSP<P> = (
ctx: GetServerSidePropsContext,
session?: Session
) => Promise<P>;

type WithAuthServerSidePropsResult = GetServerSidePropsResult<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -34,7 +37,7 @@ export function withSSRAuth(
}

if (incomingGSSP) {
const incomingGSSPResult = await incomingGSSP(ctx);
const incomingGSSPResult = await incomingGSSP(ctx, session);

if ("props" in incomingGSSPResult) {
return {
Expand Down
10 changes: 8 additions & 2 deletions apps/client/src/pages/admin/running.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const getServerSideProps = withSSRTranslations(withSSRAdmin(), {
});

export default function AdminSignup() {
const { data, isRefetching, isLoading } = useAdminRunningDynamicsList();
const { data, isRefetching, isLoading, refetch } =
useAdminRunningDynamicsList({
refetchOnMount: true
});
const { t } = useTranslation();

return (
Expand All @@ -42,7 +45,10 @@ export default function AdminSignup() {
<Spinner />
</div>
) : (
<AdminRunningDynamicsList runningDynamics={data} />
<AdminRunningDynamicsList
refetch={refetch}
runningDynamics={data}
/>
)}
</>
);
Expand Down
6 changes: 2 additions & 4 deletions apps/client/src/pages/dynamic/acpype.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import dynamic from "next/dynamic";
import { getServerSession, User } from "next-auth";
import { User } from "next-auth";
import { useTranslation } from "next-i18next";

import { FullPageLoader } from "@app/components/FullPageLoader";
import { SEO } from "@app/components/SEO";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { authOptions } from "@app/pages/api/auth/[...nextauth]";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";

const ACPYPEForm = dynamic(
Expand All @@ -18,8 +17,7 @@ const ACPYPEForm = dynamic(
);

export const getServerSideProps = withSSRTranslations(
withSSRAuth(async (ctx) => {
const session = await getServerSession(ctx.req, ctx.res, authOptions);
withSSRAuth(async (_, session) => {
if (session) {
const data = await getRunningDynamic(session.user.username);

Expand Down
7 changes: 2 additions & 5 deletions apps/client/src/pages/dynamic/apo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import dynamic from "next/dynamic";
import { getServerSession, User } from "next-auth";
import { User } from "next-auth";
import { useTranslation } from "next-i18next";

import { FullPageLoader } from "@app/components/FullPageLoader";
import { SEO } from "@app/components/SEO";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { authOptions } from "@app/pages/api/auth/[...nextauth]";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";

const APOForm = dynamic(
Expand All @@ -18,9 +17,7 @@ const APOForm = dynamic(
);

export const getServerSideProps = withSSRTranslations(
withSSRAuth(async (ctx) => {
const session = await getServerSession(ctx.req, ctx.res, authOptions);

withSSRAuth(async (_, session) => {
if (session) {
const data = await getRunningDynamic(session.user.username);

Expand Down
5 changes: 1 addition & 4 deletions apps/client/src/pages/dynamic/prodrg.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { AlertTriangle } from "lucide-react";
import dynamic from "next/dynamic";
import { getServerSession } from "next-auth";
import { User } from "next-auth";
import { useTranslation } from "next-i18next";

import { FullPageLoader } from "@app/components/FullPageLoader";
import { SEO } from "@app/components/SEO";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { authOptions } from "@app/pages/api/auth/[...nextauth]";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";

const PRODRGForm = dynamic(
Expand All @@ -20,8 +18,7 @@ const PRODRGForm = dynamic(
);

export const getServerSideProps = withSSRTranslations(
withSSRAuth(async (ctx) => {
const session = await getServerSession(ctx.req, ctx.res, authOptions);
withSSRAuth(async (_, session) => {
if (session) {
const data = await getRunningDynamic(session.user.username);

Expand Down
76 changes: 45 additions & 31 deletions apps/client/src/pages/dynamic/running.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { ArrowRight, FileCog, Slash } from "lucide-react";
import { ArrowRight, FileCog } from "lucide-react";
import dynamic from "next/dynamic";
import Link from "next/link";
import { useRouter } from "next/router";
import { User } from "next-auth";
import { useTranslation } from "next-i18next";

import { Button } from "@app/components/Button";
import { DynamicRunningAbortButton } from "@app/components/DynamicRunningAbortButton";
import { SEO } from "@app/components/SEO";
import { withSSRAuth } from "@app/hocs/withSSRAuth";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { api } from "@app/lib/api";
import { useRunningDynamic } from "@app/queries/useRunningDynamic";
import {
getRunningDynamic,
GetRunningDynamicResult,
useRunningDynamic
} from "@app/queries/useRunningDynamic";

const DynamicRunningRealtimeLog = dynamic(
() =>
Expand All @@ -32,29 +36,40 @@ const DynamicRunningStepList = dynamic(
}
);

export const getServerSideProps = withSSRTranslations(withSSRAuth(), {
namespaces: ["running"]
});
export const getServerSideProps = withSSRTranslations(
withSSRAuth(async (_, session) => {
if (session) {
const initialData = await getRunningDynamic(session.user.username);

export default function Running({ user }: { user: User }) {
const { data, isLoading, isRefetching } = useRunningDynamic(user.username);
const { t } = useTranslation(["navigation", "running"]);
const router = useRouter();

async function abortTask() {
if (!isLoading && !isRefetching) {
const formData = new FormData();

if (data && data.status === "running") {
formData.append("task_id", data.info.celeryId);
api.post("/run/abort", formData, {
headers: {
"Content-Type": "multipart/form-data"
}
});
}
return {
props: {
initialData
}
};
}

return {
props: {}
};
}),
{
namespaces: ["running"]
}
);

export default function Running({
initialData,
user
}: {
user: User;
initialData: GetRunningDynamicResult;
}) {
const { data, isRefetching } = useRunningDynamic(user.username, {
initialData,
refetchOnMount: true
});
const { t } = useTranslation(["navigation", "running"]);
const router = useRouter();

if (data?.status === "running") {
const disableAbortButton =
Expand All @@ -66,14 +81,13 @@ export default function Running({ user }: { user: User }) {
<>
<SEO title={t("running:title")} />
<div className="relative">
<Button
className="absolute right-0 top-1 w-fit bg-red-700 enabled:hover:bg-red-800"
LeftIcon={Slash}
disabled={disableAbortButton}
onClick={abortTask}
>
{t("running:abort")}
</Button>
<div className="absolute right-0 top-1">
<DynamicRunningAbortButton
folder={data.info.folder}
celeryId={data.info.celeryId}
disableAbortButton={disableAbortButton}
/>
</div>
<h4 className="font-bold uppercase text-primary-950">
{t("running:description")}
</h4>
Expand Down

0 comments on commit 1165112

Please sign in to comment.