Skip to content

Commit 93523bd

Browse files
committed
feat(ory): init
1 parent 1f5687d commit 93523bd

38 files changed

+1239
-0
lines changed

.pnp.cjs

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-kratos/package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@atls/react-kratos",
3+
"version": "0.0.0",
4+
"license": "BSD-3-Clause",
5+
"main": "src/index.ts",
6+
"files": [
7+
"dist"
8+
],
9+
"scripts": {
10+
"build": "yarn library build",
11+
"prepack": "yarn run build",
12+
"postpack": "rm -rf dist"
13+
},
14+
"dependencies": {
15+
"@ory/client": "^1.4.7",
16+
"axios": "^1.6.3"
17+
},
18+
"devDependencies": {
19+
"@types/react": "^18.2.45",
20+
"@types/react-dom": "^18.2.18",
21+
"react": "^18.2.0",
22+
"react-dom": "^18.2.0"
23+
},
24+
"peerDependencies": {
25+
"react": "^18.0.0"
26+
},
27+
"publishConfig": {
28+
"access": "public",
29+
"main": "dist/index.js",
30+
"typings": "dist/index.d.ts"
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { FlowError } from '@ory/client'
2+
import type { ReactElement } from 'react'
3+
4+
import { useError } from '../hooks'
5+
6+
export interface ErrorNodeProps {
7+
children: (node: FlowError) => ReactElement
8+
}
9+
10+
export const ErrorNode = ({ children }: ErrorNodeProps): ReactElement | null => {
11+
const { error } = useError()
12+
13+
if (error && typeof children === 'function') {
14+
return children(error)
15+
}
16+
17+
return null
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { UiNodeImageAttributes } from '@ory/client'
2+
import type { UiNode } from '@ory/client'
3+
import type { ReactElement } from 'react'
4+
5+
import { UiNodeTypeEnum } from '@ory/client'
6+
7+
import { useFlowNode } from '../hooks'
8+
9+
export interface FlowUiImageNode extends Omit<UiNode, 'attributes'> {
10+
attributes: UiNodeImageAttributes
11+
}
12+
13+
export interface FlowImageNodeProps {
14+
name: string
15+
children: (node: FlowUiImageNode) => ReactElement
16+
}
17+
18+
export const FlowImageNode = ({ name, children }: FlowImageNodeProps): ReactElement | null => {
19+
const node = useFlowNode(name)
20+
21+
if (node && node.type === UiNodeTypeEnum.Img && typeof children === 'function') {
22+
return children(node as FlowUiImageNode)
23+
}
24+
25+
return null
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { UiNodeInputAttributes } from '@ory/client'
2+
import type { UiNode } from '@ory/client'
3+
import type { ReactElement } from 'react'
4+
5+
import { UiNodeTypeEnum } from '@ory/client'
6+
import { useEffect } from 'react'
7+
import { useCallback } from 'react'
8+
9+
import { useFlowNode } from '../hooks'
10+
import { useValue } from '../hooks'
11+
12+
type OnChangeCallback = (event: Date | boolean | number | string) => void
13+
14+
export interface FlowUiInputNode extends Omit<UiNode, 'attributes'> {
15+
attributes: UiNodeInputAttributes
16+
}
17+
18+
export interface FlowInputNodeProps {
19+
name: string
20+
defaultValue?: string
21+
children: (node: FlowUiInputNode, value: string, callback: OnChangeCallback) => ReactElement
22+
}
23+
24+
export const FlowInputNode = ({
25+
name,
26+
defaultValue,
27+
children,
28+
}: FlowInputNodeProps): ReactElement | null => {
29+
const node = useFlowNode(name)
30+
const [value, setValue] = useValue(name)
31+
32+
useEffect(() => {
33+
if (!value && defaultValue) {
34+
setValue(defaultValue)
35+
}
36+
}, [defaultValue]) // eslint-disable-line react-hooks/exhaustive-deps
37+
38+
const onChange = useCallback(
39+
(event: Date | boolean | number | string) => {
40+
setValue(event)
41+
},
42+
[setValue]
43+
)
44+
45+
if (node && node.type === UiNodeTypeEnum.Input && typeof children === 'function') {
46+
return children(node as FlowUiInputNode, value, onChange)
47+
}
48+
49+
return null
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { UiText } from '@ory/client'
2+
import type { ReactElement } from 'react'
3+
4+
import { useMemo } from 'react'
5+
6+
import { useFlow } from '../hooks'
7+
8+
export interface FlowMessagesProps {
9+
children: (messages: Array<UiText>) => ReactElement
10+
}
11+
12+
export const FlowMessages = ({ children }: FlowMessagesProps): ReactElement | null => {
13+
const { flow } = useFlow()
14+
const messages = useMemo(() => flow?.ui?.messages || [], [flow])
15+
16+
if (typeof children === 'function' && messages) {
17+
return children(messages)
18+
}
19+
20+
return null
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { UiText } from '@ory/client'
2+
import type { ReactElement } from 'react'
3+
4+
import { useFlowNode } from '../hooks'
5+
6+
export interface FlowNodeMessagesProps {
7+
name: string
8+
children: (messages: Array<UiText>) => ReactElement
9+
}
10+
11+
export const FlowNodeMessages = ({
12+
name,
13+
children,
14+
}: FlowNodeMessagesProps): ReactElement | null => {
15+
const node = useFlowNode(name)
16+
17+
if (typeof children === 'function' && node?.messages) {
18+
return children(node.messages)
19+
}
20+
21+
return null
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { UiNode } from '@ory/client'
2+
import type { ReactElement } from 'react'
3+
4+
import { useCallback } from 'react'
5+
6+
import { useFlowNode } from '../hooks'
7+
import { useValue } from '../hooks'
8+
9+
type OnChangeCallback = (event: Date | boolean | number | string) => void
10+
11+
export interface FlowNodeProps {
12+
name: string
13+
children: (node: UiNode, value: string, callback: OnChangeCallback) => ReactElement
14+
}
15+
16+
export const FlowNode = ({ name, children }: FlowNodeProps): ReactElement | null => {
17+
const node = useFlowNode(name)
18+
const [value, setValue] = useValue(name)
19+
20+
const onChange = useCallback(
21+
(event: Date | boolean | number | string) => {
22+
setValue(event)
23+
},
24+
[setValue]
25+
)
26+
27+
if (node && typeof children === 'function') {
28+
return children(node, value, onChange)
29+
}
30+
31+
return null
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { UiNode } from '@ory/client'
2+
import type { ReactElement } from 'react'
3+
4+
import { useMemo } from 'react'
5+
6+
import { useFlow } from '../hooks'
7+
8+
export type FlowNodesFilterChildren = (node: Array<UiNode>) => ReactElement
9+
10+
export interface FlowNodesFilterProps {
11+
predicate: (node: UiNode) => boolean
12+
children: FlowNodesFilterChildren | ReactElement
13+
}
14+
15+
export const FlowNodesFilter = ({
16+
predicate,
17+
children,
18+
}: FlowNodesFilterProps): ReactElement | null => {
19+
const { flow } = useFlow()
20+
21+
const nodes = useMemo(() => flow?.ui?.nodes?.filter(predicate), [flow, predicate])
22+
23+
if (!(nodes && nodes.length > 0)) {
24+
return null
25+
}
26+
27+
if (typeof children === 'function') {
28+
return children(nodes)
29+
}
30+
31+
return children
32+
}

0 commit comments

Comments
 (0)