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

QuestionAnswerHistory.tsx

72 lines2.3 KB

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