Skip to content

Commit

Permalink
get JSON data:)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleguido committed Mar 7, 2024
1 parent c1d5868 commit e14b1a2
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 12 deletions.
4 changes: 3 additions & 1 deletion gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AvailableModalsViews, useBrowserStore } from './src/store'

// Logs when the client route changes
export function onRouteUpdate({ location, prevLocation }) {
console.log('[Layout] render')
console.log('[gatsby-browser]@onRouteUpdate new pathname', location)
// update zustand with search params, if any
if (location.search) {
Expand All @@ -29,7 +30,8 @@ export function onRouteUpdate({ location, prevLocation }) {
}

// Wraps every page in a component
export function wrapPageElement({ element, props }) {
export function wrapRootElement({ element, props }) {
console.log('[gatsby-browser]@wrapRootElement')
return (
<>
<Modals />
Expand Down
133 changes: 130 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,88 @@
const debug = require('debug')('gatsby-node')
console.log('gatsby-node.js')
const debug = require('debug')('datalab:gatsby-node')
const fs = require('fs')

debug('gatsby-node.js')
const NotebooksJsonFilepath = './static/data/notebooks.json'
const AuthorsJsonFilepath = './static/data/authors.json'
const CollectionsJsonFilepath = './static/data/collections.json'

exports.createPages = async function ({ actions, graphql }) {
const authorsMap = {}
const notebooksMap = {}
const collectionsMap = {}

const { data: authors } = await graphql(`
query {
allFile(filter: { sourceInstanceName: { eq: "authors" } }) {
totalCount
nodes {
name
childMdx {
excerpt
frontmatter {
fullName
}
}
}
}
}
`)

debug('createPages: n. authors: ', authors.allFile.nodes.length)
authors.allFile.nodes.forEach((node) => {
authorsMap[node.name] = {
name: node.name,
fullName: node.childMdx.frontmatter.fullName,
collections: [],
notebooks: [],
}
})

const { data: notebooks } = await graphql(`
query {
allFile(filter: { sourceInstanceName: { eq: "notebooks" } }) {
totalCount
nodes {
name
birthTime
accessTime
childMdx {
excerpt
frontmatter {
title
binderUrl
authors
}
}
}
}
}
`)
console.log('createPages: n. notebooks: ', notebooks.allFile.nodes.length)

notebooks.allFile.nodes.forEach((node) => {
notebooksMap[node.name] = {
name: node.name,
path: `/notebook/${node.name}`,
birthTime: node.birthTime,
accessTime: node.accessTime,
title: node.childMdx.frontmatter.title,
excerpt: node.childMdx.excerpt,
binderUrl: node.childMdx.frontmatter.binderUrl,
authors: node.childMdx.frontmatter.authors || [],
collections: [],
}

notebooksMap[node.name].authors.forEach((author) => {
if (authorsMap[author]) {
authorsMap[author].notebooks.push(node.name)
} else {
console.error(
`Author ${author} not found in authorsMap, skipping notebook ${node.name}`
)
}
})

actions.createPage({
path: `/notebook/${node.name}`,
component: require.resolve(`./src/templates/Notebook.js`),
Expand All @@ -30,24 +94,87 @@ exports.createPages = async function ({ actions, graphql }) {
allFile(filter: { sourceInstanceName: { eq: "collections" } }) {
totalCount
nodes {
birthTime
accessTime
name
childMdx {
excerpt
frontmatter {
title
notebooks
}
}
}
}
}
`)
console.log('createPages: n. collections: ', collections.allFile.nodes.length)

debug('createPages: n. collections: ', collections.allFile.nodes.length)
collections.allFile.nodes.forEach((node) => {
collectionsMap[node.name] = {
name: node.name,
path: `/collection/${node.name}`,
title: node.childMdx.frontmatter.title,
excerpt: node.childMdx.excerpt,
notebooks: node.childMdx.frontmatter.notebooks,
contributors: [],
}
node.childMdx.frontmatter.notebooks.forEach((notebook) => {
if (notebooksMap[notebook]) {
notebooksMap[notebook].collections.push(node.name)
notebooksMap[notebook].authors.forEach((author) => {
if (authorsMap[author]) {
collectionsMap[node.name].contributors.push(authorsMap[author])
} else {
console.error(
`Author ${author} not found in authorsMap, skipping collection ${node.name}`
)
}
})
} else {
console.error(
`Notebook ${notebook} not found in notebooksMap, skipping collection ${node.name}`
)
}
})

// flatten contributors to array of unique contributors and count
collectionsMap[node.name].contributors = Object.entries(
collectionsMap[node.name].contributors.reduce((acc, contributor) => {
if (acc[contributor.name]) {
acc[contributor.name].count++
} else {
acc[contributor.name] = { name: contributor.name, count: 1 }
}
return acc
}, {})
)
collectionsMap[node.name].contributors.forEach((contributor) => {
if (authorsMap[contributor.name]) {
authorsMap[contributor.name].collections.push(node.name)
} else {
console.error(
`Contributor ${contributor.name} not found in authorsMap, skipping collection ${node.name}`
)
}
})

actions.createPage({
path: `/notebook/${node.name}`,
component: require.resolve(`./src/templates/Collection.js`),
context: { node },
})
})

//
// Write to JSON files
//
fs.writeFileSync(AuthorsJsonFilepath, JSON.stringify(authorsMap, null, 2))
fs.writeFileSync(NotebooksJsonFilepath, JSON.stringify(notebooksMap, null, 2))
fs.writeFileSync(
CollectionsJsonFilepath,
JSON.stringify(collectionsMap, null, 2)
)
}

exports.onCreateNode = ({ node, actions, getNode }) => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
},
"dependencies": {
"@mdx-js/react": "^3.0.0",
"@tanstack/react-query": "^5.25.0",
"axios": "^1.6.7",
"bootstrap": "^5.3.2",
"debug": "^4.3.4",
"gatsby": "^5.13.2",
Expand Down
3 changes: 3 additions & 0 deletions src/authors/daniele-guido.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fullName: Daniele Guido
---
2 changes: 1 addition & 1 deletion src/authors/impresso-team.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
fullname: 'The Impresso Team'
fullName: 'The Impresso Team'
---
1 change: 1 addition & 0 deletions src/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Col, Container, Row } from 'react-bootstrap'
import Header from './Header'

const Layout = ({ children }) => {
console.log('[Layout] render')
return (
<>
<Header />
Expand Down
6 changes: 1 addition & 5 deletions src/components/ModalLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ const ModalLogin = ({ onClose, ...props }) => {
<Form>
<Form.Group className='mb-3' controlId='ModalLoginForm.email'>
<Form.Label>Email address</Form.Label>
<Form.Control
type='email'
placeholder='[email protected]'
autoFocus
/>
<Form.Control type='email' placeholder='[email protected]' />
</Form.Group>
<Form.Group className='mb-3' controlId='ModalLoginForm.password'>
<Form.Label>Password</Form.Label>
Expand Down
18 changes: 18 additions & 0 deletions src/components/ModalNotebookPreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import ModalView from './ModalView'
import { ModalNotebookPreviewView } from '../store'
import { Form, Modal } from 'react-bootstrap'

const ModalNotebookPreview = ({ onClose, ...props }) => {
return (
<ModalView viewName={ModalNotebookPreviewView} onClose={onClose} {...props}>
<Modal.Header closeButton>
<Modal.Title id='contained-modal-title-vcenter'>
Using Grid in Modal
</Modal.Title>
</Modal.Header>
<Modal.Body>This is a nice title indeed!</Modal.Body>
</ModalView>
)
}
export default ModalNotebookPreview
3 changes: 2 additions & 1 deletion src/components/Modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AvailableModalsViews } from '../store'
import { Button } from 'react-bootstrap'
import { navigate } from 'gatsby'
import ModalLogin from './ModalLogin'
import ModalNotebookPreview from './ModalNotebookPreview'

const Modals = ({ debug = true }) => {
const onCloseHandler = () => {
Expand All @@ -21,7 +22,7 @@ const Modals = ({ debug = true }) => {
{modal}
</Button>
))}

<ModalNotebookPreview onClose={onCloseHandler} centered />
<ModalLogin onClose={onCloseHandler} centered />
</>
)
Expand Down
2 changes: 2 additions & 0 deletions src/notebooks/team-starter-pack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ tags:
- demo
binderUrl: https://mybinder.org/v2/gh/binder-examples/r/master?urlpath=rstudio
repoUrl:
authors:
- impresso-team
---

# RStudio on Binder
Expand Down
16 changes: 16 additions & 0 deletions static/data/authors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"daniele-guido": {
"name": "daniele-guido",
"fullName": "Daniele Guido",
"collections": [],
"notebooks": []
},
"impresso-team": {
"name": "impresso-team",
"fullName": "The Impresso Team",
"collections": [],
"notebooks": [
"team-starter-pack"
]
}
}
20 changes: 20 additions & 0 deletions static/data/collections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"enter-impresso": {
"name": "enter-impresso",
"path": "/collection/enter-impresso",
"title": "Enter *impresso*",
"excerpt": "Three easy steps to enter the impresso way of doing research.",
"notebooks": [
"team-starter-pack"
],
"contributors": [
[
"impresso-team",
{
"name": "impresso-team",
"count": 1
}
]
]
}
}
28 changes: 28 additions & 0 deletions static/data/notebooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"setup": {
"name": "setup",
"path": "/notebook/setup",
"birthTime": "2024-02-20T11:22:42.754Z",
"accessTime": "2024-03-01T11:18:39.885Z",
"title": "Setup",
"excerpt": "",
"binderUrl": "https://mybinder.org/v2/gh/binder-examples/r/master?urlpath=rstudio",
"authors": [],
"collections": []
},
"team-starter-pack": {
"name": "team-starter-pack",
"path": "/notebook/team-starter-pack",
"birthTime": "2024-02-09T16:55:21.331Z",
"accessTime": "2024-03-07T09:16:28.892Z",
"title": "A demo of RStudio running on Binder",
"excerpt": "All together, right now.",
"binderUrl": "https://mybinder.org/v2/gh/binder-examples/r/master?urlpath=rstudio",
"authors": [
"impresso-team"
],
"collections": [
"enter-impresso"
]
}
}
Loading

0 comments on commit e14b1a2

Please sign in to comment.