|
|
@ -1,12 +1,12 @@
|
|
|
|
|
|
|
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
|
|
|
|
import { patchMe } from 'soapbox/actions/me';
|
|
|
|
import { patchMe } from 'soapbox/actions/me';
|
|
|
|
import List, { ListItem } from 'soapbox/components/list';
|
|
|
|
import List, { ListItem } from 'soapbox/components/list';
|
|
|
|
import { Button, Column, Emoji, HStack, Icon, Input, Tooltip } from 'soapbox/components/ui';
|
|
|
|
import { Button, CardHeader, CardTitle, Column, Emoji, Form, HStack, Icon, Input, Textarea, Tooltip } from 'soapbox/components/ui';
|
|
|
|
import { useNostr } from 'soapbox/contexts/nostr-context';
|
|
|
|
import { useApi, useAppDispatch, useInstance, useOwnAccount } from 'soapbox/hooks';
|
|
|
|
import { useNostrReq } from 'soapbox/features/nostr/hooks/useNostrReq';
|
|
|
|
import { adminAccountSchema } from 'soapbox/schemas/admin-account';
|
|
|
|
import { useAppDispatch, useInstance, useOwnAccount } from 'soapbox/hooks';
|
|
|
|
|
|
|
|
import toast from 'soapbox/toast';
|
|
|
|
import toast from 'soapbox/toast';
|
|
|
|
|
|
|
|
|
|
|
|
interface IEditIdentity {
|
|
|
|
interface IEditIdentity {
|
|
|
@ -18,6 +18,7 @@ const messages = defineMessages({
|
|
|
|
unverified: { id: 'edit_profile.fields.nip05_unverified', defaultMessage: 'Name could not be verified and won\'t be used.' },
|
|
|
|
unverified: { id: 'edit_profile.fields.nip05_unverified', defaultMessage: 'Name could not be verified and won\'t be used.' },
|
|
|
|
success: { id: 'edit_profile.success', defaultMessage: 'Your profile has been successfully saved!' },
|
|
|
|
success: { id: 'edit_profile.success', defaultMessage: 'Your profile has been successfully saved!' },
|
|
|
|
error: { id: 'edit_profile.error', defaultMessage: 'Profile update failed' },
|
|
|
|
error: { id: 'edit_profile.error', defaultMessage: 'Profile update failed' },
|
|
|
|
|
|
|
|
placeholder: { id: 'edit_identity.reason_placeholder', defaultMessage: 'Why do you want this name?' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/** EditIdentity component. */
|
|
|
|
/** EditIdentity component. */
|
|
|
@ -26,51 +27,59 @@ const EditIdentity: React.FC<IEditIdentity> = () => {
|
|
|
|
const instance = useInstance();
|
|
|
|
const instance = useInstance();
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const { account } = useOwnAccount();
|
|
|
|
const { account } = useOwnAccount();
|
|
|
|
const { relay, signer } = useNostr();
|
|
|
|
const { mutate } = useRequestName();
|
|
|
|
|
|
|
|
|
|
|
|
const admin = instance.nostr?.pubkey;
|
|
|
|
const { data: approvedNames } = useNames();
|
|
|
|
const pubkey = account?.nostr?.pubkey;
|
|
|
|
const { data: pendingNames } = usePendingNames();
|
|
|
|
const [username, setUsername] = useState<string>('');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { events: labels } = useNostrReq(
|
|
|
|
const [username, setUsername] = useState<string>('');
|
|
|
|
(admin && pubkey)
|
|
|
|
const [reason, setReason] = useState<string>('');
|
|
|
|
? [{ kinds: [1985], authors: [admin], '#L': ['nip05'], '#p': [pubkey] }]
|
|
|
|
|
|
|
|
: [],
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!account) return null;
|
|
|
|
if (!account) return null;
|
|
|
|
|
|
|
|
|
|
|
|
const updateNip05 = async (nip05: string): Promise<void> => {
|
|
|
|
const updateName = async (name: string): Promise<void> => {
|
|
|
|
if (account.source?.nostr?.nip05 === nip05) return;
|
|
|
|
if (account.source?.nostr?.nip05 === name) return;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await dispatch(patchMe({ nip05 }));
|
|
|
|
await dispatch(patchMe({ nip05: name }));
|
|
|
|
toast.success(intl.formatMessage(messages.success));
|
|
|
|
toast.success(intl.formatMessage(messages.success));
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
toast.error(intl.formatMessage(messages.error));
|
|
|
|
toast.error(intl.formatMessage(messages.error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
const submit = () => {
|
|
|
|
if (!admin || !signer || !relay) return;
|
|
|
|
const name = `${username}@${instance.domain}`;
|
|
|
|
|
|
|
|
mutate({ name, reason });
|
|
|
|
const event = await signer.signEvent({
|
|
|
|
|
|
|
|
kind: 5950,
|
|
|
|
|
|
|
|
content: '',
|
|
|
|
|
|
|
|
tags: [
|
|
|
|
|
|
|
|
['i', `${username}@${instance.domain}`, 'text'],
|
|
|
|
|
|
|
|
['p', admin],
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
created_at: Math.floor(Date.now() / 1000),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await relay.event(event);
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<Column label={intl.formatMessage(messages.title)}>
|
|
|
|
<Column label={intl.formatMessage(messages.title)}>
|
|
|
|
|
|
|
|
<div className='space-y-4'>
|
|
|
|
|
|
|
|
<Form>
|
|
|
|
|
|
|
|
<UsernameInput value={username} onChange={(e) => setUsername(e.target.value)} />
|
|
|
|
|
|
|
|
<Textarea
|
|
|
|
|
|
|
|
name='reason'
|
|
|
|
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
|
|
|
|
|
|
|
maxLength={500}
|
|
|
|
|
|
|
|
onChange={(e) => setReason(e.target.value)}
|
|
|
|
|
|
|
|
value={reason}
|
|
|
|
|
|
|
|
autoGrow
|
|
|
|
|
|
|
|
required
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Button theme='accent' onClick={submit}>
|
|
|
|
|
|
|
|
<FormattedMessage id='edit_identity.request' defaultMessage='Request' />
|
|
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{((approvedNames?.length ?? 0) > 0) && (
|
|
|
|
|
|
|
|
<>
|
|
|
|
|
|
|
|
<CardHeader>
|
|
|
|
|
|
|
|
<CardTitle title={<FormattedMessage id='edit_identity.names_title' defaultMessage='Names' />} />
|
|
|
|
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
|
|
<List>
|
|
|
|
<List>
|
|
|
|
{labels.map((label) => {
|
|
|
|
{approvedNames?.map(({ username, domain }) => {
|
|
|
|
const identifier = label.tags.find(([name]) => name === 'l')?.[1];
|
|
|
|
const identifier = `${username}@${domain}`;
|
|
|
|
if (!identifier) return null;
|
|
|
|
if (!identifier) return null;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
@ -89,14 +98,40 @@ const EditIdentity: React.FC<IEditIdentity> = () => {
|
|
|
|
</HStack>
|
|
|
|
</HStack>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isSelected={account.source?.nostr?.nip05 === identifier}
|
|
|
|
isSelected={account.source?.nostr?.nip05 === identifier}
|
|
|
|
onSelect={() => updateNip05(identifier)}
|
|
|
|
onSelect={() => updateName(identifier)}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
</List>
|
|
|
|
|
|
|
|
</>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{((pendingNames?.length ?? 0) > 0) && (
|
|
|
|
|
|
|
|
<>
|
|
|
|
|
|
|
|
<CardHeader>
|
|
|
|
|
|
|
|
<CardTitle title={<FormattedMessage id='edit_identity.pending_names_title' defaultMessage='Requested Names' />} />
|
|
|
|
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<List>
|
|
|
|
|
|
|
|
{pendingNames?.map(({ username, domain }) => {
|
|
|
|
|
|
|
|
const identifier = `${username}@${domain}`;
|
|
|
|
|
|
|
|
if (!identifier) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<ListItem
|
|
|
|
|
|
|
|
key={identifier}
|
|
|
|
|
|
|
|
label={
|
|
|
|
|
|
|
|
<HStack alignItems='center' space={2}>
|
|
|
|
|
|
|
|
<span>{identifier}</span>
|
|
|
|
|
|
|
|
</HStack>
|
|
|
|
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
})}
|
|
|
|
<ListItem label={<UsernameInput value={username} onChange={(e) => setUsername(e.target.value)} />}>
|
|
|
|
|
|
|
|
<Button theme='accent' onClick={submit}>Add</Button>
|
|
|
|
|
|
|
|
</ListItem>
|
|
|
|
|
|
|
|
</List>
|
|
|
|
</List>
|
|
|
|
|
|
|
|
</>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</Column>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -119,4 +154,43 @@ const UsernameInput: React.FC<React.ComponentProps<typeof Input>> = (props) => {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface NameRequestData {
|
|
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
reason?: string;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function useRequestName() {
|
|
|
|
|
|
|
|
const api = useApi();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
|
|
|
mutationFn: (data: NameRequestData) => api.post('/api/v1/ditto/names', data),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function useNames() {
|
|
|
|
|
|
|
|
const api = useApi();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
|
|
queryKey: ['names', 'approved'],
|
|
|
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
|
|
|
const { data } = await api.get('/api/v1/ditto/names?approved=true');
|
|
|
|
|
|
|
|
return adminAccountSchema.array().parse(data);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
placeholderData: [],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function usePendingNames() {
|
|
|
|
|
|
|
|
const api = useApi();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
|
|
queryKey: ['names', 'pending'],
|
|
|
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
|
|
|
const { data } = await api.get('/api/v1/ditto/names?approved=false');
|
|
|
|
|
|
|
|
return adminAccountSchema.array().parse(data);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
placeholderData: [],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default EditIdentity;
|
|
|
|
export default EditIdentity;
|