diff --git a/app/Http/Controllers/WalletBalanceController.php b/app/Http/Controllers/WalletBalanceController.php new file mode 100644 index 0000000..29d4267 --- /dev/null +++ b/app/Http/Controllers/WalletBalanceController.php @@ -0,0 +1,40 @@ +user()->public_id; + + $minor = Cache::remember("wallet_balance:{$publicId}", now()->addSeconds(30), function () use ($billing, $publicId) { + try { + return $billing->balanceMinor($publicId); + } catch (\Throwable) { + return null; + } + }); + + if ($minor === null) { + return response()->json(['available' => false]); + } + + $currency = (string) config('billing.currency', 'GHS'); + + return response()->json([ + 'available' => true, + 'balance_minor' => $minor, + 'currency' => $currency, + 'formatted' => $currency.' '.number_format($minor / 100, 2), + ]); + } +} diff --git a/app/Support/UserProfileMenu.php b/app/Support/UserProfileMenu.php index 3228d49..2806cdb 100644 --- a/app/Support/UserProfileMenu.php +++ b/app/Support/UserProfileMenu.php @@ -43,6 +43,11 @@ class UserProfileMenu ]; } + + if (Route::has((string) config("billing.wallet_balance_route", "wallet.balance"))) { + $items[] = ["type" => "wallet"]; + } + if (self::userIsAdmin($user)) { $adminHref = self::platformUrl('account', 'admin'); diff --git a/config/billing.php b/config/billing.php index 9d333a8..1a1a5e4 100644 --- a/config/billing.php +++ b/config/billing.php @@ -4,5 +4,7 @@ return [ 'api_url' => env('BILLING_API_URL', 'https://ladill.com/api/billing'), 'api_key' => env('BILLING_API_KEY_GIVE'), 'service' => 'give', + 'wallet_balance_route' => 'wallet.balance', + 'currency' => 'GHS', 'platform_settings_connection' => env('BILLING_PLATFORM_SETTINGS_CONNECTION', 'platform'), ]; diff --git a/resources/js/app.js b/resources/js/app.js index f9f7f99..90dfca5 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -1,8 +1,10 @@ import './bootstrap'; import { registerLadillConfirmStore, registerLadillModalHelpers } from './ladill-modals'; +import { registerLadillSearchShortcut } from './ladill-search-shortcut'; registerLadillModalHelpers(); +registerLadillSearchShortcut(); import Alpine from 'alpinejs'; @@ -300,6 +302,26 @@ Alpine.data('topbarSearch', (config = {}) => ({ }, })); + +// Wallet balance peek for the avatar dropdown. +Alpine.data('walletWidget', (config = {}) => ({ + display: '…', + async load() { + if (! config.url) { + this.display = 'View wallet'; + + return; + } + try { + const res = await fetch(config.url, { headers: { Accept: 'application/json', 'X-Requested-With': 'XMLHttpRequest' } }); + const data = await res.json(); + this.display = data.available ? data.formatted : 'View wallet'; + } catch (e) { + this.display = 'View wallet'; + } + }, +})); + window.Alpine = Alpine; registerLadillConfirmStore(Alpine); diff --git a/resources/js/ladill-search-shortcut.js b/resources/js/ladill-search-shortcut.js new file mode 100644 index 0000000..72b493f --- /dev/null +++ b/resources/js/ladill-search-shortcut.js @@ -0,0 +1,97 @@ +function isEditableTarget(element) { + if (!(element instanceof HTMLElement)) { + return false; + } + + const tag = element.tagName; + + if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') { + return true; + } + + return element.isContentEditable; +} + +function isVisibleInput(input) { + if (!(input instanceof HTMLElement)) { + return false; + } + + const style = window.getComputedStyle(input); + + return style.display !== 'none' + && style.visibility !== 'hidden' + && style.opacity !== '0'; +} + +function findSearchInput() { + const marked = [...document.querySelectorAll('[data-ladill-search-input]')]; + const visibleMarked = marked.find(isVisibleInput); + + if (visibleMarked) { + return visibleMarked; + } + + if (marked.length > 0) { + return marked[0]; + } + + return document.querySelector('[x-data*="topbarSearch"] input'); +} + +function focusSearchInput() { + const input = findSearchInput(); + + if (!input) { + const fallbackUrl = document.body?.dataset?.ladillSearchUrl; + + if (fallbackUrl) { + window.location.href = fallbackUrl; + } + + return false; + } + + if (!isVisibleInput(input)) { + const fallbackUrl = document.body?.dataset?.ladillSearchUrl; + + if (fallbackUrl) { + window.location.href = fallbackUrl; + + return true; + } + } + + input.focus(); + + if (input instanceof HTMLInputElement && (input.type === 'text' || input.type === 'search')) { + input.select(); + } + + return true; +} + +export function registerLadillSearchShortcut() { + if (window.__ladillSearchShortcutRegistered) { + return; + } + + window.__ladillSearchShortcutRegistered = true; + + document.addEventListener('keydown', (event) => { + if (event.key !== '/' && event.code !== 'Slash') { + return; + } + + if (event.ctrlKey || event.metaKey || event.altKey) { + return; + } + + if (isEditableTarget(document.activeElement)) { + return; + } + + event.preventDefault(); + focusSearchInput(); + }); +} diff --git a/resources/views/layouts/hosting.blade.php b/resources/views/layouts/hosting.blade.php index e12b698..9d120ab 100644 --- a/resources/views/layouts/hosting.blade.php +++ b/resources/views/layouts/hosting.blade.php @@ -10,7 +10,7 @@ @vite(['resources/css/app.css', 'resources/js/app.js']) - +