@akita-protocol/algomd-rn 1.0.0-alpha.0 → 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/presentation/runtime.tsx +12 -0
- package/src/refs/GuildProposalRef.tsx +285 -0
- package/src/refs/index.tsx +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akita-protocol/algomd-rn",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"types": "src/index.ts",
|
|
6
6
|
"react-native": "src/index.ts",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"vite": "^6.4.3"
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
|
-
"@akita-protocol/algomd-core": "1.0.0-alpha.
|
|
82
|
+
"@akita-protocol/algomd-core": "1.0.0-alpha.1"
|
|
83
83
|
},
|
|
84
84
|
"exports": {
|
|
85
85
|
".": "./src/index.ts",
|
|
@@ -67,6 +67,8 @@ export interface IdentityBadgeProps {
|
|
|
67
67
|
isCommunity?: boolean;
|
|
68
68
|
size?: number;
|
|
69
69
|
}
|
|
70
|
+
export type GuildVoteChoice = "approve" | "reject" | "abstain";
|
|
71
|
+
|
|
70
72
|
export type PresentationTextProps = TextProps & {
|
|
71
73
|
size?: keyof typeof sizes;
|
|
72
74
|
weight?: keyof typeof fonts;
|
|
@@ -117,7 +119,17 @@ export interface AppPresentation {
|
|
|
117
119
|
dualStakeMint?: (appId: string) => void;
|
|
118
120
|
dualStakeRedeem?: (appId: string) => void;
|
|
119
121
|
acceptTrade?: (offerId: string) => void;
|
|
122
|
+
/** Guild proposal actions. A returned promise shows progress on the card; a rejection shows its message. */
|
|
123
|
+
guildVote?: (guildAppId: string, proposalId: string, vote: GuildVoteChoice) => void | Promise<void>;
|
|
124
|
+
guildSubmit?: (guildAppId: string, proposalId: string) => void | Promise<void>;
|
|
125
|
+
guildFinalize?: (guildAppId: string, proposalId: string) => void | Promise<void>;
|
|
126
|
+
guildExecute?: (guildAppId: string, proposalId: string) => void | Promise<void>;
|
|
120
127
|
};
|
|
128
|
+
/**
|
|
129
|
+
* Why the viewer can't act on this Guild's proposals here, e.g. a chat that
|
|
130
|
+
* only acts for its own Guild. A returned note replaces the card's actions.
|
|
131
|
+
*/
|
|
132
|
+
guildActionBlock?: (guildAppId: string) => string | undefined;
|
|
121
133
|
/**
|
|
122
134
|
* Renders private content, which needs the viewer's keys to unlock. Without
|
|
123
135
|
* it, a private-content reference shows its public metadata only.
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { StyleSheet, View } from "react-native";
|
|
3
|
+
import {
|
|
4
|
+
GUILD_ZERO_ADDRESS,
|
|
5
|
+
formatRelativeTime,
|
|
6
|
+
guildBallotOutcome,
|
|
7
|
+
guildProposalStatusLabel,
|
|
8
|
+
guildRequirementsText,
|
|
9
|
+
splitGuildProposalId,
|
|
10
|
+
type GuildBallotData,
|
|
11
|
+
type GuildProposalData,
|
|
12
|
+
type GuildProposalStatus,
|
|
13
|
+
} from "@akita-protocol/algomd-core";
|
|
14
|
+
import { useGuildProposal } from "@akita-protocol/algomd-core/react";
|
|
15
|
+
import { UserGroupIcon } from "../presentation/icons";
|
|
16
|
+
import { Text, useAppTheme, usePresentation, type GuildVoteChoice } from "../presentation/runtime";
|
|
17
|
+
import { PersonRow } from "./AccountRef";
|
|
18
|
+
import { ActionButton, plural, useNow } from "./contractParts";
|
|
19
|
+
import {
|
|
20
|
+
Card,
|
|
21
|
+
CardHeader,
|
|
22
|
+
CardSkeleton,
|
|
23
|
+
headerMedia,
|
|
24
|
+
InlinePill,
|
|
25
|
+
KindTile,
|
|
26
|
+
Note,
|
|
27
|
+
Row,
|
|
28
|
+
RowSkeleton,
|
|
29
|
+
Section,
|
|
30
|
+
StatusBadge,
|
|
31
|
+
Title,
|
|
32
|
+
Unavailable,
|
|
33
|
+
usePalette,
|
|
34
|
+
type Tone,
|
|
35
|
+
} from "./primitives";
|
|
36
|
+
import type { RefViewProps } from "./types";
|
|
37
|
+
|
|
38
|
+
type CardAction = GuildVoteChoice | "submit" | "finalize" | "execute";
|
|
39
|
+
|
|
40
|
+
const PROGRESS: Record<CardAction, string> = {
|
|
41
|
+
approve: "Voting yes…",
|
|
42
|
+
reject: "Voting no…",
|
|
43
|
+
abstain: "Abstaining…",
|
|
44
|
+
submit: "Opening the vote…",
|
|
45
|
+
finalize: "Closing the vote…",
|
|
46
|
+
execute: "Carrying it out…",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const VOTED: Record<GuildVoteChoice, string> = {
|
|
50
|
+
approve: "You voted yes",
|
|
51
|
+
reject: "You voted no",
|
|
52
|
+
abstain: "You abstained",
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const STATUS_TONE: Record<GuildProposalStatus, Tone> = {
|
|
56
|
+
draft: "neutral",
|
|
57
|
+
invalid: "bad",
|
|
58
|
+
voting: "warn",
|
|
59
|
+
rejected: "bad",
|
|
60
|
+
approved: "good",
|
|
61
|
+
executed: "good",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function timeLeft(votingEnd: string, now: number): string {
|
|
65
|
+
const remaining = Number(votingEnd) - now;
|
|
66
|
+
if (remaining <= 0) return "Voting closed";
|
|
67
|
+
const minutes = Math.ceil(remaining / 60);
|
|
68
|
+
if (minutes < 60) return `${minutes}m left`;
|
|
69
|
+
const hours = Math.floor(minutes / 60);
|
|
70
|
+
return hours < 48 ? `${hours}h left` : `${Math.floor(hours / 24)}d left`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A Guild proposal: its status, what it does if it passes, and the tally against
|
|
75
|
+
* the Guild's requirements, all from chain. `params.title` and `params.summary`
|
|
76
|
+
* are the proposer's own words, shown as such. Members vote with the host's wallet.
|
|
77
|
+
*/
|
|
78
|
+
export function GuildProposalRef({ id, size = "large", params, onPress, visible }: RefViewProps) {
|
|
79
|
+
const { theme } = useAppTheme();
|
|
80
|
+
const presentation = usePresentation();
|
|
81
|
+
const proposal = useGuildProposal(id, { visible });
|
|
82
|
+
const data = proposal.data?.data;
|
|
83
|
+
const parts = splitGuildProposalId(id);
|
|
84
|
+
const label = params?.title || `Proposal #${parts?.proposalId ?? id}`;
|
|
85
|
+
const press = onPress ?? (presentation.onOpenContract ? () => presentation.onOpenContract!("guild-proposal", id) : undefined);
|
|
86
|
+
const retry = proposal.isError ? () => void proposal.refetch() : undefined;
|
|
87
|
+
|
|
88
|
+
if (size === "inline") {
|
|
89
|
+
return <InlinePill label={label} dim={proposal.isPending} leading={<UserGroupIcon size={12} color={theme.colors.primary} />} onPress={press} />;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (size === "small") {
|
|
93
|
+
if (proposal.isPending) return <RowSkeleton shape="rounded" />;
|
|
94
|
+
if (!data) return <Unavailable size="small" label={label} Icon={UserGroupIcon} onRetry={retry} />;
|
|
95
|
+
return (
|
|
96
|
+
<Row
|
|
97
|
+
leading={<KindTile Icon={UserGroupIcon} size={40} />}
|
|
98
|
+
title={<Title>{label}</Title>}
|
|
99
|
+
subtitle={`Guild proposal #${data.proposalId} · ${plural(data.actions.length, "action")}`}
|
|
100
|
+
trailing={<StatusBadge label={guildProposalStatusLabel(data.status)} tone={STATUS_TONE[data.status]} />}
|
|
101
|
+
onPress={press}
|
|
102
|
+
accessibilityLabel={`Open guild proposal: ${label}`}
|
|
103
|
+
/>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (proposal.isPending) return <CardSkeleton media="rounded" blocks={3} action full={size === "full"} />;
|
|
108
|
+
if (!data) return <Unavailable size={size} label={label} Icon={UserGroupIcon} onRetry={retry} />;
|
|
109
|
+
return (
|
|
110
|
+
<GuildProposalCard
|
|
111
|
+
data={data}
|
|
112
|
+
title={label}
|
|
113
|
+
summary={params?.summary}
|
|
114
|
+
full={size === "full"}
|
|
115
|
+
visible={visible}
|
|
116
|
+
refresh={() => void proposal.refetch()}
|
|
117
|
+
/>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function TallyBar({ ballot }: { ballot: GuildBallotData }) {
|
|
122
|
+
const { theme, tone, inset } = usePalette();
|
|
123
|
+
const approvals = Number(ballot.votes.approvals);
|
|
124
|
+
const rejections = Number(ballot.votes.rejections);
|
|
125
|
+
const abstains = Number(ballot.votes.abstains);
|
|
126
|
+
const total = approvals + rejections + abstains;
|
|
127
|
+
const share = (n: number) => (total ? `${(n / total) * 100}%` : "0%") as `${number}%`;
|
|
128
|
+
return (
|
|
129
|
+
<View style={[styles.track, { backgroundColor: inset }]} accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
|
|
130
|
+
<View style={{ width: share(approvals), backgroundColor: tone("good") }} />
|
|
131
|
+
<View style={{ width: share(rejections), backgroundColor: tone("bad") }} />
|
|
132
|
+
<View style={{ width: share(abstains), backgroundColor: theme.colors.textDim }} />
|
|
133
|
+
</View>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function GuildProposalCard({
|
|
138
|
+
data,
|
|
139
|
+
title,
|
|
140
|
+
summary,
|
|
141
|
+
full,
|
|
142
|
+
visible,
|
|
143
|
+
refresh,
|
|
144
|
+
}: {
|
|
145
|
+
data: GuildProposalData;
|
|
146
|
+
title: string;
|
|
147
|
+
summary?: string;
|
|
148
|
+
full: boolean;
|
|
149
|
+
visible?: boolean;
|
|
150
|
+
refresh: () => void;
|
|
151
|
+
}) {
|
|
152
|
+
const { theme, tone } = usePalette();
|
|
153
|
+
const presentation = usePresentation();
|
|
154
|
+
const actions = presentation.actions;
|
|
155
|
+
const now = useNow(60_000, data.status === "voting");
|
|
156
|
+
const [busy, setBusy] = useState<CardAction>();
|
|
157
|
+
const [error, setError] = useState<string>();
|
|
158
|
+
const [voted, setVoted] = useState<GuildVoteChoice>();
|
|
159
|
+
const ballot = data.ballot;
|
|
160
|
+
const outcome = ballot ? guildBallotOutcome(ballot, now) : undefined;
|
|
161
|
+
const blocked = presentation.guildActionBlock?.(data.guildAppId);
|
|
162
|
+
const open = data.status === "voting" && !!ballot && !ballot.resolved;
|
|
163
|
+
const canVote = open && outcome === undefined && now <= Number(ballot!.votingEnd) && !!actions?.guildVote;
|
|
164
|
+
const canFinalize = open && outcome !== undefined && !!actions?.guildFinalize;
|
|
165
|
+
const canExecute = data.status === "approved" && !!actions?.guildExecute;
|
|
166
|
+
const canSubmit =
|
|
167
|
+
data.status === "draft" && !!data.creator && data.creator === presentation.viewerAddress && !!actions?.guildSubmit;
|
|
168
|
+
const statusLabel = canFinalize ? (outcome === "approved" ? "Passing" : "Failing") : guildProposalStatusLabel(data.status);
|
|
169
|
+
const statusTone: Tone = canFinalize ? (outcome === "approved" ? "good" : "bad") : STATUS_TONE[data.status];
|
|
170
|
+
|
|
171
|
+
const run = async (action: CardAction) => {
|
|
172
|
+
if (busy || blocked) return;
|
|
173
|
+
setBusy(action);
|
|
174
|
+
setError(undefined);
|
|
175
|
+
try {
|
|
176
|
+
const { guildAppId, proposalId } = data;
|
|
177
|
+
if (action === "approve" || action === "reject" || action === "abstain") {
|
|
178
|
+
await actions?.guildVote?.(guildAppId, proposalId, action);
|
|
179
|
+
setVoted(action);
|
|
180
|
+
} else if (action === "submit") await actions?.guildSubmit?.(guildAppId, proposalId);
|
|
181
|
+
else if (action === "finalize") await actions?.guildFinalize?.(guildAppId, proposalId);
|
|
182
|
+
else await actions?.guildExecute?.(guildAppId, proposalId);
|
|
183
|
+
refresh();
|
|
184
|
+
} catch (cause) {
|
|
185
|
+
setError(cause instanceof Error ? cause.message : "The guild transaction failed.");
|
|
186
|
+
} finally {
|
|
187
|
+
setBusy(undefined);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<Card>
|
|
193
|
+
<CardHeader
|
|
194
|
+
media={<KindTile Icon={UserGroupIcon} size={headerMedia(full)} />}
|
|
195
|
+
title={title}
|
|
196
|
+
titleLines={3}
|
|
197
|
+
full={full}
|
|
198
|
+
status={<StatusBadge label={statusLabel} tone={statusTone} />}
|
|
199
|
+
meta={`Guild proposal #${data.proposalId}${data.created && data.created !== "0" ? ` · ${formatRelativeTime(Number(data.created))}` : ""}`}
|
|
200
|
+
/>
|
|
201
|
+
|
|
202
|
+
{summary ? (
|
|
203
|
+
<Text size="xs" numberOfLines={full ? undefined : 6} style={{ color: theme.colors.textDim }}>
|
|
204
|
+
{summary}
|
|
205
|
+
</Text>
|
|
206
|
+
) : null}
|
|
207
|
+
|
|
208
|
+
{data.actions.length ? (
|
|
209
|
+
<Section label={data.actions.length === 1 ? "If it passes" : `If it passes · ${data.actions.length} actions`}>
|
|
210
|
+
<View style={{ gap: 6 }}>
|
|
211
|
+
{data.actions.map((action, i) => (
|
|
212
|
+
<View key={`${action.type}:${i}`} style={{ gap: 6 }}>
|
|
213
|
+
<View style={styles.actionRow}>
|
|
214
|
+
<View style={[styles.dot, { backgroundColor: theme.colors.primary }]} />
|
|
215
|
+
<Text size="xs" style={{ flex: 1 }}>
|
|
216
|
+
{action.label}
|
|
217
|
+
{action.pluginAppId ? ` #${action.pluginAppId}` : ""}
|
|
218
|
+
{action.caller === GUILD_ZERO_ADDRESS ? " for members by vote" : ""}
|
|
219
|
+
</Text>
|
|
220
|
+
</View>
|
|
221
|
+
{action.caller && action.caller !== GUILD_ZERO_ADDRESS ? (
|
|
222
|
+
<PersonRow address={action.caller} role={action.type === 40 ? "Loses the plugin" : "Gets the plugin"} visible={visible} />
|
|
223
|
+
) : null}
|
|
224
|
+
</View>
|
|
225
|
+
))}
|
|
226
|
+
</View>
|
|
227
|
+
</Section>
|
|
228
|
+
) : null}
|
|
229
|
+
|
|
230
|
+
{ballot ? (
|
|
231
|
+
<View style={{ gap: 6 }}>
|
|
232
|
+
<TallyBar ballot={ballot} />
|
|
233
|
+
<View style={styles.legend}>
|
|
234
|
+
<Text size="xxs" style={{ color: theme.colors.textDim }}>
|
|
235
|
+
{`${ballot.votes.approvals} yes · ${ballot.votes.rejections} no · ${ballot.votes.abstains} abstain`}
|
|
236
|
+
</Text>
|
|
237
|
+
<Text size="xxs" style={{ color: theme.colors.textDim }}>
|
|
238
|
+
{ballot.resolved ? "Closed" : timeLeft(ballot.votingEnd, now)}
|
|
239
|
+
</Text>
|
|
240
|
+
</View>
|
|
241
|
+
{guildRequirementsText(ballot) ? <Note>{guildRequirementsText(ballot)}</Note> : null}
|
|
242
|
+
</View>
|
|
243
|
+
) : null}
|
|
244
|
+
|
|
245
|
+
{blocked ? (
|
|
246
|
+
<Note tone="warn">{blocked}</Note>
|
|
247
|
+
) : busy ? (
|
|
248
|
+
<Note>{PROGRESS[busy]}</Note>
|
|
249
|
+
) : voted && canVote ? (
|
|
250
|
+
<Text size="xs" weight="semiBold" style={{ color: tone("good") }}>
|
|
251
|
+
{VOTED[voted]}
|
|
252
|
+
</Text>
|
|
253
|
+
) : canVote ? (
|
|
254
|
+
<View style={styles.buttons}>
|
|
255
|
+
<View style={styles.flex}>
|
|
256
|
+
<ActionButton label="Yes" onPress={() => void run("approve")} accessibilityLabel="Vote yes" />
|
|
257
|
+
</View>
|
|
258
|
+
<View style={styles.flex}>
|
|
259
|
+
<ActionButton label="No" variant="secondary" onPress={() => void run("reject")} accessibilityLabel="Vote no" />
|
|
260
|
+
</View>
|
|
261
|
+
<View style={styles.flex}>
|
|
262
|
+
<ActionButton label="Abstain" variant="secondary" onPress={() => void run("abstain")} />
|
|
263
|
+
</View>
|
|
264
|
+
</View>
|
|
265
|
+
) : canFinalize ? (
|
|
266
|
+
<ActionButton label="Close the vote" onPress={() => void run("finalize")} />
|
|
267
|
+
) : canExecute ? (
|
|
268
|
+
<ActionButton label="Carry it out" onPress={() => void run("execute")} />
|
|
269
|
+
) : canSubmit ? (
|
|
270
|
+
<ActionButton label="Open voting" onPress={() => void run("submit")} />
|
|
271
|
+
) : null}
|
|
272
|
+
|
|
273
|
+
{error ? <Note tone="bad">{error}</Note> : null}
|
|
274
|
+
</Card>
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const styles = StyleSheet.create({
|
|
279
|
+
track: { borderRadius: 3, flexDirection: "row", height: 6, overflow: "hidden" },
|
|
280
|
+
legend: { flexDirection: "row", justifyContent: "space-between", gap: 12 },
|
|
281
|
+
actionRow: { alignItems: "flex-start", flexDirection: "row", gap: 8 },
|
|
282
|
+
dot: { borderRadius: 3, height: 6, marginTop: 6, width: 6 },
|
|
283
|
+
buttons: { flexDirection: "row", gap: 8 },
|
|
284
|
+
flex: { flex: 1 },
|
|
285
|
+
});
|
package/src/refs/index.tsx
CHANGED
|
@@ -17,6 +17,7 @@ import { RewardRef } from "./RewardRef";
|
|
|
17
17
|
import { SubscriptionRef } from "./SubscriptionRef";
|
|
18
18
|
import { TradeRef } from "./TradeRef";
|
|
19
19
|
import { DualStakeRef } from "./DualStakeRef";
|
|
20
|
+
import { GuildProposalRef } from "./GuildProposalRef";
|
|
20
21
|
import { PageCardRef, PageLinksProvider, PrivateContentRef, type PageLinks } from "./HostRefs";
|
|
21
22
|
import type { RefViewProps } from "./types";
|
|
22
23
|
|
|
@@ -35,6 +36,7 @@ export { RewardRef } from "./RewardRef";
|
|
|
35
36
|
export { SubscriptionRef } from "./SubscriptionRef";
|
|
36
37
|
export { TradeRef } from "./TradeRef";
|
|
37
38
|
export { DualStakeRef } from "./DualStakeRef";
|
|
39
|
+
export { GuildProposalRef } from "./GuildProposalRef";
|
|
38
40
|
export { PageCardRef, PageLinksProvider, PrivateContentRef, type PageLinks } from "./HostRefs";
|
|
39
41
|
export { useAssetInfo, useFormattedAmount, ALGO_ASSET, type AssetInfo } from "./assetInfo";
|
|
40
42
|
export { Amount, StatusChip, ProvenanceNote, useNow } from "./contractParts";
|
|
@@ -58,6 +60,7 @@ export const REF_VIEWS: Record<string, React.ComponentType<RefViewProps>> = {
|
|
|
58
60
|
subscription: SubscriptionRef,
|
|
59
61
|
trade: TradeRef,
|
|
60
62
|
"dual-stake": DualStakeRef,
|
|
63
|
+
"guild-proposal": GuildProposalRef,
|
|
61
64
|
"page-card": PageCardRef,
|
|
62
65
|
"private-content": PrivateContentRef,
|
|
63
66
|
};
|