Files
mkstack/ADMIN_DASHBOARD.md
2025-09-02 00:09:46 +00:00

170 lines
6.9 KiB
Markdown

# Site Administration Dashboard
The MKStack template includes a comprehensive site administration dashboard for content moderation and site management.
## Access
The admin dashboard is accessible at `/admin` and requires environment variable configuration.
**Authentication Required**: Only the site administrator (configured via `VITE_ADMIN_NPUB` environment variable) can access the admin dashboard. Other users will see an access denied message with a login prompt.
## Configuration
Set the `VITE_ADMIN_NPUB` environment variable to the npub of the site administrator:
```bash
VITE_ADMIN_NPUB=npub1abc123def456ghi789...
```
See `.env.example` for configuration details.
## Features
### Content Moderation Tab
- **Recent Content Review**: View and moderate recent posts and articles
- **Hide/Show Content**: Quickly hide inappropriate content or restore hidden content
- **Content Types**: Distinguishes between notes, articles, and other event kinds
- **Author Information**: Shows poster details with avatars and names
- **Timestamps**: Recent activity tracking for moderation decisions
### User Management Tab
- **Allow List**: Explicitly allowed users who can post content
- **Deny List**: Blocked users who cannot post content
- **Add Users**: Add users to allow/deny lists by npub
- **User Profiles**: View user avatars, names, and npub identifiers
- **Cross-List Management**: Adding to one list removes from the other
### Blocked Words Tab
- **Word Filtering**: Add words and phrases to automatically filter content
- **Case Sensitivity**: Toggle case-sensitive matching
- **Whole Words**: Option to match whole words only vs partial matches
- **Bulk Management**: Add/remove multiple blocked terms
- **Real-time Filtering**: Immediate effect on content display
### Site Analytics Tab
- **Content Statistics**: Total events, notes, articles on the site
- **User Activity**: Unique authors and active users
- **Time-based Metrics**: Activity in last 24 hours and 7 days
- **Content Breakdown**: Distribution of different content types
- **Growth Tracking**: Monitor site activity trends
### Site Settings Tab
- **Site Information**: Configure site name and description
- **Moderation Controls**: Enable/disable content moderation features
- **Auto-hide Settings**: Automatically hide content matching blocked words
- **Approval Workflow**: Require manual approval for all content
- **Environment Status**: View current configuration settings
## Implementation Details
### Route Configuration
The admin route is configured in `AppRouter.tsx` as `/admin` and takes precedence over the generic NIP-19 route.
### Authentication Flow
1. Environment variable `VITE_ADMIN_NPUB` is decoded using `nip19.decode()`
2. Current user's pubkey is compared with the admin pubkey
3. Unauthorized users see an access denied screen with login options
4. Authorized users see the full dashboard interface
### Data Fetching
- Uses TanStack Query for efficient data fetching and caching
- Implements proper loading states with skeleton components
- Includes error handling and timeout management
- Queries recent site content for moderation review
- Fetches site-wide analytics and statistics
### Nostr-Based Storage
- Uses custom event kind `39767` for persistent admin policies
- Stores all settings as addressable Nostr events
- Syncs across devices and browsers automatically
- Provides backup and recovery of admin settings
- Maintains audit trail of policy changes
### Components Used
- **Tabs**: Organized dashboard sections using shadcn/ui tabs
- **Cards**: Statistical displays and content sections
- **Badges**: Event type indicators and verification status
- **Avatars**: User profile images with fallbacks
- **Skeletons**: Loading states that match content structure
## Usage Examples
### Adding Admin Dashboard Link
Use the `AdminDashboardLink` component to add a dashboard link for logged-in users:
```tsx
import { AdminDashboardLink } from '@/components/AdminDashboardLink';
function Header() {
return (
<header className="flex items-center justify-between p-4">
<h1>My App</h1>
<AdminDashboardLink />
</header>
);
}
```
### Direct Access
Site administrators can access the dashboard directly by visiting:
```
/admin
```
### Setting Up Admin Access
To configure admin dashboard access:
1. Set `VITE_ADMIN_NPUB` environment variable to your npub
2. Log in with the corresponding Nostr account
3. Navigate to `/admin`
4. The `AdminDashboardLink` component will automatically appear for admin users
## Security Considerations
- **Environment-based Authentication**: Admin access controlled via environment variable
- **Single Admin Model**: Only one designated admin per site instance
- **Nostr Event Storage**: Admin policies stored as signed Nostr events (kind 39767)
- **Cross-Device Sync**: Settings automatically sync across all admin devices
- **Content Filtering**: Blocked words and user lists affect content display
- **Timeout Protection**: All queries include timeout limits to prevent hanging requests
- **No Private Key Access**: Uses existing authentication without requiring private keys
- **Event Integrity**: All admin policies are cryptographically signed and verifiable
## Customization
The admin dashboard is designed to be easily extensible:
### Adding New Tabs
1. Add a new `TabsTrigger` in the main dashboard component
2. Create a corresponding `TabsContent` section
3. Implement the tab's functionality as a separate component
### Extending Content Moderation
- **Automated Rules**: Add automatic content filtering based on patterns
- **Approval Queues**: Implement content approval workflows
- **Bulk Actions**: Add bulk moderation operations
- **Moderation History**: Track moderation actions and decisions
### Advanced User Management
- **Role-based Access**: Implement different user permission levels
- **Temporary Restrictions**: Time-limited bans or restrictions
- **User Reporting**: Allow community reporting of problematic users
- **Whitelist/Blacklist Import**: Bulk import of user lists
### Enhanced Analytics
- **Engagement Metrics**: Track user interaction patterns
- **Content Performance**: Analyze popular content types
- **Moderation Statistics**: Track moderation actions and effectiveness
- **Growth Trends**: Monitor site growth and user retention
## Technical Notes
- **React Hooks**: Follows React hooks rules with proper conditional rendering
- **TypeScript**: Fully typed with proper error handling (with type assertions for nostr-tools)
- **Responsive**: Works on mobile and desktop devices
- **Accessible**: Uses semantic HTML and ARIA attributes via Radix UI
- **Performance**: Implements query caching and loading optimization
- **Custom Storage Hook**: `useAdminStorage` for Nostr-based persistence
- **Environment Variables**: Requires Vite environment variable configuration
- **Icon Library**: Uses Lucide React icons throughout the interface
- **Custom NIP**: Implements event kind 39767 for admin storage (see `NIP.md`)
- **Content Filtering**: `useContentFilter` hook for policy enforcement