Skip to content

Commit

Permalink
Merge branch 'main' into feat/dark_theme
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Jul 17, 2024
2 parents 432c123 + 20b3fab commit 95b0a9d
Show file tree
Hide file tree
Showing 33 changed files with 410 additions and 103 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/joyboy-community.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,3 @@ jobs:

- name: TypeScript Check
run: yarn ts:check

- name: Build app bundle
run: yarn expo export --platform android
27 changes: 3 additions & 24 deletions JoyboyCommunity/src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export type MultiChainTokens = Record<TokenSymbol, MultiChainToken>;

export enum TokenSymbol {
ETH = 'ETH',
/* STRK = 'STRK',
JBY = 'JBY', */
STRK = 'STRK',
}

export const ETH: MultiChainToken = {
Expand All @@ -34,7 +33,7 @@ export const ETH: MultiChainToken = {
},
};

/* export const STRK: MultiChainToken = {
export const STRK: MultiChainToken = {
[constants.StarknetChainId.SN_MAIN]: {
name: 'Stark',
symbol: TokenSymbol.STRK,
Expand All @@ -53,29 +52,9 @@ export const ETH: MultiChainToken = {
},
};

export const JBY: MultiChainToken = {
[constants.StarknetChainId.SN_MAIN]: {
name: 'Joyboy',
symbol: TokenSymbol.JBY,
decimals: 18,
address: getChecksumAddress(
'0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
),
},
[constants.StarknetChainId.SN_SEPOLIA]: {
name: 'Joyboy',
symbol: TokenSymbol.JBY,
decimals: 18,
address: getChecksumAddress(
'0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
),
},
}; */

export const TOKENS: MultiChainTokens = {
[TokenSymbol.ETH]: ETH,
/* [TokenSymbol.STRK]: STRK,
[TokenSymbol.JBY]: JBY, */
[TokenSymbol.STRK]: STRK,
};

export const TOKEN_ADDRESSES: Record<
Expand Down
26 changes: 15 additions & 11 deletions JoyboyCommunity/src/context/NostrContext.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import NDK, {NDKPrivateKeySigner} from '@nostr-dev-kit/ndk';
import {createContext, useContext, useMemo} from 'react';
import {createContext, useContext, useEffect, useState} from 'react';

import {useAuth} from '../store/auth';
import {JOYBOY_RELAYS} from '../utils/relay';

export type NostrContextType = {
ndk: NDK;
relays: string[];
};

export const NostrContext = createContext<NostrContextType | null>(null);

export const NostrProvider: React.FC<React.PropsWithChildren> = ({children}) => {
const privateKey = useAuth((state) => state.privateKey);

const relays = JOYBOY_RELAYS;
const ndk = useMemo(() => {
const ndk = new NDK({
explicitRelayUrls: relays,
const [ndk, setNdk] = useState<NDK>(
new NDK({
explicitRelayUrls: JOYBOY_RELAYS,
}),
);

useEffect(() => {
const newNdk = new NDK({
explicitRelayUrls: JOYBOY_RELAYS,
signer: privateKey ? new NDKPrivateKeySigner(privateKey) : undefined,
});

ndk.connect();

return ndk;
}, [relays, privateKey]);
newNdk.connect().then(() => {
setNdk(newNdk);
});
}, [privateKey]);

return <NostrContext.Provider value={{ndk, relays}}>{children}</NostrContext.Provider>;
return <NostrContext.Provider value={{ndk}}>{children}</NostrContext.Provider>;
};

export const useNostrContext = () => {
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useContacts = (options?: UseContactsOptions) => {
const {ndk} = useNostrContext();

return useQuery({
queryKey: ['contacts', options?.authors, options?.search],
queryKey: ['contacts', ndk, options?.authors, options?.search],
queryFn: async () => {
const contacts = await ndk.fetchEvent({
kinds: [NDKKind.Contacts],
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useEditContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useEditContacts = () => {
const {publicKey} = useAuth();

return useMutation({
mutationKey: ['editContacts'],
mutationKey: ['editContacts', ndk],
mutationFn: async (data: {pubkey: string; type: 'add' | 'remove'}) => {
let contacts = await ndk.fetchEvent({
kinds: [NDKKind.Contacts],
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useEditProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useEditProfile = () => {
const {publicKey} = useAuth();

return useMutation({
mutationKey: ['editProfile'],
mutationKey: ['editProfile', ndk],
mutationFn: async (data: NDKUserProfile) => {
try {
const user = ndk.getUser({pubkey: publicKey});
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const useNote = (options: UseNoteOptions) => {
const {ndk} = useNostrContext();

return useQuery({
queryKey: ['note', options.noteId],
queryKey: ['note', ndk, options.noteId],
queryFn: async () => {
const note = await ndk.fetchEvent({
kinds: [NDKKind.Text],
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const useProfile = (options: UseProfileOptions) => {
const {ndk} = useNostrContext();

return useQuery({
queryKey: ['profile', options.publicKey],
queryKey: ['profile', ndk, options.publicKey],
queryFn: async () => {
const user = ndk.getUser({pubkey: options.publicKey});

Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useReact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useReact = () => {
const {ndk} = useNostrContext();

return useMutation({
mutationKey: ['react'],
mutationKey: ['react', ndk],
mutationFn: async (data: {event: NDKEvent; type: 'like' | 'dislike'}) => {
const event = new NDKEvent(ndk);
event.kind = NDKKind.Reaction;
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useReactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useReactions = (options?: UseReactionsOptions) => {
const {ndk} = useNostrContext();

return useQuery({
queryKey: ['reactions', options?.noteId, options?.authors, options?.search],
queryKey: ['reactions', ndk, options?.noteId, options?.authors, options?.search],
queryFn: async () => {
const notes = await ndk.fetchEvents({
kinds: [NDKKind.Reaction],
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useReplyNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useReplyNotes = (options?: UseReplyNotesOptions) => {

return useInfiniteQuery({
initialPageParam: 0,
queryKey: ['replyNotes', options?.noteId, options?.authors, options?.search],
queryKey: ['replyNotes', ndk, options?.noteId, options?.authors, options?.search],
getNextPageParam: (lastPage: any, allPages, lastPageParam) => {
if (!lastPage?.length) return undefined;

Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useReposts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useReposts = (options?: UseRepostsOptions) => {

return useInfiniteQuery({
initialPageParam: 0,
queryKey: ['reposts', options?.authors, options?.search],
queryKey: ['reposts', ndk, options?.authors, options?.search],
getNextPageParam: (lastPage: any, allPages, lastPageParam) => {
if (!lastPage?.length) return undefined;

Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useRootNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useRootNotes = (options?: UseRootNotesOptions) => {

return useInfiniteQuery({
initialPageParam: 0,
queryKey: ['rootNotes', options?.authors, options?.search],
queryKey: ['rootNotes', ndk, options?.authors, options?.search],
getNextPageParam: (lastPage: any, allPages, lastPageParam) => {
if (!lastPage?.length) return undefined;

Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/nostr/useSendNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useSendNote = () => {
const {ndk} = useNostrContext();

return useMutation({
mutationKey: ['sendNote'],
mutationKey: ['sendNote', ndk],
mutationFn: async (data: {content: string; tags?: string[][]}) => {
const event = new NDKEvent(ndk);
event.kind = NDKKind.Text;
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/hooks/useWindowDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {WEB_MAX_WIDTH} from '../constants/misc';
export const useWindowDimensions = () => {
const dimensions = useRNWindowDimensions();

if (Platform.OS === 'web') dimensions.width = WEB_MAX_WIDTH;
if (Platform.OS === 'web') dimensions.width = Math.min(dimensions.width, WEB_MAX_WIDTH);

return dimensions;
};
18 changes: 14 additions & 4 deletions JoyboyCommunity/src/modules/Post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {useProfile, useReact, useReactions, useReplyNotes, useStyles, useTheme}
import {useTipModal} from '../../hooks/modals';
import {useAuth} from '../../store/auth';
import {MainStackNavigationProps} from '../../types';
import {shortenPubkey} from '../../utils/helpers';
import {getElapsedTimeStringFull} from '../../utils/timestamp';
import stylesheet from './styles';

Expand Down Expand Up @@ -123,14 +124,23 @@ export const Post: React.FC<PostProps> = ({asComment, event}) => {
numberOfLines={1}
ellipsizeMode="middle"
>
{profile?.displayName ?? profile?.name ?? event?.pubkey}
{profile?.displayName ??
profile?.name ??
profile?.nip05 ??
shortenPubkey(event?.pubkey)}
</Text>

<View style={styles.infoDetails}>
{profile?.nip05 && (
{(profile?.nip05 || profile?.name) && (
<>
<Text color="textLight" fontSize={11} lineHeight={16}>
@{profile?.nip05}
<Text
color="textLight"
fontSize={11}
lineHeight={16}
numberOfLines={1}
ellipsizeMode="tail"
>
@{profile?.nip05 ?? profile.name}
</Text>

<View style={styles.infoDetailsDivider} />
Expand Down
3 changes: 3 additions & 0 deletions JoyboyCommunity/src/modules/TipModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {CHAIN_ID} from '../../constants/env';
import {DEFAULT_TIMELOCK, Entrypoint} from '../../constants/misc';
import {TOKENS, TokenSymbol} from '../../constants/tokens';
import {useProfile, useStyles, useWaitConnection} from '../../hooks';
import {useTransactionModal} from '../../hooks/modals';
import {useDialog} from '../../hooks/modals/useDialog';
import {useTransaction} from '../../hooks/modals/useTransaction';
import {useWalletModal} from '../../hooks/modals/useWalletModal';
Expand Down Expand Up @@ -42,6 +43,7 @@ export const TipModal = forwardRef<Modalize, TipModalProps>(
const account = useAccount();
const walletModal = useWalletModal();
const sendTransaction = useTransaction();
const {hide: hideTransactionModal} = useTransactionModal();
const waitConnection = useWaitConnection();

const {showDialog, hideDialog} = useDialog();
Expand Down Expand Up @@ -91,6 +93,7 @@ export const TipModal = forwardRef<Modalize, TipModalProps>(

if (receipt?.isSuccess()) {
hideTipModal();
hideTransactionModal();
showSuccess({
amount: Number(amount),
symbol: token,
Expand Down
18 changes: 17 additions & 1 deletion JoyboyCommunity/src/modules/TransactionModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ export const TransactionModal: React.FC<TransactionModalProps> = ({
const [status, setStatus] = useState<'confirmation' | 'processing' | 'success' | 'failure'>(
'confirmation',
);
const [trial, setTrial] = useState(0);

const {
data: transactionReceipt,
error: transactionError,
isLoading,
refetch,
} = useWaitForTransaction({hash: transactionHash});

useEffect(() => {
if (transactionHash) setStatus('processing');
}, [transactionHash]);

const {data: transactionReceipt, isLoading} = useWaitForTransaction({hash: transactionHash});
useEffect(() => {
if (transactionError) {
if (trial < 3) {
refetch().then(() => setTrial((prev) => prev + 1));
} else {
setStatus('failure');
}
}
}, [transactionError, refetch, trial]);

useEffect(() => {
if (transactionReceipt && !isLoading) {
Expand Down
13 changes: 13 additions & 0 deletions JoyboyCommunity/src/screens/Auth/CreateAccount.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {NDKPrivateKeySigner} from '@nostr-dev-kit/ndk';
import {canUseBiometricAuthentication} from 'expo-secure-store';
import {useState} from 'react';
import {Platform} from 'react-native';

import {LockIcon} from '../../assets/icons';
import {Button, Input} from '../../components';
import {useNostrContext} from '../../context/NostrContext';
import {useTheme} from '../../hooks';
import {useDialog, useToast} from '../../hooks/modals';
import {Auth} from '../../modules/Auth';
Expand All @@ -17,10 +19,16 @@ export const CreateAccount: React.FC<AuthCreateAccountScreenProps> = ({navigatio
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');

const {ndk} = useNostrContext();
const {showToast} = useToast();
const {showDialog, hideDialog} = useDialog();

const handleCreateAccount = async () => {
if (!username) {
showToast({type: 'error', title: 'Username is required'});
return;
}

if (!password) {
showToast({type: 'error', title: 'Password is required'});
return;
Expand All @@ -31,6 +39,11 @@ export const CreateAccount: React.FC<AuthCreateAccountScreenProps> = ({navigatio
await storePrivateKey(privateKey, password);
await storePublicKey(publicKey);

ndk.signer = new NDKPrivateKeySigner(privateKey);
const ndkUser = ndk.getUser({pubkey: publicKey});
ndkUser.profile = {nip05: username};
await ndkUser.publish();

const biometySupported = Platform.OS !== 'web' && canUseBiometricAuthentication();
if (biometySupported) {
showDialog({
Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/screens/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export const Login: React.FC<AuthLoginScreenProps> = ({navigation}) => {
description:
'Creating a new account will delete your current account. Are you sure you want to continue?',
buttons: [
{type: 'default', label: 'Cancel', onPress: hideDialog},
{
type: 'primary',
label: 'Continue',
Expand All @@ -75,6 +74,7 @@ export const Login: React.FC<AuthLoginScreenProps> = ({navigation}) => {
hideDialog();
},
},
{type: 'default', label: 'Cancel', onPress: hideDialog},
],
});
};
Expand Down
Loading

0 comments on commit 95b0a9d

Please sign in to comment.