Skip to content
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

Bugfix #50

Merged
merged 6 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@
"blind-signatures",
"lightning-network"
],
"version": "0.0.2-alpha",
"version": "0.0.3-alpha",
"license": "AGPL-3.0-only",
"bugs": {
"url": "https://github.com/cashubtc/eNuts/issues"
},
"main": "src/AppEntry.ts",
"private": true
}
}
52 changes: 24 additions & 28 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Bugsnag from '@bugsnag/expo'
import { getEncodedToken } from '@cashu/cashu-ts'
import Button from '@comps/Button'
import useLoading from '@comps/hooks/Loading'
import usePrompt from '@comps/hooks/Prompt'
import { env } from '@consts'
import { addAllMintIds, getBalance, getContacts, getMintsBalances, getMintsUrls, getPreferences, initDb, setPreferences } from '@db'
import { fsInfo } from '@db/fs'
import { l } from '@log'
import MyModal from '@modal'
import { PromptModal } from '@modal/Prompt'
import { IInitialProps, IPreferences, ITokenInfo } from '@model'
import { DrawerNav } from '@nav/Navigator'
import { NavigationContainer } from '@react-navigation/native'
Expand All @@ -30,6 +28,7 @@ import { AppState, Text, View } from 'react-native'

import { CustomErrorBoundary } from './ErrorScreen/ErrorBoundary'
import { ErrorDetails } from './ErrorScreen/ErrorDetails'
import Toaster from './Toaster'
import Txt from './Txt'

initCrashReporting()
Expand Down Expand Up @@ -64,8 +63,7 @@ export default function App(_initialProps: IInitialProps) {
const [, setAppStateVisible] = useState(appState.current)
const [tokenInfo, setTokenInfo] = useState<ITokenInfo | undefined>()
const [claimOpen, setClaimOpen] = useState(false)
const { prompt, openPrompt, closePrompt } = usePrompt()
const { loading, startLoading, stopLoading } = useLoading()
const { prompt, openPromptAutoClose } = usePrompt()

const handleForeground = async () => {
// TODO immediatly reading clipboard after the app comes to the foreground can result
Expand Down Expand Up @@ -100,18 +98,18 @@ export default function App(_initialProps: IInitialProps) {
}

const handleRedeem = async () => {
startLoading()
// startLoading()
if (!tokenInfo) { return }
setClaimOpen(false)
const encoded = getEncodedToken(tokenInfo.decoded)
const success = await claimToken(encoded).catch(l)
if (!success) {
alert('Token invalid or already claimed')
setClaimOpen(false)
openPromptAutoClose(false, 'Token invalid or already claimed')
return
}
const info = getTokenInfo(encoded)
if (!info) {
l('Error while getting token info')
openPromptAutoClose(false, 'Error while getting token info')
return
}
// add as history entry
Expand All @@ -121,10 +119,8 @@ export default function App(_initialProps: IInitialProps) {
value: encoded,
mints: info.mints,
})
openPrompt(`Successfully claimed ${formatInt(info.value)} Satoshi!`)
openPromptAutoClose(true, `Successfully claimed ${formatInt(info.value)} Satoshi!`)
setClaimed(true)
stopLoading()
setClaimOpen(false)
}

// update theme
Expand Down Expand Up @@ -245,11 +241,11 @@ export default function App(_initialProps: IInitialProps) {

return (
<ThemeContext.Provider value={themeData}>
<BugSnagErrorBoundary>
<FocusClaimCtx.Provider value={claimData}>
<ContactsContext.Provider value={contactData}>
<KeyboardProvider>
<NavigationContainer theme={theme === 'Light' ? light : dark}>
<NavigationContainer theme={theme === 'Light' ? light : dark}>
<BugSnagErrorBoundary>
<FocusClaimCtx.Provider value={claimData}>
<ContactsContext.Provider value={contactData}>
<KeyboardProvider>
<DrawerNav />
<StatusBar style="auto" />
{/* claim token if app comes to foreground and clipboard has valid cashu token */}
Expand All @@ -268,7 +264,7 @@ export default function App(_initialProps: IInitialProps) {
{tokenInfo?.mints.join(', ')}
</Text>
<Button
txt={loading ? 'Claiming...' : 'Claim now!'}
txt='Claim now!'
onPress={() => void handleRedeem()}
/>
<View style={{ marginVertical: 10 }} />
Expand All @@ -279,17 +275,17 @@ export default function App(_initialProps: IInitialProps) {
/>
</MyModal>
}
<PromptModal
hideIcon
header={prompt.msg}
visible={prompt.open}
close={closePrompt}
/>
</NavigationContainer>
</KeyboardProvider>
</ContactsContext.Provider>
</FocusClaimCtx.Provider>
</BugSnagErrorBoundary>
{prompt.open &&
<Toaster
success
txt={prompt.msg}
/>
}
</KeyboardProvider>
</ContactsContext.Provider>
</FocusClaimCtx.Provider>
</BugSnagErrorBoundary>
</NavigationContainer>
</ThemeContext.Provider>
)
}
29 changes: 14 additions & 15 deletions src/components/pages/History/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import Txt from '@comps/Txt'
import type { THistoryEntryPageProps } from '@model/nav'
import TopNav from '@nav/TopNav'
import { ThemeContext } from '@src/context/Theme'
import { historyStore } from '@store'
import { mainColors } from '@styles'
import { formatInt, formatMintUrl, getLnInvoiceInfo } from '@util'
import { formatInt, formatMintUrl, getLnInvoiceInfo, isUndef } from '@util'
import { isTokenSpendable } from '@wallet'
import * as Clipboard from 'expo-clipboard'
import { useContext, useEffect, useState } from 'react'
import { useContext, useState } from 'react'
import { StyleSheet, Text, View } from 'react-native'
import { TouchableOpacity } from 'react-native-gesture-handler'

Expand All @@ -23,12 +24,12 @@ const initialCopyState = {
}

export default function DetailsPage({ route }: THistoryEntryPageProps) {
const entry = route.params.entry
const { color } = useContext(ThemeContext)
const [copy, setCopy] = useState(initialCopyState)
const [isSpent, setIsSpent] = useState(false)
const [isSpent, setIsSpent] = useState(entry.isSpent)
const { loading, startLoading, stopLoading } = useLoading()
const [qr, setQr] = useState({ open: false, error: false })
const entry = route.params.entry
const isPayment = entry.amount < 0
const isLn = entry.type === 2
const LNstr = `Lightning ${isPayment ? 'payment' : 'invoice'}`
Expand Down Expand Up @@ -59,19 +60,15 @@ export default function DetailsPage({ route }: THistoryEntryPageProps) {
const handleCheckSpendable = async () => {
if (isSpent || loading) { return }
startLoading()
setIsSpent(!(await isTokenSpendable(entry.value)))
const isSpendable = await isTokenSpendable(entry.value)
setIsSpent(!isSpendable)
// update history item
await historyStore.updateHistoryEntry(entry, { ...entry, isSpent: !isSpendable })
stopLoading()
}
const handleQR = () => {
setQr({ ...qr, open: true })
}
// initial check is token spent
useEffect(() => {
if (isLn) { return }
void (async () => {
setIsSpent(!(await isTokenSpendable(entry.value)))
})()
}, [isLn, entry.value])
return (
<View style={[styles.container, { backgroundColor: color.BACKGROUND }]}>
<TopNav withBackBtn />
Expand Down Expand Up @@ -142,14 +139,16 @@ export default function DetailsPage({ route }: THistoryEntryPageProps) {
isSpent={isSpent}
handleCheckSpendable={() => void handleCheckSpendable()}
>
<Txt txt={`Token ${isSpent ? 'has been spent' : 'is pending...'}`} />
<Txt
txt={isUndef(isSpent) ? 'Check if token has been spent' : `Token ${isSpent ? 'has been spent' : 'is pending...'}`}
/>
{isSpent ?
<CheckCircleIcon width={18} height={18} color={mainColors.VALID} />
:
loading ?
<Txt txt='Loading...' />
:
<BackupIcon width={18} height={18} color={color.TEXT} />
<BackupIcon width={20} height={20} color={color.TEXT} />
}
</IsSpentContainer>
}
Expand Down Expand Up @@ -249,7 +248,7 @@ export default function DetailsPage({ route }: THistoryEntryPageProps) {
}

interface IIsSpentContainerProps {
isSpent: boolean,
isSpent?: boolean,
handleCheckSpendable: () => void
children: React.ReactNode
}
Expand Down
3 changes: 2 additions & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export interface IHistoryEntry {
value: string
mints: string[]
preImage?: string,
fee?: number
fee?: number,
isSpent?: boolean
}


Expand Down