mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-06-13 11:31:22 +00:00
Remove unused imports
This commit is contained in:
parent
d4eb2ae8ff
commit
1dfad6371a
@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { getBaseApiReact } from '../App';
|
import { getBaseApiReact } from '../App';
|
||||||
import { getData, storeData } from '../utils/chromeStorage';
|
import { getData, storeData } from '../utils/chromeStorage';
|
||||||
import { checkDifference, getNameInfoForOthers } from '../background';
|
import { checkDifference, getNameInfoForOthers } from '../background';
|
||||||
@ -7,126 +7,131 @@ import { lastPaymentSeenTimestampAtom } from '../atoms/global';
|
|||||||
import { subscribeToEvent, unsubscribeFromEvent } from '../utils/events';
|
import { subscribeToEvent, unsubscribeFromEvent } from '../utils/events';
|
||||||
|
|
||||||
export const useHandlePaymentNotification = (address) => {
|
export const useHandlePaymentNotification = (address) => {
|
||||||
const [latestTx, setLatestTx] = useState(null);
|
const [latestTx, setLatestTx] = useState(null);
|
||||||
|
|
||||||
const nameAddressOfSender = useRef({})
|
const nameAddressOfSender = useRef({});
|
||||||
const isFetchingName = useRef({})
|
const isFetchingName = useRef({});
|
||||||
|
|
||||||
|
|
||||||
const [lastEnteredTimestampPayment, setLastEnteredTimestampPayment] =
|
|
||||||
useRecoilState(lastPaymentSeenTimestampAtom);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (lastEnteredTimestampPayment && address) {
|
|
||||||
storeData(`last-seen-payment-${address}`, Date.now()).catch((error) => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [lastEnteredTimestampPayment, address]);
|
|
||||||
|
|
||||||
const getNameOrAddressOfSender = useCallback(async(senderAddress)=> {
|
|
||||||
if(isFetchingName.current[senderAddress]) return senderAddress
|
|
||||||
try {
|
|
||||||
isFetchingName.current[senderAddress] = true
|
|
||||||
const res = await getNameInfoForOthers(senderAddress)
|
|
||||||
nameAddressOfSender.current[senderAddress] = res || senderAddress
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
} finally {
|
|
||||||
isFetchingName.current[senderAddress] = false
|
|
||||||
}
|
|
||||||
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const getNameOrAddressOfSenderMiddle = useCallback(async(senderAddress)=> {
|
const [lastEnteredTimestampPayment, setLastEnteredTimestampPayment] =
|
||||||
getNameOrAddressOfSender(senderAddress)
|
useRecoilState(lastPaymentSeenTimestampAtom);
|
||||||
return senderAddress
|
|
||||||
|
useEffect(() => {
|
||||||
}, [getNameOrAddressOfSender])
|
if (lastEnteredTimestampPayment && address) {
|
||||||
|
storeData(`last-seen-payment-${address}`, Date.now()).catch((error) => {
|
||||||
const hasNewPayment = useMemo(() => {
|
|
||||||
if (!latestTx) return false;
|
|
||||||
if (!checkDifference(latestTx?.timestamp)) return false;
|
|
||||||
if (
|
|
||||||
!lastEnteredTimestampPayment ||
|
|
||||||
lastEnteredTimestampPayment < latestTx?.timestamp
|
|
||||||
)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}, [lastEnteredTimestampPayment, latestTx]);
|
|
||||||
|
|
||||||
const getLastSeenData = useCallback(async () => {
|
|
||||||
try {
|
|
||||||
if (!address) return;
|
|
||||||
const key = `last-seen-payment-${address}`;
|
|
||||||
|
|
||||||
const res = await getData<any>(key).catch(() => null);
|
|
||||||
if (res) {
|
|
||||||
setLastEnteredTimestampPayment(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(
|
|
||||||
`${getBaseApiReact()}/transactions/search?txType=PAYMENT&address=${address}&confirmationStatus=CONFIRMED&limit=5&reverse=true`
|
|
||||||
);
|
|
||||||
|
|
||||||
const responseData = await response.json();
|
|
||||||
|
|
||||||
const latestTx = responseData.filter(
|
|
||||||
(tx) => tx?.creatorAddress !== address && tx?.recipient === address
|
|
||||||
)[0];
|
|
||||||
if (!latestTx) {
|
|
||||||
return; // continue to the next group
|
|
||||||
}
|
|
||||||
|
|
||||||
setLatestTx(latestTx);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
});
|
||||||
}, [address, setLastEnteredTimestampPayment]);
|
}
|
||||||
|
}, [lastEnteredTimestampPayment, address]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
getLastSeenData();
|
|
||||||
// Handler function for incoming messages
|
|
||||||
const messageHandler = (event) => {
|
|
||||||
if (event.origin !== window.location.origin) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const message = event.data;
|
|
||||||
if (message?.action === "SET_PAYMENT_ANNOUNCEMENT" && message?.payload) {
|
|
||||||
setLatestTx(message.payload);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Attach the event listener
|
|
||||||
window.addEventListener("message", messageHandler);
|
|
||||||
|
|
||||||
// Clean up the event listener on component unmount
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener("message", messageHandler);
|
|
||||||
};
|
|
||||||
}, [getLastSeenData]);
|
|
||||||
|
|
||||||
const setLastEnteredTimestampPaymentEventFunc = useCallback(
|
const getNameOrAddressOfSender = useCallback(async (senderAddress) => {
|
||||||
(e) => {
|
if (isFetchingName.current[senderAddress]) return senderAddress;
|
||||||
setLastEnteredTimestampPayment(Date.now)
|
try {
|
||||||
},
|
isFetchingName.current[senderAddress] = true;
|
||||||
[setLastEnteredTimestampPayment]
|
const res = await getNameInfoForOthers(senderAddress);
|
||||||
|
nameAddressOfSender.current[senderAddress] = res || senderAddress;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
} finally {
|
||||||
|
isFetchingName.current[senderAddress] = false;
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const getNameOrAddressOfSenderMiddle = useCallback(
|
||||||
|
async (senderAddress) => {
|
||||||
|
getNameOrAddressOfSender(senderAddress);
|
||||||
|
return senderAddress;
|
||||||
|
},
|
||||||
|
[getNameOrAddressOfSender]
|
||||||
|
);
|
||||||
|
|
||||||
|
const hasNewPayment = useMemo(() => {
|
||||||
|
if (!latestTx) return false;
|
||||||
|
if (!checkDifference(latestTx?.timestamp)) return false;
|
||||||
|
if (
|
||||||
|
!lastEnteredTimestampPayment ||
|
||||||
|
lastEnteredTimestampPayment < latestTx?.timestamp
|
||||||
|
)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}, [lastEnteredTimestampPayment, latestTx]);
|
||||||
|
|
||||||
|
const getLastSeenData = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
if (!address) return;
|
||||||
|
const key = `last-seen-payment-${address}`;
|
||||||
|
|
||||||
|
const res = await getData<any>(key).catch(() => null);
|
||||||
|
if (res) {
|
||||||
|
setLastEnteredTimestampPayment(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`${getBaseApiReact()}/transactions/search?txType=PAYMENT&address=${address}&confirmationStatus=CONFIRMED&limit=5&reverse=true`
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
const responseData = await response.json();
|
||||||
subscribeToEvent("setLastEnteredTimestampPaymentEvent", setLastEnteredTimestampPaymentEventFunc);
|
|
||||||
|
const latestTx = responseData.filter(
|
||||||
return () => {
|
(tx) => tx?.creatorAddress !== address && tx?.recipient === address
|
||||||
unsubscribeFromEvent("setLastEnteredTimestampPaymentEvent", setLastEnteredTimestampPaymentEventFunc);
|
)[0];
|
||||||
};
|
if (!latestTx) {
|
||||||
}, [setLastEnteredTimestampPaymentEventFunc]);
|
return; // continue to the next group
|
||||||
|
}
|
||||||
|
|
||||||
|
setLatestTx(latestTx);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}, [address, setLastEnteredTimestampPayment]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getLastSeenData();
|
||||||
|
// Handler function for incoming messages
|
||||||
|
const messageHandler = (event) => {
|
||||||
|
if (event.origin !== window.location.origin) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const message = event.data;
|
||||||
|
if (message?.action === 'SET_PAYMENT_ANNOUNCEMENT' && message?.payload) {
|
||||||
|
setLatestTx(message.payload);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Attach the event listener
|
||||||
|
window.addEventListener('message', messageHandler);
|
||||||
|
|
||||||
|
// Clean up the event listener on component unmount
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', messageHandler);
|
||||||
|
};
|
||||||
|
}, [getLastSeenData]);
|
||||||
|
|
||||||
|
const setLastEnteredTimestampPaymentEventFunc = useCallback(
|
||||||
|
(e) => {
|
||||||
|
setLastEnteredTimestampPayment(Date.now);
|
||||||
|
},
|
||||||
|
[setLastEnteredTimestampPayment]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
subscribeToEvent(
|
||||||
|
'setLastEnteredTimestampPaymentEvent',
|
||||||
|
setLastEnteredTimestampPaymentEventFunc
|
||||||
|
);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unsubscribeFromEvent(
|
||||||
|
'setLastEnteredTimestampPaymentEvent',
|
||||||
|
setLastEnteredTimestampPaymentEventFunc
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}, [setLastEnteredTimestampPaymentEventFunc]);
|
||||||
return {
|
return {
|
||||||
latestTx,
|
latestTx,
|
||||||
getNameOrAddressOfSenderMiddle,
|
getNameOrAddressOfSenderMiddle,
|
||||||
hasNewPayment,
|
hasNewPayment,
|
||||||
setLastEnteredTimestampPayment,
|
setLastEnteredTimestampPayment,
|
||||||
nameAddressOfSender
|
nameAddressOfSender,
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user