diff --git a/src/components/DiscoverPluginsDialog.tsx b/src/components/DiscoverPluginsDialog.tsx new file mode 100644 index 00000000..ec1c7722 --- /dev/null +++ b/src/components/DiscoverPluginsDialog.tsx @@ -0,0 +1,229 @@ +import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { Search, Download, ExternalLink, Loader2, Check, Globe, Package } from 'lucide-react'; +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Card, CardContent } from '@/components/ui/card'; +import { Skeleton } from '@/components/ui/skeleton'; +import { useDiscoverPlugins } from '@/hooks/useDiscoverPlugins'; +import { usePlugins } from '@/hooks/usePlugins'; +import { useAuthor } from '@/hooks/useAuthor'; +import { useToast } from '@/hooks/useToast'; +import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; +import { genUserName } from '@/lib/genUserName'; +import type { NostrMetadata } from '@nostrify/nostrify'; + +interface DiscoverPluginsDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; +} + +export function DiscoverPluginsDialog({ open, onOpenChange }: DiscoverPluginsDialogProps) { + const { t } = useTranslation(); + const { data: plugins, isLoading } = useDiscoverPlugins(); + const [searchQuery, setSearchQuery] = useState(''); + + // Filter plugins based on search query + const filteredPlugins = plugins?.filter((plugin) => { + const query = searchQuery.toLowerCase(); + return ( + plugin.name.toLowerCase().includes(query) || + plugin.description.toLowerCase().includes(query) || + plugin.id.toLowerCase().includes(query) + ); + }); + + return ( + + + + + + {t('discoverPlugins')} + + + {t('discoverPluginsDescription')} + + + +
+ {/* Search Input */} +
+ + setSearchQuery(e.target.value)} + className="pl-9" + /> +
+ + {/* Plugins List */} +
+ {isLoading && ( + <> + {[...Array(3)].map((_, i) => ( + + +
+ +
+ + + +
+
+
+
+ ))} + + )} + + {!isLoading && filteredPlugins?.length === 0 && ( +
+ {searchQuery ? t('noPluginsFound') : t('noPluginsAvailable')} +
+ )} + + {!isLoading && filteredPlugins?.map((plugin) => ( + + ))} +
+
+
+
+ ); +} + +interface PluginCardProps { + plugin: { + id: string; + name: string; + description: string; + cloneUrl?: string; + webUrl?: string; + author: string; + }; +} + +function PluginCard({ plugin }: PluginCardProps) { + const { t } = useTranslation(); + const { toast } = useToast(); + const author = useAuthor(plugin.author); + const { clonePlugin, plugins: installedPlugins } = usePlugins(); + const metadata: NostrMetadata | undefined = author.data?.metadata; + + const isInstalled = installedPlugins.includes(plugin.id); + const isInstalling = clonePlugin.isPending && clonePlugin.variables?.pluginName === plugin.id; + + const handleInstall = async () => { + if (!plugin.cloneUrl) { + toast({ + title: t('noCloneUrl'), + description: t('noCloneUrlDescription'), + variant: 'destructive', + }); + return; + } + + try { + await clonePlugin.mutateAsync({ + gitUrl: plugin.cloneUrl, + pluginName: plugin.id, + }); + toast({ + title: t('pluginInstalled'), + description: t('pluginInstalledDescription', { name: plugin.name }), + }); + } catch (error) { + toast({ + title: t('pluginInstallFailed'), + description: error instanceof Error ? error.message : String(error), + variant: 'destructive', + }); + } + }; + + const displayName = metadata?.name ?? genUserName(plugin.author); + const profileImage = metadata?.picture; + + return ( + + + {/* Plugin Header with Author Info */} +
+ + + {displayName.slice(0, 2).toUpperCase()} + +
+
+

{plugin.name}

+ {isInstalled && ( + + + {t('installed')} + + )} +
+

+ {t('by')} {displayName} +

+
+
+ + {/* Plugin Description */} + {plugin.description && ( +

+ {plugin.description} +

+ )} + + {/* Action Buttons */} +
+ + {plugin.webUrl && ( + + )} +
+
+
+ ); +} diff --git a/src/components/PluginsSection.tsx b/src/components/PluginsSection.tsx index 09e7492e..3818d662 100644 --- a/src/components/PluginsSection.tsx +++ b/src/components/PluginsSection.tsx @@ -1,6 +1,6 @@ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { Puzzle, Plus, Trash2, RefreshCw, Loader2, Check } from 'lucide-react'; +import { Puzzle, Plus, Trash2, RefreshCw, Loader2, Check, Compass } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; @@ -8,12 +8,14 @@ import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/ import { usePlugins } from '@/hooks/usePlugins'; import { usePluginGitInfo } from '@/hooks/usePluginGitInfo'; import { useToast } from '@/hooks/useToast'; +import { DiscoverPluginsDialog } from '@/components/DiscoverPluginsDialog'; export function PluginsSection() { const { t } = useTranslation(); const { toast } = useToast(); const { plugins, skills, clonePlugin, syncPlugin, deletePlugin } = usePlugins(); const [gitUrl, setGitUrl] = useState(''); + const [showDiscoverDialog, setShowDiscoverDialog] = useState(false); const handleAddPlugin = async () => { if (!gitUrl.trim()) return; @@ -79,9 +81,20 @@ export function PluginsSection() {
{/* Plugins Header */}
-
- -

{t('plugins')}

+
+
+ +

{t('plugins')}

+
+

{t('pluginsDescription')} @@ -160,6 +173,12 @@ export function PluginsSection() { + + {/* Discover Plugins Dialog */} +

); } diff --git a/src/hooks/useDiscoverPlugins.test.ts b/src/hooks/useDiscoverPlugins.test.ts new file mode 100644 index 00000000..d694d27f --- /dev/null +++ b/src/hooks/useDiscoverPlugins.test.ts @@ -0,0 +1,84 @@ +import { describe, it, expect } from 'vitest'; +import type { NostrEvent } from '@nostrify/nostrify'; + +describe('useDiscoverPlugins', () => { + it('should validate NIP-34 event structure', () => { + // Example of a valid NIP-34 repository announcement event + const validEvent: NostrEvent = { + id: 'test-event-id', + pubkey: 'test-pubkey', + created_at: Math.floor(Date.now() / 1000), + kind: 30617, + tags: [ + ['d', 'test-plugin'], + ['name', 'Test Plugin'], + ['description', 'A test Shakespeare plugin'], + ['clone', 'https://github.com/user/test-plugin.git'], + ['web', 'https://github.com/user/test-plugin'], + ['t', 'shakespeare-plugin'], + ], + content: '', + sig: 'test-signature', + }; + + // Verify the event has the correct kind + expect(validEvent.kind).toBe(30617); + + // Verify required tags exist + const dTag = validEvent.tags.find(([name]) => name === 'd')?.[1]; + const nameTag = validEvent.tags.find(([name]) => name === 'name')?.[1]; + const tTag = validEvent.tags.find(([name, value]) => name === 't' && value === 'shakespeare-plugin'); + + expect(dTag).toBe('test-plugin'); + expect(nameTag).toBe('Test Plugin'); + expect(tTag).toBeDefined(); + }); + + it('should identify required fields for plugin discovery', () => { + const event: NostrEvent = { + id: 'test-event-id', + pubkey: 'test-pubkey', + created_at: Math.floor(Date.now() / 1000), + kind: 30617, + tags: [ + ['d', 'minimal-plugin'], + ['name', 'Minimal Plugin'], + ['t', 'shakespeare-plugin'], + ], + content: '', + sig: 'test-signature', + }; + + const dTag = event.tags.find(([name]) => name === 'd')?.[1]; + const nameTag = event.tags.find(([name]) => name === 'name')?.[1]; + + // These are the minimum required fields + expect(dTag).toBeDefined(); + expect(nameTag).toBeDefined(); + }); + + it('should handle optional fields gracefully', () => { + const event: NostrEvent = { + id: 'test-event-id', + pubkey: 'test-pubkey', + created_at: Math.floor(Date.now() / 1000), + kind: 30617, + tags: [ + ['d', 'plugin-without-optional'], + ['name', 'Plugin Without Optional Fields'], + ['t', 'shakespeare-plugin'], + ], + content: '', + sig: 'test-signature', + }; + + const descriptionTag = event.tags.find(([name]) => name === 'description')?.[1]; + const cloneTag = event.tags.find(([name]) => name === 'clone')?.[1]; + const webTag = event.tags.find(([name]) => name === 'web')?.[1]; + + // Optional fields should be undefined + expect(descriptionTag).toBeUndefined(); + expect(cloneTag).toBeUndefined(); + expect(webTag).toBeUndefined(); + }); +}); diff --git a/src/hooks/useDiscoverPlugins.ts b/src/hooks/useDiscoverPlugins.ts new file mode 100644 index 00000000..43443099 --- /dev/null +++ b/src/hooks/useDiscoverPlugins.ts @@ -0,0 +1,69 @@ +import { useQuery } from '@tanstack/react-query'; +import { useNostr } from '@nostrify/react'; +import type { NostrEvent } from '@nostrify/nostrify'; + +export interface DiscoveredPlugin { + id: string; + event: NostrEvent; + name: string; + description: string; + cloneUrl?: string; + webUrl?: string; + author: string; +} + +/** + * Hook to discover Shakespeare plugins on Nostr + * Queries NIP-34 git repository announcements with t-tag "shakespeare-plugin" + */ +export function useDiscoverPlugins() { + const { nostr } = useNostr(); + + return useQuery({ + queryKey: ['discover-plugins'], + queryFn: async (c) => { + const signal = AbortSignal.any([c.signal, AbortSignal.timeout(5000)]); + + // Query NIP-34 repository announcements (kind 30617) with shakespeare-plugin tag + const events = await nostr.query( + [ + { + kinds: [30617], + '#t': ['shakespeare-plugin'], + limit: 100, + }, + ], + { signal } + ); + + // Transform events into DiscoveredPlugin objects + const plugins: DiscoveredPlugin[] = events + .map((event): DiscoveredPlugin | null => { + const dTag = event.tags.find(([name]) => name === 'd')?.[1]; + const nameTag = event.tags.find(([name]) => name === 'name')?.[1]; + const descriptionTag = event.tags.find(([name]) => name === 'description')?.[1]; + const cloneTag = event.tags.find(([name]) => name === 'clone')?.[1]; + const webTag = event.tags.find(([name]) => name === 'web')?.[1]; + + // Skip events without required fields + if (!dTag || !nameTag) { + return null; + } + + return { + id: dTag, + event, + name: nameTag, + description: descriptionTag || '', + cloneUrl: cloneTag, + webUrl: webTag, + author: event.pubkey, + }; + }) + .filter((plugin): plugin is DiscoveredPlugin => plugin !== null); + + return plugins; + }, + staleTime: 5 * 60 * 1000, // 5 minutes + }); +} diff --git a/src/locales/en.json b/src/locales/en.json index ac451aa5..4efb1a28 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -478,6 +478,21 @@ "noPluginsConfigured": "No plugins configured. Add a plugin to get started.", "pluginsDirectory": "Plugins Directory", "pluginsDirectoryDescription": "Directory where plugins are stored in the virtual filesystem.", + "discoverPlugins": "Discover Plugins", + "discoverPluginsDescription": "Browse and install Shakespeare plugins shared on the Nostr network.", + "searchPlugins": "Search plugins...", + "noPluginsFound": "No plugins found matching your search.", + "noPluginsAvailable": "No plugins available at this time.", + "pluginInstalled": "Plugin Installed", + "pluginInstalledDescription": "Plugin \"{{name}}\" has been installed successfully.", + "pluginInstallFailed": "Failed to Install Plugin", + "noCloneUrl": "No Clone URL", + "noCloneUrlDescription": "This plugin does not have a clone URL configured.", + "installed": "Installed", + "install": "Install", + "installing": "Installing...", + "by": "by", + "website": "Website", "mcpServers": "MCP Servers", "mcpServersDescription": "Connect to Model Context Protocol servers to extend the AI's capabilities with external tools.", "addMcpServer": "Add MCP Server", diff --git a/src/locales/ha.json b/src/locales/ha.json index 053cd390..49073384 100644 --- a/src/locales/ha.json +++ b/src/locales/ha.json @@ -430,5 +430,20 @@ "modified": "An Canza", "restoreToDefault": "Mayar da asali", "restoredToDefault": "An Mayar da Asali", - "terminal": "Terminal" + "terminal": "Terminal", + "discoverPlugins": "Gano Plugins", + "discoverPluginsDescription": "Bincika kuma shigar da plugins na Shakespeare da aka raba akan hanyar sadarwa ta Nostr.", + "searchPlugins": "Bincika plugins...", + "noPluginsFound": "Ba a sami plugins da suka dace da bincikenka ba.", + "noPluginsAvailable": "Babu plugins a wannan lokaci.", + "pluginInstalled": "An Shigar da Plugin", + "pluginInstalledDescription": "An shigar da plugin \"{{name}}\" cikin nasara.", + "pluginInstallFailed": "An Kasa Shigar da Plugin", + "noCloneUrl": "Babu Clone URL", + "noCloneUrlDescription": "Wannan plugin ba shi da clone URL da aka saita.", + "installed": "An Shigar", + "install": "Shigar", + "installing": "Ana Shigarwa...", + "by": "ta", + "website": "Gidan Yanar Gizo" } diff --git a/src/locales/ig.json b/src/locales/ig.json index cbf2537b..68613793 100644 --- a/src/locales/ig.json +++ b/src/locales/ig.json @@ -427,5 +427,20 @@ "modified": "Gbanwere", "restoreToDefault": "Weghachi na nke mbụ", "restoredToDefault": "Weghachiri na Nke Mbụ", - "terminal": "Terminal" + "terminal": "Terminal", + "discoverPlugins": "Chọpụta Plugins", + "discoverPluginsDescription": "Chọgharịa ma wụnye plugins Shakespeare nke ekekọrịtara na netwọk Nostr.", + "searchPlugins": "Chọọ plugins...", + "noPluginsFound": "Achọtaghị plugins dabara na nchọgharị gị.", + "noPluginsAvailable": "Enweghị plugins dị ugbu a.", + "pluginInstalled": "Ewubatara Plugin", + "pluginInstalledDescription": "Ewubatara plugin \"{{name}}\" nke ọma.", + "pluginInstallFailed": "Ọ Dara Ada Ịwụnye Plugin", + "noCloneUrl": "Enweghị Clone URL", + "noCloneUrlDescription": "Plugin a enweghị clone URL edoziri.", + "installed": "Ewubatara", + "install": "Wụnye", + "installing": "Na-ewunye...", + "by": "site na", + "website": "Webụsaịtị" } diff --git a/src/locales/pt.json b/src/locales/pt.json index 9947de79..62b6bf47 100644 --- a/src/locales/pt.json +++ b/src/locales/pt.json @@ -439,5 +439,20 @@ "modified": "Modificado", "restoreToDefault": "Restaurar para padrão", "restoredToDefault": "Restaurado para Padrão", - "terminal": "Terminal" + "terminal": "Terminal", + "discoverPlugins": "Descobrir Plugins", + "discoverPluginsDescription": "Navegue e instale plugins do Shakespeare compartilhados na rede Nostr.", + "searchPlugins": "Buscar plugins...", + "noPluginsFound": "Nenhum plugin encontrado correspondente à sua busca.", + "noPluginsAvailable": "Nenhum plugin disponível no momento.", + "pluginInstalled": "Plugin Instalado", + "pluginInstalledDescription": "Plugin \"{{name}}\" foi instalado com sucesso.", + "pluginInstallFailed": "Falha ao Instalar Plugin", + "noCloneUrl": "Sem URL de Clone", + "noCloneUrlDescription": "Este plugin não tem uma URL de clone configurada.", + "installed": "Instalado", + "install": "Instalar", + "installing": "Instalando...", + "by": "por", + "website": "Site" } diff --git a/src/locales/yo.json b/src/locales/yo.json index 62286d38..f1e0967d 100644 --- a/src/locales/yo.json +++ b/src/locales/yo.json @@ -427,5 +427,20 @@ "modified": "Ti Yipada", "restoreToDefault": "Pada si atilẹwa", "restoredToDefault": "Ti Pada si Atilẹwa", - "terminal": "Terminal" + "terminal": "Terminal", + "discoverPlugins": "Ṣawari Awọn Plugins", + "discoverPluginsDescription": "Ṣayẹwo ati fi sori ẹrọ awọn plugins Shakespeare ti a pin lori nẹtiwọọki Nostr.", + "searchPlugins": "Wa awọn plugins...", + "noPluginsFound": "Ko si awọn plugins ti o baamu wiwa rẹ.", + "noPluginsAvailable": "Ko si awọn plugins ti o wa ni akoko yii.", + "pluginInstalled": "Plugin Ti Fi Sori Ẹrọ", + "pluginInstalledDescription": "A ti fi plugin \"{{name}}\" sori ẹrọ ni aṣeyọri.", + "pluginInstallFailed": "O Kuna Lati Fi Plugin Sori Ẹrọ", + "noCloneUrl": "Ko Si Clone URL", + "noCloneUrlDescription": "Plugin yii ko ni clone URL ti a ṣeto.", + "installed": "Ti Fi Sori Ẹrọ", + "install": "Fi Sori Ẹrọ", + "installing": "N Fi Sori Ẹrọ...", + "by": "nipasẹ", + "website": "Oju opo wẹẹbu" } diff --git a/src/locales/zh.json b/src/locales/zh.json index 83b361ae..7025324e 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -439,5 +439,20 @@ "modified": "已修改", "restoreToDefault": "恢复默认值", "restoredToDefault": "已恢复默认值", - "terminal": "终端" + "terminal": "终端", + "discoverPlugins": "发现插件", + "discoverPluginsDescription": "浏览并安装在 Nostr 网络上共享的 Shakespeare 插件。", + "searchPlugins": "搜索插件...", + "noPluginsFound": "未找到与您搜索匹配的插件。", + "noPluginsAvailable": "目前没有可用的插件。", + "pluginInstalled": "插件已安装", + "pluginInstalledDescription": "插件 \"{{name}}\" 已成功安装。", + "pluginInstallFailed": "安装插件失败", + "noCloneUrl": "无克隆 URL", + "noCloneUrlDescription": "此插件未配置克隆 URL。", + "installed": "已安装", + "install": "安装", + "installing": "安装中...", + "by": "作者", + "website": "网站" }