Skip to content

Commit

Permalink
feat: add tracker component in Academy (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashucoder9 authored Jul 14, 2024
2 parents 7ee8136 + 5c7d6fe commit 3418701
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 33 deletions.
2 changes: 2 additions & 0 deletions app/course/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DocsPage, DocsBody } from 'fumadocs-ui/page';
import { notFound } from 'next/navigation';
import { utils, type Page } from '@/utils/source';
import { createMetadata } from '@/utils/metadata';
import IndexedDBComponent from '../../tracker'

interface Param {
slug: string[];
Expand Down Expand Up @@ -89,6 +90,7 @@ export default function Page({
{page.data.description}
</p>
<DocsBody>
<IndexedDBComponent/>
{page.data.index ? (
<Category page={page} />
) : (
Expand Down
33 changes: 0 additions & 33 deletions app/source.ts

This file was deleted.

74 changes: 74 additions & 0 deletions app/tracker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use client';
import { useEffect } from 'react';
import { usePathname } from 'next/navigation';

const IndexedDBComponent: React.FC = () => {
const currentPath = usePathname();

useEffect(() => {
const request = indexedDB.open("PathDatabase", 1);

request.onerror = function (event: any) {
console.error("Database error:", event.target.errorCode);
};

request.onupgradeneeded = function (event: any) {
const db = event.target.result as IDBDatabase;
const objectStore = db.createObjectStore("paths", { keyPath: "id", autoIncrement: true });
objectStore.createIndex("path", "path", { unique: false });
};

request.onsuccess = function (event: any) {
const db = event.target.result as IDBDatabase;
const transaction = db.transaction(["paths"], "readwrite");
const objectStore = transaction.objectStore("paths");
const index = objectStore.index("path");

const getRequest = index.get(currentPath);

getRequest.onsuccess = function (event: any) {
if (event.target.result) {
console.log("Path already exists in Academy DB:", currentPath);
} else {
const addRequest = objectStore.add({ path: currentPath });

addRequest.onsuccess = function () {
console.log("Path has been added to Academy DB:", currentPath);
};

addRequest.onerror = function (event: any) {
console.error("Error adding path:", event.target.errorCode);
};
}
};

getRequest.onerror = function (event: any) {
console.error("Error checking path:", event.target.errorCode);
};

const getAllRequest = objectStore.getAll();

getAllRequest.onsuccess = function (event: any) {
const paths = event.target.result as { path: string }[];
paths.forEach(item => {
const sidebarItem = document.querySelector(`a[href="${item.path}"]`);
if (sidebarItem) {
const iconContainer = sidebarItem.querySelector('div');
if (iconContainer) {
iconContainer.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="green" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check"><path d="M20 6 9 17l-5-5"></path></svg>';
}
}
});
};

getAllRequest.onerror = function (event: any) {
console.error("Error retrieving all paths:", event.target.errorCode);
};
};
}, [currentPath]);

return null;
};

export default IndexedDBComponent;

0 comments on commit 3418701

Please sign in to comment.