inital working version

This commit is contained in:
Stefan Hardegger
2025-07-22 21:49:40 +02:00
parent bebb799784
commit 59d29dceaf
98 changed files with 8027 additions and 856 deletions

View File

@@ -0,0 +1,184 @@
'use client';
import { useState, useRef } from 'react';
import { Textarea } from '../ui/Input';
import Button from '../ui/Button';
interface RichTextEditorProps {
value: string;
onChange: (value: string) => void;
placeholder?: string;
error?: string;
}
export default function RichTextEditor({
value,
onChange,
placeholder = 'Write your story here...',
error
}: RichTextEditorProps) {
const [viewMode, setViewMode] = useState<'visual' | 'html'>('visual');
const [htmlValue, setHtmlValue] = useState(value);
const previewRef = useRef<HTMLDivElement>(null);
const handleVisualChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const plainText = e.target.value;
// Convert plain text to basic HTML paragraphs
const htmlContent = plainText
.split('\n\n')
.filter(paragraph => paragraph.trim())
.map(paragraph => `<p>${paragraph.replace(/\n/g, '<br>')}</p>`)
.join('\n');
onChange(htmlContent);
setHtmlValue(htmlContent);
};
const handleHtmlChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const html = e.target.value;
setHtmlValue(html);
onChange(html);
};
const getPlainText = (html: string): string => {
// Simple HTML to plain text conversion
return html
.replace(/<\/p>/gi, '\n\n')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<[^>]*>/g, '')
.replace(/\n{3,}/g, '\n\n')
.trim();
};
const formatText = (tag: string) => {
if (viewMode === 'visual') {
// For visual mode, we'll just show formatting helpers
// In a real implementation, you'd want a proper WYSIWYG editor
return;
}
const textarea = document.querySelector('textarea') as HTMLTextAreaElement;
if (!textarea) return;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selectedText = htmlValue.substring(start, end);
if (selectedText) {
const beforeText = htmlValue.substring(0, start);
const afterText = htmlValue.substring(end);
const formattedText = `<${tag}>${selectedText}</${tag}>`;
const newValue = beforeText + formattedText + afterText;
setHtmlValue(newValue);
onChange(newValue);
}
};
return (
<div className="space-y-2">
{/* Toolbar */}
<div className="flex items-center justify-between p-2 theme-card border theme-border rounded-t-lg">
<div className="flex items-center gap-2">
<Button
type="button"
size="sm"
variant="ghost"
onClick={() => setViewMode('visual')}
className={viewMode === 'visual' ? 'theme-accent-bg text-white' : ''}
>
Visual
</Button>
<Button
type="button"
size="sm"
variant="ghost"
onClick={() => setViewMode('html')}
className={viewMode === 'html' ? 'theme-accent-bg text-white' : ''}
>
HTML
</Button>
</div>
{viewMode === 'html' && (
<div className="flex items-center gap-1">
<Button
type="button"
size="sm"
variant="ghost"
onClick={() => formatText('strong')}
title="Bold"
>
<strong>B</strong>
</Button>
<Button
type="button"
size="sm"
variant="ghost"
onClick={() => formatText('em')}
title="Italic"
>
<em>I</em>
</Button>
<Button
type="button"
size="sm"
variant="ghost"
onClick={() => formatText('p')}
title="Paragraph"
>
P
</Button>
</div>
)}
</div>
{/* Editor */}
<div className="border theme-border rounded-b-lg overflow-hidden">
{viewMode === 'visual' ? (
<Textarea
value={getPlainText(value)}
onChange={handleVisualChange}
placeholder={placeholder}
rows={12}
className="border-0 rounded-none focus:ring-0"
/>
) : (
<Textarea
value={htmlValue}
onChange={handleHtmlChange}
placeholder="<p>Write your HTML content here...</p>"
rows={12}
className="border-0 rounded-none focus:ring-0 font-mono text-sm"
/>
)}
</div>
{/* Preview for HTML mode */}
{viewMode === 'html' && value && (
<div className="space-y-2">
<h4 className="text-sm font-medium theme-header">Preview:</h4>
<div
ref={previewRef}
className="p-4 border theme-border rounded-lg theme-card max-h-40 overflow-y-auto"
dangerouslySetInnerHTML={{ __html: value }}
/>
</div>
)}
{error && (
<p className="text-sm text-red-600 dark:text-red-400">{error}</p>
)}
<div className="text-xs theme-text">
<p>
<strong>Visual mode:</strong> Write in plain text, paragraphs will be automatically formatted.
</p>
<p>
<strong>HTML mode:</strong> Write HTML directly for advanced formatting.
Allowed tags: p, br, strong, em, ul, ol, li, h1-h6, blockquote.
</p>
</div>
</div>
);
}

View File

@@ -0,0 +1,261 @@
'use client';
import { useState } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { Story } from '../../types/api';
import { storyApi, getImageUrl } from '../../lib/api';
import Button from '../ui/Button';
interface StoryCardProps {
story: Story;
viewMode: 'grid' | 'list';
onUpdate: () => void;
}
export default function StoryCard({ story, viewMode, onUpdate }: StoryCardProps) {
const [rating, setRating] = useState(story.rating || 0);
const [updating, setUpdating] = useState(false);
const handleRatingClick = async (newRating: number) => {
if (updating) return;
try {
setUpdating(true);
await storyApi.updateRating(story.id, newRating);
setRating(newRating);
onUpdate();
} catch (error) {
console.error('Failed to update rating:', error);
} finally {
setUpdating(false);
}
};
const formatWordCount = (wordCount: number) => {
return wordCount.toLocaleString() + ' words';
};
const formatDate = (dateString: string) => {
return new Date(dateString).toLocaleDateString();
};
if (viewMode === 'list') {
return (
<div className="theme-card theme-shadow rounded-lg p-4 hover:shadow-lg transition-shadow">
<div className="flex gap-4">
{/* Cover Image */}
<div className="flex-shrink-0">
<Link href={`/stories/${story.id}/detail`}>
<div className="w-16 h-20 bg-gray-200 dark:bg-gray-700 rounded overflow-hidden">
{story.coverPath ? (
<Image
src={getImageUrl(story.coverPath)}
alt={story.title}
width={64}
height={80}
className="w-full h-full object-cover"
unoptimized
/>
) : (
<div className="w-full h-full flex items-center justify-center theme-text text-xs">
📖
</div>
)}
</div>
</Link>
</div>
{/* Content */}
<div className="flex-1 min-w-0">
<div className="flex items-start justify-between">
<div className="min-w-0 flex-1">
<Link href={`/stories/${story.id}/detail`}>
<h3 className="text-lg font-semibold theme-header hover:theme-accent transition-colors truncate">
{story.title}
</h3>
</Link>
<Link href={`/authors/${story.authorId}`}>
<p className="theme-text hover:theme-accent transition-colors">
{story.authorName}
</p>
</Link>
<div className="flex items-center gap-4 mt-2 text-sm theme-text">
<span>{formatWordCount(story.wordCount)}</span>
<span>{formatDate(story.createdAt)}</span>
{story.seriesName && (
<span>
{story.seriesName} #{story.volume}
</span>
)}
</div>
{/* Tags */}
{Array.isArray(story.tags) && story.tags.length > 0 && (
<div className="flex flex-wrap gap-1 mt-2">
{story.tags.slice(0, 3).map((tag) => (
<span
key={tag.id}
className="px-2 py-1 text-xs rounded theme-accent-bg text-white"
>
{tag.name}
</span>
))}
{story.tags.length > 3 && (
<span className="px-2 py-1 text-xs theme-text">
+{story.tags.length - 3} more
</span>
)}
</div>
)}
</div>
{/* Actions */}
<div className="flex flex-col items-end gap-2 ml-4">
{/* Rating */}
<div className="flex gap-1">
{[1, 2, 3, 4, 5].map((star) => (
<button
key={star}
onClick={() => handleRatingClick(star)}
className={`text-lg ${
star <= rating
? 'text-yellow-400'
: 'text-gray-300 dark:text-gray-600'
} hover:text-yellow-400 transition-colors ${
updating ? 'cursor-not-allowed' : 'cursor-pointer'
}`}
disabled={updating}
>
</button>
))}
</div>
{/* Action Buttons */}
<div className="flex flex-col gap-2">
<Link href={`/stories/${story.id}`}>
<Button size="sm" className="w-full">
Read
</Button>
</Link>
<Link href={`/stories/${story.id}/edit`}>
<Button size="sm" variant="ghost" className="w-full">
Edit
</Button>
</Link>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
// Grid view
return (
<div className="theme-card theme-shadow rounded-lg overflow-hidden hover:shadow-lg transition-shadow group">
{/* Cover Image */}
<Link href={`/stories/${story.id}`}>
<div className="aspect-[3/4] bg-gray-200 dark:bg-gray-700 overflow-hidden">
{story.coverPath ? (
<Image
src={getImageUrl(story.coverPath)}
alt={story.title}
width={300}
height={400}
className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-200"
unoptimized
/>
) : (
<div className="w-full h-full flex items-center justify-center theme-text text-6xl">
📖
</div>
)}
</div>
</Link>
<div className="p-4">
{/* Title and Author */}
<Link href={`/stories/${story.id}`}>
<h3 className="font-semibold theme-header hover:theme-accent transition-colors line-clamp-2 mb-1">
{story.title}
</h3>
</Link>
<Link href={`/authors/${story.authorId}`}>
<p className="text-sm theme-text hover:theme-accent transition-colors mb-2">
{story.authorName}
</p>
</Link>
{/* Rating */}
<div className="flex gap-1 mb-2">
{[1, 2, 3, 4, 5].map((star) => (
<button
key={star}
onClick={() => handleRatingClick(star)}
className={`text-sm ${
star <= rating
? 'text-yellow-400'
: 'text-gray-300 dark:text-gray-600'
} hover:text-yellow-400 transition-colors ${
updating ? 'cursor-not-allowed' : 'cursor-pointer'
}`}
disabled={updating}
>
</button>
))}
</div>
{/* Metadata */}
<div className="text-xs theme-text space-y-1">
<div>{formatWordCount(story.wordCount)}</div>
<div>{formatDate(story.createdAt)}</div>
{story.seriesName && (
<div>
{story.seriesName} #{story.volume}
</div>
)}
</div>
{/* Tags */}
{Array.isArray(story.tags) && story.tags.length > 0 && (
<div className="flex flex-wrap gap-1 mt-2">
{story.tags.slice(0, 2).map((tag) => (
<span
key={tag.id}
className="px-2 py-1 text-xs rounded theme-accent-bg text-white"
>
{tag.name}
</span>
))}
{story.tags.length > 2 && (
<span className="px-2 py-1 text-xs theme-text">
+{story.tags.length - 2}
</span>
)}
</div>
)}
{/* Actions */}
<div className="flex gap-2 mt-4">
<Link href={`/stories/${story.id}`} className="flex-1">
<Button size="sm" className="w-full">
Read
</Button>
</Link>
<Link href={`/stories/${story.id}/edit`}>
<Button size="sm" variant="ghost">
Edit
</Button>
</Link>
</div>
</div>
</div>
);
}

View File

@@ -0,0 +1,79 @@
'use client';
import { useState } from 'react';
interface StoryRatingProps {
rating: number;
onRatingChange: (rating: number) => void;
readonly?: boolean;
size?: 'sm' | 'md' | 'lg';
}
export default function StoryRating({
rating,
onRatingChange,
readonly = false,
size = 'md'
}: StoryRatingProps) {
const [hoveredRating, setHoveredRating] = useState(0);
const [updating, setUpdating] = useState(false);
const sizeClasses = {
sm: 'text-sm',
md: 'text-lg',
lg: 'text-2xl',
};
const handleRatingClick = async (newRating: number) => {
if (readonly || updating) return;
try {
setUpdating(true);
await onRatingChange(newRating);
} catch (error) {
console.error('Failed to update rating:', error);
} finally {
setUpdating(false);
}
};
const displayRating = hoveredRating || rating;
return (
<div className="flex items-center gap-1">
{[1, 2, 3, 4, 5].map((star) => (
<button
key={star}
onClick={() => handleRatingClick(star)}
onMouseEnter={() => !readonly && setHoveredRating(star)}
onMouseLeave={() => !readonly && setHoveredRating(0)}
disabled={readonly || updating}
className={`${sizeClasses[size]} ${
star <= displayRating
? 'text-yellow-400'
: 'text-gray-300 dark:text-gray-600'
} ${
readonly
? 'cursor-default'
: updating
? 'cursor-not-allowed'
: 'cursor-pointer hover:text-yellow-400'
} transition-colors`}
aria-label={`Rate ${star} star${star !== 1 ? 's' : ''}`}
>
</button>
))}
{!readonly && (
<span className="ml-2 text-sm theme-text">
{rating > 0 ? `(${rating}/5)` : 'Rate this story'}
</span>
)}
{updating && (
<span className="ml-2 text-sm theme-text">Saving...</span>
)}
</div>
);
}

View File

@@ -0,0 +1,54 @@
'use client';
import { Tag } from '../../types/api';
interface TagFilterProps {
tags: Tag[];
selectedTags: string[];
onTagToggle: (tagName: string) => void;
}
export default function TagFilter({ tags, selectedTags, onTagToggle }: TagFilterProps) {
if (!Array.isArray(tags) || tags.length === 0) return null;
// Sort tags by usage count (descending) and then alphabetically
const sortedTags = [...tags].sort((a, b) => {
const aCount = a.storyCount || 0;
const bCount = b.storyCount || 0;
if (bCount !== aCount) {
return bCount - aCount;
}
return a.name.localeCompare(b.name);
});
return (
<div className="space-y-2">
<h3 className="text-sm font-medium theme-header">Filter by Tags:</h3>
<div className="flex flex-wrap gap-2 max-h-32 overflow-y-auto">
{sortedTags.map((tag) => {
const isSelected = selectedTags.includes(tag.name);
return (
<button
key={tag.id}
onClick={() => onTagToggle(tag.name)}
className={`px-3 py-1 text-sm rounded-full border transition-colors ${
isSelected
? 'theme-accent-bg text-white border-transparent'
: 'theme-card theme-text theme-border hover:border-gray-400'
}`}
>
{tag.name} ({tag.storyCount || 0})
</button>
);
})}
</div>
{selectedTags.length > 0 && (
<div className="text-sm theme-text">
Filtering by: {selectedTags.join(', ')}
</div>
)}
</div>
);
}

View File

@@ -0,0 +1,168 @@
'use client';
import { useState, useEffect, useRef } from 'react';
import { tagApi } from '../../lib/api';
interface TagInputProps {
tags: string[];
onChange: (tags: string[]) => void;
placeholder?: string;
}
export default function TagInput({ tags, onChange, placeholder = 'Add tags...' }: TagInputProps) {
const [inputValue, setInputValue] = useState('');
const [suggestions, setSuggestions] = useState<string[]>([]);
const [showSuggestions, setShowSuggestions] = useState(false);
const [activeSuggestionIndex, setActiveSuggestionIndex] = useState(-1);
const inputRef = useRef<HTMLInputElement>(null);
const suggestionsRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const fetchSuggestions = async () => {
if (inputValue.length > 0) {
try {
const suggestionList = await tagApi.getTagAutocomplete(inputValue);
// Filter out already selected tags
const filteredSuggestions = suggestionList.filter(
suggestion => !tags.includes(suggestion)
);
setSuggestions(filteredSuggestions);
setShowSuggestions(filteredSuggestions.length > 0);
} catch (error) {
console.error('Failed to fetch tag suggestions:', error);
setSuggestions([]);
setShowSuggestions(false);
}
} else {
setSuggestions([]);
setShowSuggestions(false);
}
};
const debounce = setTimeout(fetchSuggestions, 300);
return () => clearTimeout(debounce);
}, [inputValue, tags]);
const addTag = (tag: string) => {
const trimmedTag = tag.trim().toLowerCase();
if (trimmedTag && !tags.includes(trimmedTag)) {
onChange([...tags, trimmedTag]);
}
setInputValue('');
setShowSuggestions(false);
setActiveSuggestionIndex(-1);
};
const removeTag = (tagToRemove: string) => {
onChange(tags.filter(tag => tag !== tagToRemove));
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
switch (e.key) {
case 'Enter':
case ',':
e.preventDefault();
if (activeSuggestionIndex >= 0 && suggestions[activeSuggestionIndex]) {
addTag(suggestions[activeSuggestionIndex]);
} else if (inputValue.trim()) {
addTag(inputValue);
}
break;
case 'Backspace':
if (!inputValue && tags.length > 0) {
removeTag(tags[tags.length - 1]);
}
break;
case 'ArrowDown':
e.preventDefault();
setActiveSuggestionIndex(prev =>
prev < suggestions.length - 1 ? prev + 1 : prev
);
break;
case 'ArrowUp':
e.preventDefault();
setActiveSuggestionIndex(prev => prev > 0 ? prev - 1 : -1);
break;
case 'Escape':
setShowSuggestions(false);
setActiveSuggestionIndex(-1);
break;
}
};
const handleSuggestionClick = (suggestion: string) => {
addTag(suggestion);
inputRef.current?.focus();
};
return (
<div className="relative">
<div className="min-h-[2.5rem] w-full px-3 py-2 border rounded-lg theme-card theme-text theme-border focus-within:outline-none focus-within:ring-2 focus-within:ring-theme-accent focus-within:border-transparent">
<div className="flex flex-wrap gap-2">
{/* Existing Tags */}
{tags.map((tag) => (
<span
key={tag}
className="inline-flex items-center px-2 py-1 text-sm bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200 rounded"
>
{tag}
<button
type="button"
onClick={() => removeTag(tag)}
className="ml-1 text-blue-600 dark:text-blue-300 hover:text-blue-800 dark:hover:text-blue-100"
>
×
</button>
</span>
))}
{/* Input */}
<input
ref={inputRef}
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={handleKeyDown}
onFocus={() => inputValue && setShowSuggestions(suggestions.length > 0)}
onBlur={() => setTimeout(() => setShowSuggestions(false), 200)}
placeholder={tags.length === 0 ? placeholder : ''}
className="flex-1 min-w-[120px] bg-transparent outline-none"
/>
</div>
</div>
{/* Suggestions Dropdown */}
{showSuggestions && suggestions.length > 0 && (
<div
ref={suggestionsRef}
className="absolute z-10 w-full mt-1 bg-white dark:bg-gray-800 border theme-border rounded-lg shadow-lg max-h-48 overflow-y-auto"
>
{suggestions.map((suggestion, index) => (
<button
key={suggestion}
type="button"
onClick={() => handleSuggestionClick(suggestion)}
className={`w-full text-left px-3 py-2 text-sm hover:bg-gray-100 dark:hover:bg-gray-700 ${
index === activeSuggestionIndex
? 'bg-blue-100 dark:bg-blue-900 text-blue-900 dark:text-blue-100'
: 'theme-text'
} ${index === 0 ? 'rounded-t-lg' : ''} ${
index === suggestions.length - 1 ? 'rounded-b-lg' : ''
}`}
>
{suggestion}
</button>
))}
</div>
)}
<p className="mt-1 text-xs text-gray-500">
Type and press Enter or comma to add tags
</p>
</div>
);
}