Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

QuestionAnswerHistory.tsx

78 lines2.6 KB

1import type {
2 EnvironmentId,
3 UserInputAttachmentAnswerPayload,
4 UserInputAttachments,
5} from "@t3tools/contracts";
6import { Image } from "expo-image";
7import { Pressable, View } from "react-native";
8import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
9import { getQuestionAnswerText } from "@t3tools/client-runtime/work-log/user-input";
10import { AppText as Text } from "../../components/ui/AppText";
11import { assetImageSource } from "../../lib/assetImageSource";
12import { useAssetUrl } from "../../state/assets";
13​
14function AnswerFile(props: {
15 environmentId: EnvironmentId;
16 attachment: UserInputAttachments[string][number];
17}) {
18 const resource = { _tag: "attachment", attachmentId: props.attachment.id } as const;
19 const url = useAssetUrl(props.environmentId, resource);
20 return (
21 <Pressable
22 accessibilityRole="link"
23 accessibilityLabel={props.attachment.name}
24 disabled={!url}
25 onPress={() => {
26 if (url) void tryOpenExternalUrl(url);
27 }}
28 className="gap-1"
29 >
30 {props.attachment.type === "image" && url ? (
31 <Image
32 source={assetImageSource(props.environmentId, resource, url)}
33 cachePolicy="memory-disk"
34 style={{ width: 160, height: 96 }}
35 contentFit="contain"
36 />
37 ) : null}
38 <Text className="text-sm text-foreground underline">{props.attachment.name}</Text>
39 </Pressable>
40 );
42​
43export function QuestionAnswerHistory(props: {
44 environmentId: EnvironmentId;
45 answer: UserInputAttachmentAnswerPayload;
46}) {
47 return (
48 <View className="gap-2">
49 {[
50 ...new Set([
51 ...Object.keys(props.answer.questionTextById ?? {}),
52 ...Object.keys(props.answer.answers),
53 ...Object.keys(props.answer.attachmentsByQuestionId),
54 ]),
55 ].map((questionId) => (
56 <View key={questionId} className="gap-1">
57 {props.answer.questionTextById?.[questionId] ? (
58 <Text className="text-sm text-foreground-muted">
59 {props.answer.questionTextById[questionId]}
60 </Text>
61 ) : null}
62 {getQuestionAnswerText(props.answer.answers[questionId]) ? (
63 <Text className="ml-3 text-sm text-foreground-muted">
64 {getQuestionAnswerText(props.answer.answers[questionId])}
65 </Text>
66 ) : null}
67 {(props.answer.attachmentsByQuestionId[questionId] ?? []).map((attachment) => (
68 <AnswerFile
69 key={attachment.id}
70 environmentId={props.environmentId}
71 attachment={attachment}
72 />
73 ))}
74 </View>
75 ))}
76 </View>
77 );