workspace with decoupled nostr package
This commit is contained in:
20
packages/app/src/Hooks/useHorizontalScroll.tsx
Normal file
20
packages/app/src/Hooks/useHorizontalScroll.tsx
Normal file
@ -0,0 +1,20 @@
|
||||
import { useEffect, useRef, LegacyRef } from "react";
|
||||
|
||||
function useHorizontalScroll() {
|
||||
const elRef = useRef<HTMLDivElement>();
|
||||
useEffect(() => {
|
||||
const el = elRef.current;
|
||||
if (el) {
|
||||
const onWheel = (ev: WheelEvent) => {
|
||||
if (ev.deltaY == 0) return;
|
||||
ev.preventDefault();
|
||||
el.scrollTo({ left: el.scrollLeft + ev.deltaY, behavior: "smooth" });
|
||||
};
|
||||
el.addEventListener("wheel", onWheel);
|
||||
return () => el.removeEventListener("wheel", onWheel);
|
||||
}
|
||||
}, []);
|
||||
return elRef as LegacyRef<HTMLDivElement> | undefined;
|
||||
}
|
||||
|
||||
export default useHorizontalScroll;
|
97
packages/app/src/Hooks/useModeration.tsx
Normal file
97
packages/app/src/Hooks/useModeration.tsx
Normal file
@ -0,0 +1,97 @@
|
||||
import { useSelector, useDispatch } from "react-redux";
|
||||
|
||||
import type { RootState } from "State/Store";
|
||||
import { HexKey } from "@snort/nostr";
|
||||
import useEventPublisher from "Feed/EventPublisher";
|
||||
import { setMuted, setBlocked } from "State/Login";
|
||||
|
||||
export default function useModeration() {
|
||||
const dispatch = useDispatch();
|
||||
const { blocked, muted } = useSelector((s: RootState) => s.login);
|
||||
const publisher = useEventPublisher();
|
||||
|
||||
async function setMutedList(pub: HexKey[], priv: HexKey[]) {
|
||||
try {
|
||||
const ev = await publisher.muted(pub, priv);
|
||||
console.debug(ev);
|
||||
publisher.broadcast(ev);
|
||||
} catch (error) {
|
||||
console.debug("Couldn't change mute list");
|
||||
}
|
||||
}
|
||||
|
||||
function isMuted(id: HexKey) {
|
||||
return muted.includes(id) || blocked.includes(id);
|
||||
}
|
||||
|
||||
function isBlocked(id: HexKey) {
|
||||
return blocked.includes(id);
|
||||
}
|
||||
|
||||
function unmute(id: HexKey) {
|
||||
const newMuted = muted.filter(p => p !== id);
|
||||
dispatch(
|
||||
setMuted({
|
||||
createdAt: new Date().getTime(),
|
||||
keys: newMuted,
|
||||
})
|
||||
);
|
||||
setMutedList(newMuted, blocked);
|
||||
}
|
||||
|
||||
function unblock(id: HexKey) {
|
||||
const newBlocked = blocked.filter(p => p !== id);
|
||||
dispatch(
|
||||
setBlocked({
|
||||
createdAt: new Date().getTime(),
|
||||
keys: newBlocked,
|
||||
})
|
||||
);
|
||||
setMutedList(muted, newBlocked);
|
||||
}
|
||||
|
||||
function mute(id: HexKey) {
|
||||
const newMuted = muted.includes(id) ? muted : muted.concat([id]);
|
||||
setMutedList(newMuted, blocked);
|
||||
dispatch(
|
||||
setMuted({
|
||||
createdAt: new Date().getTime(),
|
||||
keys: newMuted,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function block(id: HexKey) {
|
||||
const newBlocked = blocked.includes(id) ? blocked : blocked.concat([id]);
|
||||
setMutedList(muted, newBlocked);
|
||||
dispatch(
|
||||
setBlocked({
|
||||
createdAt: new Date().getTime(),
|
||||
keys: newBlocked,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function muteAll(ids: HexKey[]) {
|
||||
const newMuted = Array.from(new Set(muted.concat(ids)));
|
||||
setMutedList(newMuted, blocked);
|
||||
dispatch(
|
||||
setMuted({
|
||||
createdAt: new Date().getTime(),
|
||||
keys: newMuted,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
muted,
|
||||
mute,
|
||||
muteAll,
|
||||
unmute,
|
||||
isMuted,
|
||||
blocked,
|
||||
block,
|
||||
unblock,
|
||||
isBlocked,
|
||||
};
|
||||
}
|
25
packages/app/src/Hooks/useWebln.ts
Normal file
25
packages/app/src/Hooks/useWebln.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { useEffect } from "react";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
webln?: {
|
||||
enabled: boolean;
|
||||
enable: () => Promise<void>;
|
||||
sendPayment: (pr: string) => Promise<unknown>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default function useWebln(enable = true) {
|
||||
const maybeWebLn = "webln" in window ? window.webln : null;
|
||||
|
||||
useEffect(() => {
|
||||
if (maybeWebLn && !maybeWebLn.enabled && enable) {
|
||||
maybeWebLn.enable().catch(() => {
|
||||
console.debug("Couldn't enable WebLN");
|
||||
});
|
||||
}
|
||||
}, [maybeWebLn, enable]);
|
||||
|
||||
return maybeWebLn;
|
||||
}
|
Reference in New Issue
Block a user