This commit is contained in:
2026-06-14 07:48:17 -04:00
parent 4e0948cc19
commit 2d020c9010

View File

@@ -3,78 +3,52 @@
namespace Zotlabs\Widget; namespace Zotlabs\Widget;
/** /**
* G1wallet — sidebar wallet status widget. * G1wallet — sidebar widget.
* Shows locked/unlocked state, first 12 chars of pubkey, and cached balance. * Shows the participant's registered Ğ1 address (short) and cached balance.
* At skeleton stage: always shows locked (JS session detection not yet wired). * Links to /g1wallet for registration or update.
* Once g1wallet.js is implemented, it dispatches 'g1wallet:unlocked' and * No JS session state — all server-side.
* 'g1wallet:locked' events that this widget's JS listener updates the DOM for.
*/ */
class G1wallet { class G1wallet {
public function widget($arr) { public function widget($arr) {
$wallet_url = z_root() . '/g1wallet'; $wallet_url = z_root() . '/g1wallet';
// Retrieve cached pubkey and balance from channel settings if available. $stored_address = '';
// These are set by g1wallet_handle_pubkey_post() and the balance refresh flow.
$cached_pubkey = '';
$cached_balance = ''; $cached_balance = '';
if (local_channel()) { if (local_channel()) {
$cached_pubkey = get_pconfig(local_channel(), 'g1wallet', 'g1_pubkey') ?: ''; $stored_address = get_pconfig(local_channel(), 'g1wallet', 'g1_address') ?: '';
$cached_balance = get_pconfig(local_channel(), 'g1wallet', 'g1_balance') ?: ''; $cached_balance = get_pconfig(local_channel(), 'g1wallet', 'g1_balance') ?: '';
} }
$uid = 'g1wallet-widget-' . substr(md5(uniqid()), 0, 6); $out = '<div class="g1wallet-widget">';
$out = '<div class="g1wallet-widget" id="' . $uid . '">';
$out .= '<div class="g1wallet-widget-header">'; $out .= '<div class="g1wallet-widget-header">';
$out .= '<a href="' . $this->h($wallet_url) . '" class="g1wallet-widget-title">Ğ1 Wallet</a>'; $out .= '<a href="' . $this->h($wallet_url) . '" class="g1wallet-widget-title">Ğ1 Wallet</a>';
$out .= '</div>'; $out .= '</div>';
// Static locked state — shown on page load. if ($stored_address) {
// JS will update this div via 'g1wallet:unlocked' / 'g1wallet:locked' events. $short = substr($stored_address, 0, 12) . '…';
$out .= '<div id="' . $uid . '-status">'; $out .= '<div class="g1wallet-status mt-1">';
$out .= $this->render_locked_status($wallet_url, $cached_pubkey, $cached_balance); $out .= '<div class="g1wallet-cached-key text-muted small font-monospace">';
$out .= '</div>';
// Data attributes for JS to pick up the widget instance.
$out .= '<script>';
$out .= 'document.addEventListener("DOMContentLoaded", function() {';
$out .= ' if (window.G1WalletWidget) {';
$out .= ' window.G1WalletWidget.init("' . $uid . '", "' . $this->h($wallet_url) . '");';
$out .= ' }';
$out .= '});';
$out .= '</script>';
$out .= '</div>';
return $out;
}
private function render_locked_status($wallet_url, $cached_pubkey, $cached_balance) {
$out = '<div class="g1wallet-status g1wallet-status-locked">';
$out .= '<span class="g1wallet-status-indicator">&#x1F512;</span> ';
$out .= '<span class="text-muted small">Locked</span>';
// If we have a cached pubkey from a previous session, show it as context.
if ($cached_pubkey) {
$short = substr($cached_pubkey, 0, 12) . '…';
$out .= '<div class="g1wallet-cached-key text-muted small font-monospace mt-1">';
$out .= $this->h($short); $out .= $this->h($short);
$out .= '</div>';
if ($cached_balance) { if ($cached_balance) {
$out .= ' · ' . $this->h($cached_balance) . ' Ğ1'; $out .= '<div class="small mt-1">' . $this->h($cached_balance) . ' Ğ1</div>';
} }
$out .= '<div class="mt-1"><a href="' . $this->h($wallet_url) . '" class="btn btn-xs btn-outline-secondary btn-sm">View</a></div>';
$out .= '</div>';
} else {
$out .= '<div class="g1wallet-status mt-1">';
$out .= '<span class="text-muted small">No address registered.</span>';
$out .= '<div class="mt-1"><a href="' . $this->h($wallet_url) . '" class="btn btn-xs btn-outline-secondary btn-sm">Register</a></div>';
$out .= '</div>'; $out .= '</div>';
} }
$out .= '<div class="mt-1">';
$out .= '<a href="' . $this->h($wallet_url) . '" class="btn btn-xs btn-outline-secondary btn-sm">Unlock</a>';
$out .= '</div>';
$out .= '</div>'; $out .= '</div>';
return $out; return $out;
} }
private function h($value) { private function h($value) {
// HTML-escapes a value for safe output.
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
} }
} }