fix(ui): router.refresh after server actions so status updates show without reload

Lab side reported that after accepting a job / advancing a step the
button kept its 'Yükleniyor' state and the page didn't reflect the new
status until they hit refresh. Two issues stacked on top of each other:

1. The button forms were passing the action through an extra
   startTransition wrap — 'action={(fd) => startTransition(() => action(fd))}'.
   With React 19 + useActionState this is unnecessary; useActionState
   already manages its own transition. The double transition can leave
   the dispatch's pending flag wedged in some race orderings, which
   matches what the user saw.

2. revalidatePath() on the server invalidates the RSC cache but does not
   trigger client navigation. So even after the action returned, the
   page kept rendering the stale Job snapshot — and since the buttons
   are conditional on job.status, the now-stale 'pending' status meant
   the button stayed visible.

Fix in JobActionsPanel and the four sibling components (connections
delete row, pending inbound, pending outbound, file row delete):
  - Removed the startTransition wrap; forms point at 'action' directly.
  - Added useRouter() and call router.refresh() in the same useEffect
    branch where the success toast fires. This forces the Server
    Component tree to re-fetch, picks up the new job.status, and the
    actions panel rerenders into whatever button is next in the flow.
  - Cleaned the now-unused useTransition imports.

Net effect: tap 'İşleme Al' → spinner appears, ~400ms later the toast
hits and the row updates in place to 'Sonraki Aşama' without any
manual refresh.
This commit is contained in:
kovakmedya
2026-05-22 01:15:32 +03:00
parent 6fec52b98d
commit cdb2a15643
5 changed files with 69 additions and 78 deletions
@@ -1,6 +1,6 @@
"use client";
import { useActionState, useEffect, useRef, useState, useTransition } from "react";
import { useActionState, useEffect, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { Download, FileText, ImageIcon, Layers, Loader2, Trash2, Upload } from "lucide-react";
import { toast } from "sonner";
@@ -240,7 +240,7 @@ function FileRow({ file }: { file: JobFileWithUrl }) {
deleteJobFileAction,
initialJobFileActionState,
);
const [, startTransition] = useTransition();
const router = useRouter();
const [open, setOpen] = useState(false);
const [downloadOpen, setDownloadOpen] = useState(false);
@@ -248,10 +248,11 @@ function FileRow({ file }: { file: JobFileWithUrl }) {
if (state.ok) {
toast.success("Dosya silindi.");
setOpen(false);
router.refresh();
} else if (state.error) {
toast.error(state.error);
}
}, [state]);
}, [state, router]);
function triggerDownload() {
// Use a programmatic anchor click — the server route streams the file
@@ -319,11 +320,7 @@ function FileRow({ file }: { file: JobFileWithUrl }) {
Vazgeç
</Button>
</DialogClose>
<form
action={(fd) => {
startTransition(() => action(fd));
}}
>
<form action={action}>
<input type="hidden" name="rowId" value={file.$id} />
<Button type="submit" variant="destructive" disabled={pending}>
{pending ? <Loader2 className="size-4 animate-spin" /> : <Trash2 className="size-4" />}