feat: enhance ConceptExplainer to handle content extraction and display; add normalization for PocketBase records
All checks were successful
On Push to Main / test (push) Successful in 39s
On Push to Main / publish (push) Successful in 1m8s
On Push to Main / deploy-dev (push) Successful in 1m34s

This commit is contained in:
RaymondVerhoef
2026-05-27 21:18:17 +02:00
parent ce276f0296
commit ad5be01b70
2 changed files with 57 additions and 8 deletions

View File

@@ -94,7 +94,8 @@ export async function getOrGenerateMicroLearning(topicId, type) {
});
console.log(`[MicroLearning] Stored: ${record.id}`);
return record;
// Normalise in case PB returns the JSON field as a string on this PB version.
return normalizeRecord(record);
}
/**
@@ -154,12 +155,37 @@ export async function deleteAllForTopic(topicId) {
// ── Internal helpers ──────────────────────────────────────────────────────────
/**
* Ensure the PocketBase record's `content` field is a parsed JS object.
*
* Older PocketBase versions (or misconfigured instances) may return a JSON
* field as a raw string instead of a parsed object. The PB JS SDK normally
* handles this, but we have seen production deployments where `r.content` is
* the serialised JSON string — which causes `content.sections?.map` to throw
* "is not a function" because strings don't have a `.map` method.
*
* This guard is defensive and idempotent: if `content` is already a plain
* object it is returned as-is; only strings are re-parsed.
*/
function normalizeRecord(record) {
if (!record) return record;
if (typeof record.content === 'string') {
try {
record.content = JSON.parse(record.content);
} catch {
// Malformed JSON — replace with an empty object so the UI can handle it.
record.content = {};
}
}
return record;
}
async function findExisting(topicId, type) {
try {
const records = await pb.collection('micro_learnings').getFullList({
filter: `topic_id = "${topicId}" && type = "${type}" && status = "published"`,
});
return records.length > 0 ? records[0] : null;
return records.length > 0 ? normalizeRecord(records[0]) : null;
} catch {
return null;
}