feat: Story-Kommentare, Pfoten und Zuschauerliste in der App

- App-Viewer: Pfote, Kommentar-Sheet (schreiben, eigene löschen) und für die
  eigene Story die Zuschauerliste; Timer pausiert, solange ein Sheet offen ist
- stories.listActive: optionales viewerPetId → reactedByViewer, damit die
  Pfote gefüllt startet (abwärtskompatibel für alte App-Builds)
- Web-StoryPawButton nutzt reactedByViewer ebenfalls und wird pro Story
  neu initialisiert

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-01 20:59:02 +02:00
1 parent 204d5f6831
commit ccebfa46a6
11 files changed
+533 -22

No files matched your search

@@ -0,0 +1,174 @@
import { useMemo, useState } from "react";
import {
ActivityIndicator,
FlatList,
KeyboardAvoidingView,
Modal,
Platform,
Pressable,
StyleSheet,
Text,
View,
} from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useTranslation } from "react-i18next";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Send, Trash2, X } from "lucide-react-native";
import { useTRPC } from "../../api/trpc";
import { useTheme } from "../../context/ThemeContext";
import { PetAvatar } from "../PetAvatar";
import { TextField } from "../TextField";
import { formatPetName, formatRelativeTime } from "../../lib/format";
import { getTypography, radii, type ColorTokens } from "../../theme/tokens";
type Props = {
storyId: string;
activePetId: string;
visible: boolean;
onClose: () => void;
/** Wird nach Hinzufügen/Löschen aufgerufen, damit der Viewer den Zähler neu lädt. */
onChanged: () => void;
onPressPet: (petId: string) => void;
};
/**
* Port of web's StoryCommentsOverlay als Bottom-Sheet. Der Viewer pausiert die
* Story, solange das Sheet offen ist (anders als im Web — auf dem Handy
* verdeckt die Tastatur sonst die laufende Story und sie springt weiter,
* während man noch tippt).
*/
export function StoryCommentsSheet({ storyId, activePetId, visible, onClose, onChanged, onPressPet }: Props) {
const { t } = useTranslation();
const { colors } = useTheme();
const insets = useSafeAreaInsets();
const styles = useMemo(() => makeStyles(colors, insets.bottom), [colors, insets.bottom]);
const trpc = useTRPC();
const qc = useQueryClient();
const [body, setBody] = useState("");
const { data: comments = [], isLoading } = useQuery({
...trpc.stories.listComments.queryOptions({ storyId }),
enabled: visible,
});
const refresh = () => {
void qc.invalidateQueries({ queryKey: trpc.stories.listComments.queryKey({ storyId }) });
onChanged();
};
const add = useMutation(
trpc.stories.addComment.mutationOptions({
onSuccess: () => {
setBody("");
refresh();
},
})
);
const remove = useMutation(trpc.stories.deleteComment.mutationOptions({ onSuccess: refresh }));
const trimmed = body.trim();
return (
<Modal visible={visible} transparent animationType="slide" onRequestClose={onClose}>
<KeyboardAvoidingView style={styles.flex} behavior={Platform.OS === "ios" ? "padding" : "height"}>
<Pressable style={styles.backdrop} onPress={onClose} accessibilityLabel={t("common.close")} />
<View style={styles.sheet}>
<View style={styles.header}>
<Text style={styles.title}>{t("storyViewer.commentsTitle")}</Text>
<Pressable onPress={onClose} hitSlop={12} accessibilityLabel={t("common.close")}>
<X size={20} color={colors.ink} />
</Pressable>
</View>
{isLoading ? (
<ActivityIndicator style={styles.loading} color={colors.marigoldOrange} />
) : (
<FlatList
data={comments}
keyExtractor={(item) => item.id}
contentContainerStyle={styles.list}
ListEmptyComponent={<Text style={styles.muted}>{t("comments.empty")}</Text>}
renderItem={({ item }) => (
<View style={styles.row}>
<Pressable onPress={() => onPressPet(item.pet.id)}>
<PetAvatar name={item.pet.name} avatarKey={item.pet.avatarKey} size="sm" />
</Pressable>
<View style={styles.rowBody}>
<View style={styles.rowHeader}>
<Text style={styles.petName} onPress={() => onPressPet(item.pet.id)}>
{formatPetName(item.pet)}
</Text>
<Text style={styles.time}>{formatRelativeTime(item.createdAt)}</Text>
</View>
<Text style={styles.body}>{item.body}</Text>
</View>
{item.pet.id === activePetId && (
<Pressable
onPress={() => remove.mutate({ commentId: item.id, petId: activePetId })}
hitSlop={8}
accessibilityLabel={t("storyViewer.deleteComment")}
>
<Trash2 size={16} color={colors.quietGray} />
</Pressable>
)}
</View>
)}
/>
)}
<View style={styles.inputRow}>
<TextField
placeholder={t("comments.placeholder")}
value={body}
onChangeText={setBody}
maxLength={500}
style={styles.input}
returnKeyType="send"
onSubmitEditing={() => trimmed && add.mutate({ storyId, petId: activePetId, body: trimmed })}
/>
<Pressable
onPress={() => add.mutate({ storyId, petId: activePetId, body: trimmed })}
disabled={!trimmed || add.isPending}
style={[styles.send, (!trimmed || add.isPending) && styles.sendDisabled]}
accessibilityLabel={t("messages.send")}
>
{add.isPending ? <ActivityIndicator color="#ffffff" /> : <Send size={18} color="#ffffff" />}
</Pressable>
</View>
{add.isError && <Text style={styles.error}>{t("storyViewer.commentError")}</Text>}
</View>
</KeyboardAvoidingView>
</Modal>
);
}
function makeStyles(colors: ColorTokens, insetBottom: number) {
const typography = getTypography(colors);
return StyleSheet.create({
flex: { flex: 1 },
backdrop: { flex: 1, backgroundColor: "rgba(0,0,0,0.35)" },
sheet: {
maxHeight: "65%",
backgroundColor: colors.porchWhite,
borderTopLeftRadius: radii["2xl"],
borderTopRightRadius: radii["2xl"],
paddingBottom: insetBottom + 12,
},
header: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", padding: 16, borderBottomWidth: 1, borderBottomColor: colors.hairlineGray },
title: { ...typography.title, fontSize: 16 },
loading: { padding: 24 },
list: { padding: 16, gap: 14 },
muted: { ...typography.body, color: colors.quietGray, textAlign: "center", paddingVertical: 16 },
row: { flexDirection: "row", gap: 10, alignItems: "flex-start" },
rowBody: { flex: 1, gap: 2 },
rowHeader: { flexDirection: "row", alignItems: "baseline", gap: 8 },
petName: { ...typography.body, fontWeight: "600", fontSize: 13 },
time: { ...typography.body, fontSize: 11, color: colors.quietGray },
body: { ...typography.body },
inputRow: { flexDirection: "row", alignItems: "center", gap: 8, paddingHorizontal: 12, paddingTop: 10 },
input: { flex: 1 },
send: { width: 48, height: 48, borderRadius: radii.lg, backgroundColor: colors.marigoldOrange, alignItems: "center", justifyContent: "center" },
sendDisabled: { opacity: 0.5 },
error: { ...typography.body, fontSize: 12, color: colors.alertRed, paddingHorizontal: 12, paddingTop: 6 },
});
}
@@ -0,0 +1,72 @@
import { useState } from "react";
import { Pressable, Text, View, StyleSheet } from "react-native";
import { useTranslation } from "react-i18next";
import { useMutation } from "@tanstack/react-query";
import { PawPrint } from "lucide-react-native";
import { useTRPC } from "../../api/trpc";
import { colors } from "../../theme/tokens";
type Props = {
storyId: string;
petId: string;
initialCount: number;
initialReacted: boolean;
};
/**
* Port of web's StoryPawButton — Pfote auf einer Story. Optimistisch: Zustand
* und Zähler springen sofort um und werden bei einem Fehler zurückgesetzt.
* Der Viewer setzt `key={story.id}`, damit der Zustand pro Story neu startet.
*/
export function StoryPawButton({ storyId, petId, initialCount, initialReacted }: Props) {
const { t } = useTranslation();
const trpc = useTRPC();
const [reacted, setReacted] = useState(initialReacted);
const [count, setCount] = useState(initialCount);
const toggle = useMutation(
trpc.stories.toggleReaction.mutationOptions({
onMutate: () => {
const previous = { reacted, count };
setReacted(!reacted);
setCount(reacted ? Math.max(0, count - 1) : count + 1);
return previous;
},
// Der Server ist maßgeblich: Zähler aus dem Stand vor dem Tippen + tatsächlicher Änderung.
onSuccess: (data, _vars, previous) => {
setReacted(data.reacted);
if (!previous) return;
const delta = data.reacted === previous.reacted ? 0 : data.reacted ? 1 : -1;
setCount(Math.max(0, previous.count + delta));
},
onError: (_err, _vars, previous) => {
if (previous) {
setReacted(previous.reacted);
setCount(previous.count);
}
},
})
);
return (
<Pressable
onPress={() => !toggle.isPending && toggle.mutate({ storyId, petId })}
accessibilityRole="button"
accessibilityLabel={reacted ? t("storyViewer.removePaw") : t("storyViewer.paw")}
accessibilityState={{ selected: reacted }}
hitSlop={8}
style={styles.wrap}
>
<View style={styles.circle}>
<PawPrint size={20} color={reacted ? colors.marigoldOrange : colors.pureWhite} fill={reacted ? colors.marigoldOrange : "transparent"} />
</View>
{count > 0 && <Text style={styles.count}>{count}</Text>}
</Pressable>
);
}
const styles = StyleSheet.create({
wrap: { alignItems: "center", gap: 2 },
circle: { width: 44, height: 44, borderRadius: 22, backgroundColor: "rgba(0,0,0,0.4)", alignItems: "center", justifyContent: "center" },
count: { color: colors.pureWhite, fontSize: 12, fontWeight: "600", textShadowColor: "rgba(0,0,0,0.6)", textShadowRadius: 3 },
});
@@ -0,0 +1,88 @@
import { useMemo } from "react";
import { ActivityIndicator, FlatList, Modal, Pressable, StyleSheet, Text, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useTranslation } from "react-i18next";
import { useQuery } from "@tanstack/react-query";
import { X } from "lucide-react-native";
import { useTRPC } from "../../api/trpc";
import { useTheme } from "../../context/ThemeContext";
import { PetAvatar } from "../PetAvatar";
import { formatPetName, formatRelativeTime } from "../../lib/format";
import { getTypography, radii, type ColorTokens } from "../../theme/tokens";
type Props = {
storyId: string;
visible: boolean;
onClose: () => void;
onPressPet: (petId: string) => void;
};
/**
* Port of web's owner-only viewer Sheet — wer hat diese Story gesehen.
* stories.getViewers prüft serverseitig, dass nur der Owner der Story fragt.
*/
export function StoryViewersSheet({ storyId, visible, onClose, onPressPet }: Props) {
const { t } = useTranslation();
const { colors } = useTheme();
const insets = useSafeAreaInsets();
const styles = useMemo(() => makeStyles(colors, insets.bottom), [colors, insets.bottom]);
const trpc = useTRPC();
const { data: viewers = [], isLoading } = useQuery({
...trpc.stories.getViewers.queryOptions({ storyId }),
enabled: visible,
});
return (
<Modal visible={visible} transparent animationType="slide" onRequestClose={onClose}>
<Pressable style={styles.backdrop} onPress={onClose} accessibilityLabel={t("common.close")} />
<View style={styles.sheet}>
<View style={styles.header}>
<Text style={styles.title}>{t("storyViewer.viewersTitle", { count: viewers.length })}</Text>
<Pressable onPress={onClose} hitSlop={12} accessibilityLabel={t("common.close")}>
<X size={20} color={colors.ink} />
</Pressable>
</View>
{isLoading ? (
<ActivityIndicator style={styles.loading} color={colors.marigoldOrange} />
) : (
<FlatList
data={viewers}
keyExtractor={(item) => item.viewerPet.id}
contentContainerStyle={styles.list}
ListEmptyComponent={<Text style={styles.muted}>{t("storyViewer.noViewers")}</Text>}
renderItem={({ item }) => (
<Pressable style={styles.row} onPress={() => onPressPet(item.viewerPet.id)}>
<PetAvatar name={item.viewerPet.name} avatarKey={item.viewerPet.avatarKey} size="sm" />
<Text style={styles.name} numberOfLines={1}>{formatPetName(item.viewerPet)}</Text>
<Text style={styles.time}>{formatRelativeTime(item.viewedAt)}</Text>
</Pressable>
)}
/>
)}
</View>
</Modal>
);
}
function makeStyles(colors: ColorTokens, insetBottom: number) {
const typography = getTypography(colors);
return StyleSheet.create({
backdrop: { flex: 1, backgroundColor: "rgba(0,0,0,0.35)" },
sheet: {
maxHeight: "60%",
backgroundColor: colors.porchWhite,
borderTopLeftRadius: radii["2xl"],
borderTopRightRadius: radii["2xl"],
paddingBottom: insetBottom + 12,
},
header: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", padding: 16, borderBottomWidth: 1, borderBottomColor: colors.hairlineGray },
title: { ...typography.title, fontSize: 16 },
loading: { padding: 24 },
list: { padding: 16, gap: 12 },
muted: { ...typography.body, color: colors.quietGray, textAlign: "center", paddingVertical: 16 },
row: { flexDirection: "row", alignItems: "center", gap: 10 },
name: { ...typography.body, flex: 1, fontWeight: "600" },
time: { ...typography.body, fontSize: 11, color: colors.quietGray },
});
}