|
| 1 | +import { FC, useMemo } from "react"; |
| 2 | +import { View } from "react-native"; |
| 3 | +import { useAssistantToolUI } from "@creatorem/ai-chat"; |
| 4 | +import type { ToolCallMessagePartProps } from "@creatorem/ai-chat/types/message-part-component-types"; |
| 5 | +import { Text } from "../ui/text"; |
| 6 | +import { cn } from "~/utils/cn"; |
| 7 | + |
| 8 | +const HOUR_LABELS = ["1PM", "2PM", "3PM", "4PM", "5PM"] as const; |
| 9 | + |
| 10 | +const toCelsius = (fahrenheit: number) => |
| 11 | + Math.round(((fahrenheit ?? 0) - 32) * (5 / 9)); |
| 12 | + |
| 13 | +const getConditionEmoji = (condition?: string) => { |
| 14 | + const value = (condition ?? "").toLowerCase(); |
| 15 | + if (value.includes("rain") || value.includes("storm")) return "🌧️"; |
| 16 | + if (value.includes("snow") || value.includes("sleet")) return "❄️"; |
| 17 | + if (value.includes("cloud") || value.includes("overcast")) return "☁️"; |
| 18 | + if (value.includes("fog")) return "🌫️"; |
| 19 | + if (value.includes("wind")) return "💨"; |
| 20 | + return "☀️"; |
| 21 | +}; |
| 22 | + |
| 23 | +type ForecastDay = { |
| 24 | + date: string; |
| 25 | + temperatureF: number; |
| 26 | + condition: string; |
| 27 | + sunlightHours: number; |
| 28 | +}; |
| 29 | + |
| 30 | +const WeatherToolCard: FC<ToolCallMessagePartProps> = ({ |
| 31 | + args, |
| 32 | + result, |
| 33 | + status, |
| 34 | +}) => { |
| 35 | + const forecast = (result as any)?.forecast as ForecastDay[] | undefined; |
| 36 | + const location = (result as any)?.location ?? (args as any)?.location ?? "—"; |
| 37 | + const range = (result as any)?.range as |
| 38 | + | { startDate: string; endDate: string } |
| 39 | + | undefined; |
| 40 | + |
| 41 | + const temps = forecast?.map((day) => day.temperatureF) ?? []; |
| 42 | + const highF = temps.length ? Math.max(...temps) : undefined; |
| 43 | + const lowF = temps.length ? Math.min(...temps) : undefined; |
| 44 | + const currentF = forecast?.[0]?.temperatureF; |
| 45 | + const currentCondition = forecast?.[0]?.condition; |
| 46 | + |
| 47 | + const hourly = useMemo( |
| 48 | + () => |
| 49 | + forecast |
| 50 | + ? forecast.slice(0, HOUR_LABELS.length).map((day, index) => ({ |
| 51 | + label: HOUR_LABELS[index], |
| 52 | + tempC: toCelsius(day.temperatureF), |
| 53 | + condition: day.condition, |
| 54 | + })) |
| 55 | + : [], |
| 56 | + [forecast], |
| 57 | + ); |
| 58 | + |
| 59 | + return ( |
| 60 | + <View className="mb-4 w-full rounded-3xl border border-sky-200/50 bg-sky-500 p-4"> |
| 61 | + <View className="flex-row items-start justify-between"> |
| 62 | + <View className="flex-row items-center gap-3"> |
| 63 | + <View className="h-12 w-12 items-center justify-center rounded-full bg-white/20"> |
| 64 | + <Text className="text-2xl"> |
| 65 | + {getConditionEmoji(currentCondition)} |
| 66 | + </Text> |
| 67 | + </View> |
| 68 | + <View> |
| 69 | + <Text className="font-semibold text-3xl text-white"> |
| 70 | + {currentF !== undefined ? `${toCelsius(currentF)}°C` : "--"} |
| 71 | + </Text> |
| 72 | + <Text className="text-sm text-white/85">{location}</Text> |
| 73 | + </View> |
| 74 | + </View> |
| 75 | + <View> |
| 76 | + <Text className="text-right text-sm text-white/85"> |
| 77 | + H:{highF !== undefined ? `${toCelsius(highF)}°` : "--"} |
| 78 | + </Text> |
| 79 | + <Text className="text-right text-sm text-white/85"> |
| 80 | + L:{lowF !== undefined ? `${toCelsius(lowF)}°` : "--"} |
| 81 | + </Text> |
| 82 | + </View> |
| 83 | + </View> |
| 84 | + |
| 85 | + {range && ( |
| 86 | + <Text className="mt-1 text-white/75 text-xs"> |
| 87 | + {range.startDate} - {range.endDate} |
| 88 | + </Text> |
| 89 | + )} |
| 90 | + |
| 91 | + {!forecast && ( |
| 92 | + <Text className="mt-3 text-sm text-white/85"> |
| 93 | + {status.type === "running" |
| 94 | + ? "Fetching forecast..." |
| 95 | + : "No forecast yet."} |
| 96 | + </Text> |
| 97 | + )} |
| 98 | + |
| 99 | + {forecast && ( |
| 100 | + <View className="mt-4 flex-row justify-between gap-2"> |
| 101 | + {hourly.map((slot) => ( |
| 102 | + <View |
| 103 | + key={slot.label} |
| 104 | + className={cn( |
| 105 | + "min-w-[58px] flex-1 items-center rounded-2xl bg-white/15 px-2 py-3", |
| 106 | + )} |
| 107 | + > |
| 108 | + <Text className="text-white/80 text-xs">{slot.label}</Text> |
| 109 | + <Text className="my-1 text-xl"> |
| 110 | + {getConditionEmoji(slot.condition)} |
| 111 | + </Text> |
| 112 | + <Text className="font-medium text-sm text-white"> |
| 113 | + {slot.tempC}°C |
| 114 | + </Text> |
| 115 | + </View> |
| 116 | + ))} |
| 117 | + </View> |
| 118 | + )} |
| 119 | + </View> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +export const WeatherToolRegistration: FC = () => { |
| 124 | + useAssistantToolUI({ |
| 125 | + toolName: "weather", |
| 126 | + render: WeatherToolCard, |
| 127 | + }); |
| 128 | + return null; |
| 129 | +}; |
0 commit comments