@akita-protocol/algomd-rn 0.0.0-stage → 1.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -2
- package/package.json +118 -3
- package/src/adapters/index.ts +4 -0
- package/src/adapters/txn-graph.ts +138 -0
- package/src/components/ASA.tsx +38 -0
- package/src/components/ASASearch.tsx +79 -0
- package/src/components/Account.tsx +42 -0
- package/src/components/AccountSearch.tsx +75 -0
- package/src/components/AuctionListing.tsx +40 -0
- package/src/components/Collection.tsx +42 -0
- package/src/components/DualStakePool.tsx +244 -0
- package/src/components/NFDProfile.tsx +45 -0
- package/src/components/NFDSearch.tsx +81 -0
- package/src/components/NFTListing.tsx +73 -0
- package/src/components/NFTSearch.tsx +68 -0
- package/src/components/Poll.tsx +39 -0
- package/src/components/PollSearch.tsx +79 -0
- package/src/components/PrizeBoxBundle.tsx +1 -0
- package/src/components/RaffleListing.tsx +40 -0
- package/src/components/SelfFetching.tsx +1048 -0
- package/src/components/StakingPool.tsx +134 -0
- package/src/components/TradeOffer.tsx +47 -0
- package/src/components/TradeSearch.tsx +103 -0
- package/src/components/TransactionDetails.tsx +37 -0
- package/src/components/TransactionSearch.tsx +102 -0
- package/src/components/TransactionsGraph.tsx +60 -0
- package/src/components/index.ts +41 -0
- package/src/fonts/OFL.txt +93 -0
- package/src/fonts/SpaceGrotesk_300Light.ttf +0 -0
- package/src/fonts/SpaceGrotesk_400Regular.ttf +0 -0
- package/src/fonts/SpaceGrotesk_500Medium.ttf +0 -0
- package/src/fonts/SpaceGrotesk_600SemiBold.ttf +0 -0
- package/src/fonts/SpaceGrotesk_700Bold.ttf +0 -0
- package/src/fonts/app.css +5 -0
- package/src/graph.ts +3 -0
- package/src/hooks/index.ts +17 -0
- package/src/hooks/useAlgomdData.ts +1539 -0
- package/src/hooks/useAlgorandClient.ts +54 -0
- package/src/index.ts +147 -0
- package/src/presentation/ASADisplay.tsx +380 -0
- package/src/presentation/AccountDisplay.tsx +132 -0
- package/src/presentation/AccountMentionInline.tsx +38 -0
- package/src/presentation/AccountMentionText.tsx +80 -0
- package/src/presentation/AdaptiveImage.tsx +314 -0
- package/src/presentation/AssetMention.tsx +87 -0
- package/src/presentation/AuctionListingDisplay.tsx +428 -0
- package/src/presentation/BundleContents.tsx +228 -0
- package/src/presentation/CardContainer.tsx +61 -0
- package/src/presentation/CardEntrance.tsx +21 -0
- package/src/presentation/CollectionDisplay.tsx +168 -0
- package/src/presentation/ContractMenu.tsx +243 -0
- package/src/presentation/DualStakePoolDisplay.tsx +335 -0
- package/src/presentation/Embeds.tsx +300 -0
- package/src/presentation/GateRequirements.tsx +334 -0
- package/src/presentation/HEROICONS-LICENSE.txt +21 -0
- package/src/presentation/NFDProfileDisplay.tsx +194 -0
- package/src/presentation/NFTListingDisplay.tsx +284 -0
- package/src/presentation/OnChainActionCard.tsx +557 -0
- package/src/presentation/PollDisplay.tsx +222 -0
- package/src/presentation/PrizeBoxBundleDisplay.tsx +103 -0
- package/src/presentation/PrizeBoxMedia.tsx +46 -0
- package/src/presentation/PrizePreview.tsx +296 -0
- package/src/presentation/ProgressBar.tsx +50 -0
- package/src/presentation/RaffleListingDisplay.tsx +399 -0
- package/src/presentation/References.tsx +310 -0
- package/src/presentation/ReservedNotice.tsx +73 -0
- package/src/presentation/ResultsPanel.tsx +241 -0
- package/src/presentation/Sparkline.tsx +70 -0
- package/src/presentation/SquishyPressable.tsx +145 -0
- package/src/presentation/StakingPoolDisplay.tsx +419 -0
- package/src/presentation/StatusBadge.tsx +56 -0
- package/src/presentation/TradeOfferDisplay.tsx +275 -0
- package/src/presentation/TransactionDisplay.tsx +262 -0
- package/src/presentation/WinnerBadge.tsx +58 -0
- package/src/presentation/assetHelpers.ts +36 -0
- package/src/presentation/assets.ts +7 -0
- package/src/presentation/explorer.ts +95 -0
- package/src/presentation/format.ts +90 -0
- package/src/presentation/gates.ts +100 -0
- package/src/presentation/icons.tsx +1178 -0
- package/src/presentation/identity.tsx +175 -0
- package/src/presentation/index.ts +88 -0
- package/src/presentation/media.ts +70 -0
- package/src/presentation/pluginColors.ts +8 -0
- package/src/presentation/runtime.tsx +268 -0
- package/src/presentation/sheets.tsx +7 -0
- package/src/presentation/subject.tsx +31 -0
- package/src/presentation/textTheme.ts +17 -0
- package/src/presentation/themes.json +158 -0
- package/src/presentation/variants.ts +14 -0
- package/src/provider/AlgomdProvider.tsx +45 -0
- package/src/provider/context.ts +18 -0
- package/src/provider/imageResolver.ts +12 -0
- package/src/provider/index.ts +5 -0
- package/src/provider/networks.ts +23 -0
- package/src/provider/types.ts +29 -0
- package/src/refs/AccountRef.tsx +226 -0
- package/src/refs/ApplicationRef.tsx +72 -0
- package/src/refs/AssetRef.tsx +142 -0
- package/src/refs/AuctionRef.tsx +108 -0
- package/src/refs/CollectionRef.tsx +70 -0
- package/src/refs/DualStakeRef.tsx +88 -0
- package/src/refs/HostRefs.tsx +82 -0
- package/src/refs/ListingRef.tsx +90 -0
- package/src/refs/NfdRef.tsx +96 -0
- package/src/refs/PollRef.tsx +120 -0
- package/src/refs/RaffleRef.tsx +101 -0
- package/src/refs/RewardRef.tsx +78 -0
- package/src/refs/StakingPoolRef.tsx +136 -0
- package/src/refs/SubscriptionRef.tsx +200 -0
- package/src/refs/TradeRef.tsx +100 -0
- package/src/refs/TransactionRef.tsx +156 -0
- package/src/refs/assetInfo.ts +49 -0
- package/src/refs/contractParts.tsx +159 -0
- package/src/refs/index.tsx +137 -0
- package/src/refs/primitives.tsx +588 -0
- package/src/refs/types.ts +13 -0
- package/src/types/algorand.ts +496 -0
- package/src/types/index.ts +1 -0
- package/src/ui/CopyButton.tsx +44 -0
- package/src/ui/DataStates.tsx +45 -0
- package/src/ui/ProgressBar.tsx +64 -0
- package/src/ui/SearchSheet.tsx +141 -0
- package/src/ui/SizeContainer.tsx +25 -0
- package/src/ui/StatusBadge.tsx +51 -0
- package/src/ui/index.ts +6 -0
- package/src/utils/algomdImageUrl.ts +20 -0
- package/src/utils/arc19.ts +120 -0
- package/src/utils/arc62.ts +246 -0
- package/src/utils/capacity.ts +26 -0
- package/src/utils/format.ts +181 -0
- package/src/utils/ids.ts +31 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/nfdDisplay.ts +45 -0
- package/src/utils/search.ts +87 -0
- package/src/utils/stakingPool.ts +174 -0
- package/src/utils/stakingSnapshot.ts +132 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Shared AlgoMD presentation, migrated from akita-rn. Edit this library source.
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { Image, View } from "react-native";
|
|
4
|
+
import { IdentityResolver, Text, usePresentation } from "./runtime";
|
|
5
|
+
|
|
6
|
+
import { useAppTheme } from "./runtime";
|
|
7
|
+
import { CardContainer } from "./CardContainer";
|
|
8
|
+
import { ContractMenu } from "./ContractMenu";
|
|
9
|
+
import { AddressIdentityInline, IdentityBadge, avatarRadius } from "./identity";
|
|
10
|
+
import { CardEntrance } from "./CardEntrance";
|
|
11
|
+
import { OnChainMetric, OnChainMetricGrid } from "./OnChainActionCard";
|
|
12
|
+
import { formatSupply } from "./ASADisplay";
|
|
13
|
+
import { fallbackAvatar } from "./assets";
|
|
14
|
+
import { formatAddress, formatNumber } from "../utils/format";
|
|
15
|
+
import type { AlgorandAccount } from "../types/algorand";
|
|
16
|
+
|
|
17
|
+
export interface AccountDisplayProps {
|
|
18
|
+
data: AlgorandAccount;
|
|
19
|
+
compact?: boolean;
|
|
20
|
+
/** The large card: adds the account's badges when the host draws them. */
|
|
21
|
+
showAssets?: boolean;
|
|
22
|
+
onExternalLink?: (address: string) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const AVATAR = 48;
|
|
26
|
+
|
|
27
|
+
export function AccountDisplay({ data, compact, showAssets }: AccountDisplayProps) {
|
|
28
|
+
const { theme } = useAppTheme();
|
|
29
|
+
const { renderAccountBadges } = usePresentation();
|
|
30
|
+
const online = theme.colors.palette.akitaGreen;
|
|
31
|
+
|
|
32
|
+
if (compact) {
|
|
33
|
+
return (
|
|
34
|
+
<CardContainer compact>
|
|
35
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 3 }}>
|
|
36
|
+
{data.isOnline ? (
|
|
37
|
+
<View
|
|
38
|
+
accessibilityLabel="Participating in consensus"
|
|
39
|
+
style={{ width: 5, height: 5, borderRadius: 2.5, backgroundColor: online }}
|
|
40
|
+
/>
|
|
41
|
+
) : null}
|
|
42
|
+
<AddressIdentityInline address={data.address} />
|
|
43
|
+
</View>
|
|
44
|
+
</CardContainer>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const heldCount = data.heldAssetCount ?? data.assets.length;
|
|
49
|
+
const badges = showAssets ? renderAccountBadges?.(data.address) : null;
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<CardEntrance>
|
|
53
|
+
<View
|
|
54
|
+
style={{
|
|
55
|
+
backgroundColor: theme.colors.accentBackground,
|
|
56
|
+
borderCurve: "continuous",
|
|
57
|
+
borderRadius: 20,
|
|
58
|
+
gap: 14,
|
|
59
|
+
padding: 16,
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
<IdentityResolver address={data.address}>
|
|
63
|
+
{(identity) => {
|
|
64
|
+
const named = identity.name && identity.name !== formatAddress(data.address);
|
|
65
|
+
const subline = [
|
|
66
|
+
named ? formatAddress(data.address, 6) : null,
|
|
67
|
+
data.isOnline ? "Participating in consensus" : null,
|
|
68
|
+
].filter(Boolean).join(" · ");
|
|
69
|
+
return (
|
|
70
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 12 }}>
|
|
71
|
+
<View>
|
|
72
|
+
<Image
|
|
73
|
+
source={identity.avatarUrl ? { uri: identity.avatarUrl } : fallbackAvatar}
|
|
74
|
+
style={{
|
|
75
|
+
width: AVATAR,
|
|
76
|
+
height: AVATAR,
|
|
77
|
+
borderRadius: avatarRadius(AVATAR, identity.isCommunity),
|
|
78
|
+
backgroundColor: theme.colors.background,
|
|
79
|
+
}}
|
|
80
|
+
/>
|
|
81
|
+
{data.isOnline ? (
|
|
82
|
+
<View
|
|
83
|
+
style={{
|
|
84
|
+
position: "absolute",
|
|
85
|
+
right: -1,
|
|
86
|
+
bottom: -1,
|
|
87
|
+
width: 14,
|
|
88
|
+
height: 14,
|
|
89
|
+
borderRadius: 7,
|
|
90
|
+
borderWidth: 2,
|
|
91
|
+
borderColor: theme.colors.accentBackground,
|
|
92
|
+
backgroundColor: online,
|
|
93
|
+
}}
|
|
94
|
+
/>
|
|
95
|
+
) : null}
|
|
96
|
+
</View>
|
|
97
|
+
<View style={{ flex: 1, minWidth: 0, gap: 2 }}>
|
|
98
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 6 }}>
|
|
99
|
+
<Text size="md" weight="semiBold" numberOfLines={1} style={{ flexShrink: 1 }}>
|
|
100
|
+
{identity.name || formatAddress(data.address)}
|
|
101
|
+
</Text>
|
|
102
|
+
<IdentityBadge identity={identity} size={16} />
|
|
103
|
+
</View>
|
|
104
|
+
{subline ? (
|
|
105
|
+
<Text size="xxs" numberOfLines={1} style={{ color: theme.colors.textDim }}>
|
|
106
|
+
{subline}
|
|
107
|
+
</Text>
|
|
108
|
+
) : null}
|
|
109
|
+
</View>
|
|
110
|
+
<ContractMenu appId={data.address} type="account" />
|
|
111
|
+
</View>
|
|
112
|
+
);
|
|
113
|
+
}}
|
|
114
|
+
</IdentityResolver>
|
|
115
|
+
|
|
116
|
+
<OnChainMetricGrid>
|
|
117
|
+
<OnChainMetric label="Balance" value={`${formatSupply(data.balance, 6)} ALGO`} />
|
|
118
|
+
<OnChainMetric label="Assets held" value={formatNumber(heldCount, 0)} />
|
|
119
|
+
</OnChainMetricGrid>
|
|
120
|
+
|
|
121
|
+
{badges ? (
|
|
122
|
+
<View style={{ gap: 8 }}>
|
|
123
|
+
<Text size="xxs" weight="medium" style={{ color: theme.colors.textDim }}>
|
|
124
|
+
Badges
|
|
125
|
+
</Text>
|
|
126
|
+
{badges}
|
|
127
|
+
</View>
|
|
128
|
+
) : null}
|
|
129
|
+
</View>
|
|
130
|
+
</CardEntrance>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Text as NativeText } from "react-native";
|
|
3
|
+
import * as Haptics from "expo-haptics";
|
|
4
|
+
import { IdentityResolver, useAppTheme, usePresentation } from "./runtime";
|
|
5
|
+
|
|
6
|
+
/** Native text preserves the surrounding chat line metrics. Navigation belongs to the host. */
|
|
7
|
+
export function AccountMentionInline({ address, onPress }: { address?: string; onPress?: (address: string) => void }) {
|
|
8
|
+
const { theme } = useAppTheme();
|
|
9
|
+
const { onOpenAccount } = usePresentation();
|
|
10
|
+
return (
|
|
11
|
+
<IdentityResolver address={address}>
|
|
12
|
+
{(identity) => {
|
|
13
|
+
const target = identity.address ?? address;
|
|
14
|
+
const tag =
|
|
15
|
+
identity.tag ||
|
|
16
|
+
(!address || address.length <= 12
|
|
17
|
+
? (address ?? "")
|
|
18
|
+
: `${address.slice(0, 6)}...${address.slice(-6)}`);
|
|
19
|
+
return (
|
|
20
|
+
<NativeText
|
|
21
|
+
accessibilityRole={target ? "link" : undefined}
|
|
22
|
+
onPress={
|
|
23
|
+
target && (onPress || onOpenAccount)
|
|
24
|
+
? () => {
|
|
25
|
+
Haptics.selectionAsync();
|
|
26
|
+
(onPress ?? onOpenAccount)?.(target);
|
|
27
|
+
}
|
|
28
|
+
: undefined
|
|
29
|
+
}
|
|
30
|
+
style={{ color: theme.colors.primary, fontWeight: "500" }}
|
|
31
|
+
>
|
|
32
|
+
@{tag.replace(/^@+/, "")}
|
|
33
|
+
</NativeText>
|
|
34
|
+
);
|
|
35
|
+
}}
|
|
36
|
+
</IdentityResolver>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import {
|
|
3
|
+
Pressable,
|
|
4
|
+
StyleSheet,
|
|
5
|
+
type LayoutChangeEvent,
|
|
6
|
+
type StyleProp,
|
|
7
|
+
type TextStyle,
|
|
8
|
+
} from "react-native"
|
|
9
|
+
|
|
10
|
+
import { AddressIdentityInline } from "./identity"
|
|
11
|
+
import { useAppTheme, usePresentation } from "./runtime"
|
|
12
|
+
|
|
13
|
+
export interface AccountMentionTextProps {
|
|
14
|
+
address: string
|
|
15
|
+
fallbackText?: string
|
|
16
|
+
onLayout?: (event: LayoutChangeEvent) => void
|
|
17
|
+
onLongPress?: (address: string) => void
|
|
18
|
+
onPress?: (address: string) => void
|
|
19
|
+
onPresentationTextChange?: (address: string, text: string) => void
|
|
20
|
+
style?: StyleProp<TextStyle>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function AccountMentionText({
|
|
24
|
+
address,
|
|
25
|
+
fallbackText,
|
|
26
|
+
onLayout,
|
|
27
|
+
onLongPress,
|
|
28
|
+
onPress,
|
|
29
|
+
onPresentationTextChange,
|
|
30
|
+
style,
|
|
31
|
+
}: AccountMentionTextProps) {
|
|
32
|
+
const { theme } = useAppTheme()
|
|
33
|
+
const presentation = usePresentation()
|
|
34
|
+
const handlePress = onPress ?? presentation.onOpenAccount
|
|
35
|
+
void fallbackText
|
|
36
|
+
|
|
37
|
+
const mention = (
|
|
38
|
+
<AddressIdentityInline
|
|
39
|
+
address={address}
|
|
40
|
+
iconSize={10}
|
|
41
|
+
numberOfLines={1}
|
|
42
|
+
containerStyle={[styles.pill, { backgroundColor: theme.colors.accentBackground }]}
|
|
43
|
+
onIdentityNameChange={(name) => onPresentationTextChange?.(address, name)}
|
|
44
|
+
onLayout={onLayout}
|
|
45
|
+
textStyle={[styles.text, style]}
|
|
46
|
+
/>
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
if (!handlePress && !onLongPress) return mention
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<Pressable
|
|
53
|
+
accessibilityRole="button"
|
|
54
|
+
accessibilityLabel="Open mentioned profile"
|
|
55
|
+
onLongPress={onLongPress ? () => onLongPress(address) : undefined}
|
|
56
|
+
onPress={handlePress ? () => handlePress(address) : undefined}
|
|
57
|
+
style={styles.pressable}
|
|
58
|
+
>
|
|
59
|
+
{mention}
|
|
60
|
+
</Pressable>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const styles = StyleSheet.create({
|
|
65
|
+
pill: {
|
|
66
|
+
alignSelf: "center",
|
|
67
|
+
borderRadius: 999,
|
|
68
|
+
marginLeft: 0,
|
|
69
|
+
marginRight: 2,
|
|
70
|
+
paddingHorizontal: 7,
|
|
71
|
+
paddingVertical: 2,
|
|
72
|
+
},
|
|
73
|
+
pressable: {
|
|
74
|
+
alignSelf: "center",
|
|
75
|
+
},
|
|
76
|
+
text: {
|
|
77
|
+
fontSize: 13,
|
|
78
|
+
lineHeight: 16,
|
|
79
|
+
},
|
|
80
|
+
})
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// Shared AlgoMD presentation, migrated from akita-rn. Edit this library source.
|
|
2
|
+
import { useCallback, useState } from "react";
|
|
3
|
+
import {
|
|
4
|
+
StyleSheet,
|
|
5
|
+
View,
|
|
6
|
+
useWindowDimensions,
|
|
7
|
+
type LayoutChangeEvent,
|
|
8
|
+
} from "react-native";
|
|
9
|
+
import { Image, type ImageLoadEventData } from "expo-image";
|
|
10
|
+
import { PhotoIcon } from "./icons";
|
|
11
|
+
|
|
12
|
+
import { createAkitaPlaintextMap } from "./media";
|
|
13
|
+
import { useAppTheme } from "./runtime";
|
|
14
|
+
import {
|
|
15
|
+
getMediaDimensions,
|
|
16
|
+
setMediaDimensions,
|
|
17
|
+
type MediaDimensions,
|
|
18
|
+
} from "./media";
|
|
19
|
+
|
|
20
|
+
type CachedDisplay = {
|
|
21
|
+
key: string;
|
|
22
|
+
dimensions: MediaDimensions;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const ADAPTIVE_IMAGE_CACHE_LIMIT = 250;
|
|
26
|
+
const adaptiveImageDimensionCache = createAkitaPlaintextMap<
|
|
27
|
+
string,
|
|
28
|
+
MediaDimensions
|
|
29
|
+
>("adaptive-image.dimensions");
|
|
30
|
+
const FALLBACK_ASPECT_RATIO = 4 / 3;
|
|
31
|
+
const MIN_FRAME_ASPECT_RATIO = 0.6;
|
|
32
|
+
const MAX_FRAME_ASPECT_RATIO = 2;
|
|
33
|
+
const ASPECT_RATIO_EPSILON = 0.001;
|
|
34
|
+
|
|
35
|
+
/** Embeds may live in a narrow post column even when the app window is wide. */
|
|
36
|
+
function useMediaContainerWidth() {
|
|
37
|
+
const { width: windowWidth } = useWindowDimensions();
|
|
38
|
+
const [measuredWidth, setMeasuredWidth] = useState(0);
|
|
39
|
+
const onLayout = useCallback((event: LayoutChangeEvent) => {
|
|
40
|
+
setMeasuredWidth(event.nativeEvent.layout.width);
|
|
41
|
+
}, []);
|
|
42
|
+
return { contentWidth: measuredWidth || windowWidth - 24, onLayout };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function rememberDimensions(uri: string, dimensions: MediaDimensions) {
|
|
46
|
+
adaptiveImageDimensionCache.delete(uri);
|
|
47
|
+
adaptiveImageDimensionCache.set(uri, dimensions);
|
|
48
|
+
if (adaptiveImageDimensionCache.size <= ADAPTIVE_IMAGE_CACHE_LIMIT) return;
|
|
49
|
+
|
|
50
|
+
const oldestKey = adaptiveImageDimensionCache.keys().next().value;
|
|
51
|
+
if (oldestKey !== undefined) adaptiveImageDimensionCache.delete(oldestKey);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function cachedDimensions(assetId: string | number | undefined, uri?: string) {
|
|
55
|
+
return (
|
|
56
|
+
(assetId != null ? getMediaDimensions(assetId) : undefined) ??
|
|
57
|
+
(uri ? adaptiveImageDimensionCache.get(uri) : undefined)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Clamps an aspect ratio so that the rendered height (at the given width)
|
|
63
|
+
* does not exceed maxHeight. Returns the original ratio if it fits.
|
|
64
|
+
*/
|
|
65
|
+
function clampRatio(
|
|
66
|
+
ratio: number,
|
|
67
|
+
containerWidth: number,
|
|
68
|
+
maxHeight: number,
|
|
69
|
+
): number {
|
|
70
|
+
const height = containerWidth / ratio;
|
|
71
|
+
if (height <= maxHeight) return ratio;
|
|
72
|
+
// containerWidth / newRatio = maxHeight → newRatio = containerWidth / maxHeight
|
|
73
|
+
return containerWidth / maxHeight;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Art headers never get shorter than 4:3, so a short window crops the art rather than letterboxing it. */
|
|
77
|
+
const FILL_MAX_ASPECT_RATIO = 4 / 3;
|
|
78
|
+
/** The most of an artwork a full-bleed header may crop away before it letterboxes instead. */
|
|
79
|
+
const FILL_MAX_CROP = 0.25;
|
|
80
|
+
|
|
81
|
+
export function getAdaptiveImageLayout({
|
|
82
|
+
dimensions,
|
|
83
|
+
containerWidth,
|
|
84
|
+
maxHeight,
|
|
85
|
+
fill = false,
|
|
86
|
+
}: {
|
|
87
|
+
dimensions?: MediaDimensions;
|
|
88
|
+
containerWidth: number;
|
|
89
|
+
maxHeight?: number;
|
|
90
|
+
/** Full-bleed header: always cover the frame edge to edge. */
|
|
91
|
+
fill?: boolean;
|
|
92
|
+
}): { aspectRatio: number; contentFit: "cover" | "contain" } {
|
|
93
|
+
const naturalAspectRatio = dimensions
|
|
94
|
+
? dimensions.width / dimensions.height
|
|
95
|
+
: undefined;
|
|
96
|
+
const safeNaturalAspectRatio = naturalAspectRatio
|
|
97
|
+
? Math.max(
|
|
98
|
+
MIN_FRAME_ASPECT_RATIO,
|
|
99
|
+
Math.min(MAX_FRAME_ASPECT_RATIO, naturalAspectRatio),
|
|
100
|
+
)
|
|
101
|
+
: FALLBACK_ASPECT_RATIO;
|
|
102
|
+
const clampedAspectRatio = maxHeight
|
|
103
|
+
? clampRatio(safeNaturalAspectRatio, containerWidth, maxHeight)
|
|
104
|
+
: safeNaturalAspectRatio;
|
|
105
|
+
if (fill) {
|
|
106
|
+
const frame = Math.min(
|
|
107
|
+
clampedAspectRatio,
|
|
108
|
+
Math.max(FILL_MAX_ASPECT_RATIO, safeNaturalAspectRatio),
|
|
109
|
+
);
|
|
110
|
+
// Cover while the crop stays mild; art far taller or wider than the frame is
|
|
111
|
+
// shown whole over its own blurred copy instead of losing its subject.
|
|
112
|
+
const cropped = naturalAspectRatio
|
|
113
|
+
? 1 -
|
|
114
|
+
Math.min(frame, naturalAspectRatio) /
|
|
115
|
+
Math.max(frame, naturalAspectRatio)
|
|
116
|
+
: 0;
|
|
117
|
+
return {
|
|
118
|
+
aspectRatio: frame,
|
|
119
|
+
contentFit: cropped <= FILL_MAX_CROP ? "cover" : "contain",
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const aspectRatio = clampedAspectRatio;
|
|
123
|
+
const frameMatchesImage =
|
|
124
|
+
naturalAspectRatio != null &&
|
|
125
|
+
Math.abs(aspectRatio - naturalAspectRatio) <= ASPECT_RATIO_EPSILON;
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
aspectRatio,
|
|
129
|
+
// Unknown or constrained artwork is contained so pixels are never stretched or cropped.
|
|
130
|
+
contentFit: frameMatchesImage ? "cover" : "contain",
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface AdaptiveImageProps {
|
|
135
|
+
uri: string;
|
|
136
|
+
assetId?: string | number;
|
|
137
|
+
/** Maximum rendered height in px. The stable frame is widened if needed to fit. */
|
|
138
|
+
maxHeight?: number;
|
|
139
|
+
borderRadiusTop?: number;
|
|
140
|
+
/**
|
|
141
|
+
* Full-bleed art header: the image covers the frame edge to edge instead of being
|
|
142
|
+
* letterboxed. A short window crops it, down to 4:3 at most.
|
|
143
|
+
*/
|
|
144
|
+
fill?: boolean;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Uses cached natural dimensions immediately when available, then records dimensions
|
|
149
|
+
* learned from the image load for future mounts.
|
|
150
|
+
*
|
|
151
|
+
* Cold-cache images start in a non-distorting contained fallback. Once measured,
|
|
152
|
+
* normal artwork uses its exact ratio; only extreme or height-constrained artwork
|
|
153
|
+
* remains contained inside a bounded frame.
|
|
154
|
+
*/
|
|
155
|
+
export function AdaptiveImage({
|
|
156
|
+
uri,
|
|
157
|
+
assetId,
|
|
158
|
+
maxHeight,
|
|
159
|
+
borderRadiusTop = 16,
|
|
160
|
+
fill = false,
|
|
161
|
+
}: AdaptiveImageProps) {
|
|
162
|
+
const { theme } = useAppTheme();
|
|
163
|
+
const { contentWidth, onLayout } = useMediaContainerWidth();
|
|
164
|
+
const displayKey = `${assetId ?? "no-asset"}:${uri}`;
|
|
165
|
+
const [loadedDisplay, setLoadedDisplay] = useState<CachedDisplay | null>(
|
|
166
|
+
() => {
|
|
167
|
+
const dimensions = cachedDimensions(assetId, uri);
|
|
168
|
+
return dimensions ? { key: displayKey, dimensions } : null;
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
// FlashList can reuse this instance for another embed, so never apply dimensions learned for
|
|
172
|
+
// a previous URI. Public asset dimensions are shared with the existing collectible cache;
|
|
173
|
+
// URI-keyed dimensions use the private-content lifecycle cache.
|
|
174
|
+
const dimensions =
|
|
175
|
+
loadedDisplay?.key === displayKey
|
|
176
|
+
? loadedDisplay.dimensions
|
|
177
|
+
: cachedDimensions(assetId, uri);
|
|
178
|
+
const layout = getAdaptiveImageLayout({
|
|
179
|
+
dimensions,
|
|
180
|
+
containerWidth: contentWidth,
|
|
181
|
+
maxHeight,
|
|
182
|
+
fill,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const handleLoad = useCallback(
|
|
186
|
+
(event: ImageLoadEventData) => {
|
|
187
|
+
const { width, height } = event.source;
|
|
188
|
+
if (width <= 0 || height <= 0) return;
|
|
189
|
+
|
|
190
|
+
const nextDimensions = { width, height };
|
|
191
|
+
rememberDimensions(uri, nextDimensions);
|
|
192
|
+
if (assetId != null) setMediaDimensions(assetId, nextDimensions);
|
|
193
|
+
setLoadedDisplay({ key: displayKey, dimensions: nextDimensions });
|
|
194
|
+
},
|
|
195
|
+
[assetId, displayKey, uri],
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<View
|
|
200
|
+
onLayout={onLayout}
|
|
201
|
+
style={[
|
|
202
|
+
styles.adaptiveImageContainer,
|
|
203
|
+
{
|
|
204
|
+
aspectRatio: layout.aspectRatio,
|
|
205
|
+
borderTopLeftRadius: borderRadiusTop,
|
|
206
|
+
borderTopRightRadius: borderRadiusTop,
|
|
207
|
+
backgroundColor: `${theme.colors.textDim}10`,
|
|
208
|
+
},
|
|
209
|
+
]}
|
|
210
|
+
>
|
|
211
|
+
<Image
|
|
212
|
+
source={{ uri }}
|
|
213
|
+
cachePolicy="memory-disk"
|
|
214
|
+
recyclingKey={`${uri}:blur`}
|
|
215
|
+
style={StyleSheet.absoluteFill}
|
|
216
|
+
contentFit="cover"
|
|
217
|
+
blurRadius={40}
|
|
218
|
+
/>
|
|
219
|
+
<View
|
|
220
|
+
style={[
|
|
221
|
+
StyleSheet.absoluteFill,
|
|
222
|
+
{ backgroundColor: `${theme.colors.background}60` },
|
|
223
|
+
]}
|
|
224
|
+
/>
|
|
225
|
+
<Image
|
|
226
|
+
source={{ uri }}
|
|
227
|
+
cachePolicy="memory-disk"
|
|
228
|
+
recyclingKey={`${uri}:foreground`}
|
|
229
|
+
style={StyleSheet.absoluteFill}
|
|
230
|
+
contentFit={layout.contentFit}
|
|
231
|
+
onLoad={handleLoad}
|
|
232
|
+
/>
|
|
233
|
+
</View>
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const styles = StyleSheet.create({
|
|
238
|
+
adaptiveImageContainer: {
|
|
239
|
+
overflow: "hidden",
|
|
240
|
+
width: "100%",
|
|
241
|
+
},
|
|
242
|
+
imagePlaceholder: {
|
|
243
|
+
alignItems: "center",
|
|
244
|
+
justifyContent: "center",
|
|
245
|
+
width: "100%",
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// Vertical space budget for fullscreen algomd components in the feed.
|
|
251
|
+
//
|
|
252
|
+
// Layout:
|
|
253
|
+
// topInset + banner (60) + header (64) = HEADER
|
|
254
|
+
// bottom reserved for reactions/actions = BOTTOM (180)
|
|
255
|
+
// available = windowHeight - HEADER - BOTTOM
|
|
256
|
+
//
|
|
257
|
+
// We estimate topInset at 59px (iPhone 14/15 family, most common).
|
|
258
|
+
// On devices with different insets the component simply has more or less
|
|
259
|
+
// breathing room — the image flexes, the rest stays fixed.
|
|
260
|
+
// ---------------------------------------------------------------------------
|
|
261
|
+
const ESTIMATED_TOP_INSET = 59;
|
|
262
|
+
const HEADER_HEIGHT = ESTIMATED_TOP_INSET + 60 + 64; // 183
|
|
263
|
+
const BOTTOM_RESERVED = 180;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Returns the max image height for a fullscreen algomd component,
|
|
267
|
+
* given the approximate non-image content height of the card.
|
|
268
|
+
*/
|
|
269
|
+
export function useImageBudget(nonImageHeight: number): number {
|
|
270
|
+
const { height: windowHeight } = useWindowDimensions();
|
|
271
|
+
const available = windowHeight - HEADER_HEIGHT - BOTTOM_RESERVED;
|
|
272
|
+
// Image gets whatever is left after the non-image content, with a floor
|
|
273
|
+
return Math.max(100, available - nonImageHeight);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface ImagePlaceholderProps {
|
|
277
|
+
assetId?: string | number;
|
|
278
|
+
maxHeight?: number;
|
|
279
|
+
borderRadiusTop?: number;
|
|
280
|
+
fill?: boolean;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function ImagePlaceholder({
|
|
284
|
+
assetId,
|
|
285
|
+
maxHeight,
|
|
286
|
+
borderRadiusTop = 16,
|
|
287
|
+
fill = false,
|
|
288
|
+
}: ImagePlaceholderProps) {
|
|
289
|
+
const { theme } = useAppTheme();
|
|
290
|
+
const { contentWidth, onLayout } = useMediaContainerWidth();
|
|
291
|
+
const layout = getAdaptiveImageLayout({
|
|
292
|
+
dimensions: assetId != null ? getMediaDimensions(assetId) : undefined,
|
|
293
|
+
containerWidth: contentWidth,
|
|
294
|
+
maxHeight,
|
|
295
|
+
fill,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
return (
|
|
299
|
+
<View
|
|
300
|
+
onLayout={onLayout}
|
|
301
|
+
style={[
|
|
302
|
+
styles.imagePlaceholder,
|
|
303
|
+
{
|
|
304
|
+
aspectRatio: layout.aspectRatio,
|
|
305
|
+
borderTopLeftRadius: borderRadiusTop,
|
|
306
|
+
borderTopRightRadius: borderRadiusTop,
|
|
307
|
+
backgroundColor: `${theme.colors.textDim}10`,
|
|
308
|
+
},
|
|
309
|
+
]}
|
|
310
|
+
>
|
|
311
|
+
<PhotoIcon size={32} color={theme.colors.textDim} />
|
|
312
|
+
</View>
|
|
313
|
+
);
|
|
314
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Pressable, StyleSheet, View, type LayoutChangeEvent, type StyleProp, type TextStyle } from "react-native";
|
|
3
|
+
import { Image } from "expo-image";
|
|
4
|
+
import { Text, useAppTheme } from "./runtime";
|
|
5
|
+
export interface AssetMentionProps {
|
|
6
|
+
label: string;
|
|
7
|
+
imageUrl?: string;
|
|
8
|
+
fallbackInitial?: string;
|
|
9
|
+
onLayout?: (event: LayoutChangeEvent) => void;
|
|
10
|
+
onPress?: () => void;
|
|
11
|
+
onLongPress?: () => void;
|
|
12
|
+
style?: StyleProp<TextStyle>;
|
|
13
|
+
}
|
|
14
|
+
/** The same asset pill used in the app's editor and post text. */
|
|
15
|
+
export function AssetMention({ label, imageUrl, fallbackInitial, onLayout, onPress, onLongPress, style }: AssetMentionProps) {
|
|
16
|
+
const { theme } = useAppTheme();
|
|
17
|
+
const mention = (
|
|
18
|
+
<View
|
|
19
|
+
onLayout={onLayout}
|
|
20
|
+
style={[styles.pill, { backgroundColor: theme.colors.accentBackground }]}
|
|
21
|
+
>
|
|
22
|
+
{imageUrl ? (
|
|
23
|
+
<Image source={{ uri: imageUrl }} contentFit="cover" style={styles.image} />
|
|
24
|
+
) : (
|
|
25
|
+
<View
|
|
26
|
+
style={[styles.image, styles.fallbackImage, { backgroundColor: theme.colors.border }]}
|
|
27
|
+
>
|
|
28
|
+
<Text size="xxs" weight="bold" style={{ color: theme.colors.textDim }}>
|
|
29
|
+
{fallbackInitial ?? label.slice(0, 1).toUpperCase()}
|
|
30
|
+
</Text>
|
|
31
|
+
</View>
|
|
32
|
+
)}
|
|
33
|
+
<Text
|
|
34
|
+
size="xxs"
|
|
35
|
+
numberOfLines={1}
|
|
36
|
+
style={[{ color: theme.colors.textDim }, styles.text, style]}
|
|
37
|
+
>
|
|
38
|
+
{label}
|
|
39
|
+
</Text>
|
|
40
|
+
</View>
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
if (!onPress && !onLongPress) return mention
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<Pressable
|
|
47
|
+
accessibilityRole="button"
|
|
48
|
+
accessibilityLabel="Open asset"
|
|
49
|
+
onLongPress={onLongPress ? () => onLongPress() : undefined}
|
|
50
|
+
onPress={onPress ? () => onPress() : undefined}
|
|
51
|
+
style={styles.pressable}
|
|
52
|
+
>
|
|
53
|
+
{mention}
|
|
54
|
+
</Pressable>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const styles = StyleSheet.create({
|
|
59
|
+
fallbackImage: {
|
|
60
|
+
alignItems: "center",
|
|
61
|
+
justifyContent: "center",
|
|
62
|
+
},
|
|
63
|
+
image: {
|
|
64
|
+
borderRadius: 8,
|
|
65
|
+
height: 16,
|
|
66
|
+
width: 16,
|
|
67
|
+
},
|
|
68
|
+
pill: {
|
|
69
|
+
alignItems: "center",
|
|
70
|
+
alignSelf: "center",
|
|
71
|
+
borderRadius: 999,
|
|
72
|
+
flexDirection: "row",
|
|
73
|
+
gap: 4,
|
|
74
|
+
marginLeft: 0,
|
|
75
|
+
marginRight: 2,
|
|
76
|
+
minWidth: 0,
|
|
77
|
+
paddingHorizontal: 7,
|
|
78
|
+
paddingVertical: 2,
|
|
79
|
+
},
|
|
80
|
+
pressable: {
|
|
81
|
+
alignSelf: "center",
|
|
82
|
+
},
|
|
83
|
+
text: {
|
|
84
|
+
fontSize: 13,
|
|
85
|
+
lineHeight: 16,
|
|
86
|
+
},
|
|
87
|
+
})
|