diff --git a/src/components/ModForm.tsx b/src/components/ModForm.tsx index 38df64d..aa7b19b 100644 --- a/src/components/ModForm.tsx +++ b/src/components/ModForm.tsx @@ -25,8 +25,8 @@ import { } from '../utils' import { CheckboxField, InputError, InputField } from './Inputs' import { RelayController } from '../controllers' -import { useNavigate } from 'react-router-dom' -import { getModsInnerPageRoute } from '../routes' +import { useLocation, useNavigate } from 'react-router-dom' +import { appRoutes, getModsInnerPageRoute } from '../routes' import { DownloadUrl, ModFormState, ModDetails } from '../types' import { LoadingSpinner } from './LoadingSpinner' import { T_TAG_VALUE } from '../constants' @@ -55,6 +55,7 @@ type ModFormProps = { } export const ModForm = ({ existingModData }: ModFormProps) => { + const location = useLocation() const navigate = useNavigate() const userState = useAppSelector((state) => state.user) @@ -64,6 +65,12 @@ export const ModForm = ({ existingModData }: ModFormProps) => { initializeFormState(existingModData) ) + useEffect(() => { + if (location.pathname === appRoutes.submitMod) { + setFormState(initializeFormState()) + } + }, [location.pathname]) // Only trigger when the pathname changes to submit-mod + const [formErrors, setFormErrors] = useState({}) useEffect(() => { @@ -204,6 +211,9 @@ export const ModForm = ({ existingModData }: ModFormProps) => { const uuid = uuidv4() const currentTimeStamp = now() + const aTag = + formState.aTag || `${kinds.ClassifiedListing}:${hexPubkey}:${uuid}` + const unsignedEvent: UnsignedEvent = { kind: kinds.ClassifiedListing, created_at: currentTimeStamp, @@ -211,10 +221,7 @@ export const ModForm = ({ existingModData }: ModFormProps) => { content: formState.body, tags: [ ['d', formState.dTag || uuid], - [ - 'a', - formState.aTag || `${kinds.ClassifiedListing}:${hexPubkey}:${uuid}` - ], + ['a', aTag], ['r', formState.rTag], ['t', T_TAG_VALUE], [ @@ -267,14 +274,14 @@ export const ModForm = ({ existingModData }: ModFormProps) => { )}` ) - const nevent = nip19.neventEncode({ - id: signedEvent.id, - author: signedEvent.pubkey, + const naddr = nip19.naddrEncode({ + identifier: aTag, + pubkey: signedEvent.pubkey, kind: signedEvent.kind, relays: publishedOnRelays }) - navigate(getModsInnerPageRoute(nevent)) + navigate(getModsInnerPageRoute(naddr)) } setIsPublishing(false) diff --git a/src/pages/innerMod.tsx b/src/pages/innerMod.tsx index bb205bc..e720729 100644 --- a/src/pages/innerMod.tsx +++ b/src/pages/innerMod.tsx @@ -40,26 +40,21 @@ import { ZapButtons, ZapPresets, ZapQR } from '../components/Zap' import { getModsEditPageRoute } from '../routes' export const InnerModPage = () => { - const { nevent } = useParams() + const { naddr } = useParams() const [modData, setModData] = useState() const [isFetching, setIsFetching] = useState(true) useDidMount(async () => { - if (nevent) { - const decoded = nip19.decode<'nevent'>(nevent as `nevent1${string}`) - const eventId = decoded.data.id - const kind = decoded.data.kind - const author = decoded.data.author - const relays = decoded.data.relays || [] + if (naddr) { + const decoded = nip19.decode<'naddr'>(naddr as `naddr1${string}`) + const { identifier, kind, pubkey, relays = [] } = decoded.data const filter: Filter = { - ids: [eventId] + '#a': [identifier], + authors: [pubkey], + kinds: [kind] } - if (kind) filter.kinds = [kind] - - if (author) filter.authors = [author] - RelayController.getInstance() .fetchEvent(filter, relays) .then((event) => { @@ -115,7 +110,7 @@ export const InnerModPage = () => {
@@ -198,12 +193,12 @@ export const InnerModPage = () => { } type GameProps = { - nevent: string + naddr: string game: string author: string } -const Game = ({ nevent, game, author }: GameProps) => { +const Game = ({ naddr, game, author }: GameProps) => { const navigate = useNavigate() const userState = useAppSelector((state) => state.user) @@ -241,7 +236,7 @@ const Game = ({ nevent, game, author }: GameProps) => { {userState.auth && userState.user?.pubkey === author && ( navigate(getModsEditPageRoute(nevent))} + onClick={() => navigate(getModsEditPageRoute(naddr))} > { handleClick={() => navigate( getModsInnerPageRoute( - nip19.neventEncode({ - id: mod.id, - author: mod.author, + nip19.naddrEncode({ + identifier: mod.aTag, + pubkey: mod.author, kind: kinds.ClassifiedListing }) ) diff --git a/src/pages/submitMod.tsx b/src/pages/submitMod.tsx index 372b372..8a6304e 100644 --- a/src/pages/submitMod.tsx +++ b/src/pages/submitMod.tsx @@ -15,7 +15,7 @@ import { useDidMount } from '../hooks' export const SubmitModPage = () => { const location = useLocation() - const { nevent } = useParams() + const { naddr } = useParams() const [modData, setModData] = useState() const [isFetching, setIsFetching] = useState(false) @@ -24,21 +24,16 @@ export const SubmitModPage = () => { : 'Submit a mod' useDidMount(async () => { - if (nevent) { - const decoded = nip19.decode<'nevent'>(nevent as `nevent1${string}`) - const eventId = decoded.data.id - const kind = decoded.data.kind - const author = decoded.data.author - const relays = decoded.data.relays || [] + if (naddr) { + const decoded = nip19.decode<'naddr'>(naddr as `naddr1${string}`) + const { identifier, kind, pubkey, relays = [] } = decoded.data const filter: Filter = { - ids: [eventId] + '#a': [identifier], + authors: [pubkey], + kinds: [kind] } - if (kind) filter.kinds = [kind] - - if (author) filter.authors = [author] - setIsFetching(true) RelayController.getInstance() .fetchEvent(filter, relays) diff --git a/src/routes/index.tsx b/src/routes/index.tsx index e380edf..46d6ea7 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -13,11 +13,11 @@ export const appRoutes = { home: '/home', games: '/games', mods: '/mods', - modsInner: '/mods-inner/:nevent', + modsInner: '/mods-inner/:naddr', about: '/about', blog: '/blog', submitMod: '/submit-mod', - editMod: '/edit-mod/:nevent', + editMod: '/edit-mod/:naddr', write: '/write', settingsProfile: '/settings-profile', settingsRelays: '/settings-relays', @@ -26,10 +26,10 @@ export const appRoutes = { } export const getModsInnerPageRoute = (eventId: string) => - appRoutes.modsInner.replace(':nevent', eventId) + appRoutes.modsInner.replace(':naddr', eventId) export const getModsEditPageRoute = (eventId: string) => - appRoutes.editMod.replace(':nevent', eventId) + appRoutes.editMod.replace(':naddr', eventId) export const routes = [ {