Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/lib/dtoParsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { ChamberDto, FormationProposalPageDto } from "@/types/api";

export function parseCommaNumber(value: string): number {
const cleaned = value.replace(/,/g, "").trim();
const parsed = Number.parseInt(cleaned, 10);
return Number.isFinite(parsed) ? parsed : 0;
}

export function parsePercent(value: string): number {
const cleaned = value.replace(/%/g, "").trim();
const parsed = Number.parseInt(cleaned, 10);
return Number.isFinite(parsed) ? parsed : 0;
}

export function parseRatio(value: string): { a: number; b: number } {
const parts = value
.split("/")
.map((part) => Number.parseInt(part.trim(), 10));
if (parts.length !== 2) return { a: 0, b: 0 };
const [a, b] = parts;
return {
a: Number.isFinite(a) ? a : 0,
b: Number.isFinite(b) ? b : 0,
};
}

export function getChamberNumericStats(chamber: ChamberDto) {
return {
governors: parseCommaNumber(chamber.stats.governors),
acm: parseCommaNumber(chamber.stats.acm),
lcm: parseCommaNumber(chamber.stats.lcm),
mcm: parseCommaNumber(chamber.stats.mcm),
};
}

export function computeChamberMetrics(chambers: ChamberDto[]) {
const totalAcm = chambers.reduce((sum, chamber) => {
const { acm } = getChamberNumericStats(chamber);
return sum + acm;
}, 0);
const liveProposals = chambers.reduce(
(sum, chamber) => sum + (chamber.pipeline.vote ?? 0),
0,
);
return {
totalChambers: chambers.length,
totalAcm,
liveProposals,
};
}

export function getFormationProgress(formationPage: FormationProposalPageDto) {
return {
progressValue: parsePercent(formationPage.progress),
team: parseRatio(formationPage.teamSlots),
milestones: parseRatio(formationPage.milestones),
};
}
38 changes: 38 additions & 0 deletions src/lib/proposalSubmitErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getApiErrorPayload } from "@/lib/apiClient";
import { formatProposalType } from "@/lib/proposalTypes";

export function formatProposalSubmitError(error: unknown): string {
const payload = getApiErrorPayload(error);
const details = payload?.error ?? null;
if (!details) return (error as Error).message ?? "Submit failed.";

const code = typeof details.code === "string" ? details.code : "";
if (code === "proposal_type_ineligible" || code === "tier_ineligible") {
const requiredTier =
typeof details.requiredTier === "string"
? details.requiredTier
: "a higher tier";
const proposalType =
typeof details.proposalType === "string"
? formatProposalType(details.proposalType)
: "this";
return `Not eligible for ${proposalType} proposals. Required tier: ${requiredTier}.`;
}

if (code === "proposal_submit_ineligible") {
const chamberId =
typeof details.chamberId === "string" ? details.chamberId : "";
if (chamberId === "general") {
return "General chamber proposals require voting rights in any chamber.";
}
if (chamberId) {
return `Only chamber members can submit to ${formatProposalType(chamberId)}.`;
}
}

if (code === "draft_not_submittable") {
return "Draft is incomplete. Fill required fields before submitting.";
}

return details.message ?? (error as Error).message ?? "Submit failed.";
}
12 changes: 12 additions & 0 deletions src/lib/proposalTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const proposalTypeLabel: Record<string, string> = {
basic: "Basic",
fee: "Fee distribution",
monetary: "Monetary system",
core: "Core infrastructure",
administrative: "Administrative",
"dao-core": "DAO core",
};

export function formatProposalType(value: string): string {
return proposalTypeLabel[value] ?? value.replace(/-/g, " ");
}
27 changes: 10 additions & 17 deletions src/pages/chambers/Chambers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { Link } from "react-router";
import { InlineHelp } from "@/components/InlineHelp";
import { NoDataYetBar } from "@/components/NoDataYetBar";
import { apiChambers } from "@/lib/apiClient";
import {
computeChamberMetrics,
getChamberNumericStats,
} from "@/lib/dtoParsers";
import type { ChamberDto } from "@/types/api";
import { Surface } from "@/components/Surface";

Expand Down Expand Up @@ -77,32 +81,21 @@ const Chambers: React.FC = () => {
})
.sort((a, b) => {
if (sortBy === "name") return a.name.localeCompare(b.name);
if (sortBy === "governors")
return (
parseInt(b.stats.governors, 10) - parseInt(a.stats.governors, 10)
);
return (
parseInt(b.stats.acm.replace(/[,]/g, ""), 10) -
parseInt(a.stats.acm.replace(/[,]/g, ""), 10)
);
const statsA = getChamberNumericStats(a);
const statsB = getChamberNumericStats(b);
if (sortBy === "governors") return statsB.governors - statsA.governors;
return statsB.acm - statsA.acm;
});
}, [chambers, search, pipelineFilter, sortBy]);

const computedMetrics = useMemo((): Metric[] => {
if (!chambers) return metricCards;
const totalAcm = chambers.reduce((sum, chamber) => {
const parsed = Number(chamber.stats.acm.replace(/,/g, ""));
return sum + (Number.isFinite(parsed) ? parsed : 0);
}, 0);
const live = chambers.reduce(
(sum, chamber) => sum + (chamber.pipeline.vote ?? 0),
0,
);
const { totalAcm, liveProposals } = computeChamberMetrics(chambers);
return [
{ label: "Total chambers", value: String(chambers.length) },
{ label: "Active governors", value: "150" },
{ label: "Total ACM", value: totalAcm.toLocaleString() },
{ label: "Live proposals", value: String(live) },
{ label: "Live proposals", value: String(liveProposals) },
];
}, [chambers]);

Expand Down
52 changes: 2 additions & 50 deletions src/pages/proposals/ProposalCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { Tabs } from "@/components/primitives/tabs";
import { PageHint } from "@/components/PageHint";
import { SIM_AUTH_ENABLED } from "@/lib/featureFlags";
import { useAuth } from "@/app/auth/AuthContext";
import { formatProposalSubmitError } from "@/lib/proposalSubmitErrors";
import {
apiChambers,
apiProposalDraftDelete,
apiProposalDraftSave,
apiProposalSubmitToPool,
getApiErrorPayload,
} from "@/lib/apiClient";
import type { ChamberDto } from "@/types/api";
import { BudgetStep } from "./proposalCreation/steps/BudgetStep";
Expand All @@ -43,54 +43,6 @@ import {
} from "./proposalCreation/types";
import { getWizardTemplate } from "./proposalCreation/templates/registry";

const proposalTypeLabel: Record<string, string> = {
basic: "Basic",
fee: "Fee distribution",
monetary: "Monetary system",
core: "Core infrastructure",
administrative: "Administrative",
"dao-core": "DAO core",
};

const formatProposalType = (value: string): string =>
proposalTypeLabel[value] ?? value.replace(/-/g, " ");

const formatSubmitError = (error: unknown): string => {
const payload = getApiErrorPayload(error);
const details = payload?.error ?? null;
if (!details) return (error as Error).message ?? "Submit failed.";

const code = typeof details.code === "string" ? details.code : "";
if (code === "proposal_type_ineligible" || code === "tier_ineligible") {
const requiredTier =
typeof details.requiredTier === "string"
? details.requiredTier
: "a higher tier";
const proposalType =
typeof details.proposalType === "string"
? formatProposalType(details.proposalType)
: "this";
return `Not eligible for ${proposalType} proposals. Required tier: ${requiredTier}.`;
}

if (code === "proposal_submit_ineligible") {
const chamberId =
typeof details.chamberId === "string" ? details.chamberId : "";
if (chamberId === "general") {
return "General chamber proposals require voting rights in any chamber.";
}
if (chamberId) {
return `Only chamber members can submit to ${formatProposalType(chamberId)}.`;
}
}

if (code === "draft_not_submittable") {
return "Draft is incomplete. Fill required fields before submitting.";
}

return details.message ?? (error as Error).message ?? "Submit failed.";
};

const ProposalCreation: React.FC = () => {
const auth = useAuth();
const navigate = useNavigate();
Expand Down Expand Up @@ -435,7 +387,7 @@ const ProposalCreation: React.FC = () => {
clearDraftStorage();
navigate(`/app/proposals/${res.proposalId}/pp`);
} catch (error) {
setSubmitError(formatSubmitError(error));
setSubmitError(formatProposalSubmitError(error));
} finally {
setSubmitting(false);
}
Expand Down
57 changes: 3 additions & 54 deletions src/pages/proposals/ProposalDraft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,10 @@ import { AttachmentList } from "@/components/AttachmentList";
import { TitledSurface } from "@/components/TitledSurface";
import { SIM_AUTH_ENABLED } from "@/lib/featureFlags";
import { useAuth } from "@/app/auth/AuthContext";
import {
apiProposalDraft,
apiProposalSubmitToPool,
getApiErrorPayload,
} from "@/lib/apiClient";
import { formatProposalSubmitError } from "@/lib/proposalSubmitErrors";
import { apiProposalDraft, apiProposalSubmitToPool } from "@/lib/apiClient";
import type { ProposalDraftDetailDto } from "@/types/api";

const proposalTypeLabel: Record<string, string> = {
basic: "Basic",
fee: "Fee distribution",
monetary: "Monetary system",
core: "Core infrastructure",
administrative: "Administrative",
"dao-core": "DAO core",
};

const formatProposalType = (value: string): string =>
proposalTypeLabel[value] ?? value.replace(/-/g, " ");

const formatSubmitError = (error: unknown): string => {
const payload = getApiErrorPayload(error);
const details = payload?.error ?? null;
if (!details) return (error as Error).message ?? "Submit failed.";

const code = typeof details.code === "string" ? details.code : "";
if (code === "proposal_type_ineligible" || code === "tier_ineligible") {
const requiredTier =
typeof details.requiredTier === "string"
? details.requiredTier
: "a higher tier";
const proposalType =
typeof details.proposalType === "string"
? formatProposalType(details.proposalType)
: "this";
return `Not eligible for ${proposalType} proposals. Required tier: ${requiredTier}.`;
}

if (code === "proposal_submit_ineligible") {
const chamberId =
typeof details.chamberId === "string" ? details.chamberId : "";
if (chamberId === "general") {
return "General chamber proposals require voting rights in any chamber.";
}
if (chamberId) {
return `Only chamber members can submit to ${formatProposalType(chamberId)}.`;
}
}

if (code === "draft_not_submittable") {
return "Draft is incomplete. Fill required fields before submitting.";
}

return details.message ?? (error as Error).message ?? "Submit failed.";
};

const ProposalDraft: React.FC = () => {
const auth = useAuth();
const { id } = useParams();
Expand Down Expand Up @@ -159,7 +108,7 @@ const ProposalDraft: React.FC = () => {
const res = await apiProposalSubmitToPool({ draftId: id });
window.location.href = `/app/proposals/${res.proposalId}/pp`;
} catch (error) {
setSubmitError(formatSubmitError(error));
setSubmitError(formatProposalSubmitError(error));
} finally {
setSubmitting(false);
}
Expand Down
29 changes: 2 additions & 27 deletions src/pages/proposals/Proposals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { ProposalStage } from "@/types/stages";
import { CardActionsRow } from "@/components/CardActionsRow";
import { Surface } from "@/components/Surface";
import { NoDataYetBar } from "@/components/NoDataYetBar";
import { getFormationProgress } from "@/lib/dtoParsers";
import {
apiProposalChamberPage,
apiProposalFormationPage,
Expand Down Expand Up @@ -373,33 +374,7 @@ const Proposals: React.FC = () => {

const formationStats =
proposal.stage === "build" && formationPage
? (() => {
const progressRaw = Number.parseInt(
formationPage.progress.replace("%", ""),
10,
);
const progressValue = Number.isFinite(progressRaw)
? progressRaw
: 0;

const parsePair = (value: string) => {
const parts = value
.split("/")
.map((v) => Number(v.trim()));
if (parts.length !== 2) return { a: 0, b: 0 };
const [a, b] = parts;
return {
a: Number.isFinite(a) ? a : 0,
b: Number.isFinite(b) ? b : 0,
};
};

return {
progressValue,
team: parsePair(formationPage.teamSlots),
milestones: parsePair(formationPage.milestones),
};
})()
? getFormationProgress(formationPage)
: null;

return (
Expand Down
Loading