81 lines
3.1 KiB
PHP
81 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Zotlabs\Widget;
|
|
|
|
/**
|
|
* G1wallet — sidebar wallet status widget.
|
|
* Shows locked/unlocked state, first 12 chars of pubkey, and cached balance.
|
|
* At skeleton stage: always shows locked (JS session detection not yet wired).
|
|
* Once g1wallet.js is implemented, it dispatches 'g1wallet:unlocked' and
|
|
* 'g1wallet:locked' events that this widget's JS listener updates the DOM for.
|
|
*/
|
|
class G1wallet {
|
|
|
|
public function widget($arr) {
|
|
$wallet_url = z_root() . '/g1wallet';
|
|
|
|
// Retrieve cached pubkey and balance from channel settings if available.
|
|
// These are set by g1wallet_handle_pubkey_post() and the balance refresh flow.
|
|
$cached_pubkey = '';
|
|
$cached_balance = '';
|
|
|
|
if (local_channel()) {
|
|
$cached_pubkey = get_pconfig(local_channel(), 'g1wallet', 'g1_pubkey') ?: '';
|
|
$cached_balance = get_pconfig(local_channel(), 'g1wallet', 'g1_balance') ?: '';
|
|
}
|
|
|
|
$uid = 'g1wallet-widget-' . substr(md5(uniqid()), 0, 6);
|
|
|
|
$out = '<div class="g1wallet-widget" id="' . $uid . '">';
|
|
$out .= '<div class="g1wallet-widget-header">';
|
|
$out .= '<a href="' . $this->h($wallet_url) . '" class="g1wallet-widget-title">Ğ1 Wallet</a>';
|
|
$out .= '</div>';
|
|
|
|
// Static locked state — shown on page load.
|
|
// JS will update this div via 'g1wallet:unlocked' / 'g1wallet:locked' events.
|
|
$out .= '<div id="' . $uid . '-status">';
|
|
$out .= $this->render_locked_status($wallet_url, $cached_pubkey, $cached_balance);
|
|
$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">🔒</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);
|
|
if ($cached_balance) {
|
|
$out .= ' · ' . $this->h($cached_balance) . ' Ğ1';
|
|
}
|
|
$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>';
|
|
return $out;
|
|
}
|
|
|
|
private function h($value) {
|
|
// HTML-escapes a value for safe output.
|
|
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
}
|