<commit_before>// transaction handling
import {atomFamily, selectorFamily} from "recoil";
import {groupAccounts} from "./groups";
import {fetchTransactions, getUserIDFromToken} from "../api";
import {ws} from "../websocket";
import {userData} from "./auth";
import {DateTime} from "luxon";
import {toast} from "react-toastify";


export const groupTransactions = atomFamily({
    key: "groupTransactions",
    default: [],
    effects_UNSTABLE: groupID => [
        ({setSelf, node, getPromise}) => {
            // try to load cached state from local storage
            const localStorageKey = `groups.transactions-${groupID}`;
            const savedTransactions = localStorage.getItem(localStorageKey);

            const fullFetchPromise = () => {
                return fetchTransactions({groupID: groupID})
                    .then(result => {
                        localStorage.setItem(localStorageKey, JSON.stringify(result));
                        return result;
                    })
                    .catch(err => {
                        toast.error(`error when fetching transactions: ${err}`);
                        return [];
                    });
            }

            const partialFetchPromise = (currTransactions) => {
                let maxChangedTime = currTransactions.reduce((acc, curr) => {
                    if (curr.current_state != null && curr.current_state.committed_at != null) {
                        return Math.max(DateTime.fromISO(curr.current_state.committed_at).toSeconds(), acc);
                    }
                    return acc;
                }, 0);
                if (maxChangedTime === 0) {
                    return fullFetchPromise();
                }

                const wipTransactions = currTransactions.reduce((acc, curr) => {
                    const userID = getUserIDFromToken();
                    if (curr.pending_changes?.hasOwnProperty(userID)) {
                        return [...acc, curr.id];
                    }
                    return acc;
                }, []);

                return fetchTransactions({
                    groupID: groupID,
                    minLastChanged: DateTime.fromSeconds(maxChangedTime),
                    additionalTransactions: wipTransactions
                })
                    .then(result => {
                        const newTransactions = currTransactions.map(transaction => {
                            const newTransaction = result.find(i => i.id === transaction.id);
                            if (newTransaction !== undefined) {
                                return newTransaction;
                            }

                            return transaction;
                        });
                        console.log("partial fetch return", newTransactions);
                        localStorage.setItem(localStorageKey, JSON.stringify(newTransactions));
                        return newTransactions;
                    })
                    .catch(err => {
                        toast.error(`error when fetching transactions: ${err}`);<commit_msg>fix transaction fetching error<commit_after>// transaction handling
import {atomFamily, selectorFamily} from "recoil";
import {groupAccounts} from "./groups";
import {fetchTransactions, getUserIDFromToken} from "../api";
import {ws} from "../websocket";
import {userData} from "./auth";
import {DateTime} from "luxon";
import {toast} from "react-toastify";


export const groupTransactions = atomFamily({
    key: "groupTransactions",
    default: [],
    effects_UNSTABLE: groupID => [
        ({setSelf, node, getPromise}) => {
            // try to load cached state from local storage
            const localStorageKey = `groups.transactions-${groupID}`;
            const savedTransactions = localStorage.getItem(localStorageKey);

            const fullFetchPromise = () => {
                return fetchTransactions({groupID: groupID})
                    .then(result => {
                        localStorage.setItem(localStorageKey, JSON.stringify(result));
                        return result;
                    })
                    .catch(err => {
                        toast.error(`error when fetching transactions: ${err}`);
                        return [];
                    });
            }

            const partialFetchPromise = (currTransactions) => {
                let maxChangedTime = currTransactions.reduce((acc, curr) => {
                    if (curr.current_state != null && curr.current_state.committed_at != null) {
                        return Math.max(DateTime.fromISO(curr.current_state.committed_at).toSeconds(), acc);
                    }
                    return acc;
                }, 0);
                if (maxChangedTime === 0) {
                    return fullFetchPromise();
                }

                const wipTransactions = currTransactions.reduce((acc, curr) => {
                    const userID = getUserIDFromToken();
                    if (curr.pending_changes?.hasOwnProperty(userID)) {
                        return [...acc, curr.id];
                    }
                    return acc;
                }, []);

                return fetchTransactions({
                    groupID: groupID,
                    minLastChanged: DateTime.fromSeconds(maxChangedTime),
                    additionalTransactions: wipTransactions
                })
                    .then(result => {
                        let mappedTransactions = currTransactions.reduce((map, transaction) => {
                            map[transaction.id] = transaction;
                            return map;
                        }, {});
                        for (const newTransaction of result) {
                            mappedTransactions[newTransaction.id] = newTransaction;
                        }
                        console.log("partial fetch return", Object.values(mappedTransactions));
                        localStorage.setItem(localStorageKey, JSON.stringify(Object.values(mappedTransactions)));
                        return Object.values(mappedTransactions);
                    })
                    .catch(err => {
                        toast.error(`error when fetching transactions: ${err}`);
{'fraction_non_alphanumeric': 0.10136421319796954, 'fraction_numerical': 0.0006345177664974619, 'mean_word_length': 1.4419054996127034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4914731', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1378, 'n_words': 304}