sanity-plugin-mux-input

5.1.15.1.2
dist/index.js
~dist/index.jsModified
+163−208
Index: package/dist/index.js
===================================================================
--- package/dist/index.js
+++ package/dist/index.js
@@ -314,9 +314,9 @@
 }
 function useDialogState() {
 	return useState(!1);
 }
-const PLUGIN_VERSION_QUERY = { sanityVersion: "5.1.1" };
+const PLUGIN_VERSION_QUERY = { sanityVersion: "5.1.2" };
 function saveSecrets(client, token, secretKey, enableSignedUrls, signingKeyId, signingKeyPrivate, drmConfigId) {
 	let doc = {
 		_id: "secrets.mux",
 		_type: "mux.apiKey",
@@ -2631,45 +2631,41 @@
 }
 function useResyncAsset(options) {
 	let $ = c(10), client = useClient$1(), toast = useToast(), [resyncState, setResyncState] = useState("idle"), [resyncError, setResyncError] = useState(null), showToast = options?.showToast ?? !1, t0;
 	$[0] !== client || $[1] !== options || $[2] !== showToast || $[3] !== toast ? (t0 = async (asset) => {
-		if (!asset.assetId) {
-			showToast && toast.push({
-				title: "Cannot resync",
-				description: "Asset has no Mux ID",
-				status: "error"
-			}), options?.onError?.(/* @__PURE__ */ Error("Asset has no Mux ID"));
-			return;
+		if (!asset.assetId) showToast && toast.push({
+			title: "Cannot resync",
+			description: "Asset has no Mux ID",
+			status: "error"
+		}), options?.onError?.(/* @__PURE__ */ Error("Asset has no Mux ID"));
+		else if (!asset._id) showToast && toast.push({
+			title: "Cannot resync",
+			description: "Asset has no document ID",
+			status: "error"
+		}), options?.onError?.(/* @__PURE__ */ Error("Asset has no document ID"));
+		else {
+			setResyncState("syncing"), setResyncError(null);
+			try {
+				let muxData = (await getAsset(client, asset.assetId)).data, dataWithKeys = addKeysToMuxData(muxData);
+				return await client.patch(asset._id).set({
+					status: muxData.status,
+					data: dataWithKeys,
+					...muxData.meta?.title && { filename: muxData.meta.title }
+				}).commit({ returnDocuments: !1 }), setResyncState("success"), showToast && toast.push({
+					title: "Asset synced",
+					description: "Data has been updated from Mux",
+					status: "success"
+				}), options?.onSuccess?.(muxData), muxData;
+			} catch (t1) {
+				let error = t1;
+				setResyncState("error"), setResyncError(error), console.error("Failed to refresh asset data:", error), showToast && toast.push({
+					title: "Sync failed",
+					description: "Could not sync asset from Mux",
+					status: "error"
+				}), options?.onError?.(error);
+				return;
+			}
 		}
-		if (!asset._id) {
-			showToast && toast.push({
-				title: "Cannot resync",
-				description: "Asset has no document ID",
-				status: "error"
-			}), options?.onError?.(/* @__PURE__ */ Error("Asset has no document ID"));
-			return;
-		}
-		setResyncState("syncing"), setResyncError(null);
-		try {
-			let muxData = (await getAsset(client, asset.assetId)).data, dataWithKeys = addKeysToMuxData(muxData);
-			return await client.patch(asset._id).set({
-				status: muxData.status,
-				data: dataWithKeys,
-				...muxData.meta?.title && { filename: muxData.meta.title }
-			}).commit({ returnDocuments: !1 }), setResyncState("success"), showToast && toast.push({
-				title: "Asset synced",
-				description: "Data has been updated from Mux",
-				status: "success"
-			}), options?.onSuccess?.(muxData), muxData;
-		} catch (t1) {
-			let error = t1;
-			setResyncState("error"), setResyncError(error), console.error("Failed to refresh asset data:", error), showToast && toast.push({
-				title: "Sync failed",
-				description: "Could not sync asset from Mux",
-				status: "error"
-			}), options?.onError?.(error);
-			return;
-		}
 	}, $[0] = client, $[1] = options, $[2] = showToast, $[3] = toast, $[4] = t0) : t0 = $[4];
 	let resyncAsset = t0, t1 = resyncState === "syncing", t2;
 	return $[5] !== resyncAsset || $[6] !== resyncError || $[7] !== resyncState || $[8] !== t1 ? (t2 = {
 		resyncState,
@@ -2927,31 +2923,21 @@
 					description: errorMessage
 				}), await refreshAssetData(), onAdd(track), onClose();
 			}
 		});
-		if (!result.found || !result.track) {
-			toast.push({
-				title: "Caption track may have been added",
-				status: "warning",
-				description: "The track was created but its status could not be determined. It may still be processing. Please refresh the page to see if it appears."
-			}), onClose();
-			return;
-		}
-		if (result.status !== "errored") {
-			if (result.status === "preparing") {
-				toast.push({
-					title: "Caption track is processing",
-					status: "info",
-					description: "The track was created and is being processed. It will appear in the list shortly."
-				}), await refreshAssetData(), onAdd(result.track), onClose();
-				return;
-			}
-			await refreshAssetData(), toast.push({
-				title: "Caption track added",
-				status: "success",
-				description: "Caption track added successfully"
-			}), onAdd(result.track), onClose();
-		}
+		!result.found || !result.track ? (toast.push({
+			title: "Caption track may have been added",
+			status: "warning",
+			description: "The track was created but its status could not be determined. It may still be processing. Please refresh the page to see if it appears."
+		}), onClose()) : result.status !== "errored" && (result.status === "preparing" ? (toast.push({
+			title: "Caption track is processing",
+			status: "info",
+			description: "The track was created and is being processed. It will appear in the list shortly."
+		}), await refreshAssetData(), onAdd(result.track), onClose()) : (await refreshAssetData(), toast.push({
+			title: "Caption track added",
+			status: "success",
+			description: "Caption track added successfully"
+		}), onAdd(result.track), onClose()));
 	}, handleGenerateSubtitles = async () => {
 		if (!asset.assetId) throw Error("Asset ID is required");
 		let audioTrack = (await getAsset(client, asset.assetId)).data.tracks?.find((track) => track.type === "audio");
 		if (!audioTrack || !audioTrack.id) throw toast.push({
@@ -2997,36 +2983,32 @@
 				});
 				return;
 			}
 		}
-		if (!name.trim()) {
-			toast.push({
-				title: "Audio name required",
-				status: "error",
-				description: "Please enter an audio name for this caption track"
-			});
-			return;
+		if (!name.trim()) toast.push({
+			title: "Audio name required",
+			status: "error",
+			description: "Please enter an audio name for this caption track"
+		});
+		else if (!languageCode.trim()) toast.push({
+			title: "Language code required",
+			status: "error",
+			description: "Please enter a language code (e.g., en, es, fr)"
+		});
+		else {
+			setIsSubmitting(!0);
+			try {
+				isAutogenerated ? await handleGenerateSubtitles() : await handleAddTrackFromUrl();
+			} catch (error) {
+				toast.push({
+					title: "Failed to add caption track",
+					status: "error",
+					description: extractErrorMessage(error, "Failed to add caption track")
+				});
+			} finally {
+				setIsSubmitting(!1);
+			}
 		}
-		if (!languageCode.trim()) {
-			toast.push({
-				title: "Language code required",
-				status: "error",
-				description: "Please enter a language code (e.g., en, es, fr)"
-			});
-			return;
-		}
-		setIsSubmitting(!0);
-		try {
-			isAutogenerated ? await handleGenerateSubtitles() : await handleAddTrackFromUrl();
-		} catch (error) {
-			toast.push({
-				title: "Failed to add caption track",
-				status: "error",
-				description: extractErrorMessage(error, "Failed to add caption track")
-			});
-		} finally {
-			setIsSubmitting(!1);
-		}
 	};
 	return /* @__PURE__ */ jsx(Dialog, {
 		id: dialogId,
 		header: "Add Caption Track",
@@ -3296,17 +3278,13 @@
 					description: errorMessage
 				}), await refreshAssetData(), onUpdate(erroredTrack, oldTrackId), setIsSubmitting(!1);
 			}
 		});
-		if (!result.found || !result.track) {
-			toast.push({
-				title: "Caption track may have been updated",
-				status: "warning",
-				description: "The track was updated but its status could not be determined. It may still be processing. Please refresh the page to see if it appears."
-			}), setIsSubmitting(!1);
-			return;
-		}
-		result.status !== "errored" && (await refreshAssetData(), result.status === "preparing" ? toast.push({
+		!result.found || !result.track ? (toast.push({
+			title: "Caption track may have been updated",
+			status: "warning",
+			description: "The track was updated but its status could not be determined. It may still be processing. Please refresh the page to see if it appears."
+		}), setIsSubmitting(!1)) : result.status !== "errored" && (await refreshAssetData(), result.status === "preparing" ? toast.push({
 			title: "Caption track is processing",
 			status: "info",
 			description: "The track was updated and is being processed. It will appear in the list shortly."
 		}) : toast.push({
@@ -3314,76 +3292,72 @@
 			status: "success",
 			description: "Caption track updated successfully"
 		}), onUpdate(result.track, oldTrackId), setIsSubmitting(!1));
 	}, handleSubmit = async () => {
-		if (!name.trim()) {
-			toast.push({
-				title: "Audio name required",
-				status: "error",
-				description: "Please enter an audio name for this caption track"
-			});
-			return;
-		}
-		if (!languageCode.trim()) {
-			toast.push({
-				title: "Language code required",
-				status: "error",
-				description: "Please enter a language code (e.g., en, es, fr)"
-			});
-			return;
-		}
-		setIsSubmitting(!0);
-		try {
-			if (!asset.assetId) throw Error("Asset ID is required");
-			let originalVttUrl = (() => {
-				if (isAutogenerated || !track.id) return "";
-				let playbackId = getPlaybackId(asset);
-				if (!playbackId) return "";
-				let url = `https://stream.mux.com/${playbackId}/text/${track.id}.vtt`;
-				if (getPlaybackPolicy(asset)?.policy === "signed") {
-					let token = generateJwt(client, playbackId, "v");
-					url += `?token=${token}`;
-				}
-				return url;
-			})(), urlChanged = selectedFile !== null || vttUrl.trim() && vttUrl.trim() !== originalVttUrl;
-			if (!urlChanged) {
-				toast.push({
-					title: "No changes",
-					status: "info",
-					description: "Please provide a new VTT file or URL using the \"Replace\" button or URL field to update the track."
-				}), setIsSubmitting(!1);
-				return;
-			}
-			if (urlChanged) {
-				if (!selectedFile && vttUrl.trim()) try {
-					new URL(vttUrl.trim());
-				} catch {
+		if (!name.trim()) toast.push({
+			title: "Audio name required",
+			status: "error",
+			description: "Please enter an audio name for this caption track"
+		});
+		else if (!languageCode.trim()) toast.push({
+			title: "Language code required",
+			status: "error",
+			description: "Please enter a language code (e.g., en, es, fr)"
+		});
+		else {
+			setIsSubmitting(!0);
+			try {
+				if (!asset.assetId) throw Error("Asset ID is required");
+				let originalVttUrl = (() => {
+					if (isAutogenerated || !track.id) return "";
+					let playbackId = getPlaybackId(asset);
+					if (!playbackId) return "";
+					let url = `https://stream.mux.com/${playbackId}/text/${track.id}.vtt`;
+					if (getPlaybackPolicy(asset)?.policy === "signed") {
+						let token = generateJwt(client, playbackId, "v");
+						url += `?token=${token}`;
+					}
+					return url;
+				})(), urlChanged = selectedFile !== null || vttUrl.trim() && vttUrl.trim() !== originalVttUrl;
+				if (!urlChanged) {
 					toast.push({
-						title: "Invalid URL",
-						status: "error",
-						description: "Please enter a valid URL (e.g., https://example.com/subtitles.vtt)"
+						title: "No changes",
+						status: "info",
+						description: "Please provide a new VTT file or URL using the \"Replace\" button or URL field to update the track."
 					}), setIsSubmitting(!1);
 					return;
 				}
-				if (!selectedFile && !vttUrl.trim()) {
-					toast.push({
-						title: "VTT file or URL required",
-						status: "error",
-						description: "Please select a VTT file or enter a VTT file URL"
-					}), setIsSubmitting(!1);
-					return;
+				if (urlChanged) {
+					if (!selectedFile && vttUrl.trim()) try {
+						new URL(vttUrl.trim());
+					} catch {
+						toast.push({
+							title: "Invalid URL",
+							status: "error",
+							description: "Please enter a valid URL (e.g., https://example.com/subtitles.vtt)"
+						}), setIsSubmitting(!1);
+						return;
+					}
+					if (!selectedFile && !vttUrl.trim()) {
+						toast.push({
+							title: "VTT file or URL required",
+							status: "error",
+							description: "Please select a VTT file or enter a VTT file URL"
+						}), setIsSubmitting(!1);
+						return;
+					}
+					await handleUpdateTrackWithNewUrl();
 				}
-				await handleUpdateTrackWithNewUrl();
+				onClose();
+			} catch (error) {
+				toast.push({
+					title: "Failed to update caption track",
+					status: "error",
+					description: error instanceof Error ? error.message : "Please try again"
+				});
+			} finally {
+				setIsSubmitting(!1);
 			}
-			onClose();
-		} catch (error) {
-			toast.push({
-				title: "Failed to update caption track",
-				status: "error",
-				description: error instanceof Error ? error.message : "Please try again"
-			});
-		} finally {
-			setIsSubmitting(!1);
 		}
 	};
 	return /* @__PURE__ */ jsx(Dialog, {
 		id: dialogId,
@@ -4313,9 +4287,9 @@
 						preload: "metadata",
 						crossOrigin: "anonymous",
 						metadata: {
 							player_name: "Sanity Admin Dashboard",
-							player_version: "5.1.1",
+							player_version: "5.1.2",
 							page_type: "Preview Player"
 						},
 						audio: isAudio,
 						_hlsConfig: hlsConfig,
@@ -4430,40 +4404,39 @@
 		content: t0,
 		children: t4
 	}), $[7] = t0, $[8] = t4, $[9] = t5) : t5 = $[9], t5;
 }
+const INITIAL_PREVIEW_STATE = {
+	snapshot: null,
+	isLoading: !0,
+	original: null
+};
 function PaneItemPreview(props) {
-	let $ = c(20), { icon, layout, presence, schemaType, value } = props, title = isRecord(value.title) && isValidElement(value.title) || isString(value.title) || isNumber(value.title) ? value.title : null, t0;
+	let $ = c(19), { icon, layout, presence, schemaType, value } = props, title = isRecord(value.title) && isValidElement(value.title) || isString(value.title) || isNumber(value.title) ? value.title : null, t0;
 	$[0] !== props.documentPreviewStore || $[1] !== schemaType || $[2] !== value._id ? (t0 = getPreviewStateObservable(props.documentPreviewStore, schemaType, value._id), $[0] = props.documentPreviewStore, $[1] = schemaType, $[2] = value._id, $[3] = t0) : t0 = $[3];
-	let observable = t0, t1;
-	$[4] === Symbol.for("react.memo_cache_sentinel") ? (t1 = {
-		isLoading: !0,
-		snapshot: null,
-		original: null
-	}, $[4] = t1) : t1 = $[4];
-	let { snapshot, original, isLoading } = useObservable(observable, t1), t2;
-	$[5] !== isLoading || $[6] !== original || $[7] !== presence || $[8] !== snapshot ? (t2 = isLoading ? null : /* @__PURE__ */ jsxs(Inline, {
+	let { snapshot, original, isLoading } = useObservable(t0, INITIAL_PREVIEW_STATE), t1;
+	$[4] !== isLoading || $[5] !== original || $[6] !== presence || $[7] !== snapshot ? (t1 = isLoading ? null : /* @__PURE__ */ jsxs(Inline, {
 		gap: 4,
 		children: [
 			presence && presence.length > 0 && /* @__PURE__ */ jsx(DocumentPreviewPresence, { presence }),
 			/* @__PURE__ */ jsx(PublishedStatus, { document: original }),
 			/* @__PURE__ */ jsx(DraftStatus, { document: snapshot })
 		]
-	}), $[5] = isLoading, $[6] = original, $[7] = presence, $[8] = snapshot, $[9] = t2) : t2 = $[9];
-	let status = t2, t3;
-	$[10] !== original || $[11] !== snapshot || $[12] !== title ? (t3 = getPreviewValueWithFallback({
+	}), $[4] = isLoading, $[5] = original, $[6] = presence, $[7] = snapshot, $[8] = t1) : t1 = $[8];
+	let status = t1, t2;
+	$[9] !== original || $[10] !== snapshot || $[11] !== title ? (t2 = getPreviewValueWithFallback({
 		snapshot,
 		original,
 		fallback: { title }
-	}), $[10] = original, $[11] = snapshot, $[12] = title, $[13] = t3) : t3 = $[13];
-	let t4 = t3, t5;
-	return $[14] !== icon || $[15] !== isLoading || $[16] !== layout || $[17] !== status || $[18] !== t4 ? (t5 = /* @__PURE__ */ jsx(SanityDefaultPreview, {
-		...t4,
+	}), $[9] = original, $[10] = snapshot, $[11] = title, $[12] = t2) : t2 = $[12];
+	let t3 = t2, t4;
+	return $[13] !== icon || $[14] !== isLoading || $[15] !== layout || $[16] !== status || $[17] !== t3 ? (t4 = /* @__PURE__ */ jsx(SanityDefaultPreview, {
+		...t3,
 		isPlaceholder: isLoading,
 		icon,
 		layout,
 		status
-	}), $[14] = icon, $[15] = isLoading, $[16] = layout, $[17] = status, $[18] = t4, $[19] = t5) : t5 = $[19], t5;
+	}), $[13] = icon, $[14] = isLoading, $[15] = layout, $[16] = status, $[17] = t3, $[18] = t4) : t4 = $[18], t4;
 }
 /**
 * Return `false` if we explicitly disable the icon.
 * Otherwise return the passed icon or the schema type icon as a backup.
@@ -4754,15 +4727,9 @@
 		state === "idle" && (setState("resyncing"), await resyncAsset(props.asset), setState("idle"));
 	}, $[9] = props.asset, $[10] = resyncAsset, $[11] = state, $[12] = t4) : t4 = $[12];
 	let handleResync = t4, t5;
 	$[13] !== modified || $[14] !== props || $[15] !== state ? (t5 = function handleClose() {
-		if (state === "idle") {
-			if (modified) {
-				setState("closing");
-				return;
-			}
-			props.closeDialog();
-		}
+		state === "idle" && (modified ? setState("closing") : props.closeDialog());
 	}, $[13] = modified, $[14] = props, $[15] = state, $[16] = t5) : t5 = $[16];
 	let handleClose = t5, t6;
 	$[17] !== props || $[18] !== state ? (t6 = function confirmClose(shouldClose) {
 		state === "closing" && (shouldClose && props.closeDialog(), setState("idle"));
@@ -6843,18 +6810,9 @@
 	});
 }
 function WatermarkControls({ watermark, onChange, onValidationChange, previewContainerRef, previewVideoRef }) {
 	let [urlInput, setUrlInput] = useState(watermark.imageUrl || ""), [urlError, setUrlError] = useState(null), [isValidating, setIsValidating] = useState(!1), [isValid, setIsValid] = useState(null), validationTimeoutRef = useRef(null), [mode, setMode] = useState(watermark.overlay_settings ? "manual" : "canvas"), isUpdatingRef = useRef(!1), isValidExtension = useCallback((extension) => extension.endsWith(".png") || extension.endsWith(".jpg") || extension.endsWith(".jpeg"), []), validateUrl = useCallback((url) => {
-		if (validationTimeoutRef.current && clearTimeout(validationTimeoutRef.current), !url) {
-			setUrlError(null), setIsValid(null), setIsValidating(!1), onValidationChange?.(null), isUpdatingRef.current = !0, onChange({
-				...watermark,
-				enabled: !1,
-				imageUrl: void 0,
-				overlay_settings: void 0
-			});
-			return;
-		}
-		setIsValidating(!0), setIsValid(null), setUrlError(null), validationTimeoutRef.current = setTimeout(() => {
+		validationTimeoutRef.current && clearTimeout(validationTimeoutRef.current), url ? (setIsValidating(!0), setIsValid(null), setUrlError(null), validationTimeoutRef.current = setTimeout(() => {
 			try {
 				let pathname = new URL(url).pathname.toLowerCase();
 				if (isValidExtension(pathname)) {
 					setIsValid(!0), setUrlError(null), onValidationChange?.(null);
@@ -6897,9 +6855,14 @@
 				});
 			} finally {
 				setIsValidating(!1);
 			}
-		}, 500);
+		}, 500)) : (setUrlError(null), setIsValid(null), setIsValidating(!1), onValidationChange?.(null), isUpdatingRef.current = !0, onChange({
+			...watermark,
+			enabled: !1,
+			imageUrl: void 0,
+			overlay_settings: void 0
+		}));
 	}, [
 		watermark,
 		onChange,
 		onValidationChange,
@@ -8749,36 +8712,28 @@
 	}, handlePaste = (event) => {
 		if (event.target.closest("#vtt-url")) return;
 		event.preventDefault(), event.stopPropagation();
 		let url = (event.clipboardData || window.clipboardData)?.getData("text")?.trim();
-		if (!isValidUrl(url)) {
-			toast.push({
-				status: "error",
-				title: "Invalid URL for Mux video input."
-			});
-			return;
-		}
-		dispatch({
+		isValidUrl(url) ? dispatch({
 			action: "stageUpload",
 			input: {
 				type: "url",
 				url
 			}
+		}) : toast.push({
+			status: "error",
+			title: "Invalid URL for Mux video input."
 		});
 	}, handleDrop = (event) => {
-		if (event.preventDefault(), event.stopPropagation(), dragState === "invalid") {
-			invalidFileToast(), setDragState(null);
-			return;
-		}
-		setDragState(null), extractDroppedFiles(event.nativeEvent.dataTransfer).then((files) => {
+		event.preventDefault(), event.stopPropagation(), dragState === "invalid" ? (invalidFileToast(), setDragState(null)) : (setDragState(null), extractDroppedFiles(event.nativeEvent.dataTransfer).then((files) => {
 			dispatch({
 				action: "stageUpload",
 				input: {
 					type: "file",
 					files
 				}
 			});
-		});
+		}));
 	}, handleDragOver = (event) => {
 		event.preventDefault(), event.stopPropagation();
 	}, handleDragEnter = (event) => {
 		event.stopPropagation(), dragEnteredEls.current.push(event.target);