@@ -4,6 +4,7 @@ import React, { useState } from 'react';
44import { useCookies } from 'react-cookie' ;
55import { shallowEqual , useSelector } from 'react-redux' ;
66
7+ import getConfig from 'next/config' ;
78import { useRouter } from 'next/router' ;
89
910import {
@@ -20,19 +21,25 @@ import ShowIf from '../show-if.component';
2021import { useStyles } from './SendTipButton.style' ;
2122
2223import { COOKIE_INSTANCE_URL } from 'components/SelectServer' ;
24+ import * as nearAPI from 'near-api-js' ;
2325import { PromptComponent } from 'src/components/atoms/Prompt/prompt.component' ;
2426import { useUserHook } from 'src/hooks/use-user.hook' ;
2527import { Comment } from 'src/interfaces/comment' ;
2628import { ReferenceType } from 'src/interfaces/interaction' ;
29+ import { NetworkIdEnum } from 'src/interfaces/network' ;
2730import { People } from 'src/interfaces/people' ;
2831import { Post } from 'src/interfaces/post' ;
29- import { User } from 'src/interfaces/user' ;
32+ import { User , Wallet } from 'src/interfaces/user' ;
3033import { WalletDetail } from 'src/interfaces/wallet' ;
3134import * as CommentAPI from 'src/lib/api/comment' ;
3235import * as PostAPI from 'src/lib/api/post' ;
3336import * as UserAPI from 'src/lib/api/user' ;
37+ import { Near } from 'src/lib/services/near-api-js' ;
3438import i18n from 'src/locale' ;
3539import { RootState } from 'src/reducers' ;
40+ import { UserState } from 'src/reducers/user/reducer' ;
41+
42+ const { publicRuntimeConfig } = getConfig ( ) ;
3643
3744type SendTipButtonProps = ButtonProps & {
3845 label ?: string ;
@@ -66,16 +73,23 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
6673 const [ promptFailedTip , setPromptFailedTip ] = useState ( false ) ;
6774 const [ tipInfoOpened , setTipInfoOpened ] = useState ( false ) ;
6875 const [ prompWeb2Users , setPrompWeb2Users ] = useState ( false ) ;
76+ const [ promptNearConnection , setPromptNearConnection ] = useState ( false ) ;
6977
7078 const { wallets } = user || { wallets : [ ] } ;
79+ console . log ( 'wallet' , wallets ) ;
7180
72- const anonymous = useSelector < RootState , boolean > (
73- state => state . userState . anonymous ,
81+ const { anonymous, networks } = useSelector < RootState , UserState > (
82+ state => state . userState ,
7483 shallowEqual ,
7584 ) ;
7685
7786 const isWeb2Users = ! wallets . length && ! anonymous ;
7887
88+ const isNear =
89+ wallets . length && ( wallets [ 0 ] as Wallet ) . blockchainPlatform === 'near' ;
90+
91+ console . log ( 'is near' , isNear ) ;
92+
7993 const icon = (
8094 < SvgIcon
8195 color = "inherit"
@@ -92,6 +106,10 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
92106 setPrompWeb2Users ( false ) ;
93107 } ;
94108
109+ const handleCloseNearWallet = ( ) => {
110+ setPromptNearConnection ( false ) ;
111+ } ;
112+
95113 const handleConnectWeb3Wallet = ( ) => {
96114 router . push ( `/wallet?type=manage` ) ;
97115 } ;
@@ -118,6 +136,20 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
118136 return ;
119137 }
120138
139+ if ( isNear ) {
140+ const network = networks . find (
141+ network => network . id === NetworkIdEnum . NEAR ,
142+ ) ;
143+
144+ if ( ! network ) return ;
145+ const near = await Near . connect ( network ) ;
146+ const wallet = near . provider . wallet ;
147+ if ( ! wallet . isSignedIn ( ) ) {
148+ setPromptNearConnection ( true ) ;
149+ return ;
150+ }
151+ }
152+
121153 try {
122154 // if tipping to User
123155 if ( 'username' in reference ) {
@@ -180,9 +212,53 @@ export const SendTipButton: React.FC<SendTipButtonProps> = props => {
180212 </ Button >
181213
182214 < PromptComponent
183- icon = "tip"
184- title = { i18n . t ( 'Confirm.Anonymous.SendTip.Title' ) }
185- subtitle = { i18n . t ( 'Confirm.Anonymous.SendTip.Desc' ) }
215+ icon = "warning"
216+ title = { i18n . t ( 'Tipping.Prompt_Near.Title' ) }
217+ open = { promptNearConnection }
218+ subtitle = { '' }
219+ onCancel = { handleCloseNearWallet } >
220+ < div className = { styles . wrapperButtonFlex } >
221+ < Button
222+ variant = "outlined"
223+ color = "secondary"
224+ onClick = { handleCloseNearWallet } >
225+ { i18n . t ( 'Tipping.Prompt_Near.Btn.Left' ) }
226+ </ Button >
227+ < Button
228+ variant = "contained"
229+ color = "primary"
230+ onClick = { async ( ) => {
231+ const network = networks . find (
232+ network => network . id === NetworkIdEnum . NEAR ,
233+ ) ;
234+
235+ if ( ! network ) return ;
236+ const near = await Near . connect ( network ) ;
237+ const wallet = near . provider . wallet ;
238+
239+ const keyStore =
240+ new nearAPI . keyStores . BrowserLocalStorageKeyStore ( ) ;
241+ const signInOptions = {
242+ contractId : publicRuntimeConfig . nearTippingContractId ,
243+ methodNames : [ 'claim_tip' , 'batch_claim_tips' ] ,
244+ successUrl : publicRuntimeConfig . appAuthURL + router . asPath ,
245+ failureUrl : publicRuntimeConfig . appAuthURL + router . asPath ,
246+ } ;
247+
248+ await Promise . all ( [
249+ keyStore . clear ( ) ,
250+ wallet . requestSignIn ( signInOptions ) ,
251+ ] ) ;
252+ } } >
253+ { i18n . t ( 'Tipping.Prompt_Near.Btn.Right' ) }
254+ </ Button >
255+ </ div >
256+ </ PromptComponent >
257+
258+ < PromptComponent
259+ icon = "warning"
260+ title = { i18n . t ( 'Tipping.Prompt_Mobile.Title' ) }
261+ subtitle = { i18n . t ( 'Tipping.Prompt_Mobile.Subtitle' ) }
186262 open = { tipInfoOpened }
187263 onCancel = { handleCloseTipInfo } >
188264 < div className = { styles . wrapperButtonFlex } >
0 commit comments