Admin Feed Manager - Create system posts

cgen-3cd25e567e604d0d820275dc93b1fe2c
This commit is contained in:
Builder.io 2025-11-14 22:24:24 +00:00
parent 4933692480
commit 95117207ed

View file

@ -1,17 +1,9 @@
import { useState, useEffect } from "react"; import { useState } from "react";
import { useNavigate } from "react-router-dom";
import Layout from "@/components/Layout"; import Layout from "@/components/Layout";
import SEO from "@/components/SEO"; import { useAuth } from "@/contexts/AuthContext";
import { useToast } from "@/hooks/use-aethex-toast";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { import {
Select, Select,
@ -20,400 +12,327 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { useAuth } from "@/contexts/AuthContext"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { aethexToast } from "@/lib/aethex-toast"; import { Loader2, Sparkles } from "lucide-react";
import LoadingScreen from "@/components/LoadingScreen";
import ArmPostCard, { ArmType } from "@/components/feed/ArmPostCard";
import { PenTool, Trash2, Eye, Lock } from "lucide-react";
const API_BASE = import.meta.env.VITE_API_BASE || ""; const ARMS = [
{ id: "labs", label: "Labs", color: "bg-yellow-500" },
const ARM_OPTIONS: { id: ArmType; label: string }[] = [ { id: "gameforge", label: "GameForge", color: "bg-green-500" },
{ id: "labs", label: "Labs" }, { id: "corp", label: "Corp", color: "bg-blue-500" },
{ id: "gameforge", label: "GameForge" }, { id: "foundation", label: "Foundation", color: "bg-red-500" },
{ id: "corp", label: "Corp" }, { id: "devlink", label: "Dev-Link", color: "bg-cyan-500" },
{ id: "foundation", label: "Foundation" }, { id: "nexus", label: "Nexus", color: "bg-purple-500" },
{ id: "devlink", label: "Dev-Link" }, { id: "staff", label: "Staff", color: "bg-indigo-500" },
{ id: "nexus", label: "Nexus" },
{ id: "staff", label: "Staff" },
]; ];
interface AdminPost {
id: string;
title: string;
content: string;
arm_affiliation: ArmType;
author_id: string;
created_at: string;
is_published: boolean;
tags?: string[];
category?: string;
user_profiles?: {
id: string;
username?: string;
full_name?: string;
avatar_url?: string;
};
}
export default function AdminFeed() { export default function AdminFeed() {
const { user, roles, loading: authLoading } = useAuth(); const { user } = useAuth();
const navigate = useNavigate(); const { toast } = useToast();
const hasAccess = roles.includes("admin") || roles.includes("staff"); const [isLoading, setIsLoading] = useState(false);
// Form state
const [title, setTitle] = useState(""); const [title, setTitle] = useState("");
const [content, setContent] = useState(""); const [content, setContent] = useState("");
const [armAffiliation, setArmAffiliation] = useState<ArmType>("labs"); const [selectedArm, setSelectedArm] = useState("labs");
const [tags, setTags] = useState(""); const [tags, setTags] = useState<string[]>([]);
const [category, setCategory] = useState(""); const [tagInput, setTagInput] = useState("");
const [isPublished, setIsPublished] = useState(true); const [isDraft, setIsDraft] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
// Posts list state // Check admin access
const [posts, setPosts] = useState<AdminPost[]>([]); if (!user?.user_metadata?.is_admin) {
const [isLoadingPosts, setIsLoadingPosts] = useState(true);
// Load all posts
const loadPosts = async () => {
setIsLoadingPosts(true);
try {
const response = await fetch(`${API_BASE}/api/admin/feed`);
if (response.ok) {
const data = await response.json();
setPosts(data.posts || []);
}
} catch (error) {
console.error("Failed to load posts:", error);
aethexToast.error({
title: "Failed to load posts",
description: "Please try again",
});
} finally {
setIsLoadingPosts(false);
}
};
useEffect(() => {
if (!authLoading && (!user || !hasAccess)) {
navigate("/login", { replace: true });
} else {
loadPosts();
}
}, [user, hasAccess, authLoading, navigate]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!title.trim() || !content.trim()) {
aethexToast.error({
title: "Validation error",
description: "Title and content are required",
});
return;
}
setIsSubmitting(true);
try {
const tagsArray = tags
.split(",")
.map((t) => t.trim())
.filter((t) => t.length > 0);
const response = await fetch(`${API_BASE}/api/admin/feed`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
title,
content,
arm_affiliation: armAffiliation,
author_id: user?.id,
tags: tagsArray,
category: category || null,
is_published: isPublished,
}),
});
if (response.ok) {
aethexToast.success({
title: "Post created",
description: "Your post has been published successfully",
});
// Reset form
setTitle("");
setContent("");
setArmAffiliation("labs");
setTags("");
setCategory("");
setIsPublished(true);
// Reload posts
loadPosts();
} else {
const error = await response.json();
aethexToast.error({
title: "Failed to create post",
description: error.error || "Please try again",
});
}
} catch (error) {
console.error("Error creating post:", error);
aethexToast.error({
title: "Error",
description: error instanceof Error ? error.message : "Unknown error",
});
} finally {
setIsSubmitting(false);
}
};
if (authLoading) {
return <LoadingScreen message="Verifying access..." />;
}
if (!user || !hasAccess) {
return ( return (
<Layout> <Layout>
<div className="min-h-screen bg-aethex-gradient py-12"> <div className="min-h-screen flex items-center justify-center">
<div className="container mx-auto px-4 max-w-md"> <Card className="w-full max-w-md">
<Card className="bg-red-500/10 border-red-500/30"> <CardHeader>
<CardHeader> <CardTitle>Access Denied</CardTitle>
<CardTitle className="text-red-400">Access Denied</CardTitle> </CardHeader>
<CardDescription> <CardContent>
This page requires admin or staff access. <p className="text-muted-foreground">
</CardDescription> Only administrators can access this page.
</CardHeader> </p>
<CardContent> </CardContent>
<Button onClick={() => navigate("/dashboard")}> </Card>
Go to Dashboard
</Button>
</CardContent>
</Card>
</div>
</div> </div>
</Layout> </Layout>
); );
} }
const addTag = () => {
const trimmed = tagInput.trim();
if (trimmed && !tags.includes(trimmed)) {
setTags([...tags, trimmed]);
setTagInput("");
}
};
const removeTag = (tag: string) => {
setTags(tags.filter((t) => t !== tag));
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!title.trim() || !content.trim()) {
toast({
description: "Title and content are required",
variant: "destructive",
});
return;
}
if (!user?.id) {
toast({
description: "You must be logged in to create posts",
variant: "destructive",
});
return;
}
setIsLoading(true);
try {
const response = await fetch("/api/community/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: title.trim(),
content: content.trim(),
arm_affiliation: selectedArm,
author_id: user.id,
tags: tags,
category: "announcement",
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to create post");
}
const { post } = await response.json();
toast({
description: "Post created successfully! 🎉",
});
// Reset form
setTitle("");
setContent("");
setTags([]);
setTagInput("");
setSelectedArm("labs");
} catch (error: any) {
console.error("Failed to create post:", error);
toast({
description: error.message || "Failed to create post",
variant: "destructive",
});
} finally {
setIsLoading(false);
}
};
const selectedArmData = ARMS.find((arm) => arm.id === selectedArm);
return ( return (
<Layout> <Layout>
<SEO <div className="min-h-screen bg-[radial-gradient(circle_at_top,_rgba(110,141,255,0.12),transparent_60%)]">
title="Manage Feed | AeThex Admin" <div className="mx-auto flex w-full max-w-3xl flex-col gap-8 px-4 pb-16 pt-10 lg:px-6">
description="Create and manage community feed posts"
/>
<div className="min-h-screen bg-aethex-gradient py-8">
<div className="container mx-auto px-4 max-w-6xl">
{/* Header */} {/* Header */}
<div className="mb-8 animate-slide-down"> <div className="space-y-2">
<h1 className="text-4xl font-bold bg-gradient-to-r from-aethex-300 via-neon-blue to-aethex-400 bg-clip-text text-transparent"> <div className="flex items-center gap-2">
Feed Management <Sparkles className="h-6 w-6 text-aethex-400" />
</h1> <h1 className="text-4xl font-bold text-foreground">
<p className="text-muted-foreground mt-2"> Feed Manager
Create and manage community feed posts </h1>
</div>
<p className="text-muted-foreground">
Create system announcements and showcase Arm-to-Arm partnerships.
This is how we prove the Axiom Model in action.
</p> </p>
</div> </div>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6"> {/* Main Form */}
{/* Create Post Form */} <Card className="border-border/40 bg-background/70 shadow-xl backdrop-blur-lg">
<div className="lg:col-span-2"> <CardHeader>
<Card className="bg-card/50 border-aethex-400/30 backdrop-blur-sm"> <CardTitle>Create a New Post</CardTitle>
<CardHeader> </CardHeader>
<CardTitle className="flex items-center gap-2"> <CardContent>
<PenTool className="h-5 w-5 text-aethex-400" /> <form onSubmit={handleSubmit} className="space-y-6">
Create New Post {/* Title */}
</CardTitle> <div className="space-y-2">
<CardDescription> <label className="block text-sm font-medium text-foreground">
Create a post for the community feed with arm affiliation Title <span className="text-red-400">*</span>
</CardDescription> </label>
</CardHeader> <Input
<CardContent> placeholder="e.g., Announcing our partnership with GameForge..."
<form onSubmit={handleSubmit} className="space-y-6"> value={title}
{/* Title */} onChange={(e) => setTitle(e.target.value)}
<div className="space-y-2"> maxLength={500}
<Label htmlFor="title">Title *</Label> className="border-border/40 bg-background/80"
<Input />
id="title" <p className="text-xs text-muted-foreground">
value={title} {title.length}/500 characters
onChange={(e) => setTitle(e.target.value)} </p>
placeholder="Post title"
className="bg-background/50 border-border/50"
required
/>
</div>
{/* Content */}
<div className="space-y-2">
<Label htmlFor="content">Content *</Label>
<Textarea
id="content"
value={content}
onChange={(e) => setContent(e.target.value)}
placeholder="Write your post content here..."
rows={8}
className="bg-background/50 border-border/50 font-mono text-sm"
required
/>
<p className="text-xs text-muted-foreground">
Markdown formatting is supported
</p>
</div>
{/* Arm Affiliation */}
<div className="space-y-2">
<Label htmlFor="arm">Arm Affiliation *</Label>
<Select value={armAffiliation} onValueChange={(value: any) => setArmAffiliation(value)}>
<SelectTrigger id="arm" className="bg-background/50 border-border/50">
<SelectValue />
</SelectTrigger>
<SelectContent>
{ARM_OPTIONS.map((arm) => (
<SelectItem key={arm.id} value={arm.id}>
{arm.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Tags */}
<div className="space-y-2">
<Label htmlFor="tags">Tags (comma-separated)</Label>
<Input
id="tags"
value={tags}
onChange={(e) => setTags(e.target.value)}
placeholder="e.g., update, announcement, showcase"
className="bg-background/50 border-border/50"
/>
</div>
{/* Category */}
<div className="space-y-2">
<Label htmlFor="category">Category</Label>
<Input
id="category"
value={category}
onChange={(e) => setCategory(e.target.value)}
placeholder="e.g., announcement, showcase, discussion"
className="bg-background/50 border-border/50"
/>
</div>
{/* Published Toggle */}
<div className="flex items-center space-x-3 p-3 rounded-lg bg-background/40 border border-border/40">
<Switch
id="published"
checked={isPublished}
onCheckedChange={setIsPublished}
/>
<Label htmlFor="published" className="cursor-pointer flex-1 mb-0">
<div className="flex items-center gap-2">
{isPublished ? (
<>
<Eye className="h-4 w-4 text-green-400" />
<span>Publish immediately</span>
</>
) : (
<>
<Lock className="h-4 w-4 text-yellow-400" />
<span>Save as draft</span>
</>
)}
</div>
</Label>
</div>
{/* Submit */}
<Button
type="submit"
disabled={isSubmitting}
className="w-full bg-gradient-to-r from-aethex-600 to-neon-blue hover:from-aethex-700 hover:to-neon-blue/90"
>
{isSubmitting ? "Publishing..." : "Publish Post"}
</Button>
</form>
</CardContent>
</Card>
</div>
{/* Preview Sidebar */}
<div className="lg:col-span-1">
{title && content && (
<div className="sticky top-24">
<Card className="bg-card/50 border-border/40 mb-4">
<CardHeader>
<CardTitle className="text-sm">Preview</CardTitle>
</CardHeader>
<CardContent>
<ArmPostCard
post={{
id: "preview",
title,
content,
arm_affiliation: armAffiliation,
author_id: user?.id || "",
created_at: new Date().toISOString(),
tags: tags
.split(",")
.map((t) => t.trim())
.filter((t) => t.length > 0),
category: category || undefined,
user_profiles: {
id: user?.id || "",
full_name: user?.user_metadata?.full_name || "You",
avatar_url: user?.user_metadata?.avatar_url,
},
}}
/>
</CardContent>
</Card>
</div> </div>
)}
</div>
</div>
{/* Recent Posts */} {/* Content */}
<div className="mt-12"> <div className="space-y-2">
<Card className="bg-card/50 border-border/40"> <label className="block text-sm font-medium text-foreground">
<CardHeader> Content <span className="text-red-400">*</span>
<CardTitle className="text-2xl">Recent Posts</CardTitle> </label>
<CardDescription> <Textarea
{posts.length} posts total placeholder="Share your announcement, partnership details, or showcase..."
</CardDescription> value={content}
</CardHeader> onChange={(e) => setContent(e.target.value)}
<CardContent> maxLength={5000}
{isLoadingPosts ? ( rows={12}
<div className="text-center py-8"> className="border-border/40 bg-background/80 resize-none"
<p className="text-muted-foreground">Loading posts...</p> />
<p className="text-xs text-muted-foreground">
{content.length}/5000 characters
</p>
</div>
{/* Arm Selection */}
<div className="space-y-2">
<label className="block text-sm font-medium text-foreground">
Arm Affiliation <span className="text-red-400">*</span>
</label>
<Select value={selectedArm} onValueChange={setSelectedArm}>
<SelectTrigger className="border-border/40 bg-background/80">
<SelectValue />
</SelectTrigger>
<SelectContent>
{ARMS.map((arm) => (
<SelectItem key={arm.id} value={arm.id}>
<div className="flex items-center gap-2">
<div
className={`h-2 w-2 rounded-full ${arm.color}`}
/>
{arm.label}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
This determines which section and color badge the post
displays under.
</p>
</div>
{/* Tags */}
<div className="space-y-2">
<label className="block text-sm font-medium text-foreground">
Tags (Optional)
</label>
<div className="flex gap-2">
<Input
placeholder="Add a tag and press Enter..."
value={tagInput}
onChange={(e) => setTagInput(e.target.value)}
onKeyPress={(e) => {
if (e.key === "Enter") {
e.preventDefault();
addTag();
}
}}
className="border-border/40 bg-background/80 flex-1"
/>
<Button
type="button"
variant="outline"
onClick={addTag}
className="border-border/40"
>
Add
</Button>
</div> </div>
) : posts.length === 0 ? ( {tags.length > 0 && (
<div className="text-center py-8"> <div className="flex flex-wrap gap-2 pt-2">
<p className="text-muted-foreground">No posts yet. Create your first post above!</p> {tags.map((tag) => (
<Badge
key={tag}
variant="outline"
className="cursor-pointer border-border/40"
onClick={() => removeTag(tag)}
>
{tag}
</Badge>
))}
</div>
)}
</div>
{/* Submit Buttons */}
<div className="flex gap-3 pt-4">
<Button
type="submit"
disabled={isLoading}
className="flex-1 gap-2 rounded-full bg-gradient-to-r from-aethex-500 to-neon-blue text-white hover:shadow-lg disabled:opacity-50"
>
{isLoading && (
<Loader2 className="h-4 w-4 animate-spin" />
)}
{isLoading ? "Publishing..." : "Publish Post"}
</Button>
</div>
</form>
</CardContent>
</Card>
{/* Quick Reference */}
<Card className="border-border/40 bg-background/70 shadow-xl backdrop-blur-lg">
<CardHeader>
<CardTitle className="text-lg">Arm Color Guide</CardTitle>
</CardHeader>
<CardContent>
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
{ARMS.map((arm) => (
<div
key={arm.id}
className="flex items-center gap-2 rounded-lg border border-border/30 bg-background/60 p-3"
>
<div className={`h-3 w-3 rounded-full ${arm.color}`} />
<span className="text-sm font-medium text-foreground">
{arm.label}
</span>
</div> </div>
) : ( ))}
<div className="grid grid-cols-1 md:grid-cols-2 gap-4"> </div>
{posts.map((post) => ( </CardContent>
<div key={post.id} className="relative"> </Card>
<div className="absolute top-2 right-2 z-10 flex gap-2">
{!post.is_published && ( {/* Guidelines */}
<Badge variant="outline" className="border-yellow-500/50 text-yellow-400"> <Card className="border-border/40 bg-background/70 shadow-xl backdrop-blur-lg">
Draft <CardHeader>
</Badge> <CardTitle className="text-lg">Phase 1 Guidelines</CardTitle>
)} </CardHeader>
</div> <CardContent className="space-y-3 text-sm text-muted-foreground">
<ArmPostCard post={post as any} /> <p>
</div> <strong>System Posts Only:</strong> Use this for official
))} announcements, partnerships, and Arm-to-Arm collaborations.
</div> </p>
)} <p>
</CardContent> 🔗 <strong>Firewall in Action:</strong> Every post shows the Arm
</Card> color badge. This proves our Guardian (Foundation) Engine
</div> (Corp/Labs) separation is real.
</p>
<p>
🤝 <strong>Partnership Showcase:</strong> Use these posts to show
how different Arms collaborate. Example: "Corp hired 3 Architects
from Foundation via Nexus."
</p>
<p>
🚀 <strong>Phase 2:</strong> User-generated posts coming soon.
This Phase 1 proves the system works with curated content first.
</p>
</CardContent>
</Card>
</div> </div>
</div> </div>
</Layout> </Layout>