Skip to content

Commit

Permalink
Merge branch 'feat/api-skip-prebuild-try-2' of github.com:dac09/redwo…
Browse files Browse the repository at this point in the history
…od into feat/api-skip-prebuild-try-2

* 'feat/api-skip-prebuild-try-2' of github.com:dac09/redwood:
  chore(ci): retry detectChanges on error (redwoodjs#9769)
  useRouteName (redwoodjs#9758)
  docker.md: Fix web path (redwoodjs#9768)
  • Loading branch information
dac09 committed Dec 28, 2023
2 parents 213fbf7 + b5acbd0 commit 20370a0
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 18 deletions.
47 changes: 35 additions & 12 deletions .github/actions/detect-changes/detectChanges.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,49 @@ const getPrNumber = (githubRef) => {
return prNumber
}

async function getChangedFiles(page = 1) {
async function getChangedFiles(page = 1, retries = 0) {
const prNumber = getPrNumber()

console.log(`Getting changed files for PR ${prNumber} (page ${page})`)
if (retries) {
console.log(
`Retry ${retries}: Getting changed files for PR ${prNumber} (page ${page})`
)
} else {
console.log(`Getting changed files for PR ${prNumber} (page ${page})`)
}

let changedFiles = []

// Query the GitHub API to get the changed files in the PR
const githubToken = process.env.GITHUB_TOKEN
const url = `https://api.github.com/repos/redwoodjs/redwood/pulls/${prNumber}/files?per_page=100&page=${page}`
const resp = await fetch(url, {
headers: {
Authorization: githubToken ? `Bearer ${githubToken}` : undefined,
['X-GitHub-Api-Version']: '2022-11-28',
Accept: 'application/vnd.github+json',
},
})

const json = await resp.json()
const files = json?.map((file) => file.filename) || []
let resp
let files

try {
resp = await fetch(url, {
headers: {
Authorization: githubToken ? `Bearer ${githubToken}` : undefined,
['X-GitHub-Api-Version']: '2022-11-28',
Accept: 'application/vnd.github+json',
},
})

const json = await resp.json()
files = json.map((file) => file.filename) || []
} catch (e) {
if (retries >= 3) {
console.error(e)

console.log()
console.log('Too many retries, giving up.')

return []
} else {
await new Promise((resolve) => setTimeout(resolve, 3000))
getChangedFiles(page, ++retries)
}
}

changedFiles = changedFiles.concat(files)

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ WORKDIR /home/node/app

COPY --chown=node:node .yarnrc.yml .
COPY --chown=node:node package.json .
COPY --chown=node:node web/package.json .
COPY --chown=node:node web/package.json web/
COPY --chown=node:node yarn.lock .

RUN mkdir -p /home/node/.yarn/berry/index
Expand Down
11 changes: 11 additions & 0 deletions docs/docs/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,17 @@ const routePaths = useRoutePaths()
const aboutPath = routePaths.about // Also returns "/about"
```

## useRouteName

Use the `useRouteName()` hook to get the name of the current route (the page
the user is currently visiting). The name can then also be used with `routes`
if you need to dynamically get the url to the current page:

```jsx
const routeName = useRouteName()
const routeUrl = routeName ? routes[routeName]() : undefined
```

## Navigation

### navigate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ ${routes.map(

export function useRoutePaths(): Record<keyof AvailableRoutes, string>
export function useRoutePath(routeName: keyof AvailableRoutes): string

/** Gets the name of the current route (as defined in your Routes file) */
export function useRouteName(): keyof AvailableRoutes | undefined
}

//# sourceMappingURL=web-routerRoutes.d.ts.map
2 changes: 1 addition & 1 deletion packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export { default as RouteAnnouncement } from './route-announcement'
export * from './route-announcement'
export { default as RouteFocus } from './route-focus'
export * from './route-focus'

export * from './useRouteName'
export * from './useRoutePaths'

export { parseSearch, getRouteRegexAndParams, matchPath } from './util'
Expand Down
8 changes: 5 additions & 3 deletions packages/router/src/router-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface RouterState {
paramTypes?: Record<string, ParamType>
useAuth: UseAuth
routes: ReturnType<typeof analyzeRoutes>
activeRouteName?: string | undefined | null
}

const RouterStateContext = createContext<RouterState | undefined>(undefined)
Expand All @@ -36,17 +37,16 @@ const RouterSetContext = createContext<
React.Dispatch<Partial<RouterState>> | undefined
>(undefined)

/***
*
/**
* This file splits the context into getter and setter contexts.
* This was originally meant to optimize the number of redraws
* See https://kentcdodds.com/blog/how-to-optimize-your-context-value
*
*/
export interface RouterContextProviderProps
extends Omit<RouterState, 'useAuth'> {
useAuth?: UseAuth
routes: ReturnType<typeof analyzeRoutes>
activeRouteName?: string | undefined | null
children: React.ReactNode
}

Expand All @@ -58,12 +58,14 @@ export const RouterContextProvider: React.FC<RouterContextProviderProps> = ({
useAuth,
paramTypes,
routes,
activeRouteName,
children,
}) => {
const [state, setState] = useReducer(stateReducer, {
useAuth: useAuth || useNoAuth,
paramTypes,
routes,
activeRouteName,
})

return (
Expand Down
3 changes: 2 additions & 1 deletion packages/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function Route(_props: RouteProps | RedirectRouteProps | NotFoundRouteProps) {
}

export interface RouterProps
extends Omit<RouterContextProviderProps, 'routes'> {
extends Omit<RouterContextProviderProps, 'routes' | 'activeRouteName'> {
trailingSlashes?: TrailingSlashesTypes
pageLoadingDelay?: number
children: ReactNode
Expand Down Expand Up @@ -176,6 +176,7 @@ const LocationAwareRouter: React.FC<RouterProps> = ({
useAuth={useAuth}
paramTypes={paramTypes}
routes={analyzeRoutesResult}
activeRouteName={name}
>
<ParamsProvider allParams={allParams}>
<PageLoadingContextProvider delay={pageLoadingDelay}>
Expand Down
22 changes: 22 additions & 0 deletions packages/router/src/useRouteName.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useRouterState } from './router-context'

import { routes } from '.'
import type { AvailableRoutes } from '.'

// This needs to be a function so that we can use codegen to provide better
// types in a user's project (see web-routerRoutes.d.ts)
/** Gets the name of the current route (as defined in your Routes file) */
export function useRouteName() {
const routerState = useRouterState()
const routeName = routerState.activeRouteName

if (isAvailableRouteName(routeName)) {
return routeName
}

return undefined
}

function isAvailableRouteName(name: unknown): name is keyof AvailableRoutes {
return typeof name === 'string' && Object.keys(routes).includes(name)
}

0 comments on commit 20370a0

Please sign in to comment.