-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): User profile and viewer context (#4480)
* viewer node * feat(auth): rudamentary viewer context and profile page
- Loading branch information
1 parent
7101772
commit 6a201ce
Showing
10 changed files
with
429 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from "react"; | ||
import { graphql, useRefetchableFragment } from "react-relay"; | ||
|
||
import { | ||
ViewerContext_viewer$data, | ||
ViewerContext_viewer$key, | ||
} from "./__generated__/ViewerContext_viewer.graphql"; | ||
|
||
export type ViewerContextType = { | ||
viewer: ViewerContext_viewer$data["viewer"]; | ||
}; | ||
|
||
export const ViewerContext = React.createContext<ViewerContextType>({ | ||
viewer: null, | ||
}); | ||
|
||
export function useViewer() { | ||
const context = React.useContext(ViewerContext); | ||
if (context == null) { | ||
throw new Error("useViewer must be used within a ViewerProvider"); | ||
} | ||
return context; | ||
} | ||
|
||
export function ViewerProvider({ | ||
query, | ||
children, | ||
}: React.PropsWithChildren<{ | ||
query: ViewerContext_viewer$key; | ||
}>) { | ||
const [data] = useRefetchableFragment( | ||
graphql` | ||
fragment ViewerContext_viewer on Query | ||
@refetchable(queryName: "ViewerContextRefetchQuery") { | ||
viewer { | ||
id | ||
username | ||
role { | ||
name | ||
} | ||
} | ||
} | ||
`, | ||
query | ||
); | ||
return ( | ||
<ViewerContext.Provider value={{ viewer: data.viewer }}> | ||
{children} | ||
</ViewerContext.Provider> | ||
); | ||
} |
108 changes: 108 additions & 0 deletions
108
app/src/contexts/__generated__/ViewerContextRefetchQuery.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
app/src/contexts/__generated__/ViewerContext_viewer.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from "react"; | ||
import { useLoaderData } from "react-router"; | ||
|
||
import { ViewerProvider } from "@phoenix/contexts/ViewerContext"; | ||
|
||
import { authenticatedRootLoaderQuery$data } from "./__generated__/authenticatedRootLoaderQuery.graphql"; | ||
import { Layout } from "./Layout"; | ||
|
||
/** | ||
* The root of the authenticated application. Note that authentication might be entirely disabled | ||
*/ | ||
export function AuthenticatedRoot() { | ||
const loaderData = useLoaderData() as authenticatedRootLoaderQuery$data; | ||
return ( | ||
<ViewerProvider query={loaderData}> | ||
<Layout /> | ||
</ViewerProvider> | ||
); | ||
} |
Oops, something went wrong.