'init'
This commit is contained in:
54
app/providers/BlacklistProvider/BlacklistProvider.tsx
Normal file
54
app/providers/BlacklistProvider/BlacklistProvider.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";
|
||||
import React, { createContext, useEffect, useState } from "react";
|
||||
import { usePublicKey } from "../AccountProvider/usePublicKey";
|
||||
|
||||
export const BlacklistContext = createContext<any>({});
|
||||
|
||||
interface BlacklistProviderProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function BlacklistProvider(props : BlacklistProviderProps) {
|
||||
const [blocked, setBlocked] = useState<string[]>([]);
|
||||
const {runQuery, allQuery} = useDatabase();
|
||||
const myPublicKey = usePublicKey();
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
syncBlacklistWithLocalDb();
|
||||
}, [myPublicKey]);
|
||||
|
||||
const syncBlacklistWithLocalDb = async () => {
|
||||
const result = await allQuery("SELECT * FROM `blacklist` WHERE account = ?", [myPublicKey]);
|
||||
let publicKeysBlocked : string[] = [];
|
||||
for(let i = 0; i < result.length; i++){
|
||||
let publicKey = result[i].public_key;
|
||||
publicKeysBlocked.push(publicKey);
|
||||
}
|
||||
setBlocked(publicKeysBlocked);
|
||||
}
|
||||
|
||||
const blockUser = (publicKey : string) => {
|
||||
setBlocked((prev) => [...prev, publicKey]);
|
||||
runQuery("INSERT INTO `blacklist` (public_key, account) VALUES (?, ?)", [publicKey, myPublicKey]);
|
||||
}
|
||||
|
||||
const unblockUser = (publicKey : string) => {
|
||||
setBlocked((prev) => prev.filter(item => item != publicKey));
|
||||
runQuery("DELETE FROM `blacklist` WHERE `public_key` = ? AND `account` = ?", [publicKey, myPublicKey]);
|
||||
}
|
||||
|
||||
const isUserBlocked = (publicKey : string) => {
|
||||
return blocked.includes(publicKey);
|
||||
}
|
||||
|
||||
return (
|
||||
<BlacklistContext.Provider value={
|
||||
{isUserBlocked,
|
||||
blockUser,
|
||||
unblockUser, blocked}
|
||||
}>
|
||||
{props.children}
|
||||
</BlacklistContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user