Merge branch 'worktree-agent-ad3c3b03391b6c6fc' into claude/relaxed-maxwell-c10wof
# Conflicts: # src/i18n/locales/ar.ts # src/i18n/locales/de.ts # src/i18n/locales/en.ts # src/i18n/locales/es.ts # src/i18n/locales/fr.ts # src/i18n/locales/hi.ts # src/i18n/locales/id.ts # src/i18n/locales/it.ts # src/i18n/locales/ja.ts # src/i18n/locales/ko.ts # src/i18n/locales/nl.ts # src/i18n/locales/pl.ts # src/i18n/locales/pt.ts # src/i18n/locales/ru.ts # src/i18n/locales/th.ts # src/i18n/locales/tr.ts # src/i18n/locales/vi.ts # src/i18n/locales/zh.ts
This commit is contained in:
35 files changed
+1779
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { keepPushFresh } from '../lib/push'
|
||||
import { useMedia } from '../store'
|
||||
import { useSocial } from './social/SocialProvider'
|
||||
|
||||
/** Écrans qu'une notification push peut ouvrir (« /?open=… », voir public/push-sw.js). */
|
||||
const OPEN_RE = /^(notifications|item:[A-Za-z0-9_-]{1,64}|u:[a-z0-9_]{3,20})$/
|
||||
|
||||
/** Lit (et retire de l'adresse) l'écran demandé par une notification au lancement de l'app. */
|
||||
function takeOpenParam(): string | null {
|
||||
try {
|
||||
const params = new URLSearchParams(location.search)
|
||||
const open = params.get('open')
|
||||
if (open === null) return null
|
||||
params.delete('open')
|
||||
const q = params.toString()
|
||||
history.replaceState(history.state, '', `${location.pathname}${q ? `?${q}` : ''}${location.hash}`)
|
||||
return OPEN_RE.test(open) ? open : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lien entre les notifications push et l'app : ouvre le bon écran au clic sur une notification
|
||||
* (app lancée, ou déjà ouverte : message du service worker), met à jour la cloche quand
|
||||
* une notification arrive, et garde l'abonnement de l'appareil à jour côté serveur.
|
||||
*/
|
||||
export default function PushBridge() {
|
||||
const { account, items, loading } = useMedia()
|
||||
const social = useSocial()
|
||||
const [target, setTarget] = useState<string | null>(takeOpenParam)
|
||||
const socialRef = useRef(social)
|
||||
socialRef.current = social
|
||||
|
||||
// Messages du service worker
|
||||
useEffect(() => {
|
||||
if (!('serviceWorker' in navigator)) return
|
||||
const onMessage = (e: MessageEvent) => {
|
||||
const data = e.data as { type?: unknown; open?: unknown } | null
|
||||
if (!data || typeof data !== 'object') return
|
||||
if (data.type === 'azuu:open' && typeof data.open === 'string' && OPEN_RE.test(data.open)) setTarget(data.open)
|
||||
else if (data.type === 'azuu:push') void socialRef.current.refresh()
|
||||
}
|
||||
navigator.serviceWorker.addEventListener('message', onMessage)
|
||||
return () => navigator.serviceWorker.removeEventListener('message', onMessage)
|
||||
}, [])
|
||||
|
||||
// Ouvre l'écran demandé une fois le compte et la bibliothèque chargés
|
||||
useEffect(() => {
|
||||
if (!target) return
|
||||
if (!account) {
|
||||
// Pas (ou plus) connecté : rien à ouvrir
|
||||
if (!loading) setTarget(null)
|
||||
return
|
||||
}
|
||||
if (!social.enabled || loading) return
|
||||
setTarget(null)
|
||||
if (target === 'notifications') social.openNotifications()
|
||||
else if (target.startsWith('u:')) social.openProfile(target.slice(2))
|
||||
else {
|
||||
const item = items.find((i) => i.id === target.slice(5))
|
||||
if (item) social.openItem(item)
|
||||
else social.openNotifications()
|
||||
}
|
||||
}, [target, account, social, loading, items])
|
||||
|
||||
// Abonnement push renouvelé par le navigateur, langue changée… : on le signale au serveur
|
||||
const accountOn = !!account
|
||||
useEffect(() => {
|
||||
if (accountOn) void keepPushFresh()
|
||||
}, [accountOn])
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { BellRing, Loader2 } from 'lucide-react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { t } from '../i18n'
|
||||
import { enablePush, disablePush, getPushState, PushError, type PushState } from '../lib/push'
|
||||
import { cx } from '../lib/utils'
|
||||
|
||||
/** Réglages → Notifications : « Notifications sur cet appareil » (Web Push, même app fermée). */
|
||||
export default function PushToggle() {
|
||||
const [state, setState] = useState<PushState | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true
|
||||
void getPushState().then((s) => alive && setState(s))
|
||||
return () => {
|
||||
alive = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
if (state === null) return null
|
||||
const on = state === 'on'
|
||||
const available = state === 'on' || state === 'off'
|
||||
|
||||
const toggle = async () => {
|
||||
if (busy || !available) return
|
||||
setBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
if (on) {
|
||||
await disablePush()
|
||||
setState('off')
|
||||
} else {
|
||||
// La permission n'est demandée qu'ici, au clic
|
||||
setState(await enablePush())
|
||||
}
|
||||
} catch (e) {
|
||||
const code = e instanceof PushError ? e.code : 'failed'
|
||||
if (code === 'denied') setState('denied')
|
||||
else setError(code === 'not-ready' ? t('push.notReady') : t('push.error'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const hint =
|
||||
state === 'needs-install'
|
||||
? t('push.iosInstall')
|
||||
: state === 'unsupported'
|
||||
? t('push.unsupported')
|
||||
: state === 'denied'
|
||||
? t('push.denied')
|
||||
: t('push.deviceHint')
|
||||
|
||||
return (
|
||||
<div className="card mb-3 overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={on}
|
||||
aria-disabled={!available || busy}
|
||||
onClick={() => void toggle()}
|
||||
className={cx('flex w-full items-center gap-3.5 px-4 py-3.5 text-start', !available && 'cursor-default')}
|
||||
>
|
||||
<span className={cx('grid size-10 shrink-0 place-items-center rounded-full bg-surface-2', on ? 'text-accent' : 'text-ink-2')}>
|
||||
{busy ? <Loader2 size={18} className="animate-spin" /> : <BellRing size={18} />}
|
||||
</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block text-sm font-medium">{t('push.device')}</span>
|
||||
<span className={cx('mt-0.5 block text-xs', state === 'denied' ? 'text-accent' : 'text-ink-3')}>{hint}</span>
|
||||
</span>
|
||||
{available && (
|
||||
<span className={cx('relative h-6 w-11 shrink-0 rounded-full transition-colors', on ? 'bg-accent-fill' : 'border border-line-strong bg-surface-2')}>
|
||||
<span className={cx('absolute top-0.5 size-5 rounded-full bg-ink transition-all', on ? 'start-[22px] bg-on-accent' : 'start-0.5')} />
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
{error && (
|
||||
<p role="alert" className="border-t border-line px-4 py-2.5 text-xs text-accent">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
import { Check, CloudOff, RefreshCw, X } from 'lucide-react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { t } from '../i18n'
|
||||
import { cx } from '../lib/utils'
|
||||
import { useMedia } from '../store'
|
||||
|
||||
function useOnline(): boolean {
|
||||
const [online, setOnline] = useState(() => navigator.onLine)
|
||||
useEffect(() => {
|
||||
const up = () => setOnline(true)
|
||||
const down = () => setOnline(false)
|
||||
window.addEventListener('online', up)
|
||||
window.addEventListener('offline', down)
|
||||
return () => {
|
||||
window.removeEventListener('online', up)
|
||||
window.removeEventListener('offline', down)
|
||||
}
|
||||
}, [])
|
||||
return online
|
||||
}
|
||||
|
||||
type Kind = 'offline' | 'offline-local' | 'pending' | 'synced'
|
||||
|
||||
/**
|
||||
* Petite pastille discrète : hors-ligne, ou modifications pas encore envoyées au compte.
|
||||
* Mobile : juste au-dessus de la barre de navigation ; ordinateur : en bas de la barre latérale.
|
||||
* Elle disparaît une fois tout synchronisé (après un bref « Synchronisé »).
|
||||
* L'état vient du moteur de synchro (useMedia().account.sync) : rien n'est recalculé ici.
|
||||
*/
|
||||
export default function SyncIndicator() {
|
||||
const { account } = useMedia()
|
||||
const online = useOnline()
|
||||
const pending = account?.sync.pending ?? 0
|
||||
const syncing = account?.sync.state === 'syncing'
|
||||
const failed = account?.sync.state === 'error'
|
||||
|
||||
// Une modification part normalement en 1-2 s : on ne signale l'attente que si elle dure
|
||||
const [slow, setSlow] = useState(false)
|
||||
useEffect(() => {
|
||||
if (pending === 0) {
|
||||
setSlow(false)
|
||||
return
|
||||
}
|
||||
const timer = setTimeout(() => setSlow(true), failed ? 0 : 4000)
|
||||
return () => clearTimeout(timer)
|
||||
}, [pending > 0, failed])
|
||||
|
||||
// Sans compte, l'app marche hors-ligne : message rassurant, que l'on peut masquer
|
||||
const [dismissed, setDismissed] = useState(false)
|
||||
useEffect(() => {
|
||||
if (online) setDismissed(false)
|
||||
}, [online])
|
||||
|
||||
const base: Kind | null = !online
|
||||
? account
|
||||
? 'offline'
|
||||
: dismissed
|
||||
? null
|
||||
: 'offline-local'
|
||||
: account && pending > 0 && slow
|
||||
? 'pending'
|
||||
: null
|
||||
|
||||
// Bref « Synchronisé » quand la pastille d'attente disparaît
|
||||
const [flash, setFlash] = useState(false)
|
||||
const shown = useRef<Kind | null>(null)
|
||||
useEffect(() => {
|
||||
const was = shown.current
|
||||
shown.current = base
|
||||
if (base) {
|
||||
setFlash(false)
|
||||
return
|
||||
}
|
||||
if (account && was === 'pending' && pending === 0 && online) setFlash(true)
|
||||
}, [base, account, pending, online])
|
||||
useEffect(() => {
|
||||
if (!flash) return
|
||||
const timer = setTimeout(() => setFlash(false), 2200)
|
||||
return () => clearTimeout(timer)
|
||||
}, [flash])
|
||||
const kind: Kind | null = base ?? (flash ? 'synced' : null)
|
||||
if (!kind) return null
|
||||
|
||||
const text =
|
||||
kind === 'offline'
|
||||
? pending > 0
|
||||
? `${t('sync.offline')} · ${t('sync.pending', { count: pending })}`
|
||||
: t('netPill.offlineSync')
|
||||
: kind === 'offline-local'
|
||||
? t('netPill.offlineLocal')
|
||||
: kind === 'pending'
|
||||
? syncing
|
||||
? `${t('sync.syncing')} · ${t('sync.pending', { count: pending })}`
|
||||
: t('sync.pending', { count: pending })
|
||||
: t('netPill.synced')
|
||||
|
||||
const icon =
|
||||
kind === 'synced' ? (
|
||||
<Check size={14} className="shrink-0 text-accent" />
|
||||
) : kind === 'pending' ? (
|
||||
<RefreshCw size={14} className={cx('shrink-0', syncing && 'animate-spin', failed && 'text-accent')} />
|
||||
) : (
|
||||
<CloudOff size={14} className="shrink-0" />
|
||||
)
|
||||
|
||||
const pill = 'pointer-events-auto inline-flex max-w-full items-center gap-2 rounded-full border border-line bg-surface px-3.5 py-1.5 text-xs font-medium text-ink-2 shadow-lg lg:w-full lg:rounded-xl lg:px-3 lg:py-2 lg:text-start'
|
||||
|
||||
return (
|
||||
<div
|
||||
className="pointer-events-none fixed inset-x-0 bottom-[calc(4.75rem+env(safe-area-inset-bottom))] z-20 flex justify-center px-4 lg:inset-x-auto lg:start-0 lg:bottom-5 lg:z-[31] lg:w-60 lg:justify-start"
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
data-testid="sync-indicator"
|
||||
data-kind={kind}
|
||||
>
|
||||
{kind === 'pending' ? (
|
||||
// Envoi en attente : un appui relance la synchro
|
||||
<button type="button" onClick={() => void account?.syncNow().catch(() => {})} className={pill} title={t('sync.now')}>
|
||||
{icon}
|
||||
<span className="truncate lg:overflow-visible lg:whitespace-normal">{text}</span>
|
||||
</button>
|
||||
) : kind === 'offline-local' ? (
|
||||
<span className={pill}>
|
||||
{icon}
|
||||
<span className="truncate lg:overflow-visible lg:whitespace-normal">{text}</span>
|
||||
<button type="button" onClick={() => setDismissed(true)} aria-label={t('netPill.hide')} className="-me-1 ms-0.5 grid size-5 shrink-0 place-items-center rounded-full text-ink-3 hover:text-ink">
|
||||
<X size={12} />
|
||||
</button>
|
||||
</span>
|
||||
) : (
|
||||
<span className={pill}>
|
||||
{icon}
|
||||
<span className="truncate lg:overflow-visible lg:whitespace-normal">{text}</span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user