# Examples & Templates

Ready-to-use code examples and templates for VibesEZ-Laptop development.

## Quick Start Templates

### Simple App Template

```typescript
// src/App.tsx - Minimal app structure
import React, { useState, useEffect } from 'react';

export const App: React.FC = () => {
    const [data, setData] = useState(null);
    const [loading, setLoading] = useState(true);

    useEffect(() => {
        loadData();
    }, []);

    const loadData = async () => {
        try {
            // Get data from server
            const response = await fetch('https://get-data');
            const result = await response.json();
            setData(result);
        } catch (error) {
            console.error('Load failed:', error);
        } finally {
            setLoading(false);
        }
    };

    if (loading) return <div>Loading...</div>;

    return (
        <div className=\"p-4\">
            <h1>My App</h1>
            {data && <div>{/* Your content */}</div>}
        </div>
    );
};

export default App;
```

### Registration Template

```typescript
// src/main.tsx - App registration
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const registerApp = () => {
    if (window.exports?.['VibesEZ-Laptop']) {
        exports['VibesEZ-Laptop'].RegisterApp({
            id: 'my_app_id',
            label: 'My App',
            event: 'my_app:open'
        });
    }
};

registerApp();

ReactDOM.createRoot(document.getElementById('root')!).render(
    <React.StrictMode>
        <App />
    </React.StrictMode>
);
```

***

## Integration Examples

### Job Reward Integration

```lua
-- resources/my_job/reward_system.lua
local function giveJobReward(src, jobData)
    local xPlayer = QBCore.Functions.GetPlayer(src)
    local reward = jobData.reward or 100
    
    -- Add crypto reward
    local success, err = exports['VibesEZ-Laptop']:AddCrypto(
        xPlayer.identifier,
        'BTC',
        reward,
        'Job: ' .. jobData.name .. ' completed'
    )
    
    if success then
        TriggerClientEvent('notify', src, 
            'Job Complete', 
            '₿' .. reward .. ' earned',
            'success'
        )
    else
        print('Failed to give reward: ' .. err)
    end
end

RegisterNetEvent('job:finish', function(jobData)
    giveJobReward(source, jobData)
end)
```

### Payment Processing

```lua
-- Payment handler with validation
local function processPayment(buyerId, sellerId, amount, itemName)
    local buyer = QBCore.Functions.GetPlayer(buyerId)
    local seller = QBCore.Functions.GetPlayer(sellerId)
    
    if not buyer or not seller then
        return false, 'Player not found'
    end
    
    -- Check balance
    local balance = exports['VibesEZ-Laptop']:GetBalance(
        buyer.identifier, 'BTC'
    )
    
    if not balance or balance < amount then
        return false, 'Insufficient balance'
    end
    
    -- Execute transfer
    local success, err = exports['VibesEZ-Laptop']:TransferCrypto(
        buyer.identifier,
        seller.identifier,
        'BTC',
        amount,
        'Purchase: ' .. itemName
    )
    
    if success then
        TriggerClientEvent('notify', buyerId,
            'Purchase', 'Bought ' .. itemName, 'success'
        )
        TriggerClientEvent('notify', sellerId,
            'Sale', 'Sold ' .. itemName, 'success'
        )
    end
    
    return success, err
end

RegisterNetEvent('store:processPurchase', function(data)
    local success, err = processPayment(
        source,
        data.sellerId,
        data.amount,
        data.itemName
    )
    
    if not success then
        TriggerClientEvent('notify', source, 'Error', err, 'error')
    end
end)
```

***

## UI Component Examples

### Balance Display Component

```typescript
import React, { useEffect, useState } from 'react';

interface BalanceDisplayProps {
    currency: string;
    updateInterval?: number;
}

export const BalanceDisplay: React.FC<BalanceDisplayProps> = ({
    currency,
    updateInterval = 5000
}) => {
    const [balance, setBalance] = useState<number>(0);
    const [loading, setLoading] = useState(true);

    useEffect(() => {
        const fetchBalance = async () => {
            try {
                const response = await fetch(
                    `https://get-balance/${currency}`
                );
                const data = await response.json();
                setBalance(data.balance);
                setLoading(false);
            } catch (error) {
                console.error('Failed to fetch balance:', error);
            }
        };

        fetchBalance();
        const interval = setInterval(fetchBalance, updateInterval);

        return () => clearInterval(interval);
    }, [currency, updateInterval]);

    return (
        <div className=\"bg-gradient-to-r from-blue-500 to-blue-600 p-4 rounded\">
            <p className=\"text-white text-sm opacity-75\">Balance</p>
            <p className=\"text-3xl font-bold text-white\">
                {loading ? '...' : balance.toFixed(2)}
            </p>
            <p className=\"text-white text-sm mt-1\">{currency}</p>
        </div>
    );
};
```

### Transfer Form Component

```typescript
import React, { useState } from 'react';

interface TransferFormProps {
    onTransfer?: (recipient: string, amount: number) => void;
}

export const TransferForm: React.FC<TransferFormProps> = ({ onTransfer }) => {
    const [recipient, setRecipient] = useState('');
    const [amount, setAmount] = useState('');
    const [loading, setLoading] = useState(false);

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault();
        
        if (!recipient || !amount) {
            alert('Please fill all fields');
            return;
        }

        const numAmount = parseFloat(amount);
        if (numAmount <= 0) {
            alert('Amount must be positive');
            return;
        }

        setLoading(true);
        try {
            if (onTransfer) {
                await onTransfer(recipient, numAmount);
            }
            setRecipient('');
            setAmount('');
        } finally {
            setLoading(false);
        }
    };

    return (
        <form onSubmit={handleSubmit} className=\"space-y-4\">
            <div>
                <label className=\"block text-sm font-medium mb-1\">
                    Recipient
                </label>
                <input
                    type=\"text\"
                    value={recipient}
                    onChange={(e) => setRecipient(e.target.value)}
                    placeholder=\"Player name or address\"
                    className=\"w-full px-3 py-2 border rounded\"
                />
            </div>

            <div>
                <label className=\"block text-sm font-medium mb-1\">
                    Amount
                </label>
                <input
                    type=\"number\"
                    value={amount}
                    onChange={(e) => setAmount(e.target.value)}
                    placeholder=\"0.00\"
                    step=\"0.01\"
                    className=\"w-full px-3 py-2 border rounded\"
                />
            </div>

            <button
                type=\"submit\"
                disabled={loading}
                className=\"w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700 disabled:opacity-50\"
            >
                {loading ? 'Processing...' : 'Transfer'}
            </button>
        </form>
    );
};
```

***

## Configuration Examples

### Advanced Config

```lua
-- config/crypto.lua - Full configuration example
Config = {}

Config.Debug = false
Config.Enabled = true

-- Economy Settings
Config.Crypto = {
    Enabled = true,
    EconomyMode = 'simulated',
    DefaultCurrency = 'BTC',
    
    -- Transfer Settings
    MinTransfer = 0.01,
    MaxTransfer = 999999,
    TransactionFee = 0.1,  -- 0.1% fee
    DailyLimitPerPlayer = 1000000,
    
    -- Wallet Settings
    MaxWalletsPerPlayer = 100,
    TrackHistory = true,
    OfflineTransfers = true
}

-- Currencies Configuration
Config.Crypto.Currencies = {
    BTC = {
        name = 'Bitcoin',
        symbol = 'BTC',
        icon = '₿',
        basePrice = 50000,
        volatility = 3.5,
        trend = 0.1
    },
    ETH = {
        name = 'Ethereum', 
        symbol = 'ETH',
        icon = 'Ξ',
        basePrice = 3000,
        volatility = 5.0,
        trend = 0
    },
    SERVER = {
        name = 'Server Coin',
        symbol = 'SERVER',
        icon = '$',
        basePrice = 100,
        volatility = 0,  -- Stable
        trend = 0
    }
}

-- Pre-installed Apps
Config.PreinstalledApps = {
    { id = 'notes', label = 'Notes', defaultInstalled = true, pinned = true },
    { id = 'tasks', label = 'Tasks', defaultInstalled = true, pinned = true },
    { id = 'calculator', label = 'Calculator', defaultInstalled = true },
    { id = 'crypto', label = 'Crypto', defaultInstalled = true, pinned = true },
    { id = 'settings', label = 'Settings', defaultInstalled = true }
}

-- Feature Flags
Config.Features = {
    Notes = true,
    Tasks = true,
    Crypto = true,
    Sharing = true,
    ExternalApps = true,
    Notifications = true
}

-- Appearance Settings
Config.Appearance = {
    DefaultTheme = 'dark',
    AllowThemeChange = true,
    AccentColor = 'blue'
}

return Config
```

***

## Debugging Examples

### Debug Utilities

```lua
-- Debug helper functions
local Debug = {}

function Debug:log(message, level)
    if not Config.Debug then return end
    level = level or 'INFO'
    print('^2[VibesEZ-DEBUG]^7 [' .. level .. '] ' .. message)
end

function Debug:logTransfer(from, to, amount, currency)
    self:log(string.format(
        'Transfer: %s -> %s | %d %s',
        from, to, amount, currency
    ))
end

function Debug:logBalance(identifier, currency, balance)
    self:log(string.format(
        'Balance: %s | %s = %d',
        identifier, currency, balance
    ))
end

return Debug
```

### Error Logging

```typescript
interface ErrorLog {
    timestamp: number;
    message: string;
    stack?: string;
    context?: Record<string, any>;
}

const errorLogs: ErrorLog[] = [];

const captureError = (error: Error, context?: any) => {
    const log: ErrorLog = {
        timestamp: Date.now(),
        message: error.message,
        stack: error.stack,
        context
    };
    errorLogs.push(log);
    console.error('Captured error:', log);
};

const exportErrorLogs = () => {
    return JSON.stringify(errorLogs, null, 2);
};
```

***

## Testing Templates

### Unit Test Template

```typescript
import { describe, it, expect, beforeEach } from 'vitest';

describe('CryptoService', () => {
    let service: CryptoService;

    beforeEach(() => {
        service = new CryptoService();
    });

    it('should add crypto to wallet', async () => {
        const result = await service.add('player1', 'BTC', 100);
        expect(result).toBe(true);
    });

    it('should get wallet balance', async () => {
        await service.add('player1', 'BTC', 100);
        const balance = await service.getBalance('player1', 'BTC');
        expect(balance).toBe(100);
    });

    it('should reject transfer with insufficient funds', async () => {
        const result = await service.transfer('poor', 'rich', 'BTC', 9999);
        expect(result).toBe(false);
    });
});
```

***

For more help, see [Integration Guide](https://github.com/VibesEZ/VibesEZ-Laptop/blob/main/docs/developer/integration.md) or [API Reference](/vibesez/docs/laptop/for-developers/api-reference.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vibesez.gitbook.io/vibesez/docs/laptop/for-developers/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
