Skip to content

Commit

Permalink
Fixing / syncing crossposting settings, cleaning up and seperating ou…
Browse files Browse the repository at this point in the history
…t nostr functions
  • Loading branch information
AustinKelsay committed Sep 26, 2023
1 parent f14d7d5 commit 53d1c24
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 212 deletions.
24 changes: 23 additions & 1 deletion components/adv-post-form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import AccordianItem from './accordian-item'
import { Input, InputUserSuggest, VariableInput } from './form'
import { Input, InputUserSuggest, VariableInput, Checkbox } from './form'
import InputGroup from 'react-bootstrap/InputGroup'
import { BOOST_MIN, BOOST_MULT, MAX_FORWARDS } from '../lib/constants'
import { DEFAULT_CROSSPOSTING_RELAYS } from '../lib/nostr'
import Info from './info'
import { numWithUnits } from '../lib/format'
import styles from './adv-post-form.module.css'
Expand Down Expand Up @@ -77,6 +78,27 @@ export default function AdvPostForm () {
)
}}
</VariableInput>
<Checkbox
label={
<div className='d-flex align-items-center'>crosspost to nostr
<Info>
<ul className='fw-bold'>
<li>crosspost this discussion item to nostr</li>
<li>requires NIP-07 extension for signing</li>
<li>we use your NIP-05 relays if set</li>
<li>otherwise we default to these relays:</li>
<ul>
{DEFAULT_CROSSPOSTING_RELAYS.map((relay, i) => (
<li key={i}>{relay}</li>
))}
</ul>
</ul>
</Info>
</div>
}
name='crosspost'
hint={<span className='text-muted'>crosspost to nostr</span>}
/>
</>
}
/>
Expand Down
35 changes: 10 additions & 25 deletions components/discussion-form.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Form, Input, MarkdownInput, SubmitButton } from '../components/form'
import { useRef } from 'react'
import { useRouter } from 'next/router'
import { gql, useApolloClient, useLazyQuery, useMutation } from '@apollo/client'
import Countdown from './countdown'
Expand All @@ -17,6 +16,7 @@ import { useCallback } from 'react'
import { crosspostDiscussion } from '../lib/nostr'
import { normalizeForwards } from '../lib/form'
import { MAX_TITLE_LENGTH } from '../lib/constants'
import {DEFAULT_CROSSPOSTING_RELAYS} from '../lib/nostr'
import { useMe } from './me'
import { useToast } from './toast'

Expand All @@ -32,7 +32,7 @@ export function DiscussionForm({
// if Web Share Target API was used
const shareTitle = router.query.title
const Toast = useToast()
const currentRetryRemoveToastRef = useRef(null);
const relays = [...DEFAULT_CROSSPOSTING_RELAYS, ...me?.nostrRelays || []];

const [upsertDiscussion] = useMutation(
gql`
Expand All @@ -43,20 +43,7 @@ export function DiscussionForm({
}`
)

let relays = [
"wss://nostrue.com/",
"wss://relay.damus.io/",
"wss://relay.nostr.band/",
"wss://relay.snort.social/",
"wss://nostr21.com/",
"wss://nostr.mutinywallet.com"
];

if (me.nostrRelays) {
relays = relays.concat(me.nostrRelays);
}

const promptUserWithToast = (failedRelays) => {
const relayError = (failedRelays) => {
return new Promise((resolve) => {
const { removeToast } = Toast.danger(
<>
Expand All @@ -71,12 +58,12 @@ export function DiscussionForm({
<Button variant="link" onClick={() => {
resolve('skip');
}}>Skip</Button>
</>
</>,
() => resolve('skip') // will skip if user closes the toast
);
});
};


const handleCrosspost = async (values, id) => {
let failedRelays;
let allSuccessful = false;
Expand All @@ -91,8 +78,7 @@ export function DiscussionForm({
failedRelays = result.failedRelays.map(relayObj => relayObj.relay);

if (failedRelays.length > 0) {
console.log("failed relays", failedRelays);
const userAction = await promptUserWithToast(failedRelays);
const userAction = await relayError(failedRelays);

if (userAction === 'skip') {
Toast.success("Crossposting skipped.");
Expand All @@ -108,7 +94,7 @@ export function DiscussionForm({
};

const onSubmit = useCallback(
async ({ boost, ...values }) => {
async ({ boost, crosspost, ...values }) => {
const { data, error } = await upsertDiscussion({
variables: {
sub: item?.subName || sub?.name,
Expand All @@ -123,13 +109,11 @@ export function DiscussionForm({
throw new Error({ message: error.toString() })
}

const userHasCrosspostingEnabled = me?.nostrCrossposting || false;
const shouldCrosspost = me?.nostrCrossposting && crosspost

if (userHasCrosspostingEnabled && data?.upsertDiscussion?.id) {
if (shouldCrosspost && data?.upsertDiscussion?.id) {
const results = await handleCrosspost(values, data.upsertDiscussion.id);
if (results.allSuccessful) {
Toast.success("Crossposting succeeded.");

if (item) {
await router.push(`/items/${item.id}`)
} else {
Expand Down Expand Up @@ -167,6 +151,7 @@ export function DiscussionForm({
initial={{
title: item?.title || shareTitle || '',
text: item?.text || '',
crosspost: me?.nostrCrossposting,
...AdvPostInitial({ forward: normalizeForwards(item?.forwards), boost: item?.boost }),
...SubSelectInitial({ sub: item?.subName || sub?.name })
}}
Expand Down
10 changes: 6 additions & 4 deletions components/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const ToastProvider = ({ children }) => {
}, [])

const removeToast = useCallback(id => {
console.log("Removing toast with id:", id); // Add this line
setToasts(toasts => toasts.filter(toast => toast.id !== id))
}, [])

Expand All @@ -35,14 +34,14 @@ export const ToastProvider = ({ children }) => {
delay: 5000
})
},
danger: body => {
danger: (body, onCloseCallback) => {
const id = toastId.current;
console.log('id', id)
dispatchToast({
id,
body,
variant: 'danger',
autohide: false,
onCloseCallback,
})
return {
removeToast: () => removeToast(id)
Expand Down Expand Up @@ -75,7 +74,10 @@ export const ToastProvider = ({ children }) => {
variant={null}
className='p-0 ps-2'
aria-label='close'
onClick={() => removeToast(toast.id)}
onClick={() => {
if (toast.onCloseCallback) toast.onCloseCallback();
removeToast(toast.id);
}}
><div className={styles.toastClose}>X</div>
</Button>
</div>
Expand Down
Loading

0 comments on commit 53d1c24

Please sign in to comment.