import AsyncStorage from "@react-native-async-storage/async-storage"; import * as Notifications from "expo-notifications"; import React, { useEffect, useState } from "react"; import { Linking, SafeAreaView, ScrollView, Text, TouchableOpacity, View } from "react-native"; import { Item } from "types/Item"; import { usePushNotifications } from "../hooks/usePushNotifications"; import { styles } from "./HomeScreen.styles"; const API_URL = "https://notifier.gansejunge.com"; const STORAGE_KEY = "notifications"; type Category = "home" | "royal-road" | "podcasts" | "mixtapes"; export default function HomeScreen() { const [data, setData] = useState([]); const [selected, setSelected] = useState("home"); const [menuOpen, setMenuOpen] = useState(false); // Register push notifications usePushNotifications({ userId: 1, apiKey: "super-secret-api-key", backendUrl: API_URL, appVersion: "1.0.0", locale: "en-uk", }); // Load saved notifications on startup useEffect(() => { (async () => { try { const stored = await AsyncStorage.getItem(STORAGE_KEY); if (stored) { setData(JSON.parse(stored)); } } catch (err) { console.error("Failed to load stored notifications:", err); } })(); }, []); // Listen for incoming notifications useEffect(() => { const subscription = Notifications.addNotificationReceivedListener(notification => { // Try to extract category from push payload const rawCategory = notification.request.content.data?.category as Category | undefined; const category: Category = rawCategory && ["royal-road", "podcasts", "mixtapes"].includes(rawCategory) ? rawCategory : "home"; const item: Item = { timestamp: Date.now(), category, title: notification.request.content.title || "No title", info: notification.request.content.body || "No description", link: (notification.request.content.data?.link as string) || "#", }; setData(prev => { const updated = [...prev, item].sort((a, b) => b.timestamp - a.timestamp); AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(updated)); // persist return updated; }); }); return () => subscription.remove(); }, []); // Filtered view: home shows everything const filteredData = selected === "home" ? data : data.filter(item => item.category === selected); const menuItems: { label: string; key: Category }[] = [ { label: "Home", key: "home" }, { label: "Royal Road", key: "royal-road" }, { label: "Podcasts", key: "podcasts" }, { label: "Mixtapes", key: "mixtapes" }, ]; return ( {/* Side Menu */} {menuItems.map(item => ( { setSelected(item.key); setMenuOpen(false); }} > {item.label} ))} {/* Main Content */} setMenuOpen(!menuOpen)}> {selected === "home" && "Welcome to Home!"} {selected === "royal-road" && "Royal Road Updates"} {selected === "podcasts" && "Latest Podcasts"} {selected === "mixtapes" && "New Mixtapes"} {filteredData.length === 0 ? ( No items yet ) : ( filteredData.map(item => ( {item.title} {item.info} Linking.openURL(item.link)}> Read more )) )} ); }