Refactor to have IndexedDB use id instead of name

This commit is contained in:
Jared Schoeny
2025-10-22 23:11:58 -10:00
parent 6d8c3bdbf4
commit 5c1b380203
8 changed files with 133 additions and 129 deletions

View File

@@ -11,7 +11,7 @@ interface HackActionsProps {
title: string;
version: string;
author: string;
baseRom: string;
baseRomId: string;
platform?: "GBA" | "GBC" | "GB" | "NDS";
patchUrl: string;
}
@@ -20,7 +20,7 @@ const HackActions: React.FC<HackActionsProps> = ({
title,
version,
author,
baseRom,
baseRomId,
platform,
patchUrl,
}) => {
@@ -30,10 +30,10 @@ const HackActions: React.FC<HackActionsProps> = ({
const [error, setError] = React.useState<string | null>(null);
React.useEffect(() => {
if (isLinked(baseRom) && (hasPermission(baseRom) || hasCached(baseRom))) {
if (isLinked(baseRomId) && (hasPermission(baseRomId) || hasCached(baseRomId))) {
setStatus("ready");
}
}, [baseRom, isLinked, hasPermission, hasCached]);
}, [baseRomId, isLinked, hasPermission, hasCached]);
function onSelectFile(e: React.ChangeEvent<HTMLInputElement>) {
const f = e.target.files?.[0] ?? null;
@@ -47,12 +47,12 @@ const HackActions: React.FC<HackActionsProps> = ({
setError(null);
let baseFile = file;
if (!baseFile) {
if (!isLinked(baseRom) && !hasCached(baseRom)) return;
if (!hasCached(baseRom)) {
const perm = await ensurePermission(baseRom, true);
if (!isLinked(baseRomId) && !hasCached(baseRomId)) return;
if (!hasCached(baseRomId)) {
const perm = await ensurePermission(baseRomId, true);
if (perm !== "granted") return;
}
const linkedFile = await getFileBlob(baseRom);
const linkedFile = await getFileBlob(baseRomId);
if (!linkedFile) return;
baseFile = linkedFile;
}
@@ -100,9 +100,9 @@ const HackActions: React.FC<HackActionsProps> = ({
author={author}
onPatch={onPatch}
status={status}
isLinked={isLinked(baseRom)}
ready={hasPermission(baseRom) || hasCached(baseRom)}
onClickLink={() => (isLinked(baseRom) ? ensurePermission(baseRom, true) : linkRom(baseRom))}
isLinked={isLinked(baseRomId)}
ready={hasPermission(baseRomId) || hasCached(baseRomId)}
onClickLink={() => (isLinked(baseRomId) ? ensurePermission(baseRomId, true) : linkRom(baseRomId))}
supported={supported}
onUploadChange={onSelectFile}
/>

View File

@@ -39,9 +39,9 @@ export default function HackPatchForm(props: HackPatchFormProps) {
const baseRomName = baseRomEntry?.name;
const { isLinked, hasPermission, hasCached, importUploadedBlob, ensurePermission, getFileBlob, supported } = useBaseRoms();
const baseRomReady = !!baseRomName && (hasPermission(baseRomName) || hasCached(baseRomName));
const baseRomNeedsPermission = !!baseRomName && isLinked(baseRomName) && !baseRomReady;
const baseRomMissing = !!baseRomName && !isLinked(baseRomName) && !hasCached(baseRomName);
const baseRomReady = !!baseRomId && (hasPermission(baseRomId) || hasCached(baseRomId));
const baseRomNeedsPermission = !!baseRomId && isLinked(baseRomId) && !baseRomReady;
const baseRomMissing = !!baseRomId && !isLinked(baseRomId) && !hasCached(baseRomId);
const isVersionTaken = version.trim() && existingVersions.includes(version.trim());
const canSubmit = React.useMemo(() => {
@@ -78,8 +78,8 @@ export default function HackPatchForm(props: HackPatchFormProps) {
}, [patchMode]);
async function onGrantPermission() {
if (!baseRomName) return;
await ensurePermission(baseRomName, true);
if (!baseRomId) return;
await ensurePermission(baseRomId, true);
}
async function onUploadBaseRom(e: React.ChangeEvent<HTMLInputElement>) {
@@ -92,7 +92,7 @@ export default function HackPatchForm(props: HackPatchFormProps) {
setGenError("That ROM doesn't match any supported base ROM.");
return;
}
if (matched !== baseRomName) {
if (matched !== baseRomId) {
setGenError(`This ROM matches "${matched}", but this hack requires "${baseRomName}".`);
return;
}
@@ -106,11 +106,11 @@ export default function HackPatchForm(props: HackPatchFormProps) {
setGenStatus("generating");
setGenError("");
const mod = e.target.files?.[0] || null;
if (!mod || !baseRomName) {
if (!mod || !baseRomId) {
setGenStatus("idle");
return;
}
let baseFile = await getFileBlob(baseRomName);
let baseFile = await getFileBlob(baseRomId);
if (!baseFile) {
setGenStatus("idle");
setGenError("Base ROM not available.");

View File

@@ -97,9 +97,9 @@ export default function HackSubmitForm({ dummy = false }: HackSubmitFormProps) {
const baseRomPlatform = baseRomEntry?.platform;
const { isLinked, hasPermission, hasCached, importUploadedBlob, ensurePermission, getFileBlob, supported } = useBaseRoms();
const baseRomReady = baseRomName && (hasPermission(baseRomName) || hasCached(baseRomName));
const baseRomNeedsPermission = baseRomName && isLinked(baseRomName) && !baseRomReady;
const baseRomMissing = baseRomName && !isLinked(baseRomName) && !hasCached(baseRomName);
const baseRomReady = baseRom && (hasPermission(baseRom) || hasCached(baseRom));
const baseRomNeedsPermission = baseRom && isLinked(baseRom) && !baseRomReady;
const baseRomMissing = baseRom && !isLinked(baseRom) && !hasCached(baseRom);
const coverPreviews = React.useMemo(() => {
return newCoverFiles.map((f) => URL.createObjectURL(f));
@@ -251,8 +251,8 @@ export default function HackSubmitForm({ dummy = false }: HackSubmitFormProps) {
};
async function onGrantPermission() {
if (!baseRomName) return;
await ensurePermission(baseRomName, true);
if (!baseRom) return;
await ensurePermission(baseRom, true);
}
async function onUploadBaseRom(e: React.ChangeEvent<HTMLInputElement>) {
@@ -260,13 +260,14 @@ export default function HackSubmitForm({ dummy = false }: HackSubmitFormProps) {
setGenError("");
const f = e.target.files?.[0];
if (!f) return;
const matchedName = await importUploadedBlob(f);
if (!matchedName) {
const matchedId = await importUploadedBlob(f);
if (!matchedId) {
setGenError("That ROM doesn't match any supported base ROM.");
return;
}
if (matchedName !== baseRomName) {
setGenError(`This ROM matches "${matchedName}", but the form requires "${baseRomName}".`);
if (matchedId !== baseRom) {
const matchedName = baseRoms.find(r => r.id === matchedId)?.name;
setGenError(`This ROM matches "${matchedName ?? matchedId}", but the form requires "${baseRomName}".`);
return;
}
} catch {
@@ -279,11 +280,11 @@ export default function HackSubmitForm({ dummy = false }: HackSubmitFormProps) {
setGenStatus("generating");
setGenError("");
const mod = e.target.files?.[0] || null;
if (!mod || !baseRomName) {
if (!mod || !baseRom) {
setGenStatus("idle");
return;
}
let baseFile = await getFileBlob(baseRomName);
let baseFile = await getFileBlob(baseRom);
if (!baseFile) {
setGenStatus("idle");
setGenError("Base ROM not available.");