import React, { useMemo, useSyncExternalStore } from 'react'; import { IcNavForward } from '../res/icons'; import { SettingsHeader } from './SettingsHeader'; import { PreferenceCategory } from './PreferenceCategory'; import NotificationService from '../../../os/NotificationService'; import { AppIcon } from '../../../os/components/AppIcon'; import { APP_REGISTRY, getLocalizedAppName } from '../../../os/data/appRegistry'; import { useOsStateStore } from '../../../os/OsStateStore'; import type { AppId } from '../../../os/types'; import type { AppManifest } from '../../../os/types/manifest'; import { strings } from '../res/strings'; import { stringsEn } from '../res/strings.en'; import { useAppStrings } from '@/os/useAppStrings'; import { useSettingsGestures } from '../hooks/useSettingsGestures'; function prefKey(appId: string, field: string): string { return `notif.app.${appId}.${field}`; } function getBoolPref(prefs: Record, key: string, fallback: boolean): boolean { const v = prefs[key]; return typeof v === 'boolean' ? v : fallback; } const AppRow: React.FC<{ manifest: AppManifest; value?: string; showDivider: boolean; rowProps?: React.HTMLAttributes; }> = ({ manifest, value, showDivider, rowProps }) => { return (
{getLocalizedAppName(manifest.id)}
{manifest.packageName ? (
{manifest.packageName}
) : null}
{value ? {value} : null}
{showDivider ?
: null}
); }; export const NotificationManagingPage: React.FC = () => { const { bindTap } = useSettingsGestures(); const s = useAppStrings(strings, stringsEn); const prefs = useOsStateStore((state) => state.preferences); const snapshot = useSyncExternalStore( (onChange) => NotificationService.subscribe(() => onChange()), () => NotificationService.getState(), ); const unreadByApp = useMemo(() => { const map = new Map(); for (const it of snapshot.items) { if (!it.appId) continue; if (it.read) continue; map.set(it.appId, (map.get(it.appId) || 0) + 1); } return map; }, [snapshot.items]); const apps = useMemo(() => { const arr = [...APP_REGISTRY]; arr.sort((a, b) => { // System apps first, then by name. if (a.type !== b.type) return a.type === 'system' ? -1 : 1; return getLocalizedAppName(a.id).localeCompare(getLocalizedAppName(b.id), 'zh-Hans-CN'); }); return arr; }, []); return (
{apps.map((app, idx) => { const enabled = getBoolPref(prefs, prefKey(app.id, 'enabled'), true); const unread = unreadByApp.get(app.id) || 0; const value = enabled ? (unread > 0 ? `${unread} ${s.unread_count_suffix}` : s.on_2) : s.off_2; return ( ('page.open', { params: { pageId: `notification_app__${encodeURIComponent(app.id)}` }, })} /> ); })}
); }; export default NotificationManagingPage;