Skip to content

Commit

Permalink
Get status on upload, show toast, redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrain99 committed Nov 8, 2024
1 parent bcc739e commit 9ad778b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 38 deletions.
8 changes: 7 additions & 1 deletion src/dal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ const uploadSchema = z
tags: z.array(z.object({ name: z.string(), value: z.union([z.string(), z.array(z.string())]) }))
})
)
.returns(z.promise(z.string()))
.returns(z.promise(
z.object({
status: z.string(),
error: z.string().nullish(),
txId: z.string().nullish()
})
))

type Services = {
connect: z.infer<typeof getActiveAddressSchema>
Expand Down
3 changes: 1 addition & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ export default {
}
return Rejected('Not Vouched')
})
.bichain(
Rejected,
.chain(
({ addr }) => {
return stamp(tx, addr)
},
Expand Down
3 changes: 1 addition & 2 deletions src/pages/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ const EditorComponent: preact.FunctionComponent<Props> = ({ tx }) => {
};

useEffect(() => {
console.log(2, { current, loaded, context })
if (current === "ready" && !loaded && context.spec) {
setLoaded(true);

Expand Down Expand Up @@ -194,7 +193,7 @@ const EditorComponent: preact.FunctionComponent<Props> = ({ tx }) => {
</p>
</div>
<button class="btn btn-outline btn-block btn-error" onClick={() => {
route("/", true)
route(`/view/${context.txId}`)
}}>
ok
</button>
Expand Down
16 changes: 11 additions & 5 deletions src/pages/form/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import yaml from 'js-yaml'
import services from "../../services"
import Api from "../../lib"
import { FormMachineContext, FormMachineCurrent, FormMachineEvent, FormMachineSend } from "./types"
import { ZodError } from "zod"

const api = Api.init(services)

Expand Down Expand Up @@ -60,17 +61,22 @@ const machine = createMachine({
${ctx.md}`,
)
// add saved doc to local cache -- hold for now...
//.map(({ id }) => (cache.update(assoc(id, ctx.md)), { id }))
.map(({ id }) => ({ ...ctx, id }))
.toPromise()
},
transition("done", "confirm"),
transition(
"done",
"confirm",
reduce((ctx: FormMachineContext, ev: FormMachineEvent) => {
const { txId } = ev.data as { txId?: string }
return { ...ctx, txId }
}),
),
transition(
"error",
"ready",
reduce((ctx: FormMachineContext, ev: FormMachineEvent) => {
return { ...ctx, error: ev.error }
const { error } = ev.data as { error?: string }
return { ...ctx, saveError: error }
}),
),
),
Expand Down
5 changes: 4 additions & 1 deletion src/pages/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import { ZodError } from "zod"
export interface FormMachineContext {
type?: string
tx?: string | null
txId?: string | null
spec?: FormSpec
md?: string
metadata?: Metadata
error?: ZodError
saveError?: string
id?: string
}

export interface FormMachineEvent {
type: string
md?: string
data?: FormSpec
data?: FormSpec | { txId?: string, error?: string }
metadata?: Metadata
error?: ZodError
saveError?: string
[key: string]: unknown
}

Expand Down
1 change: 0 additions & 1 deletion src/pages/home/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const machine = createMachine({
transition(
"done",
"view",
// TODO: fix this type when stamping is working
reduce((ctx: HomeMachineContext, ev: { data: number }) => {
const specs = ctx.specs.map((s) =>
s.id === ctx.selected.id ? assoc("stamps", ev.data, s) : s,
Expand Down
44 changes: 26 additions & 18 deletions src/services/ao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ const VOUCHER_WHITELIST = [
"3y0YE11i21hpP8UY0Z1AVhtPoJD4V_AbEBx-g0j9wRc", // Vouch-wAR-Stake
]
const MIN_VOUCH_SCORE = 2
const getFirstMessage = compose(head, propOr([], 'Messages'))
export const upload = async (md: {
data: string
tags: {
name: string
value: string
}[]
}) => {
const getTag = (n: string): string | undefined =>
md.tags.find(tag => tag.name === n)?.value
const getTag = (tags: Array<{ name: string, value: string }>) => (n: string): string | undefined =>
tags.find(tag => tag.name === n)?.value

const getMetadataTag = getTag(md.tags)
const args = {
process: SPEC_PID,
tags: [
Expand All @@ -29,56 +31,62 @@ export const upload = async (md: {
},
{
name: "Spec-DataProtocol",
value: getTag('Data-Protocol')
value: getMetadataTag('Data-Protocol')
},
{
name: "Spec-GroupId",
value: getTag('GroupId')
value: getMetadataTag('GroupId')
},
{
name: "Spec-Variant",
value: getTag('Variant')
value: getMetadataTag('Variant')
},
{
name: "Spec-Title",
value: getTag('Title')
value: getMetadataTag('Title')
},
{
name: "Spec-Description",
value: getTag('Description')
value: getMetadataTag('Description')
},
{
name: "Spec-Topics",
value: getTag('Topics').toString()
value: getMetadataTag('Topics').toString()
},
{
name: "Spec-Authors",
value: getTag('Authors').toString()
value: getMetadataTag('Authors').toString()
},
{
name: "Spec-Type",
value: getTag('Type')
value: getMetadataTag('Type')
},
{
name: "Spec-Forks",
value: getTag('Forks')
value: getMetadataTag('Forks')
},
{
name: "Spec-Content-Type",
value: getTag('Content-Type')
value: getMetadataTag('Content-Type')
},
{
name: "Spec-Render-With",
value: getTag('Render-With')
value: getMetadataTag('Render-With')
},
],
data: md.data,
signer: createDataItemSigner(window.arweaveWallet)
}

const result = await message(args)
const messageId = await message(args)
const messageResult = await result({ process: SPEC_PID, message: messageId })
const tags = compose(
propOr([], 'Tags'),
getFirstMessage
)(messageResult) as Array<{ name: string, value: string }>

return result
const getResultTag = getTag(tags)
return { status: getResultTag('Result'), error: getResultTag('Error'), txId: getResultTag('ID') }
}

export const query = async (tx: string) => {
Expand Down Expand Up @@ -165,8 +173,7 @@ export const isVouched = async (tx: string) => {
propOr({}, 'Vouchers'),
JSON.parse,
propOr('{}', 'Data'),
head,
propOr([], 'Messages')
getFirstMessage
)(messageResult)

let vouchScore = 0
Expand All @@ -177,6 +184,7 @@ export const isVouched = async (tx: string) => {
if (vouchFor != tx) {
throw new Error('Vouch has Vouch-For mismatch')
}
// The value is a string like "4.5-USD" or "5-USD". We want to match the number.
const valueStr = vouch['Value'].match(/^(\d+\.\d+)|(\d+)/g)?.[0] ?? "0"
const value = parseFloat(valueStr)
if (valueStr == null || value == null) {
Expand All @@ -185,5 +193,5 @@ export const isVouched = async (tx: string) => {
vouchScore += value
}
}
return { addr: tx, vouched: Boolean(vouchScore > MIN_VOUCH_SCORE) }
return { addr: tx, vouched: vouchScore > MIN_VOUCH_SCORE }
}
17 changes: 9 additions & 8 deletions src/services/stamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ export const stampCounts = (txs: string[]) => {

export const stamp = (tx: string) => {
return stamps.hasStamped(tx).then((s) => {
return !s
? stamps
.stamp(tx)
.then(() => new Promise((r) => setTimeout(r, 500)))
.then(() => stamps.count(tx).then(prop("vouched")))
: Promise.reject("Already Stamped!")
if (s) {
return Promise.reject('Already Stamped!')
}
)
const addOne = (n: number) => n + 1
const count = stampCount(tx).then(addOne)
return stamps
.stamp(tx)
.then(() => count)
})
}

export const stampCount = (tx) => {
export const stampCount = (tx: string) => {
return stamps.count(tx).then(prop("vouched"));
}

0 comments on commit 9ad778b

Please sign in to comment.