Live video on the AT Protocol
1import {
2 Link,
3 NavigationProp,
4 ParamListBase,
5 useLinkBuilder,
6 useNavigation,
7} from "@react-navigation/native";
8import usePlatform from "hooks/usePlatform";
9import { useEffect } from "react";
10import { Pressable, StyleProp, ViewStyle } from "react-native";
11import Loading from "./loading/loading";
12
13export type LinkParams = { screen: string; params?: Record<string, string> };
14
15// Web and native have some disagreements about link styling
16// so we have a custom component that handles that
17export default function AQLink({
18 children,
19 to,
20 style,
21}: {
22 children: React.ReactNode;
23 to: LinkParams;
24 style?: StyleProp<ViewStyle>;
25}) {
26 const { isWeb } = usePlatform();
27 const navigation = useNavigation<NavigationProp<ParamListBase>>();
28 const baseStyle: StyleProp<ViewStyle> = {
29 display: "flex",
30 };
31
32 if (isWeb) {
33 return (
34 <Link style={[baseStyle, style]} to={to as any}>
35 {children}
36 </Link>
37 );
38 }
39
40 return (
41 <Pressable
42 style={[baseStyle, style]}
43 onPress={() => navigation.navigate(to.screen, to.params)}
44 >
45 {children}
46 </Pressable>
47 );
48}
49
50export function Redirect({ to }: { to: LinkParams }) {
51 const navigation = useNavigation<NavigationProp<ParamListBase>>();
52 useEffect(() => {
53 console.log("redirecting to", to);
54 navigation.navigate(to.screen, to.params);
55 }, []);
56 return <Loading />;
57}
58
59// generates the proper href for a given LinkParams object, for better web support
60export function useAQLinkHref(to: LinkParams): { href?: string } {
61 const { isWeb } = usePlatform();
62 const buildLink = useLinkBuilder();
63
64 if (!isWeb) {
65 return { href: undefined };
66 }
67
68 try {
69 const href = buildLink(to.screen, to.params);
70 return { href };
71 } catch (e) {
72 console.warn("Failed to build link for", to, e);
73 return { href: undefined };
74 }
75}