-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate UI elements into components. (#62)
* refactor: create dirTree.js * refactor: detach Categories component from Aside component * refactor: detach Home component from Aside component * refactor: make Aside component as a single wrapper * rename: move src/layouts to src/components/layouts * refactor: make Article components as a single wrapper * refactor: move every grid related properties to main component * refactor: detach Title component from Header component
- Loading branch information
1 parent
479910f
commit 5592b86
Showing
29 changed files
with
175 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { USER } from '@/constants/github'; | ||
|
||
import Article from '@/layouts/Article'; | ||
|
||
export default function Page() { | ||
return <Article>{`Hello, It's ${USER.name}'s blog!`}</Article>; | ||
return <>{`Hello, It's ${USER.name}'s blog!`}</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { join } from 'path'; | ||
|
||
import Link from 'next/link'; | ||
|
||
import { DOCS } from '@/constants/path'; | ||
import { getDirTree } from '@/utils/dirTree'; | ||
|
||
export default async function Categories() { | ||
const dirTree = await getDirTree(DOCS); | ||
|
||
return <>{renderDirTree(dirTree)}</>; | ||
} | ||
|
||
function renderDirTree(dirTree, basePath = '') { | ||
return ( | ||
<ul> | ||
{dirTree.map(dirTreeNode => { | ||
const currPath = join(basePath, dirTreeNode.name); | ||
|
||
return ( | ||
<li key={currPath}> | ||
{dirTreeNode.name.endsWith('.md') ? ( | ||
<Link href={`/docs/${currPath.replace('.md', '')}`}> | ||
{dirTreeNode.name.replace('.md', '')} | ||
</Link> | ||
) : ( | ||
<> | ||
<span>{dirTreeNode.name}</span> | ||
{renderDirTree(dirTreeNode.children, currPath)} | ||
</> | ||
)} | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Categories from './Categories'; | ||
|
||
export default Categories; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Link from 'next/link'; | ||
|
||
export default async function Home() { | ||
return ( | ||
<ul> | ||
<li> | ||
<Link href="/">Home</Link> | ||
</li> | ||
</ul> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Home from './Home'; | ||
|
||
export default Home; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Link from 'next/link'; | ||
|
||
import { USER } from '@/constants/github'; | ||
|
||
export default function Title() { | ||
return <Link href={USER.htmlUrl}>{USER.name}</Link>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Title from './Title'; | ||
|
||
export default Title; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styles from './Aside.module.scss'; | ||
|
||
export default function Aside({ children }) { | ||
return <aside className={styles.aside}>{children}</aside>; | ||
} |
1 change: 0 additions & 1 deletion
1
src/layouts/Aside/Aside.module.scss → ...omponents/layouts/Aside/Aside.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
.aside { | ||
position: sticky; | ||
top: 0; | ||
grid-area: 1 / 1 / 3 / 2; | ||
width: 300px; | ||
height: 100vh; | ||
overflow-y: scroll; | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.body { | ||
display: grid; | ||
grid-template-rows: 80px 1fr; | ||
grid-template-columns: 300px 1fr; | ||
|
||
> header { | ||
grid-area: 1 / 2 / 2 / 3; | ||
} | ||
|
||
> aside { | ||
grid-area: 1 / 1 / 3 / 2; | ||
} | ||
|
||
> main { | ||
grid-area: 2 / 2 / 3 / 3; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styles from './Header.module.scss'; | ||
|
||
export default function Header({ children }) { | ||
return <header className={styles.header}>{children}</header>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
position: sticky; | ||
top: 0; | ||
z-index: 1; | ||
grid-area: 1 / 2 / 2 / 3; | ||
font-size: 32px; | ||
background-color: inherit; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Main({ children }) { | ||
return <main>{children}</main>; | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { promises as fs } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
/** | ||
* @typedef {object} DirTreeNode | ||
* | ||
* @property {string} name The name of the node. | ||
* @property {Array<DirTreeNode>} [children] The array of child nodes if the node is a directory. This is a recursive structure. | ||
*/ | ||
|
||
/** | ||
* Asynchronously retrieves the directory tree structure. | ||
* | ||
* @async | ||
* @param {string} dirPath The path of the directory. | ||
* @returns {Promise<Array<DirTreeNode>>} A promise that resolves to an array of `DirTreeNode`. | ||
* | ||
* @example | ||
* // Get the directory tree structure | ||
* const dirTree = await getDirTree('/path/to/dir'); | ||
* console.log(dirTree); | ||
*/ | ||
export async function getDirTree(dirPath) { | ||
// `readdir` automatically throws an error when `dirPath` is not a directory. | ||
const dirents = await fs.readdir(dirPath, { withFileTypes: true }); | ||
|
||
return await Promise.all( | ||
dirents.map(async dirent => ({ | ||
name: dirent.name, | ||
...(dirent.isDirectory() | ||
? { children: await getDirTree(join(dirPath, dirent.name)) } | ||
: {}), | ||
})), | ||
); | ||
} | ||
|
||
/** | ||
* Checks if a `DirTreeNode` is a directory. | ||
* | ||
* @param {DirTreeNode} dirTreeNode The `DirTreeNode` object. | ||
* @returns {boolean} `true` if the node is a directory. otherwise, `false`. | ||
* | ||
* @example | ||
* // Check if a node is a directory | ||
* const isDir = isDirectory({ name: 'folder', children: [...] }); | ||
* console.log(isDir); // true | ||
*/ | ||
export function isDirectory(dirTreeNode) { | ||
return Boolean(dirTreeNode.children); | ||
} |