99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import React, { type ReactNode } from "react";
|
|
import { Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle } from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import { useRouter } from "expo-router";
|
|
|
|
import { Colors } from "@/theme/colors";
|
|
import { Radius } from "@/theme/radius";
|
|
import { Spacing } from "@/theme/spacing";
|
|
import type { NavigableContentReference } from "../../types/content-reference.types";
|
|
import { buildContentReferenceRoute } from "../../utils/content-reference";
|
|
|
|
export interface ContentLinkProps {
|
|
reference: NavigableContentReference;
|
|
/** Optional custom child content. When omitted, the reference label is rendered. */
|
|
children?: ReactNode;
|
|
/** Optional accessibility label override. */
|
|
accessibilityLabel?: string;
|
|
/** Optional wrapper style for renderer-specific placement. */
|
|
style?: StyleProp<ViewStyle>;
|
|
/** Called immediately before navigation. */
|
|
onPress?: (reference: NavigableContentReference) => void;
|
|
/** Disable navigation while retaining the visual content. */
|
|
disabled?: boolean;
|
|
}
|
|
|
|
export function ContentLink({
|
|
reference,
|
|
children,
|
|
accessibilityLabel,
|
|
style,
|
|
onPress,
|
|
disabled = false,
|
|
}: ContentLinkProps) {
|
|
const router = useRouter();
|
|
const label = reference.label?.trim() || defaultReferenceLabel(reference);
|
|
|
|
return (
|
|
<Pressable
|
|
accessibilityRole="link"
|
|
accessibilityLabel={accessibilityLabel ?? label}
|
|
accessibilityState={{ disabled }}
|
|
disabled={disabled}
|
|
onPress={() => {
|
|
onPress?.(reference);
|
|
router.push(buildContentReferenceRoute(reference) as never);
|
|
}}
|
|
style={({ pressed }) => [
|
|
styles.container,
|
|
style,
|
|
pressed && !disabled ? styles.pressed : null,
|
|
disabled ? styles.disabled : null,
|
|
]}
|
|
>
|
|
{children ?? (
|
|
<View style={styles.defaultContent}>
|
|
<Text style={styles.label}>{label}</Text>
|
|
<Ionicons name="chevron-forward" size={18} color={Colors.primary} />
|
|
</View>
|
|
)}
|
|
</Pressable>
|
|
);
|
|
}
|
|
|
|
function defaultReferenceLabel(reference: NavigableContentReference): string {
|
|
if (reference.type === "content_ref") {
|
|
return reference.target.source_key?.trim() || "Open content";
|
|
}
|
|
return "Open section";
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
borderRadius: Radius.md,
|
|
},
|
|
defaultContent: {
|
|
minHeight: 44,
|
|
paddingHorizontal: Spacing.md,
|
|
paddingVertical: Spacing.sm,
|
|
borderRadius: Radius.md,
|
|
backgroundColor: "#E8F5E9",
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: Spacing.sm,
|
|
},
|
|
label: {
|
|
flex: 1,
|
|
color: Colors.primaryDark,
|
|
fontSize: 15,
|
|
lineHeight: 20,
|
|
fontWeight: "700",
|
|
},
|
|
pressed: {
|
|
opacity: 0.78,
|
|
},
|
|
disabled: {
|
|
opacity: 0.5,
|
|
},
|
|
});
|