From a7504db9b7db149d1a39fc010a18f3cf76e505cd Mon Sep 17 00:00:00 2001 From: shubham Date: Tue, 23 Jul 2024 20:17:07 +0530 Subject: [PATCH] Refactor NotesStore to add notes and improve note retrieval in Activity view --- stores/NotesStore.ts | 16 +++++- views/Activity/Activity.tsx | 108 ++++++++++++++---------------------- 2 files changed, 57 insertions(+), 67 deletions(-) diff --git a/stores/NotesStore.ts b/stores/NotesStore.ts index 5d01f41788..f882360502 100644 --- a/stores/NotesStore.ts +++ b/stores/NotesStore.ts @@ -5,6 +5,7 @@ const NOTES_KEY = 'note-Keys'; export default class NotesStore { @observable public noteKeys: string[] = []; + @observable public notes: { [key: string]: string } = {}; constructor() { this.loadNoteKeys(); @@ -12,6 +13,8 @@ export default class NotesStore { @action public storeNoteKeys = async (key: string, notes: string) => { + this.notes[key] = notes; + if (!this.noteKeys.includes(key)) { if (notes) { this.noteKeys.push(key); @@ -25,18 +28,27 @@ export default class NotesStore { const index = this.noteKeys.indexOf(key); if (index !== -1) { this.noteKeys.splice(index, 1); - // write updated keys to storage + delete this.notes[key]; await this.writeNoteKeysToLocalStorage(); } }; @action public async loadNoteKeys() { - console.log('Loading note keys...'); + console.log('Loading notes...'); try { const storedKeys = await EncryptedStorage.getItem(NOTES_KEY); if (storedKeys) { this.noteKeys = JSON.parse(storedKeys); + // Load all notes + await Promise.all( + this.noteKeys.map(async (key) => { + const note = await EncryptedStorage.getItem(key); + if (note) { + this.notes[key] = note; + } + }) + ); } } catch (error) { console.error( diff --git a/views/Activity/Activity.tsx b/views/Activity/Activity.tsx index cb31870d64..663c9f99a2 100644 --- a/views/Activity/Activity.tsx +++ b/views/Activity/Activity.tsx @@ -32,6 +32,7 @@ import { SATS_PER_BTC } from '../../stores/UnitsStore'; import Filter from '../../assets/images/SVG/Filter On.svg'; import Invoice from '../../models/Invoice'; +import EncryptedStorage from 'react-native-encrypted-storage'; interface ActivityProps { navigation: StackNavigationProp; @@ -228,33 +229,12 @@ export default class Activity extends React.PureComponent< ); - const hasMatchingNoteKey = (item: any, noteKeys: string[]): boolean => { - const strippedNoteKeys = noteKeys.map((key) => + const getMatchingNote = (item: any) => { + const { NotesStore } = this.props; + const strippedNoteKeys = NotesStore.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]; @@ -265,12 +245,15 @@ export default class Activity extends React.PureComponent< } for (let value of valuesToCompare) { - if (isMatchingValue(value, strippedNoteKeys)) { - return true; + const matchKey = `note-${value}`; + if ( + strippedNoteKeys.includes(value) && + NotesStore.notes[matchKey] + ) { + return NotesStore.notes[matchKey]; } } - - return false; + return null; }; return ( @@ -303,8 +286,7 @@ export default class Activity extends React.PureComponent< { - const noteKeys = NotesStore?.noteKeys; - const hasNotes = hasMatchingNoteKey(item, noteKeys); + const note = getMatchingNote(item); let displayName = item.model; let subTitle = item.model; @@ -614,41 +596,37 @@ export default class Activity extends React.PureComponent< )} - - - Notes - - - - {hasNotes - ? 'Notes Available' - : 'No Notes'} - - + {note && ( + + + {localeString( + 'general.note' + )} + + + + {note} + + + )}