@localazy/strapi-plugin
Version:
The official Strapi Plugin by Localazy.
84 lines (83 loc) • 3.04 kB
JavaScript
import { jsxs, jsx } from "react/jsx-runtime";
import { useState, useEffect } from "react";
import { useLocation } from "react-router-dom";
import { T as Typography, a as Toggle } from "./index-DOCSzcPH.mjs";
import EntryExclusionService from "./entry-exclusion-service-BlyFqyBY.mjs";
import "./i18n-RoETLXdU.mjs";
import { u as useTranslation } from "./useTranslation-CZQ4lU1m.mjs";
const LocalazyPanel = () => {
const { t } = useTranslation();
const location = useLocation();
const [isExcluded, setIsExcluded] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [contentType, setContentType] = useState(null);
const [documentId, setDocumentId] = useState(null);
useEffect(() => {
const extractEntryInfo = () => {
const pathParts = location.pathname.split("/");
const contentManagerIndex = pathParts.indexOf("content-manager");
if (contentManagerIndex !== -1 && pathParts[contentManagerIndex + 1] === "collection-types") {
const extractedContentType = pathParts[contentManagerIndex + 2];
const extractedDocumentId = pathParts[contentManagerIndex + 3];
if (extractedContentType && extractedDocumentId) {
setContentType(extractedContentType);
setDocumentId(extractedDocumentId);
}
}
};
extractEntryInfo();
}, [location.pathname]);
useEffect(() => {
const loadExclusionState = async () => {
if (!contentType || !documentId) {
setIsLoading(false);
return;
}
try {
setIsLoading(true);
const exclusionState = await EntryExclusionService.getEntryExclusion(contentType, documentId);
setIsExcluded(exclusionState);
} catch (error) {
console.error("Failed to load entry exclusion state:", error);
setIsExcluded(false);
} finally {
setIsLoading(false);
}
};
loadExclusionState();
}, [contentType, documentId]);
const handleToggleChange = async (event) => {
const { checked } = event.target;
const value = checked;
if (!contentType || !documentId) {
console.warn("Cannot save exclusion state: missing content type or document ID");
return;
}
try {
await EntryExclusionService.setEntryExclusion(contentType, documentId, value);
setIsExcluded(value);
} catch (error) {
console.error("Failed to save entry exclusion state:", error);
setIsExcluded(!value);
}
};
return {
title: "Localazy",
content: /* @__PURE__ */ jsxs("div", { children: [
/* @__PURE__ */ jsx("div", { style: { marginBottom: "8px" }, children: /* @__PURE__ */ jsx(Typography, { children: t("plugin_settings.exclude_from_translation") }) }),
/* @__PURE__ */ jsx(
Toggle,
{
checked: isExcluded,
onLabel: "True",
offLabel: "False",
disabled: isLoading || !contentType || !documentId,
onChange: handleToggleChange
}
)
] })
};
};
export {
LocalazyPanel as default
};