2 Commits

Author SHA1 Message Date
48973560d1 pull latest changes from git every 10 mins
All checks were successful
Deploy & Publish Image / deploy (ubuntu-latest, 2.44.0) (push) Successful in 3m23s
Took 19 minutes
2024-10-13 14:20:20 -04:00
d022209305 dont assume the slug for the first page is "intro"
Took 22 minutes
2024-10-13 14:01:20 -04:00
4 changed files with 42 additions and 5 deletions

View File

@ -34,7 +34,7 @@ const DocsPage = async ({
const decodedSlug: string = decodeURIComponent(slug || ""); const decodedSlug: string = decodeURIComponent(slug || "");
const page: DocsContentMetadata | undefined = pages.find( const page: DocsContentMetadata | undefined = pages.find(
(metadata: DocsContentMetadata): boolean => (metadata: DocsContentMetadata): boolean =>
metadata.slug === (decodedSlug || "intro") metadata.slug === (decodedSlug || pages[0].slug)
); );
if (!page) { if (!page) {
notFound(); notFound();

View File

@ -17,7 +17,8 @@ const DocsFooter = ({
const current: number = pages.findIndex( const current: number = pages.findIndex(
(page: DocsContentMetadata) => (page: DocsContentMetadata) =>
(path === "/" && page.slug === "intro") || path === `/${page.slug}` (path === "/" && page.slug === pages[0].slug) ||
path === `/${page.slug}`
); );
const previous: DocsContentMetadata | undefined = const previous: DocsContentMetadata | undefined =
current > 0 ? pages[current - 1] : undefined; current > 0 ? pages[current - 1] : undefined;

View File

@ -22,7 +22,7 @@ const SidebarLinks = ({
return ( return (
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
{Object.values(tree).map((node: TreeNode) => ( {Object.values(tree).map((node: TreeNode) => (
<CategoryItem key={node.slug} node={node} /> <CategoryItem key={node.slug} pages={pages} node={node} />
))} ))}
</div> </div>
); );
@ -36,17 +36,20 @@ type TreeNode = {
}; };
const CategoryItem = ({ const CategoryItem = ({
pages,
node, node,
depth = 0, depth = 0,
isLast = true, isLast = true,
}: { }: {
pages: DocsContentMetadata[];
node: TreeNode; node: TreeNode;
depth?: number; depth?: number;
isLast?: boolean; isLast?: boolean;
}) => { }) => {
const path = decodeURIComponent(usePathname()); const path = decodeURIComponent(usePathname());
const active = const active =
(path === "/" && node.slug === "intro") || path === `/${node.slug}`; (path === "/" && node.slug === pages[0].slug) ||
path === `/${node.slug}`;
const [isOpen, setIsOpen] = useState(true); const [isOpen, setIsOpen] = useState(true);
const hasChildren = Object.keys(node.children).length > 0; const hasChildren = Object.keys(node.children).length > 0;
@ -121,6 +124,7 @@ const CategoryItem = ({
(child, index, array) => ( (child, index, array) => (
<CategoryItem <CategoryItem
key={child.slug} key={child.slug}
pages={pages}
node={child} node={child}
depth={depth + 1} depth={depth + 1}
isLast={index === array.length - 1} isLast={index === array.length - 1}

View File

@ -24,6 +24,13 @@ const DOCS_DIR: string = isGitUrl(config.contentSource)
? config.contentSource ? config.contentSource
: path.join(config.contentSource.replace("{process}", process.cwd())); : path.join(config.contentSource.replace("{process}", process.cwd()));
const LAST_UPDATE_FILE = path.join(
os.tmpdir(),
"docs_cache",
"last_update.json"
);
const UPDATE_INTERVAL_MS: number = 10 * 60 * 1000; // 10 minutes in milliseconds
/** /**
* Clone the Git repository if DOCS_DIR is a URL, else use the local directory. * Clone the Git repository if DOCS_DIR is a URL, else use the local directory.
* If it's a Git URL, clone it to a cache directory and reuse it. * If it's a Git URL, clone it to a cache directory and reuse it.
@ -35,15 +42,40 @@ const getDocsDirectory = async (): Promise<string> => {
// Pull the latest changes from the repo if we don't have it // Pull the latest changes from the repo if we don't have it
if (!fs.existsSync(cacheDir) || fs.readdirSync(cacheDir).length < 1) { if (!fs.existsSync(cacheDir) || fs.readdirSync(cacheDir).length < 1) {
console.log("Fetching initial docs from Git...");
try { try {
await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 }); await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 });
} catch (error) {} } catch (error) {
// Simply ignore this error. When cloning the repo for
// the first time, it'll sometimes error saying the dir
// is already created.
}
} else if (shouldUpdateRepo()) {
// Pull the latest changes from Git
console.log("Updating docs content from Git...");
await simpleGit().pull(cacheDir);
fs.writeFileSync(
LAST_UPDATE_FILE,
JSON.stringify({ lastUpdate: Date.now() }),
"utf-8"
);
} }
return cacheDir; return cacheDir;
} }
return DOCS_DIR; return DOCS_DIR;
}; };
const shouldUpdateRepo = (): boolean => {
if (!fs.existsSync(LAST_UPDATE_FILE)) {
return true;
}
return (
Date.now() -
JSON.parse(fs.readFileSync(LAST_UPDATE_FILE, "utf-8")).lastUpdate >
UPDATE_INTERVAL_MS
);
};
/** /**
* Get the content to display in the docs. * Get the content to display in the docs.
*/ */