dashboard: always-visible "Buy a credit pack" link in My usage

Members looked for a way to add usage from the dashboard and could not
find it — the buy path was invisible unless someone pointed them at it.
Add an always-visible green button in the "My usage" card, opening in a
new tab, plus the member's real credit status and a docs link.

The destination comes from CREDIT_PACK_URL on the dashboard app (same
convention the portal uses), defaulting to the Open Collective
contribute page — configurable in one place, not hardcoded in the
template. When the member has purchased credits the hint reads
"Credits never expire ($X purchased all-time)"; otherwise it keeps the
static "Credit packs never expire · used after your monthly allowance".

Docs link points at getting-started.md#user-content-need-more-than-the-allowance
(the rendered heading id Gitea actually serves), not the bare anchor.
This commit is contained in:
InferenceBot committed 2026-11-02 00:50:45 +00:00
1 parent c28a5b3fe7
commit b352b1f427
1 file changed
+46 -6
+46 -6
View File
@@ -8,8 +8,12 @@ Security model:
every request to the logged-in member. every request to the logged-in member.
Environment (all provided by Cloudron env vars): Environment (all provided by Cloudron env vars):
PORTAL_BASE — base URL of the member portal (e.g. https://portal.inference.coop) PORTAL_BASE — base URL of the member portal (e.g. https://portal.inference.coop)
BROKER_SECRET — shared secret for the portal's broker endpoints BROKER_SECRET — shared secret for the portal's broker endpoints
CREDIT_PACK_URL — where the "Buy a credit pack" link sends members (defaults to
the Open Collective contribute page, which is what actually
credits a member). Configuration, not code, because the
destination can move (a future pack page) without a deploy here.
""" """
import os import os
@@ -28,6 +32,14 @@ logger = logging.getLogger("member-dashboard")
PORTAL_BASE = os.environ.get("PORTAL_BASE", "").rstrip("/") PORTAL_BASE = os.environ.get("PORTAL_BASE", "").rstrip("/")
BROKER_SECRET = os.environ.get("BROKER_SECRET", "") BROKER_SECRET = os.environ.get("BROKER_SECRET", "")
# Where the always-visible "Buy a credit pack" link points. Configuration, not a
# constant in the template: the co-op sets this per app (same convention the
# portal uses) so the destination changes in one place. The Open Collective
# contribution page is the default because that is what actually credits a
# member today (the webhook settles contributions into credits).
DEFAULT_CREDIT_PACK_URL = "https://opencollective.com/inference-cooperative/contribute"
CREDIT_PACK_URL = (os.environ.get("CREDIT_PACK_URL") or "").strip() or DEFAULT_CREDIT_PACK_URL
app = FastAPI() app = FastAPI()
@@ -296,7 +308,10 @@ def _esc(s: str) -> str:
def render_page(identity: str) -> str: def render_page(identity: str) -> str:
return PAGE_HTML.replace("@@IDENTITY@@", _esc(identity)) return (
PAGE_HTML.replace("@@IDENTITY@@", _esc(identity))
.replace("@@CREDIT_PACK_URL@@", _esc(CREDIT_PACK_URL))
)
PAGE_HTML = """<!DOCTYPE html> PAGE_HTML = """<!DOCTYPE html>
@@ -357,6 +372,20 @@ PAGE_HTML = """<!DOCTYPE html>
.bar .fill { height: 100%; background: var(--green-mid); border-radius: 6px; transition: width .3s ease; } .bar .fill { height: 100%; background: var(--green-mid); border-radius: 6px; transition: width .3s ease; }
.bar .fill.warn { background: #d99a3e; } .bar .fill.warn { background: #d99a3e; }
.meta { color: var(--muted); font-size: 13px; margin-top: 8px; } .meta { color: var(--muted); font-size: 13px; margin-top: 8px; }
/* Always-visible buy path: members could not find a way to add usage.
A green button (same green + radius as the other buttons) out to the
configured pack page, plus the member's real credit status and a link
to the docs section that explains allowance vs. credits. */
.buy-row { display: flex; align-items: center; gap: 14px; margin-top: 14px; flex-wrap: wrap; }
a.buy-btn {
display: inline-block; background: var(--green-deep); color: #fff;
border-radius: 8px; font-size: 14px; font-weight: 600;
padding: 8px 16px; text-decoration: none; transition: background .15s ease;
}
a.buy-btn:hover { background: #4e7a4d; }
.buy-hint { color: var(--muted); font-size: 12px; }
a.buy-docs { color: var(--green-deep); font-weight: 600; text-decoration: none; white-space: nowrap; }
a.buy-docs:hover { text-decoration: underline; }
.models { margin-top: 16px; } .models { margin-top: 16px; }
.model-row { .model-row {
display: flex; align-items: center; justify-content: space-between; gap: 12px; display: flex; align-items: center; justify-content: space-between; gap: 12px;
@@ -557,6 +586,12 @@ PAGE_HTML = """<!DOCTYPE html>
</div> </div>
<div class="bar"><div class="fill" id="fill" style="width:0%"></div></div> <div class="bar"><div class="fill" id="fill" style="width:0%"></div></div>
<div class="meta" id="meta">Shared across chat and API.</div> <div class="meta" id="meta">Shared across chat and API.</div>
<div class="buy-row">
<a class="buy-btn" href="@@CREDIT_PACK_URL@@" target="_blank" rel="noopener">Buy a credit pack</a>
<span class="buy-hint"><span id="buy-credits">Credit packs never expire · used after your monthly allowance</span>
<a class="buy-docs" href="https://git.inference.coop/co-op/docs/src/branch/main/getting-started.md#user-content-need-more-than-the-allowance" target="_blank" rel="noopener">More about allowance &amp; credits →</a>
</span>
</div>
<div class="models" id="models"></div> <div class="models" id="models"></div>
</div> </div>
@@ -685,9 +720,14 @@ async function load() {
if (d.reset_at) { if (d.reset_at) {
const d2 = new Date(d.reset_at); const d2 = new Date(d.reset_at);
const opts = { year: 'numeric', month: 'long', day: 'numeric' }; const opts = { year: 'numeric', month: 'long', day: 'numeric' };
let meta = 'Shared across chat and API · Allowance renews ' + d2.toLocaleDateString(undefined, opts); $('meta').textContent = 'Shared across chat and API · Allowance renews ' + d2.toLocaleDateString(undefined, opts) + '.';
if (creditsTotal > 0) meta += ' · Credits never expire (' + fmtMoney(creditsTotal) + ' purchased all-time)'; }
$('meta').textContent = meta + '.';
// Credit status lives next to the buy button, not in the meta line: with a
// pack bought it reads the real all-time figure, otherwise it stays the
// static "credit packs never expire" line already in the markup.
if (creditsTotal > 0) {
$('buy-credits').textContent = 'Credits never expire (' + fmtMoney(creditsTotal) + ' purchased all-time)';
} }
const calls = (d.models || []).reduce((s, m) => s + (m.calls || 0), 0); const calls = (d.models || []).reduce((s, m) => s + (m.calls || 0), 0);