blowater/app/UI/editor.test.tsx

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
/** @jsx h */
import { h, render } from "https://esm.sh/preact@10.17.1";
2023-06-30 14:05:57 +00:00
import { Editor } from "./editor.tsx";
2023-09-11 15:27:38 +00:00
import { testEventBus } from "./_setup.test.ts";
2024-03-23 08:15:56 +00:00
import { InMemoryAccountContext, NostrKind } from "../../libs/nostr.ts/nostr.ts";
2024-04-12 09:04:58 +00:00
import { prepareEncryptedNostrEvent, prepareNormalNostrEvent } from "../../libs/nostr.ts/event.ts";
import { getTags, Parsed_Event, Profile_Nostr_Event } from "../nostr.ts";
2024-03-23 08:15:56 +00:00
import { NostrEvent } from "../../libs/nostr.ts/nostr.ts";
2024-04-12 09:04:58 +00:00
const author = InMemoryAccountContext.Generate();
const event = await prepareEncryptedNostrEvent(author, {
encryptKey: author.publicKey,
kind: NostrKind.DIRECT_MESSAGE,
tags: [["p", InMemoryAccountContext.Generate().publicKey.hex]],
content: "hi",
}) as NostrEvent<NostrKind.DIRECT_MESSAGE>;
const parsedEvent: Parsed_Event = {
...event,
parsedTags: getTags(event),
publicKey: author.publicKey,
};
const prepareProfileEvent = async (profile: { name?: string; display_name?: string }) => {
const profileEvent = await prepareNormalNostrEvent(author, {
kind: NostrKind.META_DATA,
content: JSON.stringify(profile),
}) as NostrEvent<NostrKind.META_DATA>;
return {
...profileEvent,
profile,
publicKey: author.publicKey,
parsedTags: {
p: [],
e: [],
},
} as Profile_Nostr_Event;
};
const onlyName = await prepareProfileEvent({ name: "test_name" });
const onlyDisplayName = await prepareProfileEvent({ display_name: "test_display_name" });
const empty = await prepareProfileEvent({});
function TextBook() {
return (
<div class="w-screen h-screen flex-col items-center justify-center gap-2">
<EditorTest />
2024-05-24 09:54:27 +00:00
<EditorTest nip96 />
2024-04-12 09:04:58 +00:00
<EditorTest profileEvent={onlyName} />
<EditorTest profileEvent={onlyDisplayName} />
<EditorTest profileEvent={empty} />
</div>
);
2024-03-23 08:15:56 +00:00
}
2023-06-30 14:05:57 +00:00
2024-04-12 09:04:58 +00:00
testEventBus.emit({
type: "ReplyToMessage",
event: parsedEvent,
});
function EditorTest(props: {
profileEvent?: Profile_Nostr_Event;
2024-05-24 09:54:27 +00:00
nip96?: boolean;
2024-04-12 09:04:58 +00:00
}) {
const { profileEvent } = props;
return (
2023-12-22 06:55:15 +00:00
<Editor
placeholder="Message @xxx"
maxHeight="50vh"
emit={testEventBus.emit}
2024-04-12 09:04:58 +00:00
sub={testEventBus}
2024-03-23 08:15:56 +00:00
getters={{
2024-04-12 09:04:58 +00:00
getProfileByPublicKey: () => profileEvent,
getProfilesByText: () => [onlyName, onlyDisplayName, empty],
2024-03-23 08:15:56 +00:00
}}
2024-05-24 09:54:27 +00:00
nip96={props.nip96}
2023-12-22 06:55:15 +00:00
/>
2024-04-12 09:04:58 +00:00
);
}
render(<TextBook />, document.body);