diff --git a/src/components/git/AnnounceRepositoryDialog.tsx b/src/components/git/AnnounceRepositoryDialog.tsx index 30d9ae16..dece235a 100644 --- a/src/components/git/AnnounceRepositoryDialog.tsx +++ b/src/components/git/AnnounceRepositoryDialog.tsx @@ -3,6 +3,7 @@ import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; +import { TagInput } from '@/components/ui/tag-input'; import { Dialog, DialogContent, @@ -33,6 +34,8 @@ import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useNostr } from '@/hooks/useNostr'; import { useToast } from '@/hooks/useToast'; import { useAppContext } from '@/hooks/useAppContext'; +import { useFS } from '@/hooks/useFS'; +import { DotAI } from '@/lib/DotAI'; import { nip19 } from 'nostr-tools'; import { createRepositoryAnnouncementEvent, @@ -75,6 +78,7 @@ export function AnnounceRepositoryDialog({ const [webUrls, setWebUrls] = useState([]); const [cloneUrls, setCloneUrls] = useState([]); const [relays, setRelays] = useState([]); + const [tTags, setTTags] = useState([]); const [earliestCommit, setEarliestCommit] = useState(''); const [isCreating, setIsCreating] = useState(false); const [error, setError] = useState(null); @@ -89,6 +93,7 @@ export function AnnounceRepositoryDialog({ const { nostr } = useNostr(); const { toast } = useToast(); const { config } = useAppContext(); + const { fs } = useFS(); const projectPath = `${projectsPath}/${projectId}`; @@ -169,9 +174,31 @@ export function AnnounceRepositoryDialog({ } } + // Prepopulate t-tags if empty + if (tTags.length === 0) { + const defaultTTags: string[] = ['shakespeare']; + + // Try to read template.json to get template name + try { + const dotAI = new DotAI(fs, projectPath); + const template = await dotAI.readTemplate(); + if (template?.name) { + // Convert template name to lowercase and add as t-tag + const templateTag = template.name.toLowerCase(); + if (!defaultTTags.includes(templateTag)) { + defaultTTags.push(templateTag); + } + } + } catch (error) { + console.warn('Failed to read template for t-tags:', error); + } + + setTTags(defaultTTags); + } + // Get earliest commit await getEarliestCommit(); - }, [projectId, config.relayMetadata, config.ngitServers, user?.pubkey, getEarliestCommit, cloneUrls.length, relays.length]); + }, [projectId, projectPath, fs, config.relayMetadata, config.ngitServers, user?.pubkey, getEarliestCommit, cloneUrls.length, relays.length, tTags.length]); // Load existing repository data if editing useEffect(() => { @@ -221,6 +248,13 @@ export function AnnounceRepositoryDialog({ setRelays(parsed.relays); setEarliestCommit(parsed.earliestCommit || ''); + // Extract t-tags from the event + const extractedTTags = parsed.event.tags + .filter(([name]) => name === 't') + .map(([, value]) => value) + .filter((value): value is string => !!value); + setTTags(extractedTTags); + setIsPrepopulated(true); } catch (error) { console.error('Failed to load repository data:', error); @@ -313,6 +347,7 @@ export function AnnounceRepositoryDialog({ webUrls, cloneUrls, relays, + tTags, earliestCommit, }); @@ -385,6 +420,7 @@ export function AnnounceRepositoryDialog({ setWebUrls([]); setCloneUrls([]); setRelays(['wss://relay.nostr.band']); + setTTags([]); setEarliestCommit(''); setError(null); setNewWebUrl(''); @@ -636,6 +672,23 @@ export function AnnounceRepositoryDialog({

+ {/* Category Tags (t-tags) */} +
+ + tag.trim().toLowerCase()} + allowDuplicates={false} + /> +

+ Tags for categorizing your repository. Press space or comma to add a tag. +

+
+ {/* Earliest Commit */}
diff --git a/src/components/ui/tag-input.test.tsx b/src/components/ui/tag-input.test.tsx new file mode 100644 index 00000000..d651182d --- /dev/null +++ b/src/components/ui/tag-input.test.tsx @@ -0,0 +1,256 @@ +import { describe, it, expect, vi } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { TagInput } from './tag-input'; + +describe('TagInput', () => { + it('renders with placeholder when no tags', () => { + const onChange = vi.fn(); + render( + + ); + + const input = screen.getByPlaceholderText('Add tags...'); + expect(input).toBeInTheDocument(); + }); + + it('displays existing tags', () => { + const onChange = vi.fn(); + render( + + ); + + expect(screen.getByText('tag1')).toBeInTheDocument(); + expect(screen.getByText('tag2')).toBeInTheDocument(); + expect(screen.getByText('tag3')).toBeInTheDocument(); + }); + + it('adds tag on space key', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByPlaceholderText('Add tags...'); + await user.type(input, 'newtag '); + + expect(onChange).toHaveBeenCalledWith(['newtag']); + }); + + it('adds tag on comma key', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByPlaceholderText('Add tags...'); + await user.type(input, 'newtag,'); + + expect(onChange).toHaveBeenCalledWith(['newtag']); + }); + + it('adds tag on Enter key', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByPlaceholderText('Add tags...'); + await user.type(input, 'newtag{Enter}'); + + expect(onChange).toHaveBeenCalledWith(['newtag']); + }); + + it('removes last tag on Backspace when input is empty', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByRole('textbox'); + await user.click(input); + await user.keyboard('{Backspace}'); + + expect(onChange).toHaveBeenCalledWith(['tag1']); + }); + + it('removes tag when clicking X button', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const removeButtons = screen.getAllByRole('button'); + await user.click(removeButtons[1]); // Remove tag2 + + expect(onChange).toHaveBeenCalledWith(['tag1', 'tag3']); + }); + + it('transforms tags using transformTag function', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + tag.toUpperCase()} + placeholder="Add tags..." + /> + ); + + const input = screen.getByPlaceholderText('Add tags...'); + await user.type(input, 'lowercase '); + + expect(onChange).toHaveBeenCalledWith(['LOWERCASE']); + }); + + it('prevents duplicate tags by default', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByRole('textbox'); + await user.type(input, 'existing '); + + expect(onChange).not.toHaveBeenCalled(); + }); + + it('allows duplicate tags when allowDuplicates is true', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByRole('textbox'); + await user.type(input, 'existing '); + + expect(onChange).toHaveBeenCalledWith(['existing', 'existing']); + }); + + it('validates tags using validateTag function', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + tag.length >= 3} + placeholder="Add tags..." + /> + ); + + const input = screen.getByPlaceholderText('Add tags...'); + + // Try adding tag that's too short + await user.type(input, 'ab '); + expect(onChange).not.toHaveBeenCalled(); + + // Add valid tag + await user.type(input, 'valid '); + expect(onChange).toHaveBeenCalledWith(['valid']); + }); + + it('does not add empty tags', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByPlaceholderText('Add tags...'); + await user.type(input, ' '); // Just spaces + + expect(onChange).not.toHaveBeenCalled(); + }); + + it('focuses input when container is clicked', async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + + const { container } = render( + + ); + + const inputContainer = container.querySelector('div'); + if (inputContainer) { + await user.click(inputContainer); + } + + const input = screen.getByRole('textbox'); + expect(input).toHaveFocus(); + }); + + it('is disabled when disabled prop is true', () => { + const onChange = vi.fn(); + + render( + + ); + + const input = screen.getByRole('textbox'); + expect(input).toBeDisabled(); + }); +}); diff --git a/src/components/ui/tag-input.tsx b/src/components/ui/tag-input.tsx new file mode 100644 index 00000000..71484c03 --- /dev/null +++ b/src/components/ui/tag-input.tsx @@ -0,0 +1,172 @@ +import * as React from 'react'; +import { X } from 'lucide-react'; +import { Badge } from '@/components/ui/badge'; +import { cn } from '@/lib/utils'; + +export interface TagInputProps + extends Omit, 'value' | 'onChange'> { + value: string[]; + onChange: (tags: string[]) => void; + /** Character(s) that trigger tag creation. Default: [' ', ','] */ + delimiters?: string[]; + /** Transform tag before adding (e.g., toLowerCase). Default: (tag) => tag.trim() */ + transformTag?: (tag: string) => string; + /** Validate tag before adding. Return false to reject. */ + validateTag?: (tag: string) => boolean; + /** Allow duplicate tags. Default: false */ + allowDuplicates?: boolean; +} + +export const TagInput = React.forwardRef( + ( + { + className, + value = [], + onChange, + delimiters = [' ', ','], + transformTag = (tag) => tag.trim(), + validateTag, + allowDuplicates = false, + disabled, + placeholder, + ...props + }, + ref + ) => { + const [inputValue, setInputValue] = React.useState(''); + const inputRef = React.useRef(null); + const containerRef = React.useRef(null); + + // Combine external ref with internal ref + React.useImperativeHandle(ref, () => inputRef.current!); + + const addTag = (tag: string) => { + const transformed = transformTag(tag); + + if (!transformed) return; + + // Check for duplicates if not allowed + if (!allowDuplicates && value.includes(transformed)) { + setInputValue(''); + return; + } + + // Validate if validator provided + if (validateTag && !validateTag(transformed)) { + setInputValue(''); + return; + } + + onChange([...value, transformed]); + setInputValue(''); + }; + + const removeTag = (index: number) => { + onChange(value.filter((_, i) => i !== index)); + }; + + const handleKeyDown = (e: React.KeyboardEvent) => { + // Handle delimiters (space, comma, etc.) + if (delimiters.includes(e.key)) { + e.preventDefault(); + if (inputValue.trim()) { + addTag(inputValue); + } + return; + } + + // Handle Enter key + if (e.key === 'Enter') { + e.preventDefault(); + if (inputValue.trim()) { + addTag(inputValue); + } + return; + } + + // Handle Backspace - delete last tag if input is empty + if (e.key === 'Backspace' && !inputValue && value.length > 0) { + e.preventDefault(); + removeTag(value.length - 1); + return; + } + }; + + const handleInputChange = (e: React.ChangeEvent) => { + const newValue = e.target.value; + + // Check if any delimiter was typed + const hasDelimiter = delimiters.some(delimiter => newValue.includes(delimiter)); + + if (hasDelimiter) { + // Extract the part before the delimiter + const parts = newValue.split(new RegExp(`[${delimiters.map(d => d === ' ' ? '\\s' : d).join('')}]`)); + const tagToAdd = parts[0]; + + if (tagToAdd.trim()) { + addTag(tagToAdd); + } + } else { + setInputValue(newValue); + } + }; + + const handleContainerClick = () => { + inputRef.current?.focus(); + }; + + return ( +
+ {/* Render tags */} + {value.map((tag, index) => ( + + {tag} + + + ))} + + {/* Input field */} + +
+ ); + } +); + +TagInput.displayName = 'TagInput'; diff --git a/src/lib/announceRepository.ts b/src/lib/announceRepository.ts index 0d6ae314..fff9fa24 100644 --- a/src/lib/announceRepository.ts +++ b/src/lib/announceRepository.ts @@ -15,6 +15,8 @@ export interface RepositoryAnnouncementData { cloneUrls: string[]; /** Relays for patch submissions */ relays?: string[]; + /** Category tags for the repository */ + tTags?: string[]; /** Earliest unique commit (root commit) */ earliestCommit?: string; } @@ -95,6 +97,14 @@ export function createRepositoryAnnouncementEvent( } } + // Add t-tags for categorization (each as a separate tag for relay-level filtering) + if (data.tTags && data.tTags.length > 0) { + const trimmedTags = data.tTags.map(tag => tag.trim().toLowerCase()).filter(tag => tag); + for (const tag of trimmedTags) { + tags.push(['t', tag]); + } + } + // Add earliest unique commit (euc) for repository identification if (data.earliestCommit?.trim()) { tags.push(['r', data.earliestCommit.trim(), 'euc']);