Skip to content

Commit

Permalink
Load noteKey on start + display if the notes are available or not on …
Browse files Browse the repository at this point in the history
…Activity view
  • Loading branch information
shubhamkmr04 committed Jul 18, 2024
1 parent 8f6ba57 commit 093f447
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
20 changes: 20 additions & 0 deletions stores/NotesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const NOTES_KEY = 'note-Keys';
export default class NotesStore {
@observable public noteKeys: string[] = [];

constructor() {
this.loadNoteKeys();
}

@action
public storeNoteKeys = async (key: string, notes: string) => {
if (!this.noteKeys.includes(key)) {
Expand All @@ -26,6 +30,22 @@ export default class NotesStore {
}
};

@action
public async loadNoteKeys() {
console.log('Loading note keys...');
try {
const storedKeys = await EncryptedStorage.getItem(NOTES_KEY);
if (storedKeys) {
this.noteKeys = JSON.parse(storedKeys);
}
} catch (error) {
console.error(
'Error loading note keys from encrypted storage',
error
);
}
}

writeNoteKeysToLocalStorage = async () => {
try {
await EncryptedStorage.setItem(
Expand Down
88 changes: 87 additions & 1 deletion views/Activity/Activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import ActivityStore from '../../stores/ActivityStore';
import FiatStore from '../../stores/FiatStore';
import PosStore from '../../stores/PosStore';
import SettingsStore from '../../stores/SettingsStore';
import NotesStore from '../../stores/NotesStore';
import { SATS_PER_BTC } from '../../stores/UnitsStore';

import Filter from '../../assets/images/SVG/Filter On.svg';
Expand All @@ -38,14 +39,15 @@ interface ActivityProps {
FiatStore: FiatStore;
PosStore: PosStore;
SettingsStore: SettingsStore;
NotesStore: NotesStore;
route: Route<'Activity', { order: any }>;
}

interface ActivityState {
selectedPaymentForOrder: any;
}

@inject('ActivityStore', 'FiatStore', 'PosStore', 'SettingsStore')
@inject('ActivityStore', 'FiatStore', 'PosStore', 'SettingsStore', 'NotesStore')
@observer
export default class Activity extends React.PureComponent<
ActivityProps,
Expand Down Expand Up @@ -139,6 +141,7 @@ export default class Activity extends React.PureComponent<
FiatStore,
PosStore,
SettingsStore,
NotesStore,
route
} = this.props;
const { selectedPaymentForOrder } = this.state;
Expand Down Expand Up @@ -225,6 +228,51 @@ export default class Activity extends React.PureComponent<
</TouchableOpacity>
);

const hasMatchingNoteKey = (item: any, noteKeys: string[]): boolean => {
const strippedNoteKeys = noteKeys.map((key) =>
key.replace(/^note-/, '')
);

const isMatchingValue = (
value: any,
strippedNoteKeys: string[]
): boolean => {
if (
(typeof value === 'string' || typeof value === 'number') &&
value.toString().length === 64
) {
const valueString = value.toString();
for (let key of strippedNoteKeys) {
console.log(
`Comparing item value: ${valueString} with noteKey: ${key}`
);
if (key === valueString) {
console.log(`Match found: ${valueString}`);
return true;
}
}
}
return false;
};

let valuesToCompare: string[] = [];
if (item.model === 'Invoice') {
valuesToCompare = [item.getRPreimage, item.payment_hash];
} else if (item.model === 'Payment') {
valuesToCompare = [item.paymentHash, item.getPreimage];
} else if (item.model === 'Transaction') {
valuesToCompare = [item.tx];
}

for (let value of valuesToCompare) {
if (isMatchingValue(value, strippedNoteKeys)) {
return true;
}
}

return false;
};

return (
<Screen>
<Header
Expand Down Expand Up @@ -255,6 +303,9 @@ export default class Activity extends React.PureComponent<
<FlatList
data={filteredActivity}
renderItem={({ item }: { item: any }) => {
const noteKeys = NotesStore?.noteKeys;
const hasNotes = hasMatchingNoteKey(item, noteKeys);

let displayName = item.model;
let subTitle = item.model;

Expand Down Expand Up @@ -563,6 +614,41 @@ export default class Activity extends React.PureComponent<
</ListItem.Subtitle>
</View>
)}
<View style={styles.row}>
<ListItem.Subtitle
style={{
...styles.leftCell,
color:
item ===
selectedPaymentForOrder
? themeColor(
'highlight'
)
: themeColor(
'secondaryText'
),
fontFamily:
'Lato-Regular'
}}
>
Notes
</ListItem.Subtitle>

<ListItem.Subtitle
style={{
...styles.rightCell,
color: themeColor(
'secondaryText'
),
fontFamily:
'Lato-Regular'
}}
>
{hasNotes
? 'Notes Available'
: 'No Notes'}
</ListItem.Subtitle>
</View>
</ListItem.Content>
</ListItem>
</React.Fragment>
Expand Down
10 changes: 8 additions & 2 deletions views/Wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import SettingsStore, {
import SyncStore from '../../stores/SyncStore';
import UnitsStore from '../../stores/UnitsStore';
import UTXOsStore from '../../stores/UTXOsStore';
import NotesStore from '../../stores/NotesStore';

import Bitcoin from '../../assets/images/SVG/Bitcoin.svg';
import CaretUp from '../../assets/images/SVG/Caret Up.svg';
Expand All @@ -93,6 +94,7 @@ interface WalletProps {
ModalStore: ModalStore;
SyncStore: SyncStore;
LSPStore: LSPStore;
NotesStore: NotesStore;
ChannelBackupStore: ChannelBackupStore;
LightningAddressStore: LightningAddressStore;
LnurlPayStore: LnurlPayStore;
Expand All @@ -118,7 +120,8 @@ interface WalletState {
'LSPStore',
'LnurlPayStore',
'ChannelBackupStore',
'LightningAddressStore'
'LightningAddressStore',
'NotesStore'
)
@observer
export default class Wallet extends React.Component<WalletProps, WalletState> {
Expand Down Expand Up @@ -299,7 +302,8 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
ChannelBackupStore,
SyncStore,
LightningAddressStore,
LnurlPayStore
LnurlPayStore,
NotesStore
} = this.props;
const {
settings,
Expand Down Expand Up @@ -345,6 +349,8 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {

LnurlPayStore.reset();

NotesStore?.loadNoteKeys();

if (
pos?.posEnabled &&
pos.posEnabled !== PosEnabled.Disabled &&
Expand Down

0 comments on commit 093f447

Please sign in to comment.