50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
|
import { UserAccount, UserName, getAuthToken, useSSESubscription } from "@frontend/kitui"
|
||
|
import { useUserStore } from "@root/stores/user";
|
||
|
|
||
|
type Ping = [{ event: "ping" }]
|
||
|
|
||
|
type SomeChange = [{
|
||
|
"id": UserAccount["_id"],
|
||
|
"userId": UserAccount["userId"],
|
||
|
"cart": UserAccount["cart"],
|
||
|
"wallet": {
|
||
|
"cash": UserAccount["wallet"]["cash"],
|
||
|
"currency": UserAccount["wallet"]["currency"],
|
||
|
"spent": UserAccount["wallet"]["spent"],
|
||
|
"purchasesAmount": UserAccount["wallet"]["purchasesAmount"],
|
||
|
"money": UserAccount["wallet"]["money"],
|
||
|
"lastPaymentId": string;
|
||
|
},
|
||
|
"name": UserName,
|
||
|
"status": UserAccount["status"],
|
||
|
"isDeleted": UserAccount["isDeleted"],
|
||
|
"createdAt": UserAccount["createdAt"];
|
||
|
"updatedAt": UserAccount["updatedAt"];
|
||
|
"from": string;
|
||
|
"partner": string;
|
||
|
}]
|
||
|
|
||
|
type PipeMessage = Ping | SomeChange
|
||
|
|
||
|
export const usePipeSubscriber = () => {
|
||
|
const token = getAuthToken();
|
||
|
const userId = useUserStore((state) => state.userId);
|
||
|
|
||
|
useSSESubscription({
|
||
|
enabled: Boolean(token) && Boolean(userId),
|
||
|
url:
|
||
|
process.env.REACT_APP_DOMAIN +
|
||
|
`/customer/v1.0.1/account/pipe?Authorization=${token}`,
|
||
|
onNewData: (data) => {
|
||
|
let message = data as PipeMessage
|
||
|
//Пропускаем пингование
|
||
|
if ('event' in message[0] && message[0].event === "ping") return
|
||
|
|
||
|
console.log("!pipe new message!:")
|
||
|
console.log(message)
|
||
|
console.log("произошёл мем")
|
||
|
|
||
|
},
|
||
|
marker: "pipe",
|
||
|
});
|
||
|
}
|