Skip to content

Commit

Permalink
use base64 for all prefill queries, such as duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Apr 6, 2024
1 parent 1927671 commit 15d73f7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
8 changes: 4 additions & 4 deletions packages/stateful/components/dao/CreateDaoProposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,24 @@ const InnerCreateDaoProposal = ({
return
}

const potentialDefaultValue = router.query.prefill
if (typeof potentialDefaultValue !== 'string' || !potentialDefaultValue) {
const potentialPrefill = router.query.prefill
if (typeof potentialPrefill !== 'string' || !potentialPrefill) {
setPrefillChecked(true)
return
}

// Try to parse as JSON.
let prefillData
try {
prefillData = JSON.parse(potentialDefaultValue)
prefillData = JSON.parse(potentialPrefill)
} catch (error) {
console.error(error)
}

// Try to parse as base64.
if (!prefillData) {
try {
prefillData = decodeJsonFromBase64(potentialDefaultValue)
prefillData = decodeJsonFromBase64(potentialPrefill)
} catch (error) {
console.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
CommonProposalInfo,
ProposalPrefill,
} from '@dao-dao/types'
import { keyFromPreProposeStatus } from '@dao-dao/utils'
import { encodeJsonToBase64, keyFromPreProposeStatus } from '@dao-dao/utils'

import { useActionsForMatching } from '../../actions'
import { useEntity } from '../../hooks'
Expand Down Expand Up @@ -61,7 +61,7 @@ export const DaoPreProposeApprovalProposalContentDisplay = ({
// duplicate button remains hidden until the form data is loaded.
const duplicateUrl = duplicateFormData
? getDaoProposalPath(coreAddress, 'create', {
prefill: JSON.stringify(prefill),
prefill: encodeJsonToBase64(prefill),
})
: undefined

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PreProposeModuleType,
ProposalPrefill,
} from '@dao-dao/types'
import { encodeJsonToBase64 } from '@dao-dao/utils'

import { useActionsForMatching } from '../../actions'
import { useEntity } from '../../hooks'
Expand Down Expand Up @@ -59,7 +60,7 @@ export const DaoProposalContentDisplay = ({
// duplicate button remains hidden until the form data is loaded.
const duplicateUrl = duplicateFormData
? getDaoProposalPath(coreAddress, 'create', {
prefill: JSON.stringify(prefill),
prefill: encodeJsonToBase64(prefill),
})
: undefined

Expand Down
8 changes: 4 additions & 4 deletions packages/stateful/components/gov/NewGovProposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,24 @@ export const NewGovProposal = (innerProps: NewGovProposalProps) => {
return
}

const potentialDefaultValue = router.query.prefill
if (typeof potentialDefaultValue !== 'string' || !potentialDefaultValue) {
const potentialPrefill = router.query.prefill
if (typeof potentialPrefill !== 'string' || !potentialPrefill) {
setPrefillChecked(true)
return
}

// Try to parse as JSON.
let prefillData
try {
prefillData = JSON.parse(potentialDefaultValue)
prefillData = JSON.parse(potentialPrefill)
} catch (error) {
console.error(error)
}

// Try to parse as base64.
if (!prefillData) {
try {
prefillData = decodeJsonFromBase64(potentialDefaultValue)
prefillData = decodeJsonFromBase64(potentialPrefill)
} catch (error) {
console.error(error)
}
Expand Down
34 changes: 29 additions & 5 deletions packages/stateful/components/profile/ProfileActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import {
CHAIN_GAS_MULTIPLIER,
KVPK_API_BASE,
ME_SAVED_TX_PREFIX,
decodeJsonFromBase64,
getRpcForChainId,
getSignerOptions,
objectMatchesStructure,
processError,
} from '@dao-dao/utils'

Expand Down Expand Up @@ -73,14 +75,36 @@ export const ProfileActions = () => {
// Load from prefill query.
const router = useRouter()
useEffect(() => {
if (router.query.prefill) {
const potentialPrefill = router.query.prefill
if (typeof potentialPrefill !== 'string' || !potentialPrefill) {
return
}

// Try to parse as JSON.
let prefillData
try {
prefillData = JSON.parse(potentialPrefill)
} catch (error) {
console.error(error)
}

// Try to parse as base64.
if (!prefillData) {
try {
const prefill = JSON.parse(router.query.prefill as string)
formMethods.reset(prefill)
} catch (err) {
console.error(err)
prefillData = decodeJsonFromBase64(potentialPrefill)
} catch (error) {
console.error(error)
}
}

// If prefillData looks valid, use it.
if (
objectMatchesStructure(prefillData, {
actions: {},
})
) {
formMethods.reset(prefillData)
}
}, [formMethods, router.query])

const meTransaction = formMethods.watch()
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import queryString from 'query-string'
import { ActionKeyAndDataNoId, DaoPageMode, DaoTabId } from '@dao-dao/types'

import { DaoProposalSingleAdapterId } from './constants/adapters'
import { encodeJsonToBase64 } from './messages'

// Create a path to a DAO page based on the app's page mode.
export const getDaoPath = (
Expand Down Expand Up @@ -77,7 +78,7 @@ export const getAccountPath = (
export const getMeTxPrefillPath = (actions: ActionKeyAndDataNoId[]) => {
const base = '/me/actions'
const query = `?${queryString.stringify({
prefill: JSON.stringify({
prefill: encodeJsonToBase64({
actions: actions.map((action, index) => ({
_id: index.toString(),
...action,
Expand All @@ -98,7 +99,7 @@ export const getDaoProposalSinglePrefill = ({
title?: string
description?: string
}): string =>
JSON.stringify({
encodeJsonToBase64({
id: DaoProposalSingleAdapterId,
data: {
title,
Expand Down

0 comments on commit 15d73f7

Please sign in to comment.