Skip to content

Commit

Permalink
feat: support render a header from folder or meta
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 2, 2023
1 parent cd7e61a commit 39186de
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/hooks/usePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const usePath = () => {
handleFolder(path, globalPage)
} else {
ObjStore.setReadme(data.readme)
ObjStore.setHeader(data.header)
ObjStore.setRelated(data.related ?? [])
ObjStore.setRawUrl(data.raw_url)
ObjStore.setState(State.File)
Expand Down Expand Up @@ -184,6 +185,7 @@ export const usePath = () => {
ObjStore.setTotal(data.total)
}
ObjStore.setReadme(data.readme)
ObjStore.setHeader(data.header)
ObjStore.setWrite(data.write)
ObjStore.setProvider(data.provider)
ObjStore.setState(State.Folder)
Expand Down
6 changes: 5 additions & 1 deletion src/pages/home/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ export const Body = () => {
w="$full"
gap="$4"
>
<Readme files={["header.md", "top.md"]} fromMeta="header" />
<Nav />
<Obj />
<Readme />
<Readme
files={["readme.md", "footer.md", "bottom.md"]}
fromMeta="readme"
/>
</VStack>
</Container>
)
Expand Down
18 changes: 13 additions & 5 deletions src/pages/home/Readme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { useLink, useRouter } from "~/hooks"
import { objStore, recoverScroll, State } from "~/store"
import { fetchText } from "~/utils"

export const Readme = () => {
export function Readme(props: {
files: string[]
fromMeta: keyof typeof objStore
}) {
const cardBg = useColorModeValue("white", "$neutral3")
const { proxyLink } = useLink()
const { pathname } = useRouter()
Expand All @@ -21,15 +24,20 @@ export const Readme = () => {
return ""
}
if ([State.FetchingMore, State.Folder].includes(objStore.state)) {
const obj = objStore.objs.find(
(item) => item.name.toLowerCase() === "readme.md",
const obj = objStore.objs.find((item) =>
props.files.find(
(file) => file.toLowerCase() === item.name.toLowerCase(),
),
)
if (obj) {
return proxyLink(obj, true)
}
}
if (objStore.readme) {
return objStore.readme
if (
objStore[props.fromMeta] &&
typeof objStore[props.fromMeta] === "string"
) {
return objStore[props.fromMeta] as string
}
return ""
},
Expand Down
3 changes: 3 additions & 0 deletions src/store/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const [objStore, setObjStore] = createStore<{
write?: boolean

readme: string
header: string
provider: string
// pageIndex: number;
// pageSize: number;
Expand All @@ -41,6 +42,7 @@ const [objStore, setObjStore] = createStore<{
total: 0,

readme: "",
header: "",
provider: "",
// pageIndex: 1,
// pageSize: 50,
Expand Down Expand Up @@ -72,6 +74,7 @@ export const ObjStore = {
setObjStore("total", total)
},
setReadme: (readme: string) => setObjStore("readme", readme),
setHeader: (header: string) => setObjStore("header", header),
setRelated: (related: Obj[]) => setObjStore("related", related),
setWrite: (write: boolean) => setObjStore("write", write),
// setGetResp: (resp: FsGetResp) => {
Expand Down
2 changes: 2 additions & 0 deletions src/types/resp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type FsListResp = Resp<{
content: Obj[]
total: number
readme: string
header: string
write: boolean
provider: string
}>
Expand All @@ -34,6 +35,7 @@ export type FsGetResp = Resp<
Obj & {
raw_url: string
readme: string
header: string
provider: string
related: Obj[]
}
Expand Down

0 comments on commit 39186de

Please sign in to comment.