Skip to content

Commit

Permalink
show action result on status hover
Browse files Browse the repository at this point in the history
  • Loading branch information
3mcd committed May 29, 2024
1 parent 44171e9 commit a734d3a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 15 deletions.
1 change: 1 addition & 0 deletions core/actions/_lib/runActionInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export async function runActionInstance(args: RunActionInstanceArgs) {
pub_id: args.pubId,
user_id: "userId" in args ? args.userId : null,
status: "error" in result ? ActionRunStatus.failure : ActionRunStatus.success,
result,
config: actionInstanceResult.value.config,
params: args,
event: "userId" in args ? undefined : args.event,
Expand Down
24 changes: 16 additions & 8 deletions core/actions/api/serverAction.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"use server";

import { revalidateTag } from "next/cache";
import { revalidateTag, unstable_cache } from "next/cache";

import { db } from "~/kysely/database";
import { UsersId } from "~/kysely/types/public/Users";
import { getLoginData } from "~/lib/auth/loginData";
import { defineServerAction } from "~/lib/server/defineServerAction";
import {
ActionInstanceRunResult,
RunActionInstanceArgs,
runActionInstance as runActionInstanceInner,
} from "../_lib/runActionInstance";

export const runActionInstance = async function runActionInstance(
export const runActionInstance = defineServerAction(async function runActionInstance(
args: Omit<RunActionInstanceArgs, "userId" | "event">
): Promise<ActionInstanceRunResult> {
const user = await getLoginData();
Expand All @@ -24,11 +25,18 @@ export const runActionInstance = async function runActionInstance(

// Retrieve the pub's community id in order to revalidate the next server
// cache after the action is run.
const pub = await db
.selectFrom("pubs")
.select(["community_id as communityId"])
.where("id", "=", args.pubId)
.executeTakeFirst();
const pub = await unstable_cache(
async () =>
db
.selectFrom("pubs")
.select(["community_id as communityId"])
.where("id", "=", args.pubId)
.executeTakeFirst(),
[args.pubId],
{
revalidate: 60 * 60 * 24 * 7,
}
)();

const result = await runActionInstanceInner({
...args,
Expand All @@ -44,4 +52,4 @@ export const runActionInstance = async function runActionInstance(
}

return result;
};
});
2 changes: 1 addition & 1 deletion core/app/c/[communitySlug]/SideNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const Links = ({
<NavLink
href={`${prefix}/activity/actions`}
text="Activity"
icon={<Activity className="w-4" />}
icon={<Activity className="h-4 w-4" />}
/>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Event } from "@prisma/client";

import { Badge } from "ui/badge";
import { DataTableColumnHeader } from "ui/data-table";
import { HoverCard, HoverCardContent, HoverCardTrigger } from "ui/hover-card";

import { PubTitle } from "~/app/components/PubTitle";

Expand All @@ -19,6 +20,7 @@ export type ActionRun = {
values: { field: { slug: string }; value: unknown }[] | Record<string, unknown>;
createdAt: Date;
} | null;
result: unknown;
} & (
| {
event: Event;
Expand Down Expand Up @@ -83,15 +85,29 @@ export const getActionRunsTableColumns = () =>
{
header: ({ column }) => <DataTableColumnHeader column={column} title="Status" />,
accessorKey: "status",
cell: ({ getValue }) => {
cell: ({ row, getValue }) => {
let badge: React.ReactNode;
switch (getValue()) {
case "success":
return <Badge>success</Badge>;
badge = <Badge>success</Badge>;
break;
case "failure":
return <Badge variant="destructive">failure</Badge>;
badge = <Badge variant="destructive">failure</Badge>;
break;
default:
return <Badge variant="outline">unknown</Badge>;
badge = <Badge variant="outline">unknown</Badge>;
break;
}
return (
<HoverCard>
<HoverCardTrigger className="cursor-default">{badge}</HoverCardTrigger>
<HoverCardContent>
<pre>
<code>{JSON.stringify(row.original.result, null, 2)}</code>
</pre>
</HoverCardContent>
</HoverCard>
);
},
},
] as const satisfies ColumnDef<ActionRun, unknown>[];
1 change: 1 addition & 0 deletions core/app/c/[communitySlug]/activity/actions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default async function Page({
"action_runs.event",
"action_runs.params",
"action_runs.status",
"action_runs.result",
"action_runs.created_at as createdAt",
jsonObjectFrom(
eb
Expand Down
2 changes: 2 additions & 0 deletions core/kysely/types/public/ActionRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default interface ActionRunsTable {
created_at: ColumnType<Date, Date | string | undefined, Date | string>;

updated_at: ColumnType<Date, Date | string | undefined, Date | string>;

result: ColumnType<unknown, unknown, unknown>;
}

export type ActionRuns = Selectable<ActionRunsTable>;
Expand Down
4 changes: 2 additions & 2 deletions core/kysely/types/public/PublicSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { type default as StagesTable } from "./Stages";
import { type default as UsersTable } from "./Users";

export default interface PublicSchema {
action_runs: ActionRunsTable;

_prisma_migrations: PrismaMigrationsTable;

users: UsersTable;
Expand Down Expand Up @@ -84,6 +86,4 @@ export default interface PublicSchema {
PubsInStages: PubsInStagesTable;

rules: RulesTable;

action_runs: ActionRunsTable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `result` to the `action_runs` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "action_runs" ADD COLUMN "result" JSONB NOT NULL;
1 change: 1 addition & 0 deletions core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ model ActionRun {
event Event?
params Json?
status ActionRunStatus
result Json
user User? @relation(fields: [userId], references: [id])
userId String? @map(name: "user_id")
createdAt DateTime @default(now()) @map(name: "created_at")
Expand Down

0 comments on commit a734d3a

Please sign in to comment.