mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2025-08-27 04:59:22 +00:00
Fix AccountSwitcher
This commit is contained in:
parent
b4605cf672
commit
e07e98c882
@ -16,6 +16,13 @@ import { NSchema as n, NostrEvent, NostrMetadata } from '@nostrify/nostrify';
|
|||||||
import LoginForm from './LoginForm';
|
import LoginForm from './LoginForm';
|
||||||
import SignupForm from './SignupForm';
|
import SignupForm from './SignupForm';
|
||||||
|
|
||||||
|
interface Account {
|
||||||
|
id: string;
|
||||||
|
pubkey: string;
|
||||||
|
event?: NostrEvent;
|
||||||
|
metadata: NostrMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
export function AccountSwitcher() {
|
export function AccountSwitcher() {
|
||||||
const { nostr } = useNostr();
|
const { nostr } = useNostr();
|
||||||
const { logins, setLogin, removeLogin } = useNostrLogin();
|
const { logins, setLogin, removeLogin } = useNostrLogin();
|
||||||
@ -23,11 +30,22 @@ export function AccountSwitcher() {
|
|||||||
const [loginDialogOpen, setLoginDialogOpen] = useState(false);
|
const [loginDialogOpen, setLoginDialogOpen] = useState(false);
|
||||||
const [signupDialogOpen, setSignupDialogOpen] = useState(false);
|
const [signupDialogOpen, setSignupDialogOpen] = useState(false);
|
||||||
|
|
||||||
const { data: authors } = useQuery({
|
const { data: authors = [] } = useQuery({
|
||||||
queryKey: ['logins', logins],
|
queryKey: ['logins', logins.map((l) => l.id).join(';')],
|
||||||
queryFn: async () => {
|
queryFn: async ({ signal }) => {
|
||||||
const events = await nostr.query([{ kinds: [0], authors: logins.map((l) => l.pubkey) }]);
|
let events: NostrEvent[] = [];
|
||||||
return logins.map(({ id, pubkey }): { id: string; pubkey: string; event?: NostrEvent; metadata: NostrMetadata } => {
|
|
||||||
|
try {
|
||||||
|
events = await nostr.query(
|
||||||
|
[{ kinds: [0], authors: logins.map((l) => l.pubkey) }],
|
||||||
|
{ signal: AbortSignal.any([signal, AbortSignal.timeout(500)]) },
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching accounts:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return logins.map(({ id, pubkey }): Account => {
|
||||||
const event = events.find((e) => e.pubkey === pubkey);
|
const event = events.find((e) => e.pubkey === pubkey);
|
||||||
try {
|
try {
|
||||||
const metadata = n.json().pipe(n.metadata()).parse(event?.content);
|
const metadata = n.json().pipe(n.metadata()).parse(event?.content);
|
||||||
@ -39,15 +57,21 @@ export function AccountSwitcher() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const [currentUser, ...otherUsers] = authors || [];
|
const [_, ...otherUsers] = (authors || []) as [Account | undefined, ...Account[]];
|
||||||
const isLoggedIn = !!currentUser;
|
|
||||||
|
|
||||||
const handleLogin = () => {
|
const handleLogin = () => {
|
||||||
setLoginDialogOpen(false);
|
setLoginDialogOpen(false);
|
||||||
setSignupDialogOpen(false);
|
setSignupDialogOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!isLoggedIn) {
|
const currentUser: Account | undefined = (() => {
|
||||||
|
const login = logins[0];
|
||||||
|
if (!login) return undefined;
|
||||||
|
const author = authors.find((a) => a.id === login.id);
|
||||||
|
return { metadata: {}, ...author, id: login.id, pubkey: login.pubkey };
|
||||||
|
})();
|
||||||
|
|
||||||
|
if (!currentUser) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Button
|
<Button
|
||||||
|
Loading…
x
Reference in New Issue
Block a user