Remove Users store

Performance improvements for profile loader
This commit is contained in:
2023-01-16 13:17:29 +00:00
parent f456c09dbe
commit 514616b170
16 changed files with 141 additions and 254 deletions

View File

@ -1,4 +1,3 @@
import { useSelector } from "react-redux";
import { useLiveQuery } from "dexie-react-hooks";
import ReactTextareaAutocomplete from "@webscopeio/react-textarea-autocomplete";
@ -12,7 +11,7 @@ import "./Textarea.css";
import Nostrich from "../nostrich.jpg";
import { hexToBech32 } from "../Util";
import { db } from "../db";
import { MetadataCache } from "../state/Users";
import { MetadataCache } from "../db/User";
function searchUsers(query: string, users: MetadataCache[]) {
const q = query.toLowerCase()
@ -38,40 +37,28 @@ const UserItem = ({ pubkey, display_name, picture, nip05, ...rest }: MetadataCac
)
}
function normalizeUser({ pubkey, picture, nip05, name, display_name }: MetadataCache) {
return { pubkey, nip05, name, picture, display_name }
}
const Textarea = ({ users, onChange, ...rest }: any) => {
const normalizedUsers = Object.keys(users).reduce((acc, pk) => {
return { ...acc, [pk]: normalizeUser(users[pk]) }
}, {})
const dbUsers = useLiveQuery(
() => db.users.toArray().then(usrs => {
return usrs.reduce((acc, usr) => {
return { ...acc, [usr.pubkey]: normalizeUser(usr) }
}, {})
})
)
const allUsers: MetadataCache[] = Object.values({ ...normalizedUsers, ...dbUsers })
const allUsers = useLiveQuery(
() => db.users.toArray()
);
return (
<ReactTextareaAutocomplete
{...rest}
loadingComponent={() => <span>Loading....</span>}
placeholder="Say something!"
onChange={onChange}
textAreaComponent={TextareaAutosize}
trigger={{
"@": {
afterWhitespace: true,
dataProvider: token => dbUsers ? searchUsers(token, allUsers) : [],
component: (props: any) => <UserItem {...props.entity} />,
output: (item: any) => `@${hexToBech32("npub", item.pubkey)}`
}
}}
/>
)
return (
<ReactTextareaAutocomplete
{...rest}
loadingComponent={() => <span>Loading....</span>}
placeholder="Say something!"
onChange={onChange}
textAreaComponent={TextareaAutosize}
trigger={{
"@": {
afterWhitespace: true,
dataProvider: token => allUsers ? searchUsers(token, allUsers) : [],
component: (props: any) => <UserItem {...props.entity} />,
output: (item: any) => `@${hexToBech32("npub", item.pubkey)}`
}
}}
/>
)
}
export default Textarea