Languages: English (canonical) · Русский (this file)
Shared data hooks for AgentStack SDK - AI-First Design
These hooks are designed for AI agents and AI-assisted development:
- 3-Second Rule: AI understands hook purpose in 3 seconds
- Type-Safe: Full TypeScript inference
- Self-Documenting: JSDoc comments explain everything
- Predictable: Standard React Query pattern
- Copy-Paste Ready: Examples work immediately
npm install @agentstack/hooks @tanstack/react-queryimport { useSecurityData } from '@agentstack/hooks';
function SecurityPage() {
const { data, isLoading, error } = useSecurityData();
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<div>
{data.map(event => (
<SecurityCard key={event.id} {...event} />
))}
</div>
);
}Fetch security events with filtering and auto-refresh.
const { data, isLoading } = useSecurityData({
filters: { severity: 'critical' },
refreshInterval: 30000 // 30 seconds
});
// data: SecurityEvent[]Fetch audit logs for compliance and tracking.
const { data } = useAuditData({
filters: { action: 'delete', resource_type: 'project' }
});
// data: AuditLog[]Fetch analytics metrics for dashboards.
const { data } = useAnalyticsData({
period: '7d' // Last 7 days
});
// data: AnalyticsMetrics
// - totalUsers, activeUsers
// - totalProjects, activeProjects
// - apiCalls, errorRateFetch multi-currency wallets.
const { data } = useWalletData({
filters: { type: 'game', is_active: true }
});
// data: Wallet[]
// Each wallet has balances for multiple currenciesAll hooks follow the same pattern:
const {
data, // The actual data (never undefined)
isLoading, // Loading state
error, // Error state
refetch // Manual refetch function
} = useHook({
filters, // Optional filters
refreshInterval, // Auto-refresh (ms)
enabled, // Enable/disable hook
staleTime // Cache duration
});{
refreshInterval?: number; // Auto-refresh interval (default: 60000)
enabled?: boolean; // Enable hook (default: true)
staleTime?: number; // Cache duration (default: 300000)
}useSecurityData:
{
filters?: {
severity?: 'low' | 'medium' | 'high' | 'critical';
type?: 'login' | 'logout' | 'failed_login' | ...;
user_id?: number;
from_date?: string;
to_date?: string;
}
}useAuditData:
{
filters?: {
action?: 'create' | 'update' | 'delete' | 'read';
resource_type?: 'user' | 'project' | 'session' | ...;
user_id?: number;
from_date?: string;
to_date?: string;
}
}useAnalyticsData:
{
period?: '24h' | '7d' | '30d' | '90d' | 'all';
project_id?: number;
}useWalletData:
{
filters?: {
type?: 'user' | 'agentpay' | 'game' | 'savings';
is_active?: boolean;
currency?: string;
},
user_id?: number;
}All types are fully documented with JSDoc:
import type {
SecurityEvent,
AuditLog,
AnalyticsMetrics,
Wallet
} from '@agentstack/hooks';
// TypeScript shows full documentation on hover!import {
useSecurityData,
useAuditData,
useAnalyticsData
} from '@agentstack/hooks';
function AdminDashboard() {
const security = useSecurityData({
filters: { severity: 'critical' }
});
const audit = useAuditData({
filters: { action: 'delete' }
});
const analytics = useAnalyticsData({
period: '7d'
});
return (
<div>
<MetricsGrid data={analytics.data} />
<SecurityAlerts data={security.data} />
<AuditTimeline data={audit.data} />
</div>
);
}function CustomSecurityDashboard() {
const { data, isLoading, refetch } = useSecurityData();
return (
<div className="my-custom-layout">
<button onClick={() => refetch()}>Refresh</button>
<div className="grid">
{data.map(event => (
<MyCustomCard
key={event.id}
severity={event.severity}
type={event.type}
details={event.details}
timestamp={event.timestamp}
onResolve={() => {/* handle */}}
/>
))}
</div>
</div>
);
}For AI Agents:
-
Import the hook:
import { useSecurityData } from '@agentstack/hooks';
-
Use in component:
const { data, isLoading } = useSecurityData();
-
TypeScript shows types automatically:
data→SecurityEvent[]isLoading→booleanerror→Error | null
-
Render UI:
return data.map(item => <Card {...item} />)
AI understands this pattern in 3 seconds!
All hooks use React Query internally:
- ✅ Automatic caching
- ✅ Background refetching
- ✅ Stale-while-revalidate
- ✅ Error retry logic
- ✅ Request deduplication
MIT