feat(mobile): Empfehlungsprogramm in der App

- Bildschirm "Freunde einladen" (/invite, aus den Einstellungen erreichbar):
  eigener Code mit nativem Teilen-Dialog, Rangleiter mit Fortschritt,
  eingeladene Tiere, nachträgliche Code-Eingabe im Einlösefenster
- Onboarding-Schritt "Wie hast du von PawFeed erfahren?" mit Code-Eingabe,
  einmal pro Owner (referrals.onboardingState)
- Rang-Abzeichen in PetIdentityBadges (Feed und Profil), Profilring in
  Rangfarbe, "Eingeladen von …" im Tierprofil
- Benachrichtigungen REFERRAL_JOINED / REFERRAL_RANK_UP
- Keine neuen nativen Module (React Native Share) — per OTA auslieferbar

Typecheck grün; nicht auf einem Gerät getestet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-01 06:30:11 +02:00
1 parent 4d0aa7ab9e
commit 78e16c9210
17 files changed
+725 -13

No files matched your search

+8 -2
View File
@@ -2,21 +2,27 @@ import { View, StyleSheet } from "react-native";
import { BadgeCheck, Sparkles } from "lucide-react-native";
import { isActiveSponsor } from "../lib/sponsor";
import { colors } from "../theme/tokens";
import { ReferralIcon } from "../lib/referral-icons";
import { useOwnerRankWithPerk, type ReferralOwnerFields } from "../hooks/useReferralRanks";
type Props = {
isVerified?: boolean;
sponsor?: { sponsorSince: Date | string | null; sponsorPeriodEnd: Date | string | null } | null;
/** Owner-Felder referralRankId/referralPerks — Rang-Abzeichen des Empfehlungsprogramms. */
referral?: ReferralOwnerFields | null;
};
/** Port of web's src/components/ui/pet-identity-badges.tsx. */
export function PetIdentityBadges({ isVerified, sponsor }: Props) {
export function PetIdentityBadges({ isVerified, sponsor, referral }: Props) {
const showSponsor = sponsor ? isActiveSponsor(sponsor) : false;
if (!isVerified && !showSponsor) return null;
const rank = useOwnerRankWithPerk(referral, "NAME_BADGE");
if (!isVerified && !showSponsor && !rank) return null;
return (
<View style={styles.row}>
{isVerified && <BadgeCheck size={14} color={colors.marigoldOrange} />}
{showSponsor && <Sparkles size={14} color={colors.marigoldOrange} />}
{rank && <ReferralIcon icon={rank.icon} size={14} color={rank.color} fill={rank.color} accessibilityLabel={rank.name} />}
</View>
);
}
+7 -2
View File
@@ -59,7 +59,12 @@ export type PostCardPost = {
nickname?: string | null;
avatarKey?: string | null;
isVerified?: boolean;
owner?: { sponsorSince: Date | string | null; sponsorPeriodEnd: Date | string | null } | null;
owner?: {
sponsorSince: Date | string | null;
sponsorPeriodEnd: Date | string | null;
referralRankId?: string | null;
referralPerks?: string[] | null;
} | null;
};
};
@@ -143,7 +148,7 @@ export function PostCard({ post, actingPetId, isTrending, isVisible = true }: Pr
<View style={styles.headerText}>
<View style={styles.nameRow}>
<Text style={styles.name}>{formatPetName(post.pet)}</Text>
<PetIdentityBadges isVerified={post.pet.isVerified} sponsor={post.pet.owner} />
<PetIdentityBadges isVerified={post.pet.isVerified} sponsor={post.pet.owner} referral={post.pet.owner} />
{isTrending && (
<View style={styles.trendingBadge}>
<Flame size={11} color={colors.marigoldDeep} fill={colors.marigoldDeep} />
@@ -0,0 +1,107 @@
import { useMemo, useState } from "react";
import { View, Text, Pressable, StyleSheet } from "react-native";
import { useTranslation } from "react-i18next";
import { useMutation } from "@tanstack/react-query";
import { CheckCircle2 } from "lucide-react-native";
import { useTRPC } from "../../api/trpc";
import { useTheme } from "../../context/ThemeContext";
import { Button } from "../Button";
import { RedeemCodeForm } from "../referrals/RedeemCodeForm";
import { radii, getTypography, type ColorTokens } from "../../theme/tokens";
const SOURCES = [
"FRIENDS",
"TIKTOK",
"INSTAGRAM",
"YOUTUBE",
"FACEBOOK",
"SHELTER_OR_PARTNER",
"STORE_SEARCH",
"PRESS",
"OTHER",
] as const;
type Source = (typeof SOURCES)[number];
type Props = { canRedeem: boolean; onDone: () => void };
/**
* Port of web's src/components/referrals/AcquisitionStep.tsx — freiwillige
* Herkunftsangabe nach dem ersten Tier, bei „Freunde“ mit Code-Eingabe.
* Kein Vorausfüllen wie im Web (dort per Cookie aus /r/<code>): nach dem
* Store-Umweg gibt es in der App keinen Weg, den Link-Code mitzunehmen.
*/
export function AcquisitionStep({ canRedeem, onDone }: Props) {
const { t } = useTranslation();
const { colors } = useTheme();
const styles = useMemo(() => makeStyles(colors), [colors]);
const trpc = useTRPC();
const [source, setSource] = useState<Source | null>(null);
const [inviterName, setInviterName] = useState<string | null | undefined>(undefined);
const saveSource = useMutation(trpc.referrals.setAcquisitionSource.mutationOptions({ onSettled: onDone }));
function handleContinue() {
if (source) saveSource.mutate({ source });
else onDone();
}
return (
<View style={styles.container}>
<Text style={styles.title}>{t("referrals.onboardingTitle")}</Text>
<Text style={styles.subtitle}>{t("referrals.onboardingSubtitle")}</Text>
<View style={styles.chips} accessibilityRole="radiogroup">
{SOURCES.map((option) => {
const isSelected = source === option;
return (
<Pressable
key={option}
accessibilityRole="radio"
accessibilityState={{ checked: isSelected }}
onPress={() => setSource(option)}
style={[styles.chip, isSelected && styles.chipSelected]}
>
<Text style={[styles.chipText, isSelected && styles.chipTextSelected]}>{t(`referrals.source_${option}`)}</Text>
</Pressable>
);
})}
</View>
{source === "FRIENDS" && canRedeem && (
<View style={styles.redeemBox}>
{inviterName !== undefined ? (
<View style={styles.redeemedRow}>
<CheckCircle2 size={18} color={colors.marigoldOrange} />
<Text style={styles.redeemedText}>
{inviterName ? t("referrals.redeemed", { name: inviterName }) : t("referrals.redeemedGeneric")}
</Text>
</View>
) : (
<RedeemCodeForm onRedeemed={setInviterName} />
)}
</View>
)}
<Button title={saveSource.isPending ? "…" : t("common.continue")} disabled={saveSource.isPending} onPress={handleContinue} style={styles.cta} />
<Button title={t("common.skip")} variant="outline" onPress={onDone} />
</View>
);
}
function makeStyles(colors: ColorTokens) {
const typography = getTypography(colors);
return StyleSheet.create({
container: { padding: 24, gap: 8 },
title: { ...typography.title, fontSize: 18 },
subtitle: { ...typography.body, color: colors.quietGray, marginBottom: 12 },
chips: { flexDirection: "row", flexWrap: "wrap", gap: 8, marginBottom: 12 },
chip: { paddingHorizontal: 14, paddingVertical: 9, borderRadius: 999, borderWidth: 1, borderColor: colors.hairlineGray },
chipSelected: { borderColor: colors.marigoldOrange, backgroundColor: colors.marigoldTint },
chipText: { ...typography.body, fontSize: 14 },
chipTextSelected: { color: colors.marigoldDeep, fontWeight: "600" },
redeemBox: { padding: 14, borderRadius: radii.xl, borderWidth: 1, borderColor: colors.hairlineGray, marginBottom: 12 },
redeemedRow: { flexDirection: "row", alignItems: "center", gap: 8 },
redeemedText: { ...typography.body, flex: 1 },
cta: { marginTop: 4 },
});
}
@@ -0,0 +1,90 @@
import { useMemo } from "react";
import { View, Text, StyleSheet } from "react-native";
import { useTranslation } from "react-i18next";
import { Check } from "lucide-react-native";
import { useTheme } from "../../context/ThemeContext";
import { ReferralIcon } from "../../lib/referral-icons";
import type { ReferralRankView } from "../../hooks/useReferralRanks";
import { radii, getTypography, type ColorTokens } from "../../theme/tokens";
type Props = {
ranks: ReferralRankView[];
achievedRankIds: ReadonlySet<string>;
qualifiedCount: number;
nextRankId: string | null;
};
/** Port of web's RankLadder — Fortschritt zum nächsten Rang plus alle aktiven Ränge. */
export function RankLadder({ ranks, achievedRankIds, qualifiedCount, nextRankId }: Props) {
const { t } = useTranslation();
const { colors } = useTheme();
const styles = useMemo(() => makeStyles(colors), [colors]);
const activeRanks = ranks.filter((rank) => rank.isActive);
const next = activeRanks.find((rank) => rank.id === nextRankId) ?? null;
const progress = next ? Math.min(100, Math.round((qualifiedCount / next.threshold) * 100)) : 100;
return (
<View style={styles.card}>
<View style={styles.headerRow}>
<Text style={styles.heading}>{t("referrals.ranksHeading")}</Text>
<Text style={styles.muted}>{t("referrals.countedInvites", { count: qualifiedCount })}</Text>
</View>
{next ? (
<>
<View style={styles.track} accessibilityRole="progressbar" accessibilityValue={{ min: 0, max: next.threshold, now: qualifiedCount }}>
<View style={[styles.fill, { width: `${progress}%`, backgroundColor: next.color }]} />
</View>
<Text style={styles.muted}>
{t("referrals.nextRank", { count: Math.max(0, next.threshold - qualifiedCount), name: next.name })}
</Text>
</>
) : (
<Text style={styles.muted}>{t("referrals.allRanksReached")}</Text>
)}
<View style={styles.list}>
{activeRanks.map((rank) => {
const isAchieved = achievedRankIds.has(rank.id);
return (
<View
key={rank.id}
style={[styles.row, isAchieved ? { borderColor: rank.color } : styles.rowPending]}
>
<View style={[styles.iconCircle, { borderColor: rank.color, backgroundColor: isAchieved ? rank.color : "transparent" }]}>
<ReferralIcon icon={rank.icon} size={16} color={isAchieved ? "#ffffff" : rank.color} />
</View>
<View style={styles.rowText}>
<Text style={styles.rankName}>{rank.name}</Text>
<Text style={styles.rankDescription}>{rank.description}</Text>
</View>
{isAchieved ? (
<Check size={16} color={colors.marigoldOrange} accessibilityLabel={t("referrals.achieved")} />
) : (
<Text style={styles.muted}>{t("referrals.threshold", { count: rank.threshold })}</Text>
)}
</View>
);
})}
</View>
</View>
);
}
function makeStyles(colors: ColorTokens) {
const typography = getTypography(colors);
return StyleSheet.create({
card: { gap: 10, padding: 16, borderRadius: radii["2xl"], borderWidth: 1, borderColor: colors.hairlineGray, backgroundColor: colors.pureWhite },
headerRow: { flexDirection: "row", justifyContent: "space-between", alignItems: "baseline", gap: 8 },
heading: { ...typography.title, fontSize: 16 },
muted: { ...typography.body, fontSize: 13, color: colors.quietGray },
track: { height: 8, borderRadius: 4, backgroundColor: colors.whisperGray, overflow: "hidden" },
fill: { height: 8, borderRadius: 4 },
list: { gap: 8, marginTop: 6 },
row: { flexDirection: "row", alignItems: "center", gap: 12, padding: 10, borderRadius: radii.xl, borderWidth: 1 },
rowPending: { borderColor: colors.hairlineGray, borderStyle: "dashed" },
iconCircle: { width: 32, height: 32, borderRadius: 16, borderWidth: 2, alignItems: "center", justifyContent: "center" },
rowText: { flex: 1 },
rankName: { ...typography.body, fontWeight: "600" },
rankDescription: { ...typography.body, fontSize: 12, color: colors.quietGray },
});
}
@@ -0,0 +1,74 @@
import { useMemo, useState } from "react";
import { View, Text, StyleSheet } from "react-native";
import { useTranslation } from "react-i18next";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useTRPC } from "../../api/trpc";
import { useTheme } from "../../context/ThemeContext";
import { Button } from "../Button";
import { TextField } from "../TextField";
import { getTypography, type ColorTokens } from "../../theme/tokens";
/** Fehler-Schlüssel aus REFERRAL_ERRORS (src/lib/referrals.ts im Web) → Übersetzungsschlüssel. */
const ERROR_KEYS: Record<string, string> = {
REFERRAL_PROGRAM_PAUSED: "referrals.errorPaused",
REFERRAL_CODE_INVALID: "referrals.errorInvalid",
REFERRAL_OWN_CODE: "referrals.errorOwnCode",
REFERRAL_ALREADY_REDEEMED: "referrals.errorAlreadyRedeemed",
REFERRAL_WINDOW_EXPIRED: "referrals.errorWindowExpired",
REFERRAL_NO_PET: "referrals.errorNoPet",
};
/** Port of web's RedeemCodeForm — genutzt im Onboarding und auf dem Einladen-Bildschirm. */
export function RedeemCodeForm({ onRedeemed }: { onRedeemed?: (inviterPetName: string | null) => void }) {
const { t } = useTranslation();
const { colors } = useTheme();
const styles = useMemo(() => makeStyles(colors), [colors]);
const trpc = useTRPC();
const qc = useQueryClient();
const [code, setCode] = useState("");
const [errorKey, setErrorKey] = useState<string | null>(null);
const redeem = useMutation(
trpc.referrals.redeem.mutationOptions({
onSuccess: (result) => {
setErrorKey(null);
void qc.invalidateQueries({ queryKey: trpc.referrals.me.queryKey() });
onRedeemed?.(result.inviterPetName);
},
onError: (err) => {
const isRateLimited = err.data?.code === "TOO_MANY_REQUESTS";
setErrorKey(isRateLimited ? "referrals.errorTooMany" : (ERROR_KEYS[err.message] ?? "referrals.errorGeneric"));
},
})
);
return (
<View style={styles.container}>
<Text style={styles.label}>{t("referrals.redeemLabel")}</Text>
<TextField
value={code}
onChangeText={(value) => setCode(value.toUpperCase())}
placeholder="BUBI-7K3M"
autoCapitalize="characters"
autoCorrect={false}
maxLength={32}
accessibilityLabel={t("referrals.redeemLabel")}
/>
{errorKey && <Text style={styles.error}>{t(errorKey)}</Text>}
<Button
title={redeem.isPending ? "…" : t("referrals.redeemCta")}
disabled={!code.trim() || redeem.isPending}
onPress={() => redeem.mutate({ code: code.trim() })}
/>
</View>
);
}
function makeStyles(colors: ColorTokens) {
const typography = getTypography(colors);
return StyleSheet.create({
container: { gap: 8 },
label: { ...typography.body, fontWeight: "600" },
error: { ...typography.body, fontSize: 13, color: colors.alertRed },
});
}
@@ -0,0 +1,19 @@
import type { ReactNode } from "react";
import { View } from "react-native";
import { useOwnerRankWithPerk, type ReferralOwnerFields } from "../../hooks/useReferralRanks";
const RING_WIDTH = 3;
const RING_GAP = 2;
/**
* Port of web's ReferralAvatarRing — Ring in der Rangfarbe um das Tierbild
* im Profilkopf (Vorteil PROFILE_RING). Nur im Profil, nicht im Feed: dort
* steht der Ring (PawRing) für Storys.
*/
export function ReferralAvatarRing({ owner, children }: { owner: ReferralOwnerFields | null | undefined; children: ReactNode }) {
const rank = useOwnerRankWithPerk(owner, "PROFILE_RING");
if (!rank) return <>{children}</>;
return (
<View style={{ borderWidth: RING_WIDTH, borderColor: rank.color, borderRadius: 999, padding: RING_GAP }}>{children}</View>
);
}
@@ -0,0 +1,36 @@
import { useMemo } from "react";
import { View, Text, Pressable, StyleSheet } from "react-native";
import { useRouter } from "expo-router";
import { useTranslation } from "react-i18next";
import { ChevronRight, Gift } from "lucide-react-native";
import { useTheme } from "../../context/ThemeContext";
import { getTypography, type ColorTokens } from "../../theme/tokens";
import { sectionTitleStyle } from "./sectionTitleStyle";
/** Einstieg zum Empfehlungsprogramm (Bildschirm /invite) aus den Einstellungen. */
export function InviteFriendsSection() {
const { t } = useTranslation();
const router = useRouter();
const { colors } = useTheme();
const styles = useMemo(() => makeStyles(colors), [colors]);
return (
<View>
<Text style={styles.sectionTitle}>{t("referrals.title")}</Text>
<Pressable style={styles.row} onPress={() => router.push("/invite")} accessibilityRole="button">
<Gift size={20} color={colors.marigoldOrange} />
<Text style={styles.label}>{t("referrals.settingsLabel")}</Text>
<ChevronRight size={18} color={colors.quietGray} />
</Pressable>
</View>
);
}
function makeStyles(colors: ColorTokens) {
const typography = getTypography(colors);
return StyleSheet.create({
sectionTitle: sectionTitleStyle(colors),
row: { flexDirection: "row", alignItems: "center", gap: 12, paddingVertical: 6 },
label: { ...typography.body, flex: 1 },
});
}