Python 68.8%
TypeScript 18.6%
CSS 8.7%
JavaScript 3.3%
HTML 0.6%
1// -----------------------------------------------------------------------------2// Rent-Ka — Rental listings aggregator (Canada, outside Québec)3// Author: Simon-Pierre Boucher — contact@spboucher.ai4// account.tsx: shared account context — signed-in profile + saved rentals5// One fetch at app mount; card hearts, the account menu and the6// Favorites/Manage pages read the same state (optimistic toggle).7// -----------------------------------------------------------------------------8import {9 ReactNode, createContext, useCallback, useContext, useEffect, useState,10} from "react";11import { Me, addFavorite, fetchFavorites, fetchMe, removeFavorite } from "./api";1213interface AccountState {14 me: Me | null;15 loaded: boolean; // first fetch done16 favs: Set<string>; // saved uids17 toggleFav: (uid: string) => void;18 refresh: () => void; // re-fetch profile + saved (after login/role)19}2021const Ctx = createContext<AccountState>({22 me: null, loaded: false, favs: new Set(),23 toggleFav: () => {}, refresh: () => {},24});2526export const useAccount = () => useContext(Ctx);2728export function AccountProvider({ children }: { children: ReactNode }) {29 const [me, setMe] = useState<Me | null>(null);30 const [loaded, setLoaded] = useState(false);31 const [favs, setFavs] = useState<Set<string>>(new Set());3233 const refresh = useCallback(() => {34 fetchMe().then((m) => {35 setMe(m);36 setLoaded(true);37 if (m) {38 fetchFavorites()39 .then((f) => setFavs(new Set(f.uids)))40 .catch(() => setFavs(new Set()));41 } else {42 setFavs(new Set());43 }44 });45 }, []);4647 useEffect(() => { refresh(); }, [refresh]);4849 const toggleFav = useCallback((uid: string) => {50 setFavs((prev) => {51 const next = new Set(prev);52 if (next.has(uid)) {53 next.delete(uid);54 removeFavorite(uid).catch(() => {});55 } else {56 next.add(uid);57 addFavorite(uid).catch(() => {});58 }59 return next;60 });61 }, []);6263 return (64 <Ctx.Provider value={{ me, loaded, favs, toggleFav, refresh }}>65 {children}66 </Ctx.Provider>67 );68}69