mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-07 15:23:06 +00:00
create hooks file
This commit is contained in:
parent
7b3661b011
commit
2410f8df48
@ -4,17 +4,12 @@ import React, { useEffect, useState } from "react";
|
|||||||
import "./App.css";
|
import "./App.css";
|
||||||
import handleCommand from "./command";
|
import handleCommand from "./command";
|
||||||
import Room from "./Room";
|
import Room from "./Room";
|
||||||
import { generate } from "server-name-generator";
|
|
||||||
import { Message } from "./Message";
|
import { Message } from "./Message";
|
||||||
import { Decoder } from "@waku/core/lib/message/version_0";
|
import { PageDirection, LightNode } from "@waku/interfaces";
|
||||||
import { PageDirection, LightNode, StoreQueryOptions } from "@waku/interfaces";
|
|
||||||
|
|
||||||
import {
|
import { useWaku, useContentPair } from "@waku/react";
|
||||||
useWaku,
|
|
||||||
useFilterMessages,
|
import { useMessages, usePersistentNick } from "./hooks";
|
||||||
useStoreMessages,
|
|
||||||
useContentPair,
|
|
||||||
} from "@waku/react";
|
|
||||||
|
|
||||||
export const ChatContentTopic = "/toy-chat/2/huilong/proto";
|
export const ChatContentTopic = "/toy-chat/2/huilong/proto";
|
||||||
const startTime = new Date();
|
const startTime = new Date();
|
||||||
@ -22,38 +17,6 @@ const startTime = new Date();
|
|||||||
startTime.setTime(Date.now() - 1000 * 60 * 60 * 24 * 7);
|
startTime.setTime(Date.now() - 1000 * 60 * 60 * 24 * 7);
|
||||||
const endTime = new Date();
|
const endTime = new Date();
|
||||||
|
|
||||||
const usePersistentNick = (): [
|
|
||||||
string,
|
|
||||||
React.Dispatch<React.SetStateAction<string>>
|
|
||||||
] => {
|
|
||||||
const [nick, setNick] = useState<string>(() => {
|
|
||||||
const persistedNick = window.localStorage.getItem("nick");
|
|
||||||
return persistedNick !== null ? persistedNick : generate();
|
|
||||||
});
|
|
||||||
useEffect(() => {
|
|
||||||
localStorage.setItem("nick", nick);
|
|
||||||
}, [nick]);
|
|
||||||
|
|
||||||
return [nick, setNick];
|
|
||||||
};
|
|
||||||
|
|
||||||
type UseMessagesParams = {
|
|
||||||
node: undefined | LightNode;
|
|
||||||
decoder: Decoder;
|
|
||||||
options: StoreQueryOptions;
|
|
||||||
};
|
|
||||||
|
|
||||||
const useMessages = (params: UseMessagesParams): Message[] => {
|
|
||||||
const { messages: newMessages } = useFilterMessages(params);
|
|
||||||
const { messages: storedMessages } = useStoreMessages(params);
|
|
||||||
|
|
||||||
return React.useMemo((): Message[] => {
|
|
||||||
return [...storedMessages, ...newMessages]
|
|
||||||
.map(Message.fromWakuMessage)
|
|
||||||
.filter((v): v is Message => !!v);
|
|
||||||
}, [storedMessages, newMessages]);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const { node } = useWaku<LightNode>();
|
const { node } = useWaku<LightNode>();
|
||||||
const { decoder } = useContentPair(ChatContentTopic);
|
const { decoder } = useContentPair(ChatContentTopic);
|
||||||
|
|||||||
39
examples/web-chat/src/hooks.ts
Normal file
39
examples/web-chat/src/hooks.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import React, { useEffect, useState } from "react";
|
||||||
|
import { generate } from "server-name-generator";
|
||||||
|
import { Message } from "./Message";
|
||||||
|
import { Decoder } from "@waku/core/lib/message/version_0";
|
||||||
|
import { LightNode, StoreQueryOptions } from "@waku/interfaces";
|
||||||
|
|
||||||
|
import { useFilterMessages, useStoreMessages } from "@waku/react";
|
||||||
|
|
||||||
|
export const usePersistentNick = (): [
|
||||||
|
string,
|
||||||
|
React.Dispatch<React.SetStateAction<string>>
|
||||||
|
] => {
|
||||||
|
const [nick, setNick] = useState<string>(() => {
|
||||||
|
const persistedNick = window.localStorage.getItem("nick");
|
||||||
|
return persistedNick !== null ? persistedNick : generate();
|
||||||
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem("nick", nick);
|
||||||
|
}, [nick]);
|
||||||
|
|
||||||
|
return [nick, setNick];
|
||||||
|
};
|
||||||
|
|
||||||
|
type UseMessagesParams = {
|
||||||
|
node: undefined | LightNode;
|
||||||
|
decoder: Decoder;
|
||||||
|
options: StoreQueryOptions;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useMessages = (params: UseMessagesParams): Message[] => {
|
||||||
|
const { messages: newMessages } = useFilterMessages(params);
|
||||||
|
const { messages: storedMessages } = useStoreMessages(params);
|
||||||
|
|
||||||
|
return React.useMemo((): Message[] => {
|
||||||
|
return [...storedMessages, ...newMessages]
|
||||||
|
.map(Message.fromWakuMessage)
|
||||||
|
.filter((v): v is Message => !!v);
|
||||||
|
}, [storedMessages, newMessages]);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user