feat: mod submission try again
This commit is contained in:
parent
a247f05f6e
commit
5d479102d4
@ -18,9 +18,9 @@ import { useGames } from '../hooks'
|
|||||||
import '../styles/styles.css'
|
import '../styles/styles.css'
|
||||||
import {
|
import {
|
||||||
DownloadUrl,
|
DownloadUrl,
|
||||||
FormErrors,
|
|
||||||
ModFormState,
|
ModFormState,
|
||||||
ModPageLoaderResult
|
ModPageLoaderResult,
|
||||||
|
SubmitModActionResult
|
||||||
} from '../types'
|
} from '../types'
|
||||||
import { initializeFormState, MOD_DRAFT_CACHE_KEY } from '../utils'
|
import { initializeFormState, MOD_DRAFT_CACHE_KEY } from '../utils'
|
||||||
import { CheckboxField, InputField, InputFieldWithImageUpload } from './Inputs'
|
import { CheckboxField, InputField, InputFieldWithImageUpload } from './Inputs'
|
||||||
@ -41,7 +41,11 @@ interface GameOption {
|
|||||||
export const ModForm = () => {
|
export const ModForm = () => {
|
||||||
const data = useLoaderData() as ModPageLoaderResult
|
const data = useLoaderData() as ModPageLoaderResult
|
||||||
const mod = data?.mod
|
const mod = data?.mod
|
||||||
const formErrors = useActionData() as FormErrors
|
const actionData = useActionData() as SubmitModActionResult
|
||||||
|
const formErrors = useMemo(
|
||||||
|
() => (actionData?.type === 'validation' ? actionData.error : undefined),
|
||||||
|
[actionData]
|
||||||
|
)
|
||||||
const navigation = useNavigation()
|
const navigation = useNavigation()
|
||||||
const submit = useSubmit()
|
const submit = useSubmit()
|
||||||
const games = useGames()
|
const games = useGames()
|
||||||
@ -56,7 +60,17 @@ export const ModForm = () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
!isEditing && setCache(formState)
|
if (!isEditing) {
|
||||||
|
const newCache = _.cloneDeep(formState)
|
||||||
|
|
||||||
|
// Remove aTag, dTag and published_at from cache
|
||||||
|
// These are used for editing and try again timeout
|
||||||
|
newCache.aTag = ''
|
||||||
|
newCache.dTag = ''
|
||||||
|
newCache.published_at = 0
|
||||||
|
|
||||||
|
setCache(newCache)
|
||||||
|
}
|
||||||
}, [formState, isEditing, setCache])
|
}, [formState, isEditing, setCache])
|
||||||
|
|
||||||
const editorRef = useRef<EditorRef>(null)
|
const editorRef = useRef<EditorRef>(null)
|
||||||
@ -154,6 +168,32 @@ export const ModForm = () => {
|
|||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
|
const [showTryAgainPopup, setShowTryAgainPopup] = useState<boolean>(false)
|
||||||
|
useEffect(() => {
|
||||||
|
const isTimeout = actionData?.type === 'timeout'
|
||||||
|
setShowTryAgainPopup(isTimeout)
|
||||||
|
if (isTimeout) {
|
||||||
|
setFormState((prev) => ({
|
||||||
|
...prev,
|
||||||
|
aTag: actionData.data.aTag,
|
||||||
|
dTag: actionData.data.dTag,
|
||||||
|
published_at: actionData.data.published_at
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}, [actionData])
|
||||||
|
const handleTryAgainConfirm = useCallback(
|
||||||
|
(confirm: boolean) => {
|
||||||
|
setShowTryAgainPopup(false)
|
||||||
|
|
||||||
|
// Cancel if not confirmed
|
||||||
|
if (!confirm) return
|
||||||
|
submit(JSON.stringify(formState), {
|
||||||
|
method: isEditing ? 'put' : 'post',
|
||||||
|
encType: 'application/json'
|
||||||
|
})
|
||||||
|
},
|
||||||
|
[formState, isEditing, submit]
|
||||||
|
)
|
||||||
|
|
||||||
const [showConfirmPopup, setShowConfirmPopup] = useState<boolean>(false)
|
const [showConfirmPopup, setShowConfirmPopup] = useState<boolean>(false)
|
||||||
const handleReset = useCallback(() => {
|
const handleReset = useCallback(() => {
|
||||||
@ -426,13 +466,21 @@ export const ModForm = () => {
|
|||||||
{navigation.state === 'submitting' ? 'Publishing...' : 'Publish'}
|
{navigation.state === 'submitting' ? 'Publishing...' : 'Publish'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
{showTryAgainPopup && (
|
||||||
|
<AlertPopup
|
||||||
|
handleConfirm={handleTryAgainConfirm}
|
||||||
|
handleClose={() => setShowTryAgainPopup(false)}
|
||||||
|
header={'Publish'}
|
||||||
|
label={`Submission timed out. Do you want to try again?`}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{showConfirmPopup && (
|
{showConfirmPopup && (
|
||||||
<AlertPopup
|
<AlertPopup
|
||||||
handleConfirm={handleResetConfirm}
|
handleConfirm={handleResetConfirm}
|
||||||
handleClose={() => setShowConfirmPopup(false)}
|
handleClose={() => setShowConfirmPopup(false)}
|
||||||
header={'Are you sure?'}
|
header={'Are you sure?'}
|
||||||
label={
|
label={
|
||||||
mod
|
isEditing
|
||||||
? `Are you sure you want to clear all changes?`
|
? `Are you sure you want to clear all changes?`
|
||||||
: `Are you sure you want to clear all field data?`
|
: `Are you sure you want to clear all field data?`
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,12 @@ import { ActionFunctionArgs, redirect } from 'react-router-dom'
|
|||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { getModPageRoute } from 'routes'
|
import { getModPageRoute } from 'routes'
|
||||||
import { store } from 'store'
|
import { store } from 'store'
|
||||||
import { FormErrors, ModFormState } from 'types'
|
import {
|
||||||
|
FormErrors,
|
||||||
|
ModFormState,
|
||||||
|
SubmitModActionResult,
|
||||||
|
TimeoutError
|
||||||
|
} from 'types'
|
||||||
import {
|
import {
|
||||||
isReachable,
|
isReachable,
|
||||||
isValidImageUrl,
|
isValidImageUrl,
|
||||||
@ -14,7 +19,8 @@ import {
|
|||||||
LogType,
|
LogType,
|
||||||
MOD_DRAFT_CACHE_KEY,
|
MOD_DRAFT_CACHE_KEY,
|
||||||
now,
|
now,
|
||||||
removeLocalStorageItem
|
removeLocalStorageItem,
|
||||||
|
timeout
|
||||||
} from 'utils'
|
} from 'utils'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
import { T_TAG_VALUE } from '../../constants'
|
import { T_TAG_VALUE } from '../../constants'
|
||||||
@ -51,29 +57,31 @@ export const submitModRouteAction =
|
|||||||
// Check for errors
|
// Check for errors
|
||||||
const formErrors = await validateState(formState)
|
const formErrors = await validateState(formState)
|
||||||
|
|
||||||
// Return earily if there are any errors
|
// Return early if there are any errors
|
||||||
if (Object.keys(formErrors).length) return formErrors
|
if (Object.keys(formErrors).length) {
|
||||||
|
return {
|
||||||
|
type: 'validation',
|
||||||
|
error: formErrors
|
||||||
|
} as SubmitModActionResult
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentTimeStamp = now()
|
||||||
|
|
||||||
// Check if we are editing or this is a new mob
|
|
||||||
const { naddr } = params
|
const { naddr } = params
|
||||||
|
// Check if we are editing or this is a new mob
|
||||||
const isEditing = naddr && request.method === 'PUT'
|
const isEditing = naddr && request.method === 'PUT'
|
||||||
|
|
||||||
const uuid = formState.dTag || uuidv4()
|
const uuid = formState.dTag || uuidv4()
|
||||||
const currentTimeStamp = now()
|
|
||||||
const aTag =
|
const aTag =
|
||||||
formState.aTag || `${kinds.ClassifiedListing}:${hexPubkey}:${uuid}`
|
formState.aTag || `${kinds.ClassifiedListing}:${hexPubkey}:${uuid}`
|
||||||
|
const published_at = formState.published_at || currentTimeStamp
|
||||||
|
|
||||||
const tags = [
|
const tags = [
|
||||||
['d', uuid],
|
['d', uuid],
|
||||||
['a', aTag],
|
['a', aTag],
|
||||||
['r', formState.rTag],
|
['r', formState.rTag],
|
||||||
['t', T_TAG_VALUE],
|
['t', T_TAG_VALUE],
|
||||||
[
|
['published_at', published_at.toString()],
|
||||||
'published_at',
|
|
||||||
isEditing
|
|
||||||
? formState.published_at.toString()
|
|
||||||
: currentTimeStamp.toString()
|
|
||||||
],
|
|
||||||
['game', formState.game],
|
['game', formState.game],
|
||||||
['title', formState.title],
|
['title', formState.title],
|
||||||
['featuredImageUrl', formState.featuredImageUrl],
|
['featuredImageUrl', formState.featuredImageUrl],
|
||||||
@ -131,32 +139,52 @@ export const submitModRouteAction =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const ndkEvent = new NDKEvent(ndkContext.ndk, signedEvent)
|
const ndkEvent = new NDKEvent(ndkContext.ndk, signedEvent)
|
||||||
const publishedOnRelays = await ndkContext.publish(ndkEvent)
|
// Publishing a mod sometime hangs (ndk.publish has internal timeout of 10s)
|
||||||
|
// Make sure to actually throw a timeout error (30s)
|
||||||
|
try {
|
||||||
|
const publishedOnRelays = await Promise.race([
|
||||||
|
ndkContext.publish(ndkEvent),
|
||||||
|
timeout(30000)
|
||||||
|
])
|
||||||
|
// Handle cases where publishing failed or succeeded
|
||||||
|
if (publishedOnRelays.length === 0) {
|
||||||
|
toast.error('Failed to publish event on any relay')
|
||||||
|
} else {
|
||||||
|
toast.success(
|
||||||
|
`Event published successfully to the following relays\n\n${publishedOnRelays.join(
|
||||||
|
'\n'
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
|
||||||
// Handle cases where publishing failed or succeeded
|
!isEditing && removeLocalStorageItem(MOD_DRAFT_CACHE_KEY)
|
||||||
if (publishedOnRelays.length === 0) {
|
|
||||||
toast.error('Failed to publish event on any relay')
|
|
||||||
} else {
|
|
||||||
toast.success(
|
|
||||||
`Event published successfully to the following relays\n\n${publishedOnRelays.join(
|
|
||||||
'\n'
|
|
||||||
)}`
|
|
||||||
)
|
|
||||||
|
|
||||||
!isEditing && removeLocalStorageItem(MOD_DRAFT_CACHE_KEY)
|
const naddr = nip19.naddrEncode({
|
||||||
|
identifier: aTag,
|
||||||
|
pubkey: signedEvent.pubkey,
|
||||||
|
kind: signedEvent.kind,
|
||||||
|
relays: publishedOnRelays
|
||||||
|
})
|
||||||
|
|
||||||
const naddr = nip19.naddrEncode({
|
return redirect(getModPageRoute(naddr))
|
||||||
identifier: aTag,
|
}
|
||||||
pubkey: signedEvent.pubkey,
|
} catch (error) {
|
||||||
kind: signedEvent.kind,
|
if (error instanceof TimeoutError) {
|
||||||
relays: publishedOnRelays
|
const result: SubmitModActionResult = {
|
||||||
})
|
type: 'timeout',
|
||||||
|
data: {
|
||||||
|
dTag: uuid,
|
||||||
|
aTag,
|
||||||
|
published_at: published_at
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
return redirect(getModPageRoute(naddr))
|
// Rethrow non-timeout for general catch
|
||||||
|
throw error
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(true, LogType.Error, 'Failed to sign the event!', error)
|
log(true, LogType.Error, 'Failed to sign the event!', error)
|
||||||
toast.error('Failed to sign the event!')
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +68,17 @@ export interface ModPageLoaderResult {
|
|||||||
isRepost: boolean
|
isRepost: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SubmitModActionResult =
|
||||||
|
| { type: 'validation'; error: FormErrors }
|
||||||
|
| {
|
||||||
|
type: 'timeout'
|
||||||
|
data: {
|
||||||
|
dTag: string
|
||||||
|
aTag: string
|
||||||
|
published_at: number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface FormErrors {
|
export interface FormErrors {
|
||||||
game?: string
|
game?: string
|
||||||
title?: string
|
title?: string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user