Skip to content

Commit

Permalink
fix: active links are persistent and does not get reset on reload (#58)
Browse files Browse the repository at this point in the history
* Sidebar active file highlight issue fixed on reload
  • Loading branch information
sboy99 committed Jan 29, 2023
1 parent c940945 commit bfc6eae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/components/guidespage/SubLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type SubLinksProps = {

const SubLinks: FC<SubLinksProps> = ({ fileList, onLinkClick }) => {
const { activeLink, setActiveLink } = useDocumentationContext();
console.log(activeLink);

fileList = [
...fileList.filter((file) => file.name === 'welcome'),
Expand Down
8 changes: 1 addition & 7 deletions src/context/DocumentationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SetStateAction,
createContext,
useContext,
useEffect,
useState,
} from 'react';

Expand Down Expand Up @@ -36,12 +35,7 @@ type DocumentationProps = {
// Context Provider
const DocumentationProvider: FC<DocumentationProps> = ({ children }) => {
const [isSidebarOpen, setIsSidebarOpen] = useState<boolean>(false);
const [activeLink, setActiveLink] = useState<PostFile['link']>(``);

useEffect(() => {
setActiveLink(links[2].to);
return () => setActiveLink(``);
}, []);
const [activeLink, setActiveLink] = useState<PostFile['link']>(links[2].to);

// Functions...
function openSidebar() {
Expand Down
3 changes: 2 additions & 1 deletion src/layouts/DocumentationLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const DocumentationLayout: FC<DocumentationLayoutProps> = ({
{width < 1024 && <DocList mobile data={menu} />}
<BreadCrumb dirName={meta.dirName} fileName={meta.fileName} />
</div>
<a href={meta.edit}>
{/* edit on github */}
<a href={meta.edit} target="_blank" rel="noreferrer">
<ToolTip tip="Edit on Github" direction="button">
<DocEditIcon className="h-6 w-6" />
</ToolTip>
Expand Down
17 changes: 15 additions & 2 deletions src/pages/guides/[...guide].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mdxComponents from '@/components/mdxcomponents';
import TypoComp from '@/components/utilities/TypoComponent';
import { useDocumentationContext } from '@/context/DocumentationContext';
import Container from '@/layouts/Container';
import DocumentationLayout from '@/layouts/DocumentationLayout';
import {
Expand All @@ -10,12 +11,22 @@ import {
import type { FolderStructure, Post } from '@/types/client/FileSystem';
import type { GetStaticPaths, GetStaticProps, NextPage } from 'next';
import { MDXRemote } from 'next-mdx-remote';
import { useEffect } from 'react';

type DocsProps = Post & {
menu: FolderStructure;
activeLink: string;
};

const Guides: NextPage<DocsProps> = ({ menu, meta, source }) => {
const Guides: NextPage<DocsProps> = ({ activeLink, menu, meta, source }) => {
const { setActiveLink } = useDocumentationContext();

useEffect(() => {
setActiveLink(`/guides/${activeLink}`);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activeLink]);

return (
<DocumentationLayout menu={menu} meta={meta} className="text-skin-base">
<Container className="relative py-4">
Expand Down Expand Up @@ -44,13 +55,15 @@ export const getStaticProps: GetStaticProps<DocsProps> = async ({ params }) => {
const menu = await getDocumentsMenu();

const { guide } = params as { guide: string[] };
const { meta, source } = await getContentsFromSlug(guide.join('/'));
const activeLink = guide.join('/');
const { meta, source } = await getContentsFromSlug(activeLink);

return {
props: {
meta,
source,
menu,
activeLink,
},
};
} catch (error) {
Expand Down

1 comment on commit bfc6eae

@vercel
Copy link

@vercel vercel bot commented on bfc6eae Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.