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

Display chain information in key listing #36

Merged
merged 2 commits into from
Dec 19, 2024
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
1 change: 0 additions & 1 deletion mobile/app/(app)/chain-selection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ function Page() {
await gnonative.getChainID();

const currentChain = await dispatch(getCurrentChain()).unwrap();
console.log('xxxxx', currentChain);
dispatch(setSelectedChain(currentChain));

setLoading(undefined);
Expand Down
12 changes: 10 additions & 2 deletions mobile/app/(app)/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FlatList, TouchableOpacity, View } from "react-native";
import { useNavigation, useRouter } from "expo-router";
import { Layout } from "@/components/index";
import Text from "@/components/text";
import { checkForKeyOnChains, initSignUpState, selectMasterPassword, useAppDispatch, useAppSelector } from "@/redux";
import { checkForKeyOnChains, initSignUpState, selectMasterPassword, useAppDispatch, useAppSelector, selectKeyInfoChains } from "@/redux";
import { KeyInfo, useGnoNativeContext } from "@gnolang/gnonative";
import Octicons from '@expo/vector-icons/Octicons';
import TextInput from "@/components/textinput";
Expand All @@ -24,6 +24,8 @@ export default function Page() {
const dispatch = useAppDispatch();
const masterPassword = useAppSelector(selectMasterPassword)

const keyInfoChains = useAppSelector(selectKeyInfoChains)

useEffect(() => {
const unsubscribe = navigation.addListener("focus", async () => {
try {
Expand Down Expand Up @@ -76,6 +78,12 @@ export default function Page() {
route.push("/add-key");
}

const getChainNamePerKey = (keyInfo: KeyInfo): string[] | undefined => {
if (keyInfoChains instanceof Map && keyInfoChains?.has(keyInfo.address.toString())) {
return keyInfoChains.get(keyInfo.address.toString())
}
}

if (loading) {
return (
<Layout.Container>
Expand Down Expand Up @@ -103,7 +111,7 @@ export default function Page() {
<FlatList
data={filteredAccounts}
renderItem={({ item }) => (
<VaultListItem vault={item} onVaultPress={onChangeAccountHandler} />
<VaultListItem vault={item} onVaultPress={onChangeAccountHandler} chains={getChainNamePerKey(item)} />
)}
keyExtractor={(item) => item.name}
ListEmptyComponent={<Text.Body>There are no items to list.</Text.Body>}
Expand Down
3 changes: 1 addition & 2 deletions mobile/app/(app)/tosignin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Button from "@/components/button";
import VaultListItem from "@/components/list/vault-list/VaultListItem";
import Spacer from "@/components/spacer";
import Text from "@/components/text";
import { checkForKeyOnChains, clearLinking, selectCallback, selectClientName, sendAddressToSoliciting, useAppDispatch, useAppSelector } from "@/redux";
import { clearLinking, selectCallback, selectClientName, sendAddressToSoliciting, useAppDispatch, useAppSelector } from "@/redux";
import { KeyInfo, useGnoNativeContext } from "@gnolang/gnonative";
import { router, useNavigation } from "expo-router";
import { useCallback, useEffect, useState } from "react";
Expand Down Expand Up @@ -45,7 +45,6 @@ export default function Page() {

const onCancel = () => {
dispatch(clearLinking())
dispatch(checkForKeyOnChains())
Linking.openURL(`${callback}?status=cancelled`);
router.replace("/home")
}
Expand Down
13 changes: 10 additions & 3 deletions mobile/components/list/vault-list/VaultListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import { KeyInfo } from "@gnolang/gnonative";
import { TouchableOpacity, View } from "react-native";
import styled from "styled-components/native";
import AntDesign from '@expo/vector-icons/AntDesign';
import Row from "@/components/row";

interface Props {
vault: KeyInfo;
chains?: string[];
onVaultPress: (vault: KeyInfo) => void;
}

const VaultListItem = ({ vault, onVaultPress }: Props) => {
const VaultListItem = ({ vault, onVaultPress, chains = [] }: Props) => {

return (
<Container onPress={() => onVaultPress(vault)}>
<View style={{ flex: 1 }}>
<Text.Title>{vault.name}</Text.Title>
<View style={{ flex: 1, height: 40 }}>
<View style={{ flex: 1 }}>
<Text.BodyMedium>{vault.name}</Text.BodyMedium>
</View>
<View style={{ flex: 1 }}>
{chains ? <Text.Caption1 style={{ textAlign: 'left', paddingTop: 4 }}>{chains.join(', ')}</Text.Caption1> : null}
</View>
</View>
<AntDesign name="edit" size={24} color="black" />
</Container>
Expand Down
35 changes: 22 additions & 13 deletions mobile/redux/features/vaultEditSlice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { selectChainsAvailable } from "./signupSlice";
import { GnoNativeApi, KeyInfo } from "@gnolang/gnonative";
import { ThunkExtra } from "@/providers/redux-provider";
import { RootState } from "../root-reducer";

export interface VaultState {
vaultToEdit: KeyInfo | undefined;
keyinfosChains?: Map<string, string[]>;
keyInfoChains?: Map<string, string[]>;
}

const initialState: VaultState = {
Expand Down Expand Up @@ -44,25 +45,29 @@ export const saveChanges = createAsyncThunk<boolean, SaveChangesParam, ThunkExtr
* */
export const checkForKeyOnChains = createAsyncThunk<Map<string, string[]>, void, ThunkExtra>("vault/checkForKeyOnChains", async (_, thunkAPI) => {
const gnonative = thunkAPI.extra.gnonative as GnoNativeApi;

const keyinfos = await gnonative.listKeyInfo();
const chains = (thunkAPI.getState() as RootState).signUp.chainsAvailable;
const chains = await selectChainsAvailable(thunkAPI.getState() as RootState);

const keyinfosChains = new Map<string, string[]>();
const keyInfoChains = new Map<string, string[]>();

for (const chain of chains) {
console.log("checking keys on chain", chain.chainName);

await gnonative.setChainID(chain.chainId);
await gnonative.setRemote(chain.gnoAddress);

for (const key of keyinfos) {
console.log(`Checking key ${key.name} on chain ${chain.chainName}`);
const queryResponse = await hasCoins(gnonative, key.address);
console.log(`Key ${key.name} on chain ${chain.chainName} has coins: ${queryResponse}`);
const keyHasCoins = await hasCoins(gnonative, key.address);
console.log(`Key ${key.name} on chain ${chain.chainName} has coins: ${keyHasCoins}`);

keyinfosChains.has(key.name) ? keyinfosChains.get(key.name)?.push(chain.chainId) : keyinfosChains.set(key.name, [chain.chainId]);
if (keyHasCoins) {
console.log(`Key ${key.name} on chain ${chain.chainName} has coins`, JSON.stringify(key));
keyInfoChains.has(key.address.toString()) ? keyInfoChains.get(key.address.toString())?.push(chain.chainName) : keyInfoChains.set(key.address.toString(), [chain.chainName]);
}
}
}
return keyinfosChains;
return keyInfoChains;
});

const hasCoins = async (gnonative: GnoNativeApi, address: Uint8Array) => {
Expand Down Expand Up @@ -93,16 +98,20 @@ export const vaultSlice = createSlice({
}
},
extraReducers: (builder) => {
builder.addCase(checkForKeyOnChains.fulfilled, (state, action) => {
console.log("checkForKeyOnChains.fulfilled", action.payload);
state.keyinfosChains = action.payload;
})
builder.addCase(checkForKeyOnChains.rejected, (state, action) => {
console.error("checkForKeyOnChains.rejected", action.error);
}),
builder.addCase(checkForKeyOnChains.fulfilled, (state, action) => {
console.log("checkForKeyOnChains.fulfilled", action.payload);
state.keyInfoChains = action.payload;
})
},
selectors: {
selectVaultToEdit: (state) => state.vaultToEdit,
selectKeyInfoChains: (state) => state.keyInfoChains,
},
});

export const { setVaultToEdit } = vaultSlice.actions;

export const { selectVaultToEdit } = vaultSlice.selectors;
export const { selectVaultToEdit, selectKeyInfoChains } = vaultSlice.selectors;
Loading