diff --git a/apps/mobile/src/features/chats/KinChatScreen.tsx b/apps/mobile/src/features/chats/KinChatScreen.tsx index cb0ad49c09369..17abb60a7fd27 100644 --- a/apps/mobile/src/features/chats/KinChatScreen.tsx +++ b/apps/mobile/src/features/chats/KinChatScreen.tsx @@ -94,7 +94,7 @@ import { type KinChatState, type KinInboxState, } from "./state/kinInboxModel"; -import { kinChatTitle, kinDirectAgentName } from "./state/kinScreenState"; +import { kinChatTitle, kinDirectAgentName, kinLevelsAgentId } from "./state/kinScreenState"; import { useKinHostAt } from "./state/useKinHost"; import { KinChatPushWatch } from "./notifications/KinChatPushWatch"; @@ -172,6 +172,9 @@ function KinLiveChat(props: { inbox?._tag === "Success" ? inbox.value.value.chats.find((chat) => chat.chatId === chatId) : undefined; + const canReadLevels = useAtomValue(kinClient.levelsRead.permissionAtom(props.environmentId)); + const levelsAgentId = + summary === undefined || !canReadLevels ? null : kinLevelsAgentId(summary, viewer.personId); const retry = () => { if (environmentId === null) return; @@ -190,6 +193,11 @@ function KinLiveChat(props: { chatId={chatId ?? (props.chatId as KinChatId)} title={summary === undefined ? "Chat" : kinChatTitle(summary, viewer.agentNames)} agentName={summary === undefined ? null : kinDirectAgentName(summary, viewer.agentNames)} + levels={ + levelsAgentId === null + ? null + : { environmentId: props.environmentId, orgId: props.orgId, agentId: levelsAgentId } + } unread={summary?.unread ?? null} markRead={ environmentId === null || chatId === null @@ -263,6 +271,12 @@ function KinChatPage(props: { readonly title: string; /** The agent a direct chat is with, for the empty chat's copy; null when unknown. */ readonly agentName: string | null; + /** The lead's Levels screen, for its owner in their direct chat; null elsewhere. */ + readonly levels?: { + readonly environmentId: string; + readonly orgId: string; + readonly agentId: string; + } | null; /** The inbox's unread count for this chat; null until the inbox loads. */ readonly unread: number | null; /** Null where nothing can be marked, such as the dev fixture. */ @@ -277,9 +291,25 @@ function KinChatPage(props: { const topInset = useContentTopInset(); const { state } = props; const subtitle = state._tag === "ready" ? kinChatStatusLine(state.chat.value.widgets) : null; + const { levels } = props; return ( - navigation.goBack()} /> + navigation.goBack()} + actions={ + levels == null + ? undefined + : [ + { + accessibilityLabel: "Levels", + icon: "slider.horizontal.3", + onPress: () => navigation.navigate("KinLevels", levels), + }, + ] + } + /> {state._tag === "ready" ? ( ) { + const navigation = useNavigation(); + const environmentId = Option.getOrNull(decodeEnvironmentId(route.params.environmentId)); + const orgId = Option.getOrNull(decodeOrgId(route.params.orgId)); + const agentId = Option.getOrNull(decodeAgentId(route.params.agentId)); + return ( + + navigation.goBack()} /> + + + {environmentId === null || orgId === null || agentId === null ? ( + + ) : ( + + )} + + + + ); +} + +function KinLevelsBody(props: { + readonly environmentId: EnvironmentId; + readonly input: KinLevelsGetInput; +}) { + const { environmentId, input } = props; + const atom = kinClient.levels({ environmentId, input }); + const result = useAtomValue(atom); + const host = useKinHostAt(environmentId); + const { agentNames } = kinDirectory(host._tag === "unpaired" ? { _tag: "none" } : host); + const inbox = useAtomValue(kinClient.inbox({ environmentId, input: {} })); + const canChange = useAtomValue(kinClient.lower.permissionAtom(environmentId)); + const lower = useAtomCommand(kinClient.lower, { reportFailure: false }); + const raise = useAtomCommand(kinClient.raise, { reportFailure: false }); + const [pending, setPending] = useState(null); + const [notice, setNotice] = useState<{ readonly text: string; readonly error: boolean } | null>( + null, + ); + + if (AsyncResult.isInitial(result)) { + return ( + + + + ); + } + if (AsyncResult.isFailure(result)) { + const cause = Cause.squash(result.cause); + if (isKinChatError(cause) && cause.reason === "not_found") { + return ( + + ); + } + if (isKinChatError(cause) && cause.reason === "not_allowed") { + return ( + + ); + } + return ( + + + +