/* eslint-disable import/no-extraneous-dependencies */ import React, { useState, useEffect } from 'react'; import { Form, Button, Container, Accordion, Image, } from 'react-bootstrap'; import { TbInfoHexagon, TbHelp } from 'react-icons/tb'; import OutputLog from '../components/OutputLog'; import Info from '../components/Help/Info'; import HeaderImg from '../img/ctm.png'; import ModrinthProjId from '../components/Help/ModrinthProjId'; import { CreateModrinthUri, ModrinthOauthCallbackHandler } from '../shared'; export default () => { const [dark, setDark] = useState(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches); const [modals, setModals] = useState({ info: false, mpid: false }); const [jobId, setJobId] = useState(window.localStorage.getItem('last-jobid')); const [formData, setFormData] = useState({ curseforgeSlug: '', modrinthId: '', }); /* Begins the Modrinth OAuth flow, creating a URL with a callback that has the data we need in the query params and will return back with the client's oauth code that will be needed back on the backend */ const startOauthFlow = (evt) => { evt && evt.preventDefault(); const uri = CreateModrinthUri(formData.curseforgeSlug, formData.modrinthId); window.location.href = uri; }; useEffect(() => { // Handle the Modrinth OAuth callback const internalCreateJob = async (formDataIn) => { if (!formDataIn.curseforgeSlug.length) return; if (!formDataIn.modrinthId.length) return; if (!formDataIn.code.length) return; const body = { curseforgeSlug: formDataIn.curseforgeSlug, modrinthId: formDataIn.modrinthId, clientOauthCode: formDataIn.code, }; const resp = await fetch('/api/v1/jobs', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); if (resp && resp.ok) { const data = await resp.text(); setJobId(data); window.localStorage.setItem('last-jobid', data); } }; const newFormData = ModrinthOauthCallbackHandler(); if (!newFormData) return () => { }; internalCreateJob(newFormData); // Push state w/o URLQuery to avoid infinite reload of the page if (window.history.pushState) { const url = new URL(window.location.href); url.search = ''; window.history.pushState({ path: url.toString() }, '', url.toString()); } return () => { }; }, [ModrinthOauthCallbackHandler]); useEffect(() => { // DARK MODE STUFF HERE // Set initial state before even bothering with the update state window.document.scrollingElement.setAttribute('data-bs-theme', dark ? 'dark' : 'light'); const pref = window.matchMedia('(prefers-color-scheme: dark)'); const onChange = (query) => { window.document.scrollingElement.setAttribute( 'data-bs-theme', query.matches ? 'dark' : 'light', ); setDark(query.matches); }; pref.addEventListener('change', onChange); return () => pref.removeEventListener('change', onChange); }, [dark, setDark]); return (
setModals({ ...modals, info: false })} /> setModals({ ...modals, mpid: false })} />

C urse {' '} T o {' '} M odrinth {' '} M od {' '} M igrator

CurseForge Project Slug setFormData({ ...formData, curseforgeSlug: x.target.value, })} /> Modrinth Project ID {' '} setFormData({ ...formData, modrinthId: x.target.value, })} />
{jobId && (
Status & Output Log for Job {' '} {`#${jobId}`}
{jobId && ()}
)}
); };