All files / client/src/components template-library-dialog.tsx

0% Statements 0/25
0% Branches 0/15
0% Functions 0/11
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                                                                                                                                                                                                                                                                                     
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { apiRequest } from "@/lib/queryClient";
import type { PromptTemplate, UseCaseCategory } from "@shared/schema";
import { Code, Lightbulb, Search, FileText, GraduationCap, Briefcase, Sparkles } from "lucide-react";
 
interface TemplateLibraryDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onSelectTemplate: (template: PromptTemplate) => void;
}
 
const categoryIcons: Record<string, any> = {
  writing: FileText,
  research: Search,
  coding: Code,
  analysis: Search,
  creative: Lightbulb,
  education: GraduationCap,
  business: Briefcase,
  general: Sparkles,
};
 
const categoryColors: Record<string, string> = {
  writing: "bg-blue-500",
  research: "bg-purple-500",
  coding: "bg-green-500",
  analysis: "bg-orange-500",
  creative: "bg-pink-500",
  education: "bg-indigo-500",
  business: "bg-gray-500",
  general: "bg-teal-500",
};
 
export function TemplateLibraryDialog({ open, onOpenChange, onSelectTemplate }: TemplateLibraryDialogProps) {
  const [selectedCategory, setSelectedCategory] = useState<string>("all");
 
  const { data: templates = [], isLoading } = useQuery<PromptTemplate[]>({
    queryKey: ["/api/prompt-templates"],
    queryFn: async () => {
      const res = await apiRequest("GET", "/api/prompt-templates");
      return res.json();
    },
    enabled: open,
  });
 
  const categories = Array.from(new Set(templates.map(t => t.category)));
  const filteredTemplates = selectedCategory === "all" 
    ? templates 
    : templates.filter(t => t.category === selectedCategory);
 
  const handleSelectTemplate = (template: PromptTemplate) => {
    onSelectTemplate(template);
    onOpenChange(false);
  };
 
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-4xl max-h-[80vh]">
        <DialogHeader>
          <DialogTitle>Template Library</DialogTitle>
          <DialogDescription>
            Choose from pre-built templates for different use cases
          </DialogDescription>
        </DialogHeader>
 
        <Tabs value={selectedCategory} onValueChange={setSelectedCategory} className="w-full">
          <TabsList className="grid w-full grid-cols-4 lg:grid-cols-9">
            <TabsTrigger value="all">All</TabsTrigger>
            {categories.map(category => (
              <TabsTrigger key={category} value={category} className="capitalize">
                {category}
              </TabsTrigger>
            ))}
          </TabsList>
 
          <ScrollArea className="h-[50vh] mt-4">
            {isLoading ? (
              <div className="flex items-center justify-center h-40">
                <p className="text-muted-foreground">Loading templates...</p>
              </div>
            ) : filteredTemplates.length === 0 ? (
              <div className="flex items-center justify-center h-40">
                <p className="text-muted-foreground">No templates found</p>
              </div>
            ) : (
              <div className="grid gap-4 p-1">
                {filteredTemplates.map(template => {
                  const Icon = categoryIcons[template.category] || Sparkles;
                  const colorClass = categoryColors[template.category] || "bg-gray-500";
 
                  return (
                    <div
                      key={template.id}
                      className="border rounded-lg p-4 hover:bg-muted/50 cursor-pointer transition-colors"
                      onClick={() => handleSelectTemplate(template)}
                    >
                      <div className="flex items-start gap-3">
                        <div className={`${colorClass} p-2 rounded-lg text-white`}>
                          <Icon className="h-5 w-5" />
                        </div>
                        <div className="flex-1">
                          <div className="flex items-start justify-between gap-2">
                            <h4 className="font-semibold text-sm">{template.name}</h4>
                            <Badge variant="outline" className="capitalize">
                              {template.category}
                            </Badge>
                          </div>
                          <p className="text-sm text-muted-foreground mt-1">
                            {template.description}
                          </p>
                          {template.tags && (() => {
                            try {
                              const tags = JSON.parse(template.tags);
                              return (
                                <div className="flex flex-wrap gap-1 mt-2">
                                  {tags.map((tag: string) => (
                                    <Badge key={tag} variant="secondary" className="text-xs">
                                      {tag}
                                    </Badge>
                                  ))}
                                </div>
                              );
                            } catch (e) {
                              return null;
                            }
                          })()}
                          {template.systemPrompt && (
                            <p className="text-xs text-muted-foreground mt-2 line-clamp-2">
                              System: {template.systemPrompt}
                            </p>
                          )}
                        </div>
                      </div>
                    </div>
                  );
                })}
              </div>
            )}
          </ScrollArea>
        </Tabs>
 
        <div className="flex justify-end gap-2">
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            Cancel
          </Button>
        </div>
      </DialogContent>
    </Dialog>
  );
}