-
Notifications
You must be signed in to change notification settings - Fork 16
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
react hooks #1687
Draft
turbocrime
wants to merge
1
commit into
main
Choose a base branch
from
turbocrime/react2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
react hooks #1687
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,57 @@ | ||
{ | ||
"name": "@penumbra-zone/react", | ||
"version": "1.2.0", | ||
"license": "(MIT OR Apache-2.0)", | ||
"description": "React package for connecting to any Penumbra extension, including Prax.", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc --build --verbose", | ||
"clean": "rm -rfv dist *.tsbuildinfo package penumbra-zone-*.tgz", | ||
"dev:pack": "tsc-watch --onSuccess \"$npm_execpath pack\"", | ||
"lint": "eslint src", | ||
"lint:fix": "eslint src --fix", | ||
"lint:strict": "tsc --noEmit && eslint src --max-warnings 0" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./components/*": "./src/components/*.tsx", | ||
"./hooks/*": "./src/hooks/*.ts" | ||
}, | ||
"publishConfig": { | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"./components/*": { | ||
"types": "./dist/components/*.d.ts", | ||
"default": "./dist/components/*.js" | ||
}, | ||
"./hooks/*": { | ||
"types": "./dist/hooks/*.d.ts", | ||
"default": "./dist/hooks/*.js" | ||
} | ||
} | ||
}, | ||
"dependencies": { | ||
"@penumbra-zone/client": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@bufbuild/protobuf": "^1.10.0", | ||
"@connectrpc/connect": "^1.4.0", | ||
"@penumbra-zone/transport-dom": "workspace:*", | ||
"@tanstack/react-query": "5.51.23", | ||
"@types/react": "^18.3.2", | ||
"react": "^18.3.1" | ||
}, | ||
"peerDependencies": { | ||
"@bufbuild/protobuf": "^1.10.0", | ||
"@connectrpc/connect": "^1.4.0", | ||
"@penumbra-zone/protobuf": "workspace:*", | ||
"@penumbra-zone/transport-dom": "workspace:*", | ||
"react": "^18.3.1" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/react/src/components/penumbra-context-provider.tsx
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,45 @@ | ||
import { penumbraContext } from '../context/penumbra-context.js'; | ||
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { | ||
PenumbraContextInput, | ||
resolvePenumbraContextInput, | ||
} from '../context/penumbra-context-input.js'; | ||
import { PenumbraEventDetail } from '@penumbra-zone/client'; | ||
|
||
export const PenumbraContextProvider = ({ | ||
client, | ||
provider, | ||
children, | ||
}: PenumbraContextInput & { children?: ReactNode }) => { | ||
const penumbra = useMemo( | ||
() => resolvePenumbraContextInput({ client, provider }), | ||
[client, provider], | ||
); | ||
|
||
const [providerState, setProviderState] = useState(penumbra.state); | ||
const [providerConnected, setProviderConnected] = useState(penumbra.connected); | ||
|
||
useEffect(() => { | ||
console.log('setting providerConnected', providerState, penumbra.connected); | ||
setProviderConnected(penumbra.connected); | ||
}, [penumbra, providerState]); | ||
|
||
const listener = useCallback((update: PenumbraEventDetail<'penumbrastate'>) => { | ||
console.log('listener', update); | ||
setProviderState(update.state); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const ac = new AbortController(); | ||
penumbra.onConnectionStateChange(listener, ac.signal); | ||
return () => ac.abort(); | ||
}, [penumbra, listener]); | ||
|
||
console.log('providerConnected', providerConnected); | ||
|
||
return ( | ||
<penumbraContext.Provider value={providerConnected ? penumbra : penumbra}> | ||
{children} | ||
</penumbraContext.Provider> | ||
); | ||
}; |
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,25 @@ | ||
import { PenumbraClient, PenumbraProvider } from '@penumbra-zone/client'; | ||
import { PenumbraClientOptions } from '@penumbra-zone/client/client'; | ||
|
||
export interface PenumbraContextInput { | ||
client?: PenumbraClient | PenumbraClientOptions; | ||
provider?: PenumbraProvider | string; | ||
} | ||
|
||
export const resolvePenumbraContextInput = (input: PenumbraContextInput): PenumbraClient => { | ||
let client = input.client instanceof PenumbraClient ? input.client : undefined; | ||
const options = input.client instanceof PenumbraClient ? undefined : input.client; | ||
|
||
const providerOrigin = | ||
typeof input.provider === 'string' | ||
? input.provider | ||
: input.provider && new URL(input.provider.manifest).origin; | ||
|
||
client ??= new PenumbraClient(providerOrigin, options); | ||
|
||
if (providerOrigin) { | ||
void client.attach(providerOrigin); | ||
} | ||
|
||
return client; | ||
}; |
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,4 @@ | ||
import { PenumbraClient } from '@penumbra-zone/client/client'; | ||
import { createContext } from 'react'; | ||
|
||
export const penumbraContext = createContext(new PenumbraClient()); |
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,60 @@ | ||
import { ServiceType } from '@bufbuild/protobuf'; | ||
import { usePenumbra } from './use-penumbra.js'; | ||
import { QueryOptions, useQuery, UseQueryResult } from '@tanstack/react-query'; | ||
import { PromiseClient } from '@connectrpc/connect'; | ||
|
||
export const usePenumbraQuery = <S extends ServiceType>( | ||
serviceType: S, | ||
): PenumbraQuerier<S> | undefined => { | ||
const penumbra = usePenumbra(); | ||
|
||
if (!penumbra.transport) { | ||
return; | ||
} | ||
|
||
const wrappedMethods = Object.keys(serviceType.methods).map( | ||
<N extends keyof S['methods']>(methodName: N) => { | ||
const serviceClient = penumbra.service(serviceType); | ||
|
||
const queryFn: QueryOptions['queryFn'] = ({ meta }) => { | ||
const { | ||
params: [input, options], | ||
} = meta as { params: Parameters<PromiseClient<S>[N]> }; | ||
const response = serviceClient[methodName](input as never, options); | ||
|
||
if (Symbol.asyncIterator in response) { | ||
return Array.fromAsync(response); | ||
} else { | ||
return response; | ||
} | ||
}; | ||
|
||
const useMethodQuery: PenumbraQuerierMethod<S, N> = (queryOptions, ...params) => | ||
useQuery({ | ||
...queryOptions, | ||
queryKey: [serviceType.typeName, methodName, params], | ||
queryFn, | ||
meta: { params }, | ||
}); | ||
|
||
return [methodName, useMethodQuery] as const; | ||
}, | ||
); | ||
|
||
return Object.fromEntries(wrappedMethods) as PenumbraQuerier<S>; | ||
}; | ||
|
||
type PromiseClientMethod<S extends ServiceType, M extends keyof S['methods']> = PromiseClient<S>[M]; | ||
|
||
type PenumbraQuerierMethod<S extends ServiceType, M extends keyof S['methods']> = ( | ||
queryOptions: Omit<QueryOptions, 'queryFn' | 'queryKey' | 'meta'>, | ||
...args: Parameters<PromiseClientMethod<S, M>> | ||
) => UsePenumbraQueryResult<S, M>; | ||
|
||
type UsePenumbraQueryResult<S extends ServiceType, M extends keyof S['methods']> = UseQueryResult< | ||
ReturnType<PromiseClientMethod<S, M>> | ||
>; | ||
|
||
type PenumbraQuerier<S extends ServiceType> = { | ||
[localName in keyof S['methods']]: PenumbraQuerierMethod<S, localName>; | ||
}; |
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,15 @@ | ||
import { PromiseClient } from '@connectrpc/connect'; | ||
import { useMemo } from 'react'; | ||
import { usePenumbra } from './use-penumbra.js'; | ||
import { ServiceType } from '@bufbuild/protobuf'; | ||
|
||
export const usePenumbraService = <S extends ServiceType>( | ||
serviceType: S, | ||
): PromiseClient<S> | undefined => { | ||
const penumbra = usePenumbra(); | ||
const connected = penumbra.connected; | ||
return useMemo( | ||
() => (connected ? penumbra.service(serviceType) : undefined), | ||
[connected, penumbra, serviceType], | ||
); | ||
}; |
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,4 @@ | ||
import { useContext } from 'react'; | ||
import { penumbraContext } from '../context/penumbra-context.js'; | ||
|
||
export const usePenumbra = () => useContext(penumbraContext); |
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,5 @@ | ||
export { usePenumbra } from './hooks/use-penumbra.js'; | ||
export { usePenumbraQuery } from './hooks/use-penumbra-query.js'; | ||
export { usePenumbraService } from './hooks/use-penumbra-service.js'; | ||
export { penumbraContext } from './context/penumbra-context.js'; | ||
export { PenumbraContextProvider } from './components/penumbra-context-provider.js'; |
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,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"exactOptionalPropertyTypes": false, | ||
"composite": true, | ||
"jsx": "react-jsx", | ||
"module": "Node16", | ||
"outDir": "dist", | ||
"preserveWatchOutput": true, | ||
"rootDir": "src", | ||
"target": "ESNext" | ||
}, | ||
"extends": "@tsconfig/strictest/tsconfig.json", | ||
"include": ["src"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably the value of the context should be a reactive object similar to
PenumbraClient
but without static methods,service
,onConnectionStateChange
and others non-needed functions replaced by hooks