-
Notifications
You must be signed in to change notification settings - Fork 6
React Client #1
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
Open
marsian83
wants to merge
15
commits into
TalentLayer:develop
Choose a base branch
from
marsian83:develop
base: develop
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.
Open
React Client #1
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c89c2e0
Initial setup for React client
marsian83 2580d84
Added talentLayerContext, a provider for it and custom hook to call it
marsian83 602f4a7
added exports via index.ts
marsian83 e4f14b3
Merge branch 'TalentLayer:develop' into develop
marsian83 a4dc6e1
Merge branch 'TalentLayer:develop' into develop
marsian83 c1bda0e
Merge branch 'TalentLayer:develop' into develop
marsian83 2fb5b40
React SDK - initial hooks and context
marsian83 9a83c34
Merge branch 'TalentLayer:develop' into develop
marsian83 11eef43
Merge branch 'TalentLayer:develop' into develop
marsian83 51b903f
React SDK changes
marsian83 a575aa2
change license
marsian83 ad2a622
remove unused dependancies
marsian83 c1a0dd7
renamed tsx hook to .ts
marsian83 d26fc36
fix reinitialization of client every render
marsian83 27dc211
Use new client to remove queries from
eact
marsian83 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 hidden or 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,27 @@ | ||
| { | ||
| "name": "@talentlayer/react", | ||
| "version": "0.0.1", | ||
| "description": "The TalentLayer Client for React with abstractions for easy interaction with the TalentLayer protocol", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "license": "Apache-2.0", | ||
| "private": false, | ||
| "scripts": { | ||
| "lint": "eslint .", | ||
| "prepare": "npm run build", | ||
| "build": "tsc" | ||
| }, | ||
| "devDependencies": { | ||
| "@turbo/gen": "^1.10.12", | ||
| "@types/node": "^20.6.0", | ||
| "eslint-config-custom": "*", | ||
| "tsconfig": "*", | ||
| "typescript": "^5.2.2" | ||
| }, | ||
| "dependencies": { | ||
| "@talentlayer/client": "^0.1.7", | ||
| "net": "^1.0.2", | ||
| "react": "^18.2.0", | ||
| "wagmi": "^1.4.2" | ||
| } | ||
| } |
This file contains hidden or 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,22 @@ | ||
| import React from 'react'; | ||
|
|
||
| interface TalentLayerErrorProps { | ||
| error: any; | ||
| } | ||
|
|
||
| export default function TalentLayerError(props: TalentLayerErrorProps) { | ||
| const { error } = props; | ||
|
|
||
| return ( | ||
| <div style={{ zIndex: Infinity, background: '#000000', color: '#ffffff' }}> | ||
| <h1>TalentLayer Error</h1> | ||
| {(() => { | ||
| try { | ||
| return JSON.stringify(error, null, 4); | ||
| } catch { | ||
| return `Error : ${error}`; | ||
| } | ||
| })()} | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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,116 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { ContextType } from 'react'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to import react here? |
||
| import { createContext, ReactNode, useEffect, useMemo, useState } from 'react'; | ||
| import { useAccount } from 'wagmi'; | ||
| import { IAccount, IUser, NetworkEnum } from '../types'; | ||
| import { TalentLayerClient } from '@talentlayer/client'; | ||
|
|
||
| interface TalentLayerProviderProps { | ||
| children: ReactNode; | ||
| config: ConstructorParameters<typeof TalentLayerClient>[0] & { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anyway to simplify the config here? |
||
| account: ReturnType<typeof useAccount>; | ||
| }; | ||
| } | ||
|
|
||
| export interface Subgraph { | ||
| query: (query: string) => any; | ||
| } | ||
|
|
||
| const TalentLayerContext = createContext<{ | ||
| user?: IUser; | ||
| account?: IAccount; | ||
| refreshData: () => Promise<boolean>; | ||
| loading: boolean; | ||
| client: TalentLayerClient | undefined; | ||
| platformId: number; | ||
| chainId?: NetworkEnum; | ||
| subgraph: Subgraph; | ||
| }>({ | ||
| user: undefined, | ||
| account: undefined, | ||
| refreshData: async () => { | ||
| return false; | ||
| }, | ||
| loading: true, | ||
| client: {} as TalentLayerClient, | ||
| platformId: -1, | ||
| chainId: undefined, | ||
| subgraph: { query: _ => new Promise(resolve => resolve(null)) }, | ||
| }); | ||
|
|
||
| export function TalentLayerProvider(props: TalentLayerProviderProps) { | ||
| const { children, config } = props; | ||
| const { chainId, platformId } = config; | ||
|
|
||
| const account = config.account; | ||
|
|
||
| const [user, setUser] = useState<IUser | undefined>(); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| const [talentLayerClient, setTalentLayerClient] = useState<TalentLayerClient>(); | ||
|
|
||
| async function loadData() { | ||
| if (!talentLayerClient) return false; | ||
|
|
||
| if (!account.address || !account.isConnected) { | ||
| setLoading(false); | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| const userResponse = await talentLayerClient.profile.getByAddress(account.address); | ||
|
|
||
| const currentUser = userResponse; | ||
|
|
||
| const platformResponse = await talentLayerClient.platform.getOne( | ||
| config.platformId.toString(), | ||
| ); | ||
|
|
||
| const platform = platformResponse; | ||
| currentUser.isAdmin = platform?.address === currentUser?.address; | ||
|
|
||
| setUser(currentUser); | ||
|
|
||
| return true; | ||
| } catch (err: any) { | ||
| console.error(err); | ||
|
|
||
| return false; | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| if (chainId && account.address && !talentLayerClient) { | ||
| const tlClient = new TalentLayerClient(config); | ||
| setTalentLayerClient(tlClient); | ||
| } | ||
| }, [chainId, account.address]); | ||
|
|
||
| useEffect(() => { | ||
| if (!talentLayerClient) return; | ||
|
|
||
| loadData(); | ||
| }, [talentLayerClient]); | ||
|
|
||
| const value = useMemo<ContextType<typeof TalentLayerContext>>(() => { | ||
| return { | ||
| user, | ||
| account: account ? account : undefined, | ||
| refreshData: loadData, | ||
| loading, | ||
| client: talentLayerClient, | ||
| chainId, | ||
| platformId, | ||
| subgraph: { | ||
| query: (query: string) => (talentLayerClient as TalentLayerClient).graphQlClient.get(query), | ||
| }, | ||
| } as any; | ||
| }, [account.address, user?.id, loading]); | ||
|
|
||
| return <TalentLayerContext.Provider value={value}>{children}</TalentLayerContext.Provider>; | ||
| } | ||
|
|
||
| export default TalentLayerContext; | ||
This file contains hidden or 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,49 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { IFees } from '../types'; | ||
| import useTalentLayer from './useTalentLayer'; | ||
|
|
||
| export default function useFees( | ||
| originServicePlatformId: string, | ||
| originValidatedProposalPlatformId: string, | ||
| ) { | ||
| const [fees, setFees] = useState({ | ||
| protocolEscrowFeeRate: 0, | ||
| originServiceFeeRate: 0, | ||
| originValidatedProposalFeeRate: 0, | ||
| }); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState<any>(null); | ||
|
|
||
| const talentLayer = useTalentLayer(); | ||
|
|
||
| async function loadData() { | ||
| setLoading(true); | ||
| const fees: IFees = { | ||
| protocolEscrowFeeRate: 0, | ||
| originServiceFeeRate: 0, | ||
| originValidatedProposalFeeRate: 0, | ||
| }; | ||
| try { | ||
| const response = await talentLayer.client?.escrow.getProtocolAndPlatformsFees(originServicePlatformId, originValidatedProposalPlatformId) | ||
|
|
||
| if (response) { | ||
| fees.protocolEscrowFeeRate = response.protocols[0].protocolEscrowFeeRate; | ||
| fees.originServiceFeeRate = response.servicePlatform.originServiceFeeRate; | ||
| fees.originValidatedProposalFeeRate = response.proposalPlatform.originValidatedProposalFeeRate; | ||
| } | ||
|
|
||
| setFees(fees); | ||
| } catch (error: any) { | ||
| console.error(error); | ||
| setError(error); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| loadData(); | ||
| }, [originServicePlatformId, originValidatedProposalPlatformId]); | ||
|
|
||
| return [fees, loading, error] as const; | ||
| } |
This file contains hidden or 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,36 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import useTalentLayer from './useTalentLayer'; | ||
|
|
||
| export default function useMintFee() { | ||
| const [mintFee, setMintFee] = useState(0); | ||
| const [shortHandlesMaxPrice, setShortHandlesMaxPrice] = useState(0); | ||
|
|
||
| const talentLayer = useTalentLayer(); | ||
|
|
||
| async function loadData() { | ||
| try { | ||
| const response = await talentLayer.client?.profile.getMintFees(); | ||
|
|
||
| if (response) { | ||
| const protocol = response.protocols[0]; | ||
| setMintFee(protocol.userMintFee); | ||
| setShortHandlesMaxPrice(protocol.shortHandlesMaxPrice); | ||
| } | ||
| } catch (err: any) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(err); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| loadData(); | ||
| }, [talentLayer.chainId]); | ||
|
|
||
| const calculateMintFee = (handle: string) => { | ||
| const length = handle.length; | ||
| const handlePrice = length > 4 ? mintFee : shortHandlesMaxPrice / Math.pow(2, length - 1); | ||
| return handlePrice; | ||
| }; | ||
|
|
||
| return { calculateMintFee }; | ||
| } |
This file contains hidden or 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,32 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { IPayment, PaymentTypeEnum } from '../types'; | ||
| import useTalentLayer from './useTalentLayer'; | ||
|
|
||
| export default function usePaymentsByService(id: string, paymentType?: PaymentTypeEnum) { | ||
| const [payments, setPayments] = useState<IPayment[]>([]); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(true); | ||
|
|
||
| const talentLayer = useTalentLayer(); | ||
|
|
||
| async function loadData() { | ||
| try { | ||
| const response = await talentLayer.client?.escrow.getByService(id) | ||
|
|
||
| if (response) { | ||
| setPayments(response); | ||
| } | ||
| } catch (error: any) { | ||
| console.error(error); | ||
| setError(error); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| loadData(); | ||
| }, [id]); | ||
|
|
||
| return [payments, loading, error] as const; | ||
| } |
This file contains hidden or 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,72 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { IPayment } from '../types'; | ||
| import useTalentLayer from './useTalentLayer'; | ||
|
|
||
| export default function usePaymentsForUser(options: { | ||
| id: string; | ||
| numberPerPage: number; | ||
| startDate?: string; | ||
| endDate?: string; | ||
| }) { | ||
| const { id, numberPerPage } = options; | ||
| const startDate = options.startDate; | ||
| const endDate = options.endDate; | ||
|
|
||
| const [payments, setPayments] = useState<IPayment[]>([]); | ||
| const [hasMoreData, setHasMoreData] = useState(true); | ||
| const [offset, setOffset] = useState(1); | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState<any>(null); | ||
|
|
||
| const talentLayer = useTalentLayer(); | ||
|
|
||
| const total = offset * numberPerPage; | ||
|
|
||
| const start = startDate ? new Date(startDate).getTime() / 1000 : ''; | ||
| const end = endDate ? new Date(endDate).getTime() / 1000 : ''; | ||
|
|
||
| async function loadData() { | ||
| if (!talentLayer.client) return; | ||
|
|
||
| setLoading(true); | ||
| try { | ||
| const response = await talentLayer.client?.profile.getPayments( | ||
| options.id, | ||
| options.numberPerPage, | ||
| offset, | ||
| options.startDate, | ||
| options.endDate, | ||
| ); | ||
|
|
||
| if (response) { | ||
| setPayments([...response]); | ||
|
|
||
| if (response.length < total) { | ||
| setHasMoreData(false); | ||
| } | ||
| } | ||
| } catch (error: any) { | ||
| console.error(error); | ||
| setError(error); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| loadData(); | ||
| }, [total, id, start, end]); | ||
|
|
||
| useEffect(() => { | ||
| if (!!start && !!end) { | ||
| setOffset(1); | ||
| setHasMoreData(true); | ||
| } | ||
| }, [start, end]); | ||
|
|
||
| function loadMore() { | ||
| setOffset(offset + 1); | ||
| } | ||
|
|
||
| return [{ items: payments, hasMoreData, loadMore } as const, loading, error] as const; | ||
| } |
This file contains hidden or 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,37 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { IPlatform } from '../types'; | ||
| import useTalentLayer from './useTalentLayer'; | ||
|
|
||
| export default function usePlatform(id?: string) { | ||
| const [platforms, setAddresses] = useState<IPlatform | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState<any>(null); | ||
|
|
||
| const talentLayer = useTalentLayer(); | ||
|
|
||
| async function loadData() { | ||
| setLoading(true); | ||
|
|
||
| if (!talentLayer.client) return; | ||
|
|
||
| try { | ||
| const response = await talentLayer.client.platform.getOne( | ||
| id || talentLayer.platformId.toString(), | ||
| ); | ||
|
|
||
| if (response) setAddresses(response); | ||
| else throw new Error('Unable to find platform'); | ||
| } catch (error: any) { | ||
| console.error(error); | ||
| setError(error); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| loadData(); | ||
| }, [id]); | ||
|
|
||
| return [platforms, loading, error] as const; | ||
| } |
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.
I am not seeing this component being used anywhere. Do we need it?