mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -9,6 +9,7 @@ import { ArrowLeft, UploadCloud, X, FileVideo, FileText, Image, FileAudio } from
|
||||
|
||||
import { useAssets } from "@/contexts/AdminAssetsContext";
|
||||
import { useAuth } from "@/contexts/AuthContext";
|
||||
import { useUnsavedChangesGuard } from "@/hooks/useUnsavedChangesGuard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
@@ -130,7 +131,7 @@ export default function AddAsset() {
|
||||
watch,
|
||||
setError,
|
||||
clearErrors,
|
||||
formState: { errors },
|
||||
formState: { errors, isDirty },
|
||||
} = useForm({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: {
|
||||
@@ -145,6 +146,10 @@ export default function AddAsset() {
|
||||
const isVideo = file?.type?.startsWith("video/");
|
||||
const isAudio = file?.type?.startsWith("audio/");
|
||||
|
||||
// setValue("_file", ...) doesn't mark isDirty (no shouldDirty), so a
|
||||
// picked-but-unsubmitted file wouldn't otherwise be caught by the guard.
|
||||
const { bypassOnce, dialog: unsavedChangesDialog } = useUnsavedChangesGuard(isDirty || !!file);
|
||||
|
||||
// ── Auto-derive file_type from MIME ───────────────────────────────────────
|
||||
const fileType = file ? resolveFileType(file.type) : null;
|
||||
|
||||
@@ -188,7 +193,7 @@ export default function AddAsset() {
|
||||
createdBy: user?.user_id,
|
||||
});
|
||||
|
||||
if (result) navigate(-1);
|
||||
if (result) { bypassOnce(); navigate(-1); }
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -357,6 +362,8 @@ export default function AddAsset() {
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{unsavedChangesDialog}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { ArrowLeft, UploadCloud, X, FileVideo, FileText, Image, RefreshCw } from
|
||||
|
||||
import { useAssets } from "@/contexts/AdminAssetsContext";
|
||||
import { useAuth } from "@/contexts/AuthContext";
|
||||
import { useUnsavedChangesGuard } from "@/hooks/useUnsavedChangesGuard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
@@ -177,6 +178,8 @@ export default function EditAsset() {
|
||||
const isVideo = asset?.file_type === "video";
|
||||
const hasThumbnailChange = !!thumbnailRef.current;
|
||||
|
||||
const { bypassOnce, dialog: unsavedChangesDialog } = useUnsavedChangesGuard(isDirty || hasThumbnailChange);
|
||||
|
||||
const onSubmit = async (data) => {
|
||||
const result = await updateAsset(
|
||||
assetId,
|
||||
@@ -191,6 +194,7 @@ export default function EditAsset() {
|
||||
);
|
||||
|
||||
if (!result) return;
|
||||
bypassOnce();
|
||||
navigate(-1);
|
||||
};
|
||||
|
||||
@@ -340,6 +344,8 @@ export default function EditAsset() {
|
||||
</div>
|
||||
|
||||
</form>
|
||||
|
||||
{unsavedChangesDialog}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user