The Wayback Machine - https://web.archive.org/web/20230606091123/https://github.com/github/docs/commit/f806ee701295c3f061cab17ebca7a67ac93685f9
Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move more files into src/webhooks (#36578)
  • Loading branch information
heiskr committed May 10, 2023
1 parent a02ae16 commit f806ee7
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 119 deletions.
2 changes: 1 addition & 1 deletion middleware/api/index.js
Expand Up @@ -5,7 +5,7 @@ import events from '../../src/events/middleware.js'
import anchorRedirect from '../../src/rest/api/anchor-redirect.js'
import search from '../../src/search/middleware/search.js'
import pageInfo from '../../src/pageinfo/middleware.js'
import webhooks from './webhooks.js'
import webhooks from '../../src/webhooks/middleware/webhooks.js'

const router = express.Router()

Expand Down
1 change: 1 addition & 0 deletions next.config.js
Expand Up @@ -37,6 +37,7 @@ export default {
webpack: (config) => {
config.experiments = config.experiments || {}
config.experiments.topLevelAwait = true
config.resolve.fallback = { fs: false }
return config
},

Expand Down
@@ -1,114 +1,4 @@
import { GetServerSideProps } from 'next'
import { useRouter } from 'next/router'
import { useEffect } from 'react'

import { getInitialPageWebhooks } from 'src/webhooks/lib'
import { getMainContext, MainContext, MainContextT } from 'components/context/MainContext'
import {
getAutomatedPageContextFromRequest,
AutomatedPageContext,
AutomatedPageContextT,
} from 'components/context/AutomatedPageContext'
import { WebhookAction } from 'components/webhooks/types'
import { Webhook } from 'components/webhooks/Webhook'
import { getAutomatedPageMiniTocItems } from 'lib/get-mini-toc-items'
import { AutomatedPage } from 'components/article/AutomatedPage'

type Props = {
mainContext: MainContextT
automatedPageContext: AutomatedPageContextT
webhooks: WebhookAction[]
}

export default function WebhooksEventsAndPayloads({
mainContext,
automatedPageContext,
webhooks,
}: Props) {
const router = useRouter()
const { locale } = router
const content = webhooks.map((webhook: WebhookAction, index) => {
return (
<div key={`${webhook.data.requestPath}-${index}`}>
<Webhook webhook={webhook} />
</div>
)
})

// When someone clicks on a minitoc hash anchor link on this page, we want to
// remove the type query parameter from the URL because the type won't make
// sense anymore (e.g. ?actionTtype=closed#issues and you click on the fork minitoc
// we don't want the URL to be ?actionType=closed#fork).
useEffect(() => {
const hashChangeHandler = () => {
const { asPath } = router
let [pathRoot, pathQuery = ''] = asPath.split('?')

if (pathRoot.includes('#')) {
pathRoot = pathRoot.split('#')[0]
}

// carry over any other query parameters besides `actionType` for the webhook
// action type
if (pathQuery.includes('#')) {
pathQuery = pathQuery.split('#')[0]
}
const params = new URLSearchParams(pathQuery)
params.delete('actionType')

if (location.hash) {
router.replace(
{ pathname: pathRoot, query: params.toString(), hash: location.hash },
undefined,
{
shallow: true,
locale,
}
)
}
}

window.addEventListener('hashchange', hashChangeHandler)

return () => {
window.removeEventListener('hashchange', hashChangeHandler)
}
}, [locale])

return (
<MainContext.Provider value={mainContext}>
<AutomatedPageContext.Provider value={automatedPageContext}>
<AutomatedPage>{content}</AutomatedPage>
</AutomatedPageContext.Provider>
</MainContext.Provider>
)
}

export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const req = context.req as object
const res = context.res as object
const currentVersion = context.query.versionId as string
const mainContext = await getMainContext(req, res)
const { miniTocItems } = getAutomatedPageContextFromRequest(req)

// Get data for initial webhooks page (i.e. only 1 action type per webhook and
// no nested parameters)
const webhooks = (await getInitialPageWebhooks(currentVersion)) as WebhookAction[]

// Build the minitocs for the webhooks page which is based on the webhook
// categories in addition to the Markdown in the webhook-events-and-payloads.md
// content file
const webhooksMiniTocs = await getAutomatedPageMiniTocItems(
webhooks.map((webhook) => webhook.data.category),
context
)
webhooksMiniTocs && miniTocItems.push(...webhooksMiniTocs)

return {
props: {
webhooks,
mainContext,
automatedPageContext: getAutomatedPageContextFromRequest(req),
},
}
}
export {
default,
getServerSideProps,
} from '../../../../src/webhooks/pages/webhook-events-and-payloads'
File renamed without changes.
@@ -1,4 +1,4 @@
import { Parameter, StatusCode, CodeSample, BodyParameter } from '../rest/types'
import { Parameter, StatusCode, CodeSample, BodyParameter } from '../../../components/rest/types'

export interface WebhookT {
actions: string[]
Expand Down
@@ -1,7 +1,7 @@
import express from 'express'
import { getWebhook } from '../../src/webhooks/lib/index.js'
import { allVersions } from '../../lib/all-versions.js'
import { defaultCacheControl } from '../cache-control.js'
import { getWebhook } from '../lib/index.js'
import { allVersions } from '../../../lib/all-versions.js'
import { defaultCacheControl } from '../../../middleware/cache-control.js'

const router = express.Router()

Expand Down
115 changes: 115 additions & 0 deletions src/webhooks/pages/webhook-events-and-payloads.tsx
@@ -0,0 +1,115 @@
import { GetServerSideProps } from 'next'
import { useRouter } from 'next/router'
import { useEffect } from 'react'

import { getMainContext, MainContext, MainContextT } from 'components/context/MainContext'
import {
getAutomatedPageContextFromRequest,
AutomatedPageContext,
AutomatedPageContextT,
} from 'components/context/AutomatedPageContext'
import { WebhookAction } from 'src/webhooks/components/types'
import { Webhook } from 'src/webhooks/components/Webhook'
import { AutomatedPage } from 'components/article/AutomatedPage'

type Props = {
mainContext: MainContextT
automatedPageContext: AutomatedPageContextT
webhooks: WebhookAction[]
}

export default function WebhooksEventsAndPayloads({
mainContext,
automatedPageContext,
webhooks,
}: Props) {
const router = useRouter()
const { locale } = router
const content = webhooks.map((webhook: WebhookAction, index) => {
return (
<div key={`${webhook.data.requestPath}-${index}`}>
<Webhook webhook={webhook} />
</div>
)
})

// When someone clicks on a minitoc hash anchor link on this page, we want to
// remove the type query parameter from the URL because the type won't make
// sense anymore (e.g. ?actionTtype=closed#issues and you click on the fork minitoc
// we don't want the URL to be ?actionType=closed#fork).
useEffect(() => {
const hashChangeHandler = () => {
const { asPath } = router
let [pathRoot, pathQuery = ''] = asPath.split('?')

if (pathRoot.includes('#')) {
pathRoot = pathRoot.split('#')[0]
}

// carry over any other query parameters besides `actionType` for the webhook
// action type
if (pathQuery.includes('#')) {
pathQuery = pathQuery.split('#')[0]
}
const params = new URLSearchParams(pathQuery)
params.delete('actionType')

if (location.hash) {
router.replace(
{ pathname: pathRoot, query: params.toString(), hash: location.hash },
undefined,
{
shallow: true,
locale,
}
)
}
}

window.addEventListener('hashchange', hashChangeHandler)

return () => {
window.removeEventListener('hashchange', hashChangeHandler)
}
}, [locale])

return (
<MainContext.Provider value={mainContext}>
<AutomatedPageContext.Provider value={automatedPageContext}>
<AutomatedPage>{content}</AutomatedPage>
</AutomatedPageContext.Provider>
</MainContext.Provider>
)
}

export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const { getInitialPageWebhooks } = await import('src/webhooks/lib')
const { getAutomatedPageMiniTocItems } = await import('lib/get-mini-toc-items')

const req = context.req as object
const res = context.res as object
const currentVersion = context.query.versionId as string
const mainContext = await getMainContext(req, res)
const { miniTocItems } = getAutomatedPageContextFromRequest(req)

// Get data for initial webhooks page (i.e. only 1 action type per webhook and
// no nested parameters)
const webhooks = (await getInitialPageWebhooks(currentVersion)) as WebhookAction[]

// Build the minitocs for the webhooks page which is based on the webhook
// categories in addition to the Markdown in the webhook-events-and-payloads.md
// content file
const webhooksMiniTocs = await getAutomatedPageMiniTocItems(
webhooks.map((webhook) => webhook.data.category),
context
)
webhooksMiniTocs && miniTocItems.push(...webhooksMiniTocs)

return {
props: {
webhooks,
mainContext,
automatedPageContext: getAutomatedPageContextFromRequest(req),
},
}
}

0 comments on commit f806ee7

Please sign in to comment.