From 19bb2cbb03246650beb191fa928926eface4c12f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 28 Nov 2026 22:02:05 +0000 Subject: [PATCH] =?UTF-8?q?Profil=20affich=C3=A9=20tout=20de=20suite=20au?= =?UTF-8?q?=20lancement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Mon profil est gardé sur l'appareil (effacé à la déconnexion) : photo et pseudo sans attendre le serveur - Rafraîchi en une seule requête au lieu de deux quand le pseudo est connu - Ma page profil s'ouvre directement avec ces données, puis se met à jour Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe --- src/components/social/ProfileView.tsx | 3 +- src/components/social/SocialProvider.tsx | 11 +++++- src/lib/cloud/social.ts | 50 ++++++++++++++++++++++-- src/store.tsx | 2 +- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/components/social/ProfileView.tsx b/src/components/social/ProfileView.tsx index 1fe837a..341d070 100644 --- a/src/components/social/ProfileView.tsx +++ b/src/components/social/ProfileView.tsx @@ -54,7 +54,8 @@ function Stat({ value, label, onClick }: { value?: number; label: string; onClic export default function ProfileView({ username, onClose }: { username: string; onClose: () => void }) { const social = useSocial() const { settings, items: myItems } = useMedia() - const [profile, setProfile] = useState() + // Mon profil : affiché tout de suite avec ce qu'on a déjà, puis rechargé + const [profile, setProfile] = useState(() => (social.me?.username === username ? social.me : undefined)) const [error, setError] = useState() const [tab, setTab] = useState<'profile' | 'top' | 'library'>('profile') const [busy, setBusy] = useState(false) diff --git a/src/components/social/SocialProvider.tsx b/src/components/social/SocialProvider.tsx index 59f01dc..2ad0f83 100644 --- a/src/components/social/SocialProvider.tsx +++ b/src/components/social/SocialProvider.tsx @@ -1,5 +1,5 @@ import { createContext, lazy, Suspense, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react' -import { getFollowRequests, getMyProfile, unreadNotifications, type Profile, type ProfileCard } from '../../lib/cloud/social' +import { cachedMyProfile, getFollowRequests, getMyProfile, unreadNotifications, type Profile, type ProfileCard } from '../../lib/cloud/social' import { useMedia } from '../../store' import type { MediaItem } from '../../types' import type { PeekOwner } from './ItemPeek' @@ -106,7 +106,14 @@ export function SocialProvider({ }) { const { account, items } = useMedia() const enabled = !!account - const [me, setMe] = useState(null) + // Profil gardé sur l'appareil : photo et pseudo tout de suite, sans attendre le serveur + const [me, setMe] = useState(() => (enabled ? cachedMyProfile() : null)) + const [meFor, setMeFor] = useState(account?.email) + if (meFor !== account?.email) { + // Autre compte (ou déconnexion) : on repart de son propre profil gardé + setMeFor(account?.email) + setMe(enabled ? cachedMyProfile() : null) + } const [requests, setRequests] = useState([]) const [unread, setUnread] = useState(0) const [stack, setStack] = useState<(Overlay & { uid: number })[]>([]) diff --git a/src/lib/cloud/social.ts b/src/lib/cloud/social.ts index d9e9a88..9f2c399 100644 --- a/src/lib/cloud/social.ts +++ b/src/lib/cloud/social.ts @@ -133,12 +133,56 @@ export async function getProfile(name: string): Promise { return toProfile(await rpc('get_profile', { p_username: clean })) } -/** Mon profil (pseudo, nom, photo…) — lu directement dans la table. */ +// Mon profil gardé sur l'appareil : affiché dès le lancement, puis rafraîchi en arrière-plan +const meKey = (userId: string) => `azuucine:${userId}:me` + +/** Dernier profil connu du compte connecté (réponse serveur relue et vérifiée). */ +export function cachedMyProfile(): Profile | null { + const id = getSession()?.user.id + if (!id) return null + try { + const p = toProfile(JSON.parse(localStorage.getItem(meKey(id)) ?? 'null')) + return p && p.id === id && p.isMe ? p : null + } catch { + return null + } +} + +function rememberMe(userId: string, raw: unknown) { + try { + if (raw) localStorage.setItem(meKey(userId), JSON.stringify(raw)) + else localStorage.removeItem(meKey(userId)) + } catch { + /* stockage plein ou bloqué : le profil sera simplement rechargé */ + } +} + +/** Mon profil (pseudo, nom, photo…). Avec le pseudo déjà connu, une seule requête au lieu de deux. */ export async function getMyProfile(): Promise { - const res = await cloudFetch(`/rest/v1/profiles?id=eq.${me()}&select=username`) + const id = me() + const known = cachedMyProfile()?.username + if (known) { + const raw = await rpc('get_profile', { p_username: known }) + const p = toProfile(raw) + if (p?.isMe && p.id === id) { + rememberMe(id, raw) + return p + } + // Pseudo changé ailleurs : on repasse par la table + } + const res = await cloudFetch(`/rest/v1/profiles?id=eq.${id}&select=username`) if (!res.ok) throw new Error(t('social.err.server', { status: res.status })) const [row] = (await res.json()) as { username?: string }[] - return row?.username ? getProfile(row.username) : null + if (!row?.username) { + rememberMe(id, null) + return null + } + const clean = row.username.trim().toLowerCase() + if (!USERNAME_RE.test(clean)) return null + const raw = await rpc('get_profile', { p_username: clean }) + const p = toProfile(raw) + rememberMe(id, p?.isMe ? raw : null) + return p } /** Nombre de lignes renvoyées par le serveur (avant le tri des lignes invalides) : sert au décalage de la page suivante. */ diff --git a/src/store.tsx b/src/store.tsx index 41abafb..0d101f6 100644 --- a/src/store.tsx +++ b/src/store.tsx @@ -106,7 +106,7 @@ export interface AccountApi { /** Efface de l'appareil tout ce qui appartient à un compte (base, réglages, synchro, caches). */ async function wipeAccountFromDevice(userId: string) { await deleteDatabase(dbNameFor(userId)) - for (const name of ['settings', 'lists', 'sync', 'device-import']) { + for (const name of ['settings', 'lists', 'sync', 'device-import', 'me']) { try { localStorage.removeItem(scopedKey(name, userId)) } catch {