feat: enhance ConceptExplainer to handle content extraction and display; add normalization for PocketBase records
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user