/** @jsx h */ import { Fragment, h } from "https://esm.sh/preact@10.11.3"; import { tw } from "https://esm.sh/twind@0.16.16"; import { Database_Contextual_View } from "../database.ts"; import { Avatar } from "./components/avatar.tsx"; import { CenterClass, IconButtonClass, LinearGradientsClass } from "./components/tw.ts"; import { sortUserInfo, UserInfo } from "./contact-list.ts"; import { EventEmitter } from "../event-bus.ts"; import { PinIcon, UnpinIcon } from "./icons/mod.tsx"; import { DM_EditorModel } from "./editor.tsx"; import { Search } from "./search.tsx"; import { SearchModel, SearchUpdate } from "./search_model.ts"; import { PublicKey } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/key.ts"; import { groupBy, NostrAccountContext, } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts"; import { PinContact, UnpinContact } from "../nostr.ts"; import { AddIcon } from "./icons2/add-icon.tsx"; import { PrimaryBackgroundColor, PrimaryTextColor } from "./style/colors.ts"; type Props = { myAccountContext: NostrAccountContext; database: Database_Contextual_View; eventEmitter: EventEmitter; // Model userInfoMap: Map; currentSelected: PublicKey | undefined; search: SearchModel; editors: Map; selectedContactGroup: ContactGroup; hasNewMessages: Set; }; export type ContactGroup = "Contacts" | "Strangers"; export type ContactUpdate = SelectGroup | SearchUpdate | PinContact | UnpinContact; export type SelectGroup = { type: "SelectGroup"; group: ContactGroup; }; export function ContactList(props: Props) { const t = Date.now(); const groups = groupBy(props.userInfoMap, ([_, userInfo]) => { if ( userInfo.newestEventReceivedByMe == undefined || userInfo.newestEventSendByMe == undefined ) { return "Strangers"; } else { return "Contacts"; } }); console.log("ContactList groupBy", Date.now() - t); let contacts = groups.get("Contacts"); if (contacts == undefined) { contacts = []; } let strangers = groups.get("Strangers"); if (strangers == undefined) { strangers = []; } const listToRender = props.selectedContactGroup == "Contacts" ? contacts : strangers; const contactsToRender = listToRender .map(([pubkey, contact]) => { let userInfo = props.userInfoMap.get(pubkey); if (userInfo == undefined) { throw new Error("impossible"); } return { userInfo: userInfo, isMarked: props.hasNewMessages.has(pubkey), }; }); console.log("ContactList:contactsToRender", Date.now() - t); return (
  • { props.eventEmitter.emit({ type: "SelectGroup", group: "Contacts", }); }} > Contacts: {contacts.length}
  • { props.eventEmitter.emit({ type: "SelectGroup", group: "Strangers", }); }} > Strangers: {strangers.length}
{props.search.isSearching ? ( ) : undefined}
); } type ConversationListProps = { contacts: { userInfo: UserInfo; isMarked: boolean }[]; currentSelected: PublicKey | undefined; eventEmitter: EventEmitter; }; function ContactGroup(props: ConversationListProps) { const t = Date.now(); props.contacts.sort((a, b) => { return sortUserInfo(a.userInfo, b.userInfo); }); const pinned = []; const unpinned = []; for (const contact of props.contacts) { if (contact.userInfo.pinEvent && contact.userInfo.pinEvent.content.type == "PinContact") { pinned.push(contact); } else { unpinned.push(contact); } } // console.log("ContactGroup", Date.now() - t); return (
    {pinned.map((contact) => { return (
  • { props.eventEmitter.emit({ type: "SelectProfile", pubkey: contact.userInfo.pubkey, }); }} >
  • ); })} {unpinned.map((contact) => { return (
  • { props.eventEmitter.emit({ type: "SelectProfile", pubkey: contact.userInfo.pubkey, }); }} >
  • ); })}
); } type ListItemProps = { userInfo: UserInfo; isMarked: boolean; }; function ConversationListItem(props: ListItemProps) { return (

{props.userInfo.profile?.content.name || props.userInfo.pubkey.bech32()}

{props.userInfo.newestEventReceivedByMe !== undefined ? (

{new Date( props.userInfo.newestEventReceivedByMe .created_at * 1000, ).toLocaleString()}

) : undefined} {props.isMarked ? ( ) : undefined} {props.userInfo.pinEvent != undefined && props.userInfo.pinEvent.content.type == "PinContact" ? ( ) : undefined}
); }