feat: support concurrent Reddit accounts
This commit is contained in:
1 parent
d819a0f1e6
commit
16a31335d0
51 files changed
+3241
-250
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import { useState } from 'react';
|
||||
import { RedditUsernameLink } from '../../components/reddit-username-link.js';
|
||||
import { useStartConversationMutation } from './conversation-queries.js';
|
||||
import { useRedditAccountsQuery } from '../settings/settings-queries.js';
|
||||
import { ownedConversationId } from './conversation-identity.js';
|
||||
|
||||
interface StartConversationFormProps {
|
||||
onStarted(conversationId: string): void;
|
||||
@@ -9,21 +11,25 @@ interface StartConversationFormProps {
|
||||
export function StartConversationForm({ onStarted }: StartConversationFormProps) {
|
||||
const [username, setUsername] = useState('');
|
||||
const [message, setMessage] = useState('');
|
||||
const accountsQuery = useRedditAccountsQuery();
|
||||
const runningAccounts = (accountsQuery.data ?? []).filter((account) => account.isRunning);
|
||||
const [selectedAccountId, setSelectedAccountId] = useState('');
|
||||
const accountId = selectedAccountId || runningAccounts[0]?.accountId || '';
|
||||
const [successUsername, setSuccessUsername] = useState<string | null>(null);
|
||||
const mutation = useStartConversationMutation();
|
||||
const canSubmit = username.trim().length > 0 && message.trim().length > 0 && !mutation.isPending;
|
||||
const canSubmit = accountId.length > 0 && username.trim().length > 0 && message.trim().length > 0 && !mutation.isPending;
|
||||
|
||||
function submit(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
if (!canSubmit) return;
|
||||
mutation.mutate(
|
||||
{ username, message },
|
||||
{ accountId, username, message },
|
||||
{
|
||||
onSuccess(chat) {
|
||||
setSuccessUsername(chat.username);
|
||||
setUsername('');
|
||||
setMessage('');
|
||||
onStarted(chat.roomId ?? chat.username);
|
||||
onStarted(ownedConversationId(chat));
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -31,6 +37,12 @@ export function StartConversationForm({ onStarted }: StartConversationFormProps)
|
||||
|
||||
return (
|
||||
<form className="start-conversation-form" onSubmit={submit}>
|
||||
<div className="conversation-field">
|
||||
<label htmlFor="new-conversation-account">Send from account</label>
|
||||
<select id="new-conversation-account" onChange={(event) => setSelectedAccountId(event.target.value)} value={accountId}>
|
||||
{runningAccounts.map((account) => <option key={account.accountId} value={account.accountId}>u/{account.redditUsername}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
<div className="conversation-field">
|
||||
<label htmlFor="new-conversation-username">Reddit username</label>
|
||||
<input
|
||||
|
||||
Reference in new issue
Block a user