mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
Admin Dashboard init
This commit is contained in:
1 parent
d1744b7d71
commit
c0dca78709
10 files changed
+1478
-7
No files matched your search
@@ -3,6 +3,7 @@ import { ScrollToTop } from "./components/ScrollToTop";
|
||||
|
||||
import Index from "./pages/Index";
|
||||
import { NIP19Page } from "./pages/NIP19Page";
|
||||
import { AdminDashboard } from "./pages/AdminDashboard";
|
||||
import NotFound from "./pages/NotFound";
|
||||
|
||||
export function AppRouter() {
|
||||
@@ -11,6 +12,8 @@ export function AppRouter() {
|
||||
<ScrollToTop />
|
||||
<Routes>
|
||||
<Route path="/" element={<Index />} />
|
||||
{/* Admin dashboard route - must come before NIP-19 route */}
|
||||
<Route path="/admin" element={<AdminDashboard />} />
|
||||
{/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */}
|
||||
<Route path="/:nip19" element={<NIP19Page />} />
|
||||
{/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import { Shield } from 'lucide-react';
|
||||
|
||||
// Get admin npub from environment variable
|
||||
const ADMIN_NPUB = import.meta.env.VITE_ADMIN_NPUB;
|
||||
|
||||
export function AdminDashboardLink() {
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
// Only show admin link if user is the configured admin
|
||||
if (!user || !ADMIN_NPUB) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let adminPubkey: string = '';
|
||||
try {
|
||||
const decoded = nip19.decode(ADMIN_NPUB);
|
||||
// @ts-expect-error - TypeScript has issues with nostr-tools type inference
|
||||
if (decoded.type === 'npub') {
|
||||
adminPubkey = decoded.data as unknown as string;
|
||||
}
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Only show if current user is the admin
|
||||
if (user.pubkey !== adminPubkey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Button variant="outline" size="sm" asChild>
|
||||
<a href="/admin" className="flex items-center gap-2">
|
||||
<Shield className="h-4 w-4" />
|
||||
Site Admin
|
||||
</a>
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { useCurrentUser } from './useCurrentUser';
|
||||
import { useNostrPublish } from './useNostrPublish';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
|
||||
// Custom kind for admin storage
|
||||
const ADMIN_STORAGE_KIND = 39767;
|
||||
|
||||
// Get admin npub from environment variable
|
||||
const ADMIN_NPUB = import.meta.env.VITE_ADMIN_NPUB;
|
||||
|
||||
export interface AdminStorageData {
|
||||
hiddenEvents: string[];
|
||||
allowList: string[];
|
||||
denyList: string[];
|
||||
blockedWords: string[];
|
||||
caseSensitive: boolean;
|
||||
wholeWordsOnly: boolean;
|
||||
enableModeration: boolean;
|
||||
autoHideBlocked: boolean;
|
||||
requireApproval: boolean;
|
||||
siteName: string;
|
||||
siteDescription: string;
|
||||
}
|
||||
|
||||
const defaultAdminData: AdminStorageData = {
|
||||
hiddenEvents: [],
|
||||
allowList: [],
|
||||
denyList: [],
|
||||
blockedWords: [],
|
||||
caseSensitive: false,
|
||||
wholeWordsOnly: true,
|
||||
enableModeration: true,
|
||||
autoHideBlocked: true,
|
||||
requireApproval: false,
|
||||
siteName: '',
|
||||
siteDescription: '',
|
||||
};
|
||||
|
||||
export function useAdminStorage() {
|
||||
const { nostr } = useNostr();
|
||||
const { user } = useCurrentUser();
|
||||
const { mutateAsync: publishEvent } = useNostrPublish();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
// Get admin pubkey
|
||||
let adminPubkey: string = '';
|
||||
try {
|
||||
if (ADMIN_NPUB) {
|
||||
const decoded = nip19.decode(ADMIN_NPUB);
|
||||
// @ts-expect-error - TypeScript has issues with nostr-tools type inference
|
||||
if (decoded.type === 'npub') {
|
||||
adminPubkey = decoded.data as unknown as string;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Invalid admin npub
|
||||
}
|
||||
|
||||
// Check if current user is admin
|
||||
const isAdmin = user?.pubkey === adminPubkey && !!adminPubkey;
|
||||
|
||||
// Query for admin storage events
|
||||
const adminDataQuery = useQuery({
|
||||
queryKey: ['admin-storage', adminPubkey],
|
||||
queryFn: async ({ signal }) => {
|
||||
if (!adminPubkey) return defaultAdminData;
|
||||
|
||||
const timeoutSignal = AbortSignal.timeout(1500);
|
||||
const combinedSignal = AbortSignal.any([signal, timeoutSignal]);
|
||||
|
||||
// Query for admin storage events
|
||||
const events = await nostr.query([{
|
||||
kinds: [ADMIN_STORAGE_KIND],
|
||||
authors: [adminPubkey],
|
||||
'#d': ['admin-config'],
|
||||
limit: 1
|
||||
}], { signal: combinedSignal });
|
||||
|
||||
if (events.length === 0) {
|
||||
return defaultAdminData;
|
||||
}
|
||||
|
||||
// Get the most recent event
|
||||
const latestEvent = events.sort((a, b) => b.created_at - a.created_at)[0];
|
||||
|
||||
try {
|
||||
const data = JSON.parse(latestEvent.content) as AdminStorageData;
|
||||
// Merge with defaults to handle missing fields
|
||||
return { ...defaultAdminData, ...data };
|
||||
} catch {
|
||||
return defaultAdminData;
|
||||
}
|
||||
},
|
||||
enabled: !!adminPubkey,
|
||||
});
|
||||
|
||||
// Mutation to save admin data
|
||||
const saveAdminData = useMutation({
|
||||
mutationFn: async (data: Partial<AdminStorageData>) => {
|
||||
if (!isAdmin) {
|
||||
throw new Error('Only admin can save admin data');
|
||||
}
|
||||
|
||||
const currentData = adminDataQuery.data || defaultAdminData;
|
||||
const updatedData = { ...currentData, ...data };
|
||||
|
||||
await publishEvent({
|
||||
kind: ADMIN_STORAGE_KIND,
|
||||
content: JSON.stringify(updatedData),
|
||||
tags: [
|
||||
['d', 'admin-config'],
|
||||
['alt', 'Site administration configuration and policies'],
|
||||
],
|
||||
});
|
||||
|
||||
return updatedData;
|
||||
},
|
||||
onSuccess: (updatedData) => {
|
||||
// Update the cache with new data
|
||||
queryClient.setQueryData(['admin-storage', adminPubkey], updatedData);
|
||||
},
|
||||
});
|
||||
|
||||
// Helper functions for specific data updates
|
||||
const updateHiddenEvents = (hiddenEvents: string[]) =>
|
||||
saveAdminData.mutateAsync({ hiddenEvents });
|
||||
|
||||
const updateAllowList = (allowList: string[]) =>
|
||||
saveAdminData.mutateAsync({ allowList });
|
||||
|
||||
const updateDenyList = (denyList: string[]) =>
|
||||
saveAdminData.mutateAsync({ denyList });
|
||||
|
||||
const updateBlockedWords = (blockedWords: string[]) =>
|
||||
saveAdminData.mutateAsync({ blockedWords });
|
||||
|
||||
const updateModerationSettings = (settings: {
|
||||
caseSensitive?: boolean;
|
||||
wholeWordsOnly?: boolean;
|
||||
enableModeration?: boolean;
|
||||
autoHideBlocked?: boolean;
|
||||
requireApproval?: boolean;
|
||||
}) => saveAdminData.mutateAsync(settings);
|
||||
|
||||
const updateSiteSettings = (settings: {
|
||||
siteName?: string;
|
||||
siteDescription?: string;
|
||||
}) => saveAdminData.mutateAsync(settings);
|
||||
|
||||
return {
|
||||
// Data
|
||||
data: adminDataQuery.data || defaultAdminData,
|
||||
isLoading: adminDataQuery.isLoading,
|
||||
error: adminDataQuery.error,
|
||||
|
||||
// Auth
|
||||
isAdmin,
|
||||
adminPubkey,
|
||||
|
||||
// Mutations
|
||||
saveAdminData: saveAdminData.mutateAsync,
|
||||
isSaving: saveAdminData.isPending,
|
||||
|
||||
// Helper functions
|
||||
updateHiddenEvents,
|
||||
updateAllowList,
|
||||
updateDenyList,
|
||||
updateBlockedWords,
|
||||
updateModerationSettings,
|
||||
updateSiteSettings,
|
||||
};
|
||||
}
|
||||
|
||||
// Hook for checking if content should be hidden based on admin policies
|
||||
export function useContentFilter() {
|
||||
const { data: adminData } = useAdminStorage();
|
||||
|
||||
const isContentHidden = (eventId: string): boolean => {
|
||||
return adminData.hiddenEvents.includes(eventId);
|
||||
};
|
||||
|
||||
const isUserBlocked = (pubkey: string): boolean => {
|
||||
// If allow list exists and user is not on it, they're blocked
|
||||
if (adminData.allowList.length > 0 && !adminData.allowList.includes(pubkey)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If user is on deny list, they're blocked
|
||||
return adminData.denyList.includes(pubkey);
|
||||
};
|
||||
|
||||
const isContentBlocked = (content: string): boolean => {
|
||||
if (!adminData.enableModeration || adminData.blockedWords.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const text = adminData.caseSensitive ? content : content.toLowerCase();
|
||||
|
||||
return adminData.blockedWords.some(word => {
|
||||
const searchWord = adminData.caseSensitive ? word : word.toLowerCase();
|
||||
|
||||
if (adminData.wholeWordsOnly) {
|
||||
// Match whole words only
|
||||
const regex = new RegExp(`\\b${searchWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`);
|
||||
return regex.test(text);
|
||||
} else {
|
||||
// Match partial words
|
||||
return text.includes(searchWord);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const shouldHideContent = (eventId: string, pubkey: string, content: string): boolean => {
|
||||
if (!adminData.enableModeration) return false;
|
||||
|
||||
// Check if manually hidden
|
||||
if (isContentHidden(eventId)) return true;
|
||||
|
||||
// Check if user is blocked
|
||||
if (isUserBlocked(pubkey)) return true;
|
||||
|
||||
// Check if content contains blocked words and auto-hide is enabled
|
||||
if (adminData.autoHideBlocked && isContentBlocked(content)) return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
return {
|
||||
isContentHidden,
|
||||
isUserBlocked,
|
||||
isContentBlocked,
|
||||
shouldHideContent,
|
||||
adminData,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,825 @@
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useAuthor } from '@/hooks/useAuthor';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useState } from 'react';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { LoginArea } from '@/components/auth/LoginArea';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
|
||||
import { AlertCircle, Shield, Ban, UserCheck, MessageSquareX, Trash2, Plus, Eye, EyeOff } from 'lucide-react';
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||
import { useAdminStorage } from '@/hooks/useAdminStorage';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import { genUserName } from '@/lib/genUserName';
|
||||
import NotFound from './NotFound';
|
||||
|
||||
// Get admin npub from environment variable
|
||||
const ADMIN_NPUB = import.meta.env.VITE_ADMIN_NPUB;
|
||||
|
||||
export function AdminDashboard() {
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
// Decode admin npub from environment variable
|
||||
let adminPubkey: string = '';
|
||||
let isValidAdminNpub = false;
|
||||
|
||||
try {
|
||||
if (ADMIN_NPUB) {
|
||||
const decoded = nip19.decode(ADMIN_NPUB);
|
||||
// @ts-expect-error - TypeScript has issues with nostr-tools type inference
|
||||
if (decoded.type === 'npub') {
|
||||
adminPubkey = decoded.data as unknown as string;
|
||||
isValidAdminNpub = true;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Invalid admin npub format
|
||||
}
|
||||
|
||||
// Always call hooks - use empty string as fallback to avoid conditional calls
|
||||
const adminAuthor = useAuthor(adminPubkey || '');
|
||||
|
||||
// Return NotFound if no valid admin npub is configured
|
||||
if (!isValidAdminNpub) {
|
||||
return <NotFound />;
|
||||
}
|
||||
|
||||
// Check if current user is the site admin
|
||||
const isAuthorized = user?.pubkey === adminPubkey;
|
||||
|
||||
if (!isAuthorized) {
|
||||
return (
|
||||
<div className="container mx-auto px-4 py-8 max-w-2xl">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Shield className="h-5 w-5 text-amber-500" />
|
||||
Site Admin Access Required
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
You need to be logged in as the site administrator to access this admin panel.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{adminAuthor.data?.metadata && (
|
||||
<div className="flex items-center gap-3 p-4 bg-muted rounded-lg">
|
||||
<Avatar>
|
||||
<AvatarImage src={adminAuthor.data.metadata.picture} />
|
||||
<AvatarFallback>
|
||||
{(adminAuthor.data.metadata.name || genUserName(adminPubkey)).slice(0, 2).toUpperCase()}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div>
|
||||
<p className="font-medium">
|
||||
{adminAuthor.data.metadata.display_name || adminAuthor.data.metadata.name || genUserName(adminPubkey)}
|
||||
</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Site Administrator
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Alert>
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertDescription>
|
||||
Only the site administrator can access content moderation and site management features.
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
|
||||
<LoginArea className="w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto px-4 py-8 max-w-6xl">
|
||||
<div className="mb-8">
|
||||
<h1 className="text-3xl font-bold mb-2 flex items-center gap-2">
|
||||
<Shield className="h-8 w-8" />
|
||||
Site Administration
|
||||
</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Manage content moderation, user access controls, and site settings
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Tabs defaultValue="moderation" className="space-y-6">
|
||||
<TabsList className="grid w-full grid-cols-5">
|
||||
<TabsTrigger value="moderation">Content Moderation</TabsTrigger>
|
||||
<TabsTrigger value="users">User Management</TabsTrigger>
|
||||
<TabsTrigger value="blocked-words">Blocked Words</TabsTrigger>
|
||||
<TabsTrigger value="analytics">Site Analytics</TabsTrigger>
|
||||
<TabsTrigger value="settings">Site Settings</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="moderation">
|
||||
<ContentModeration />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="users">
|
||||
<UserManagement />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="blocked-words">
|
||||
<BlockedWordsManagement />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="analytics">
|
||||
<SiteAnalytics />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="settings">
|
||||
<SiteSettings />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ContentModeration() {
|
||||
const { nostr } = useNostr();
|
||||
const { data: adminData, updateHiddenEvents, isSaving } = useAdminStorage();
|
||||
|
||||
const recentContent = useQuery({
|
||||
queryKey: ['admin-recent-content'],
|
||||
queryFn: async ({ signal }) => {
|
||||
const timeoutSignal = AbortSignal.timeout(1500);
|
||||
const combinedSignal = AbortSignal.any([signal, timeoutSignal]);
|
||||
|
||||
const events = await nostr.query([{
|
||||
kinds: [1, 30023], // Notes and articles
|
||||
limit: 50
|
||||
}], { signal: combinedSignal });
|
||||
|
||||
return events.sort((a, b) => b.created_at - a.created_at);
|
||||
},
|
||||
});
|
||||
|
||||
const hideEvent = async (eventId: string) => {
|
||||
const newHiddenEvents = [...adminData.hiddenEvents, eventId];
|
||||
await updateHiddenEvents(newHiddenEvents);
|
||||
};
|
||||
|
||||
const unhideEvent = async (eventId: string) => {
|
||||
const newHiddenEvents = adminData.hiddenEvents.filter(id => id !== eventId);
|
||||
await updateHiddenEvents(newHiddenEvents);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Eye className="h-5 w-5" />
|
||||
Recent Content Review
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Review and moderate recent content posted to the site
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{recentContent.isLoading ? (
|
||||
<div className="space-y-4">
|
||||
{Array.from({ length: 5 }).map((_, i) => (
|
||||
<div key={i} className="flex items-start gap-3 p-4 border rounded-lg">
|
||||
<Skeleton className="h-10 w-10 rounded-full" />
|
||||
<div className="flex-1 space-y-2">
|
||||
<Skeleton className="h-4 w-24" />
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-3/4" />
|
||||
</div>
|
||||
<Skeleton className="h-8 w-20" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : recentContent.data?.length ? (
|
||||
<div className="space-y-4">
|
||||
{recentContent.data.map((event) => {
|
||||
const isHidden = adminData.hiddenEvents.includes(event.id);
|
||||
return (
|
||||
<div key={event.id} className={`flex items-start gap-3 p-4 border rounded-lg ${isHidden ? 'opacity-50 bg-muted' : ''}`}>
|
||||
<UserAvatar pubkey={event.pubkey} />
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 mb-2">
|
||||
<UserName pubkey={event.pubkey} />
|
||||
<Badge variant={event.kind === 1 ? 'default' : 'secondary'}>
|
||||
{event.kind === 1 ? 'Note' : 'Article'}
|
||||
</Badge>
|
||||
<span className="text-sm text-muted-foreground">
|
||||
{new Date(event.created_at * 1000).toLocaleString()}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-sm line-clamp-3">
|
||||
{event.kind === 30023
|
||||
? event.tags.find(([tag]) => tag === 'title')?.[1] || 'Untitled Article'
|
||||
: event.content
|
||||
}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{isHidden ? (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => unhideEvent(event.id)}
|
||||
className="text-green-600 hover:text-green-700"
|
||||
disabled={isSaving}
|
||||
>
|
||||
<Eye className="h-4 w-4 mr-1" />
|
||||
Show
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => hideEvent(event.id)}
|
||||
className="text-red-600 hover:text-red-700"
|
||||
disabled={isSaving}
|
||||
>
|
||||
<EyeOff className="h-4 w-4 mr-1" />
|
||||
Hide
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground">No recent content found</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function UserManagement() {
|
||||
const { data: adminData, updateAllowList, updateDenyList, isSaving } = useAdminStorage();
|
||||
const [newNpub, setNewNpub] = useState('');
|
||||
|
||||
const addToAllowList = async () => {
|
||||
try {
|
||||
const decoded = nip19.decode(newNpub);
|
||||
if (decoded.type === 'npub') {
|
||||
const pubkey = decoded.data as unknown as string;
|
||||
if (!adminData.allowList.includes(pubkey)) {
|
||||
const newAllowList = [...adminData.allowList, pubkey];
|
||||
const newDenyList = adminData.denyList.filter(p => p !== pubkey); // Remove from deny list if present
|
||||
|
||||
await updateAllowList(newAllowList);
|
||||
if (newDenyList.length !== adminData.denyList.length) {
|
||||
await updateDenyList(newDenyList);
|
||||
}
|
||||
setNewNpub('');
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Invalid npub format
|
||||
}
|
||||
};
|
||||
|
||||
const addToDenyList = async () => {
|
||||
try {
|
||||
const decoded = nip19.decode(newNpub);
|
||||
if (decoded.type === 'npub') {
|
||||
const pubkey = decoded.data as unknown as string;
|
||||
if (!adminData.denyList.includes(pubkey)) {
|
||||
const newDenyList = [...adminData.denyList, pubkey];
|
||||
const newAllowList = adminData.allowList.filter(p => p !== pubkey); // Remove from allow list if present
|
||||
|
||||
await updateDenyList(newDenyList);
|
||||
if (newAllowList.length !== adminData.allowList.length) {
|
||||
await updateAllowList(newAllowList);
|
||||
}
|
||||
setNewNpub('');
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Invalid npub format
|
||||
}
|
||||
};
|
||||
|
||||
const removeFromAllowList = async (pubkey: string) => {
|
||||
const newAllowList = adminData.allowList.filter(p => p !== pubkey);
|
||||
await updateAllowList(newAllowList);
|
||||
};
|
||||
|
||||
const removeFromDenyList = async (pubkey: string) => {
|
||||
const newDenyList = adminData.denyList.filter(p => p !== pubkey);
|
||||
await updateDenyList(newDenyList);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Add User Form */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<UserCheck className="h-5 w-5" />
|
||||
Add User to Lists
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Add users to allow list or deny list by their npub
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
placeholder="npub1abc123..."
|
||||
value={newNpub}
|
||||
onChange={(e) => setNewNpub(e.target.value)}
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button onClick={addToAllowList} variant="outline" className="text-green-600" disabled={isSaving}>
|
||||
<Plus className="h-4 w-4 mr-1" />
|
||||
Allow
|
||||
</Button>
|
||||
<Button onClick={addToDenyList} variant="outline" className="text-red-600" disabled={isSaving}>
|
||||
<Ban className="h-4 w-4 mr-1" />
|
||||
Deny
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="grid gap-6 md:grid-cols-2">
|
||||
{/* Allow List */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2 text-green-600">
|
||||
<UserCheck className="h-5 w-5" />
|
||||
Allow List ({adminData.allowList.length})
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Users explicitly allowed to post content
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{adminData.allowList.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{adminData.allowList.map((pubkey) => (
|
||||
<UserListItem
|
||||
key={pubkey}
|
||||
pubkey={pubkey}
|
||||
onRemove={() => removeFromAllowList(pubkey)}
|
||||
variant="allow"
|
||||
disabled={isSaving}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground text-sm">No users in allow list</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Deny List */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2 text-red-600">
|
||||
<Ban className="h-5 w-5" />
|
||||
Deny List ({adminData.denyList.length})
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Users blocked from posting content
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{adminData.denyList.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{adminData.denyList.map((pubkey) => (
|
||||
<UserListItem
|
||||
key={pubkey}
|
||||
pubkey={pubkey}
|
||||
onRemove={() => removeFromDenyList(pubkey)}
|
||||
variant="deny"
|
||||
disabled={isSaving}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground text-sm">No users in deny list</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function BlockedWordsManagement() {
|
||||
const { data: adminData, updateBlockedWords, updateModerationSettings, isSaving } = useAdminStorage();
|
||||
const [newWord, setNewWord] = useState('');
|
||||
|
||||
const addBlockedWord = async () => {
|
||||
const word = newWord.trim();
|
||||
if (word && !adminData.blockedWords.includes(word)) {
|
||||
const newBlockedWords = [...adminData.blockedWords, word];
|
||||
await updateBlockedWords(newBlockedWords);
|
||||
setNewWord('');
|
||||
}
|
||||
};
|
||||
|
||||
const removeBlockedWord = async (word: string) => {
|
||||
const newBlockedWords = adminData.blockedWords.filter(w => w !== word);
|
||||
await updateBlockedWords(newBlockedWords);
|
||||
};
|
||||
|
||||
const handleKeyPress = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter') {
|
||||
addBlockedWord();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<MessageSquareX className="h-5 w-5" />
|
||||
Content Filtering Settings
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Configure automatic content filtering options
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center space-x-2">
|
||||
<Switch
|
||||
id="case-sensitive"
|
||||
checked={adminData.caseSensitive}
|
||||
onCheckedChange={(checked) => updateModerationSettings({ caseSensitive: checked })}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
<Label htmlFor="case-sensitive">Case sensitive matching</Label>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-2">
|
||||
<Switch
|
||||
id="whole-words"
|
||||
checked={adminData.wholeWordsOnly}
|
||||
onCheckedChange={(checked) => updateModerationSettings({ wholeWordsOnly: checked })}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
<Label htmlFor="whole-words">Match whole words only</Label>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Add Blocked Word</CardTitle>
|
||||
<CardDescription>
|
||||
Add words or phrases to automatically filter from content
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
placeholder="Enter word or phrase to block..."
|
||||
value={newWord}
|
||||
onChange={(e) => setNewWord(e.target.value)}
|
||||
onKeyPress={handleKeyPress}
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button onClick={addBlockedWord} disabled={isSaving}>
|
||||
<Plus className="h-4 w-4 mr-1" />
|
||||
Add
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Blocked Words ({adminData.blockedWords.length})</CardTitle>
|
||||
<CardDescription>
|
||||
Currently blocked words and phrases
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{adminData.blockedWords.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{adminData.blockedWords.map((word) => (
|
||||
<div key={word} className="flex items-center justify-between p-2 border rounded">
|
||||
<span className="font-mono text-sm">{word}</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => removeBlockedWord(word)}
|
||||
className="text-red-600 hover:text-red-700"
|
||||
disabled={isSaving}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground">No blocked words configured</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function SiteAnalytics() {
|
||||
const { nostr } = useNostr();
|
||||
|
||||
const siteStats = useQuery({
|
||||
queryKey: ['site-analytics'],
|
||||
queryFn: async ({ signal }) => {
|
||||
const timeoutSignal = AbortSignal.timeout(1500);
|
||||
const combinedSignal = AbortSignal.any([signal, timeoutSignal]);
|
||||
|
||||
// Get recent site activity
|
||||
const recentEvents = await nostr.query([{
|
||||
kinds: [1, 30023], // Notes and articles
|
||||
limit: 100
|
||||
}], { signal: combinedSignal });
|
||||
|
||||
const uniqueAuthors = new Set(recentEvents.map(e => e.pubkey)).size;
|
||||
const last24h = recentEvents.filter(e => e.created_at > Date.now() / 1000 - 86400).length;
|
||||
const last7d = recentEvents.filter(e => e.created_at > Date.now() / 1000 - 604800).length;
|
||||
|
||||
return {
|
||||
totalEvents: recentEvents.length,
|
||||
uniqueAuthors,
|
||||
last24h,
|
||||
last7d,
|
||||
notes: recentEvents.filter(e => e.kind === 1).length,
|
||||
articles: recentEvents.filter(e => e.kind === 30023).length,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-4">
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium">Total Content</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{siteStats.data?.totalEvents || 0}</div>
|
||||
<p className="text-xs text-muted-foreground">Recent events</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium">Active Users</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{siteStats.data?.uniqueAuthors || 0}</div>
|
||||
<p className="text-xs text-muted-foreground">Unique authors</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium">Last 24h</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{siteStats.data?.last24h || 0}</div>
|
||||
<p className="text-xs text-muted-foreground">New posts</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm font-medium">Last 7 days</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-2xl font-bold">{siteStats.data?.last7d || 0}</div>
|
||||
<p className="text-xs text-muted-foreground">New posts</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Content Breakdown</CardTitle>
|
||||
<CardDescription>
|
||||
Distribution of content types on your site
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{siteStats.isLoading ? (
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-3/4" />
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<span>Notes</span>
|
||||
<Badge variant="default">{siteStats.data?.notes || 0}</Badge>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span>Articles</span>
|
||||
<Badge variant="secondary">{siteStats.data?.articles || 0}</Badge>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function SiteSettings() {
|
||||
const { data: adminData, updateSiteSettings, updateModerationSettings, isSaving } = useAdminStorage();
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Site Information</CardTitle>
|
||||
<CardDescription>
|
||||
Basic information about your site
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="site-name">Site Name</Label>
|
||||
<Input
|
||||
id="site-name"
|
||||
value={adminData.siteName}
|
||||
onChange={(e) => updateSiteSettings({ siteName: e.target.value })}
|
||||
placeholder="My Nostr Site"
|
||||
disabled={isSaving}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="site-description">Site Description</Label>
|
||||
<Textarea
|
||||
id="site-description"
|
||||
value={adminData.siteDescription}
|
||||
onChange={(e) => updateSiteSettings({ siteDescription: e.target.value })}
|
||||
placeholder="A description of your site..."
|
||||
rows={3}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Content Moderation</CardTitle>
|
||||
<CardDescription>
|
||||
Configure how content moderation works on your site
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label>Enable Content Moderation</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Apply blocked words and user lists to filter content
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={adminData.enableModeration}
|
||||
onCheckedChange={(checked) => updateModerationSettings({ enableModeration: checked })}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label>Auto-hide Blocked Content</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Automatically hide content that matches blocked words
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={adminData.autoHideBlocked}
|
||||
onCheckedChange={(checked) => updateModerationSettings({ autoHideBlocked: checked })}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<Label>Require Content Approval</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
All content must be manually approved before showing
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={adminData.requireApproval}
|
||||
onCheckedChange={(checked) => updateModerationSettings({ requireApproval: checked })}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Environment Configuration</CardTitle>
|
||||
<CardDescription>
|
||||
Current environment settings
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-2 font-mono text-sm">
|
||||
<div className="flex justify-between">
|
||||
<span>VITE_ADMIN_NPUB:</span>
|
||||
<span className="text-muted-foreground">
|
||||
{ADMIN_NPUB ? `${ADMIN_NPUB.slice(0, 16)}...` : 'Not configured'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!ADMIN_NPUB && (
|
||||
<Alert className="mt-4">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertDescription>
|
||||
Set VITE_ADMIN_NPUB environment variable to configure admin access.
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Helper components
|
||||
function UserAvatar({ pubkey }: { pubkey: string }) {
|
||||
const author = useAuthor(pubkey);
|
||||
|
||||
return (
|
||||
<Avatar>
|
||||
<AvatarImage src={author.data?.metadata?.picture} />
|
||||
<AvatarFallback>
|
||||
{(author.data?.metadata?.name || genUserName(pubkey)).slice(0, 2).toUpperCase()}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
);
|
||||
}
|
||||
|
||||
function UserName({ pubkey }: { pubkey: string }) {
|
||||
const author = useAuthor(pubkey);
|
||||
|
||||
return (
|
||||
<span className="font-medium">
|
||||
{author.data?.metadata?.display_name || author.data?.metadata?.name || genUserName(pubkey)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function UserListItem({ pubkey, onRemove, variant, disabled }: {
|
||||
pubkey: string;
|
||||
onRemove: () => void;
|
||||
variant: 'allow' | 'deny';
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
const npub = nip19.npubEncode(pubkey);
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-3 p-2 rounded-lg border">
|
||||
<UserAvatar pubkey={pubkey} />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium text-sm truncate">
|
||||
<UserName pubkey={pubkey} />
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground truncate">
|
||||
{npub.slice(0, 20)}...
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onRemove}
|
||||
className={variant === 'allow' ? 'text-green-600 hover:text-green-700' : 'text-red-600 hover:text-red-700'}
|
||||
disabled={disabled}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user