156 lines
5.8 KiB
React
Raw Normal View History

2025-07-24 23:04:05 +07:00
import { useEffect, useState } from "react";
import { IoSaveOutline } from "react-icons/io5";
2025-06-01 12:40:24 -04:00
import { useNavigate } from "react-router-dom";
2025-07-24 23:04:05 +07:00
import { toast } from "sonner";
2025-06-01 12:40:24 -04:00
2025-07-24 23:04:05 +07:00
const Settings = ({ onConfigSaved }) => {
const [instanceUrl, setInstanceUrl] = useState("");
const [storedUrl, setStoredUrl] = useState(null);
const [loading, setLoading] = useState(false);
2025-06-01 12:40:24 -04:00
const navigate = useNavigate();
2025-06-01 12:40:24 -04:00
useEffect(() => {
chrome.storage.local.get("cdrm_instance", (result) => {
if (chrome.runtime.lastError) {
2025-07-24 23:04:05 +07:00
toast.error("Error fetching CDRM instance:", chrome.runtime.lastError);
console.error("Error fetching CDRM instance:", chrome.runtime.lastError);
} else if (result.cdrm_instance) {
setStoredUrl(result.cdrm_instance);
}
});
}, []);
2025-06-01 12:40:24 -04:00
const handleSave = async () => {
const trimmedUrl = instanceUrl.trim().replace(/\/+$/, "");
if (!trimmedUrl) {
2025-07-24 23:04:05 +07:00
toast.error("Please enter a valid URL.");
return;
}
2025-06-01 12:40:24 -04:00
const endpoint = trimmedUrl + "/api/extension";
setLoading(true);
2025-06-01 12:40:24 -04:00
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
2025-06-01 12:40:24 -04:00
const data = await response.json();
2025-06-01 12:40:24 -04:00
if (data.status === true) {
2025-07-24 23:04:05 +07:00
toast.success("Successfully connected to a CDRM instance");
2025-06-01 12:40:24 -04:00
const widevineRes = await fetch(`${trimmedUrl}/remotecdm/widevine/deviceinfo`);
2025-07-24 23:04:05 +07:00
if (!widevineRes.ok) {
toast.error(
`Failed to fetch Widevine device info. Reason: ${widevineRes.statusText}`
);
return;
}
const widevineData = await widevineRes.json();
2025-06-01 12:40:24 -04:00
const playreadyRes = await fetch(`${trimmedUrl}/remotecdm/playready/deviceinfo`);
2025-07-24 23:04:05 +07:00
if (!playreadyRes.ok) {
toast.error(
`Failed to fetch PlayReady device info. Reason: ${playreadyRes.statusText}`
);
return;
}
const playreadyData = await playreadyRes.json();
2025-06-01 12:40:24 -04:00
chrome.storage.local.set(
{
valid_config: true,
cdrm_instance: trimmedUrl,
widevine_device: {
device_type: widevineData.device_type,
system_id: widevineData.system_id,
security_level: widevineData.security_level,
secret: widevineData.secret,
device_name: widevineData.device_name,
host: trimmedUrl,
},
playready_device: {
security_level: playreadyData.security_level,
secret: playreadyData.secret,
device_name: playreadyData.device_name,
host: trimmedUrl,
},
},
() => {
if (chrome.runtime.lastError) {
console.error(
"Error saving to chrome.storage:",
chrome.runtime.lastError
);
2025-07-24 23:04:05 +07:00
toast.error(
`Error saving configuration. Reason: ${chrome.runtime.lastError}`
);
} else {
2025-07-24 23:04:05 +07:00
console.log("Configuration saved");
setStoredUrl(trimmedUrl);
setInstanceUrl("");
if (onConfigSaved) onConfigSaved();
navigate("/results"); // Automatically redirect after success
}
}
);
2025-06-01 12:40:24 -04:00
} else {
2025-07-24 23:04:05 +07:00
toast.error("Invalid response from endpoint.");
2025-06-01 12:40:24 -04:00
}
} catch (err) {
console.error("Connection error:", err);
2025-07-24 23:04:05 +07:00
toast.error(
`Invalid endpoint or device info could not be retrieved. Reason: ${err.message}`
);
} finally {
setLoading(false);
2025-06-01 12:40:24 -04:00
}
};
return (
2025-07-24 23:04:05 +07:00
<div className="flex h-full w-full flex-col gap-4">
{storedUrl && (
2025-07-24 23:04:05 +07:00
<p className="mb-2 text-base">
Current instance: <span className="font-mono font-semibold">{storedUrl}</span>
</p>
)}
2025-07-24 23:04:05 +07:00
<fieldset className="fieldset">
<legend className="fieldset-legend text-base">New instance URL</legend>
<input
type="text"
value={instanceUrl}
onChange={(e) => setInstanceUrl(e.target.value)}
placeholder="https://cdrm-project.com/, http://127.0.0.1:5000/"
className="input w-full font-mono"
/>
</fieldset>
<button
2025-07-24 23:04:05 +07:00
type="button"
onClick={handleSave}
disabled={loading}
2025-07-24 23:04:05 +07:00
className="btn btn-primary btn-block"
>
2025-07-24 23:04:05 +07:00
{loading ? (
<>
<span className="loading loading-spinner loading-sm"></span> Connecting...
</>
) : (
<>
<IoSaveOutline className="h-5 w-5" />
Save settings
</>
)}
</button>
</div>
);
2025-07-24 23:04:05 +07:00
};
2025-06-01 12:40:24 -04:00
export default Settings;