forked from SLoh4137/umcp-tase
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseBios.tsx
executable file
·76 lines (69 loc) · 2.49 KB
/
useBios.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { useStaticQuery, graphql } from "gatsby"
import { mapImgToNode } from "utils/hookUtils"
import { BioQuery } from "graphql-types"
// Type Definitions
type BioEdge = BioQuery["allMarkdownRemark"]["edges"][0]
type BioNode = BioEdge["node"]
export type BioArrayType = ReturnType<typeof useBios>
export type BioType = BioArrayType[0]
export type BioImageType = BioType["image"]
/**
* Returns all bios with their associated images
*/
export default function useBios() {
// Because static queries can't have parameters, we have to query for everything
const data = useStaticQuery<BioQuery>(graphql`
query Bio {
allMarkdownRemark(
filter: { frontmatter: { category: { eq: "bio" } } }
) {
edges {
node {
id
frontmatter {
majors
name
position
imgsrc
category
}
html
excerpt(format: HTML, truncate: true, pruneLength: 200)
}
}
}
allFile(
sort: { fields: relativePath, order: ASC }
filter: { absolutePath: { regex: "static/assets/" } }
) {
edges {
node {
id
relativePath
...RaisedImage
}
}
}
order: markdownRemark(fileAbsolutePath: { regex: "/bio-order/" }) {
frontmatter {
options
}
}
}
`)
if (
!data.allMarkdownRemark?.edges ||
!data.allFile?.edges ||
!data.order?.frontmatter?.options
) {
throw new Error("Error in formation of Bio query")
}
const sortingOrder = data.order.frontmatter.options
const sortingFunction = (a: BioEdge, b: BioEdge) =>
sortingOrder.indexOf(a.node.frontmatter?.position) -
sortingOrder.indexOf(b.node.frontmatter?.position)
// @ts-ignore Sort does an in-place sort and data.allMarkdownRemark.edges is a readonly array.
// Since we're not using data.allMarkdownRemark.edges again, it's okay if we use in-place
const bios = data.allMarkdownRemark.edges.sort(sortingFunction)
return mapImgToNode<BioNode>(bios, data.allFile.edges)
}