Skip to content

Commit

Permalink
Refactor NotesStore to add notes and improve note retrieval in Activi…
Browse files Browse the repository at this point in the history
…ty view
  • Loading branch information
shubhamkmr04 committed Jul 23, 2024
1 parent 093f447 commit b1316e6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 68 deletions.
16 changes: 14 additions & 2 deletions stores/NotesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const NOTES_KEY = 'note-Keys';

export default class NotesStore {
@observable public noteKeys: string[] = [];
@observable public notes: { [key: string]: string } = {};

constructor() {
this.loadNoteKeys();
}

@action
public storeNoteKeys = async (key: string, notes: string) => {
this.notes[key] = notes;

if (!this.noteKeys.includes(key)) {
if (notes) {
this.noteKeys.push(key);
Expand All @@ -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(
Expand Down
108 changes: 42 additions & 66 deletions views/Activity/Activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export default class Activity extends React.PureComponent<
FiatStore,
PosStore,
SettingsStore,
NotesStore,
route
} = this.props;
const { selectedPaymentForOrder } = this.state;
Expand Down Expand Up @@ -228,33 +227,12 @@ export default class Activity extends React.PureComponent<
</TouchableOpacity>
);

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];
Expand All @@ -265,12 +243,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 (
Expand Down Expand Up @@ -303,8 +284,7 @@ export default class Activity extends React.PureComponent<
<FlatList
data={filteredActivity}
renderItem={({ item }: { item: any }) => {
const noteKeys = NotesStore?.noteKeys;
const hasNotes = hasMatchingNoteKey(item, noteKeys);
const note = getMatchingNote(item);

let displayName = item.model;
let subTitle = item.model;
Expand Down Expand Up @@ -614,41 +594,37 @@ 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>
{note && (
<View style={styles.row}>
<ListItem.Subtitle
style={{
...styles.leftCell,
color: themeColor(
'text'
),
fontFamily:
'Lato-Regular'
}}
>
{localeString(
'general.note'
)}
</ListItem.Subtitle>

<ListItem.Subtitle
style={{
...styles.rightCell,
color: themeColor(
'secondaryText'
),
fontFamily:
'Lato-Regular'
}}
>
{note}
</ListItem.Subtitle>
</View>
)}
</ListItem.Content>
</ListItem>
</React.Fragment>
Expand Down

0 comments on commit b1316e6

Please sign in to comment.