Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat(router): Better autocomplete for <Set>s (#11769)" #11902

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .changesets/11769.md

This file was deleted.

75 changes: 25 additions & 50 deletions packages/router/src/Set.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import React from 'react'

import type { AvailableRoutes } from '@redwoodjs/router'

type RegularSetProps = {
type SetProps<P> = (P extends React.FC ? React.ComponentProps<P> : unknown) & {
/**
* A react component that the children of the Set will be wrapped
* in (typically a Layout component)
*/
wrap?: P
/**
*`Routes` nested in a `<Set>` with `private` specified require
* authentication. When a user is not authenticated and attempts to visit
Expand All @@ -18,38 +23,6 @@ type RegularSetProps = {
* @deprecated Please use `<PrivateSet>` instead and specify this prop there
*/
unauthenticated?: keyof AvailableRoutes
}

/**
* A set containing public `<Route />`s
*/
export function Set<WrapperProps>(
props: CommonSetProps<WrapperProps> & RegularSetProps,
) {
// @MARK: Virtual Component, this is actually never rendered
// See analyzeRoutes in utils.tsx, inside the isSetNode block
return <>{props.children}</>
}

type CommonSetProps<P> = (P extends React.FC<any>
? React.ComponentProps<P>
: P extends React.FC<any>[]
? React.ComponentProps<P[0]> &
React.ComponentProps<P[1]> &
React.ComponentProps<P[2]> &
React.ComponentProps<P[3]> &
React.ComponentProps<P[4]> &
React.ComponentProps<P[5]> &
React.ComponentProps<P[6]> &
React.ComponentProps<P[7]> &
React.ComponentProps<P[8]> &
React.ComponentProps<P[9]>
: unknown) & {
/**
* A React component, or an array of React components, that the children of
* the Set will be wrapped in (typically a Layout component and/or a context)
*/
wrap?: P
/**
* Route is permitted when authenticated and user has any of the provided
* roles such as "admin" or ["admin", "editor"]
Expand All @@ -63,13 +36,22 @@ type CommonSetProps<P> = (P extends React.FC<any>
whileLoadingPage?: () => ReactElement | null
}

/**
* A set containing public `<Route />`s
*/
export function Set<WrapperProps>(props: SetProps<WrapperProps>) {
// @MARK: Virtual Component, this is actually never rendered
// See analyzeRoutes in utils.tsx, inside the isSetNode block
return <>{props.children}</>
}

type PrivateSetProps<P> = Omit<SetProps<P>, 'private' | 'unauthenticated'> & {
/** The page name where a user will be redirected when not authenticated */
unauthenticated: keyof AvailableRoutes
}

/** @deprecated Please use `<PrivateSet>` instead */
export function Private<WrapperProps>(
props: CommonSetProps<WrapperProps> & {
/** The page name where a user will be redirected when not authenticated */
unauthenticated: keyof AvailableRoutes
},
) {
export function Private<WrapperProps>(props: PrivateSetProps<WrapperProps>) {
// @MARK Virtual Component, this is actually never rendered
// See analyzeRoutes in utils.tsx, inside the isSetNode block
return <>{props.children}</>
Expand All @@ -78,20 +60,15 @@ export function Private<WrapperProps>(
/**
* A set containing private `<Route />`s that require authentication to access
*/
export function PrivateSet<WrapperProps>(
props: CommonSetProps<WrapperProps> & {
/** The page name where a user will be redirected when not authenticated */
unauthenticated: keyof AvailableRoutes
},
) {
export function PrivateSet<WrapperProps>(props: PrivateSetProps<WrapperProps>) {
// @MARK Virtual Component, this is actually never rendered
// See analyzeRoutes in utils.tsx, inside the isSetNode block
return <>{props.children}</>
}

export const isSetNode = (
node: ReactNode,
): node is ReactElement<CommonSetProps<any> & RegularSetProps> => {
): node is ReactElement<SetProps<any>> => {
return (
React.isValidElement(node) &&
(node.type === Set || node.type === PrivateSet || node.type === Private) &&
Expand All @@ -102,15 +79,13 @@ export const isSetNode = (

export const isPrivateSetNode = (
node: ReactNode,
): node is ReactElement<
CommonSetProps<unknown> & { unauthenticated: keyof AvailableRoutes }
> => {
): node is ReactElement<PrivateSetProps<unknown>> => {
return React.isValidElement(node) && node.type === PrivateSet
}

// Only identifies <Private> nodes, not <Set private> nodes
export const isPrivateNode = (
node: ReactNode,
): node is ReactElement<CommonSetProps<any> & RegularSetProps> => {
): node is ReactElement<SetProps<any>> => {
return React.isValidElement(node) && node.type === Private
}
Loading