Skip to content

Commit

Permalink
feat: swap to usePageContent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Ellison committed Jul 23, 2023
1 parent ba94a3a commit 29fff80
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 121 deletions.
4 changes: 2 additions & 2 deletions components/content/ContentPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function SolutionsMenu({ solutions, open, top, drawerWidth }) {

function LeftMenu({ menu, open, top, drawerWidth }) {

console.log('LeftMenu:menu: ', menu)
// console.log('LeftMenu:menu: ', menu)

return (
<NavigationDrawer
Expand Down Expand Up @@ -360,7 +360,7 @@ const L2Menu = ({ menu }) => {


function BasicLeftMenu({ menu, open, top, drawerWidth }) {
console.log('BasicLeftMenu: ', menu)
// console.log('BasicLeftMenu: ', menu)

return (
<NavigationDrawer
Expand Down
5 changes: 3 additions & 2 deletions constants/mdxProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ function MdxImage({ props, baseContext }) {

if (baseContext.file) {
let dir = path.dirname(baseContext.file);
src = dir + '/' + src;

if (!dir.startsWith('.')) { src = dir + '/' + src}; // ignore base paths
}

src = '/api/content/github/' + baseContext.owner + '/' + baseContext.repo + '?path=' + src + '&branch=' + baseContext.branch;
console.log('src : ', src)
console.debug('mdxProvider:MdxImage:src: ', src)

} else {
src = '/image-not-found.png';
Expand Down
22 changes: 12 additions & 10 deletions lib/content/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ export async function githubExternal(frontmatter, context) {
let branch = null;
let path = null;


console.log('githubExternal:frontmatter: ', frontmatter)
console.log('githubExternal:context: ', context)


// console.log('githubExternal:frontmatter: ', frontmatter)
try {
if (frontmatter.external) {
owner = frontmatter.external.owner
Expand All @@ -23,13 +22,17 @@ export async function githubExternal(frontmatter, context) {
branch = frontmatter.external_branch || 'main'
}


// let directory = path?.includes("/") ? path.split("/")[0] : '';

// set the new context
context = {
const newContext = {
source: context.source, // The source of the content
repo: repo, // The name of the repo
owner: owner, // The owner of the repo
branch: branch ,
path: path, // the base path for the content
path: '', // the base path for the content
file: path,
reference: context.reference, // how the collection is referred to in frontmatter links
collections: context.collections
}
Expand All @@ -45,16 +48,15 @@ export async function githubExternal(frontmatter, context) {
if (response.ok) {

const data = await response.text();
return { file: path, content: data, context: context};
return { newContent: data, newContext: newContext};
} else {
throw new Error("githubExternal:Error fetching file");
return null
console.error("githubExternal:Error fetching file:");
return { newContent: null, newContext: null};
}
} catch (error) {
console.log("githubExternal:Error fetching file:", error);

console.error("githubExternal:Error fetching file:", error);
return null
return { newContent: null, newContext: null};
}

};
28 changes: 25 additions & 3 deletions lib/content/menuContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ export async function getContent(siteConfig) {
const branchSha = await getBranchSha(siteConfig.owner, siteConfig.repo, siteConfig.branch)
const cacheKey = `files:${siteConfig.path}:${branchSha}`;
// Check if the content is in the cache
const cachedContent = JSON.parse(await cacheRead(cacheKey));
let cachedContent;
try {
cachedContent = JSON.parse(await cacheRead(cacheKey));
} catch (error) {
// Handle the error when JSON parsing fails (invalid data).
console.error('Error parsing cached content:', error);
cachedContent = null; // Or use a default value if required.
}
if (cachedContent) {
console.info('[GitHub][Cache][HIT]:', cacheKey)
// If the content was found in the cache, return it
Expand Down Expand Up @@ -40,7 +47,11 @@ export async function getContent(siteConfig) {
});
});
const content = await Promise.all(contentPromises);
await cacheWrite(cacheKey, JSON.stringify(content), 60 * 60 * 24); // cache for 24 hours
try {
await cacheWrite(cacheKey, JSON.stringify(content), 60 * 60 * 24); // cache for 24 hours
} catch (error) {

}
return content
}

Expand Down Expand Up @@ -83,7 +94,11 @@ async function getSolutions(siteConfig) {
});
});
const content = await Promise.all(solutionsContentPromises);
try {
await cacheWrite(cacheKey, JSON.stringify(content), 60 * 60 * 24); // cache for 24 hours
} catch (error) {

}
return content
}

Expand Down Expand Up @@ -339,7 +354,14 @@ export async function getMenuStructure(siteConfig, collection) {

const cacheKey = `menus:${collection.path}:${branchSha}`;
// Check if the content is in the cache
const cachedContent = JSON.parse(await cacheRead(cacheKey));
let cachedContent;
try {
cachedContent = JSON.parse(await cacheRead(cacheKey));
} catch (error) {
// Handle the error when JSON parsing fails (invalid data).
// console.error('Error parsing cached content:', error);
cachedContent = null; // Or use a default value if required.
}
if (cachedContent) {
console.info('[GitHub][Cache][HIT]:', cacheKey)
// If the content was found in the cache, return it
Expand Down
29 changes: 21 additions & 8 deletions lib/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createGitHubInstance(config = getGitHubConfiguration()) {
appId: config.appId,
privateKey: config.privateKey,
installationId: config.installationId,
},
},
});
return octokit
} catch (e) {
Expand All @@ -52,20 +52,26 @@ export async function getBranchSha(owner, repo, branch) {
// If the content was found in the cache, return it
return cachedContent;
} else {
console.info('[Github][Cache][MISS]:',cacheKey )
console.info('[Github][getBranchSha][Cache][MISS]:', cacheKey)
}
const branchSha = await gitHubInstance.rest.repos.getBranch({
owner,
repo,
branch,
});
// Store the content in the cache before returning it
await cacheWrite(cacheKey, branchSha.data.commit.sha, 600);

try {
// Store the content in the cache before returning it
await cacheWrite(cacheKey, branchSha.data.commit.sha, 600);
} catch (error) {
console.error(`[GitHub][getBranchSha] Error writing cache: ${error}`);

}
return branchSha.data.commit.sha;
} catch (error) {
console.error(`[GitHub][getBranchSha] Error getting sha: ${error}`);
// throw new Error(`[GitHub][getBranchSha] Could not get sha for branch`);

}

}
Expand All @@ -74,7 +80,7 @@ export async function getBranchSha(owner, repo, branch) {

// Function to get a file content
export async function getFileContent(owner, repo, branch, path) {

const branchSha = await getBranchSha(owner, repo, branch,)

// Generate a unique cache key for this file
Expand All @@ -87,7 +93,7 @@ export async function getFileContent(owner, repo, branch, path) {
// If the content was found in the cache, return it
return cachedContent;
} else {
console.info('[Github][Cache][MISS]:',cacheKey )
console.info('[Github][Cache][MISS]:', cacheKey)
}

if (!gitHubInstance) {
Expand All @@ -110,8 +116,13 @@ export async function getFileContent(owner, repo, branch, path) {
// For text files, assume UTF-8 encoding
content = Buffer.from(response.data.content, 'utf-8');
}
try {
// Store the content in the cache before returning it
await cacheWrite(cacheKey, content, 60*60*24); // cache for 24 hours
await cacheWrite(cacheKey, content, 60 * 60 * 24); // cache for 24 hours
} catch (error) {
console.error(`[GitHub][getBranchSha] Error writing cache: ${error}`);

}
return content;

} catch (error) {
Expand All @@ -128,6 +139,8 @@ export async function getAllFiles(owner, repo, branch, path, recursive = true, f
gitHubInstance = await createGitHubInstance();
}
const branchSha = await getBranchSha(owner, repo, branch,);

console.log('getAllFiles:branchSha: ', branchSha)
const files = await getAllFilesRecursive(gitHubInstance, owner, repo, branchSha, path, recursive, filter);
return files;

Expand Down
Loading

0 comments on commit 29fff80

Please sign in to comment.