videogen/apps/mobile/app/notifications.tsx
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

221 lines
6.1 KiB
TypeScript

/**
* Notification center — the in-app counterpart to the external webhook. Lists
* the caller's notifications (newest first) with pull-to-refresh and a "Mark all
* read" action. Tapping a row marks it read (optimistically) and, when the
* event carries a `data.job_id`, deep-links to that job.
*
* Auth token comes from the session and is sent as `x-api-key` by the shared api
* client. No secrets live here. The screen owns its own loading + error state.
*/
import { useCallback, useState } from "react";
import {
ActivityIndicator,
FlatList,
Pressable,
RefreshControl,
StyleSheet,
Text,
View,
} from "react-native";
import { useFocusEffect, useRouter } from "expo-router";
import {
ApiError,
listNotifications,
markAllNotificationsRead,
markNotificationRead,
} from "@/lib/api";
import { useSession } from "@/src/shared/auth";
import type { Notification } from "@/lib/types";
import { colors } from "@/components/me/theme";
import { NotificationItem } from "@/components/notifications/NotificationItem";
import { jobIdFromNotification } from "@/components/notifications/notification-meta";
export default function NotificationsScreen() {
const { apiKey } = useSession();
const router = useRouter();
const token = apiKey ?? undefined;
const [items, setItems] = useState<Notification[]>([]);
const [unread, setUnread] = useState(0);
const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const [markingAll, setMarkingAll] = useState(false);
const [error, setError] = useState<string | null>(null);
const load = useCallback(async () => {
setError(null);
try {
const result = await listNotifications({ token });
setItems(result.notifications);
setUnread(result.unread_count);
} catch (err) {
setError(
err instanceof ApiError
? err.message
: "Failed to load notifications.",
);
}
}, [token]);
useFocusEffect(
useCallback(() => {
let active = true;
setLoading(true);
load().finally(() => {
if (active) setLoading(false);
});
return () => {
active = false;
};
}, [load]),
);
const onRefresh = useCallback(async () => {
setRefreshing(true);
await load();
setRefreshing(false);
}, [load]);
/** Mark one read (optimistic, rolled back on failure), then deep-link. */
const onPressItem = useCallback(
(notification: Notification) => {
const jobId = jobIdFromNotification(notification);
if (!notification.read) {
const previous = items;
const previousUnread = unread;
setItems((rows) =>
rows.map((r) =>
r.id === notification.id ? { ...r, read: true } : r,
),
);
setUnread((n) => Math.max(0, n - 1));
markNotificationRead(notification.id, { token }).catch(() => {
// Roll back the optimistic read on failure.
setItems(previous);
setUnread(previousUnread);
});
}
if (jobId) {
router.push(`/job/${jobId}`);
}
},
[items, unread, token, router],
);
/** Mark every notification read (optimistic), reload on failure. */
const onMarkAll = useCallback(async () => {
if (markingAll || unread === 0) return;
setMarkingAll(true);
const previous = items;
const previousUnread = unread;
setItems((rows) => rows.map((r) => (r.read ? r : { ...r, read: true })));
setUnread(0);
try {
await markAllNotificationsRead({ token });
} catch {
setItems(previous);
setUnread(previousUnread);
} finally {
setMarkingAll(false);
}
}, [markingAll, unread, items, token]);
if (loading) {
return (
<View style={[styles.container, styles.center]}>
<ActivityIndicator color={colors.accent} size="large" />
</View>
);
}
const markAllDisabled = markingAll || unread === 0;
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.count}>
{unread > 0 ? `${unread} unread` : "All caught up"}
</Text>
<Pressable
onPress={onMarkAll}
disabled={markAllDisabled}
hitSlop={8}
accessibilityRole="button"
accessibilityLabel="Mark all notifications read"
>
{markingAll ? (
<ActivityIndicator color={colors.accent} />
) : (
<Text
style={[
styles.markAll,
markAllDisabled ? styles.markAllDisabled : null,
]}
>
Mark all read
</Text>
)}
</Pressable>
</View>
<FlatList
contentContainerStyle={styles.content}
data={items}
keyExtractor={(item) => item.id}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
tintColor={colors.accent}
/>
}
renderItem={({ item }) => (
<NotificationItem notification={item} onPress={onPressItem} />
)}
ListEmptyComponent={
<View style={styles.center}>
<Text style={styles.empty}>
{error ??
"No notifications yet. You'll hear from us when your reels finish."}
</Text>
</View>
}
/>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: colors.bg },
center: {
flex: 1,
alignItems: "center",
justifyContent: "center",
paddingTop: 80,
paddingHorizontal: 32,
},
toolbar: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
paddingHorizontal: 20,
paddingVertical: 12,
borderBottomColor: colors.border,
borderBottomWidth: 1,
},
count: { color: colors.muted, fontSize: 13, fontWeight: "600" },
markAll: { color: colors.accent, fontSize: 14, fontWeight: "700" },
markAllDisabled: { color: colors.muted },
content: { padding: 20, gap: 12, flexGrow: 1 },
empty: { color: colors.muted, fontSize: 15, textAlign: "center", lineHeight: 22 },
});