managarten/maerchenzauber/apps/mobile/hooks/useStories.ts
Till-JS e7f5f942f3 chore: initial commit - consolidate 4 projects into monorepo
Projects included:
- maerchenzauber (NestJS backend + Expo mobile + SvelteKit web + Astro landing)
- manacore (Expo mobile + SvelteKit web + Astro landing)
- manadeck (NestJS backend + Expo mobile + SvelteKit web)
- memoro (Expo mobile + SvelteKit web + Astro landing)

This commit preserves the current state before monorepo restructuring.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-22 23:38:24 +01:00

44 lines
No EOL
1.3 KiB
TypeScript

import { useState, useEffect, useCallback } from 'react';
import { Story } from '../types/Story';
import { useAuth } from '../src/contexts/AuthContext';
import { dataService } from '../src/utils/dataService';
export function useStories() {
const [allStories, setAllStories] = useState<Story[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const { user, isAuthenticated } = useAuth();
const fetchStories = useCallback(async () => {
setLoading(true);
setError(null);
if (!isAuthenticated || !user) {
setLoading(false);
setError('User not authenticated');
return;
}
try {
const stories = await dataService.getStories(true); // Include archived stories
setAllStories(stories);
setLoading(false);
} catch (error) {
console.error('Error fetching stories:', error);
setError(error instanceof Error ? error.message : 'Unknown error');
setLoading(false);
}
}, [isAuthenticated, user]);
useEffect(() => {
fetchStories();
}, [fetchStories]);
const getStoryById = (id: string) => allStories.find(story => story.id === id);
const refreshStories = useCallback(async () => {
await fetchStories();
}, [fetchStories]);
return { allStories, getStoryById, loading, error, refreshStories };
}