Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions apps/mobile/src/features/chats/KinChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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. */
Expand All @@ -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 (
<View collapsable={false} className="flex-1 bg-screen">
<AppBar title={props.title} subtitle={subtitle} onBack={() => navigation.goBack()} />
<AppBar
title={props.title}
subtitle={subtitle}
onBack={() => navigation.goBack()}
actions={
levels == null
? undefined
: [
{
accessibilityLabel: "Levels",
icon: "slider.horizontal.3",
onPress: () => navigation.navigate("KinLevels", levels),
},
]
}
/>
<ScreenContent surface="screen">
{state._tag === "ready" ? (
<KinChatFeed
Expand Down
290 changes: 290 additions & 0 deletions apps/mobile/src/features/chats/KinLevelsScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
import { useAtomValue } from "@effect/atom-react";
import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
import {
EnvironmentId,
KinAgentId,
KinChatError,
KinOrgId,
type KinLevelsGetInput,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import { AsyncResult } from "effect/reactivity";
import { useState } from "react";
import { Alert, View } from "react-native";

import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
import { AppBar } from "../../components/ui/AppBar";
import { AppText as Text } from "../../components/ui/AppText";
import { Button } from "../../components/ui/Button";
import { EmptyState } from "../../components/ui/EmptyState";
import { ErrorBanner } from "../../components/ui/ErrorBanner";
import { ScreenContent } from "../../components/ui/ScreenContent";
import { ScreenScrollView } from "../../components/ui/ScreenScrollView";
import { appAtomRegistry } from "../../state/atom-registry";
import { useAtomCommand } from "../../state/use-atom-command";
import { kinClient } from "./state/kinClient";
import { kinChatTitle, kinDirectory } from "./state/kinScreenState";
import { useKinHostAt } from "./state/useKinHost";
import {
kinLevelsFailure,
kinLevelsFailureText,
kinLevelsScreenView,
type KinLevelChange,
type KinLevelsRow,
} from "./widgets/kinLevelsPresentation";

type KinLevelsParams = {
readonly environmentId: string;
readonly orgId: string;
readonly agentId: string;
};

const decodeEnvironmentId = Schema.decodeUnknownOption(EnvironmentId);
const decodeOrgId = Schema.decodeUnknownOption(KinOrgId);
const decodeAgentId = Schema.decodeUnknownOption(KinAgentId);
const isKinChatError = Schema.is(KinChatError);

/** The owner's view of one lead's levels: its own, each chat's cap, the trial and open raises. */
export function KinLevelsScreen({ route }: StaticScreenProps<KinLevelsParams>) {
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 (
<View collapsable={false} className="flex-1 bg-screen">
<AppBar title="Levels" onBack={() => navigation.goBack()} />
<ScreenContent surface="screen">
<ScreenScrollView
className="flex-1"
contentInsetAdjustmentBehavior="automatic"
contentContainerStyle={{ padding: 16, gap: 16 }}
>
{environmentId === null || orgId === null || agentId === null ? (
<EmptyState
variant="plain"
title="This link doesn't name a lead."
detail="Open Levels from your lead's chat."
/>
) : (
<KinLevelsBody environmentId={environmentId} input={{ orgId, agentId }} />
)}
</ScreenScrollView>
</ScreenContent>
</View>
);
}

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<string | null>(null);
const [notice, setNotice] = useState<{ readonly text: string; readonly error: boolean } | null>(
null,
);

if (AsyncResult.isInitial(result)) {
return (
<View className="items-center py-12" accessible accessibilityLabel="Loading levels">
<AppActivityIndicator />
</View>
);
}
if (AsyncResult.isFailure(result)) {
const cause = Cause.squash(result.cause);
if (isKinChatError(cause) && cause.reason === "not_found") {
return (
<EmptyState
variant="plain"
title="This agent has no levels."
detail="Levels belong to a lead. Open them from your lead's chat."
/>
);
}
if (isKinChatError(cause) && cause.reason === "not_allowed") {
return (
<EmptyState
variant="plain"
title="Only a lead's owner can see its levels."
detail="These show for a lead you own. Ask whoever set up this one."
/>
);
}
return (
<View className="gap-3">
<ErrorBanner message={kinLevelsFailureText(cause)} />
<View className="self-start">
<Button
tone="secondary"
size="compact"
label="Retry"
loading={result.waiting}
onPress={() => appAtomRegistry.refresh(atom)}
/>
</View>
</View>
);
}

const chats = inbox?._tag === "Success" ? inbox.value.value.chats : [];
const view = kinLevelsScreenView(result.value, (chatId) => {
const chat = chats.find((candidate) => candidate.chatId === chatId);
return chat === undefined ? "A chat" : kinChatTitle(chat, agentNames);
});
const name = agentNames.get(input.agentId) ?? "This lead";

const apply = async (change: KinLevelChange) => {
if (pending !== null || result.waiting) return;
setPending(change.key);
setNotice(null);
const target = {
...input,
level: change.level,
...(change.chatId === null ? {} : { chatId: change.chatId }),
};
const outcome =
change.kind === "lower"
? await lower({ environmentId, input: target })
: await raise({ environmentId, input: target });
const failure = kinLevelsFailure(outcome);
if (failure !== null) {
setPending(null);
setNotice({ text: kinLevelsFailureText(failure), error: true });
return;
}
// Refresh first: the rows stay disabled while it is waiting, so no tap sends a stale level.
appAtomRegistry.refresh(atom);
setPending(null);
setNotice({
text:
change.kind === "lower"
? `Lowered to ${change.level}.`
: `Raise to ${change.level} requested. It applies once you sign the approval.`,
error: false,
});
};

const press = (change: KinLevelChange, row: KinLevelsRow) => {
if (change.kind === "raise") return void apply(change);
// Lowering applies at once, and getting back up takes a signed raise.
Alert.alert(
`Lower ${row.key === "lead" ? name : row.title} to ${change.level}?`,
row.key === "lead"
? "This applies now. Raising it again takes another trial and your signed approval."
: "This applies now. Raising it again needs your signed approval.",
[
{ text: "Cancel", style: "cancel" },
{ text: change.label, style: "destructive", onPress: () => void apply(change) },
],
);
};

const renderRow = (row: KinLevelsRow, title: string) => (
<View key={row.key} className="gap-2 rounded-[20px] border border-border bg-card-alt p-4">
<View accessible className="flex-row items-baseline justify-between gap-3">
<Text textRole="headline" className="min-w-0 shrink font-t3-bold text-foreground">
{title}
</Text>
<Text textRole="headline" className="font-t3-bold text-foreground">
{row.level}
</Text>
</View>
<Text textRole="caption" className="text-foreground-muted">
{row.since}
</Text>
{canChange && row.changes.length > 0 ? (
<View className="flex-row flex-wrap gap-2 pt-1">
{row.changes.map((change) => (
<Button
key={change.key}
size="compact"
tone={change.kind === "raise" ? "primary" : "secondary"}
label={change.label}
accessibilityHint={change.blocked ?? undefined}
loading={pending === change.key}
disabled={
change.blocked !== null ||
result.waiting ||
(pending !== null && pending !== change.key)
}
onPress={() => press(change, row)}
/>
))}
</View>
) : null}
{row.changes
.filter((change) => change.blocked !== null)
.map((change) => (
<Text key={change.key} textRole="caption" className="text-foreground-muted">
{`${change.label}: ${change.blocked}`}
</Text>
))}
</View>
);

return (
<>
{notice === null ? null : (
<Text
accessibilityRole={notice.error ? "alert" : undefined}
accessibilityLiveRegion="polite"
textRole="footnote"
className={
notice.error
? "font-t3-medium text-danger-foreground"
: "font-t3-medium text-foreground-secondary"
}
>
{notice.text}
</Text>
)}
{renderRow(view.lead, name)}
<View className="gap-1 px-1">
<Text textRole="footnote" className="font-t3-medium text-foreground-secondary">
Trial
</Text>
{view.trial.map((line) => (
<Text key={line} textRole="footnote" className="text-foreground-muted">
{line}
</Text>
))}
</View>
{view.pending.length === 0 ? null : (
<View className="gap-1 px-1">
<Text textRole="footnote" className="font-t3-medium text-foreground-secondary">
Waiting raises
</Text>
{view.pending.map((item) => (
<Text key={item.key} textRole="footnote" className="text-foreground-muted">
{item.text}
</Text>
))}
</View>
)}
{view.chats.length === 0 ? null : (
<View className="gap-3">
<Text textRole="footnote" className="px-1 font-t3-medium text-foreground-secondary">
Chats
</Text>
{view.chats.map((row) => renderRow(row, row.title))}
</View>
)}
{canChange ? null : (
<Text textRole="footnote" className="px-1 text-foreground-muted">
This device can't change levels. Pair it again from the Mac.
</Text>
)}
</>
);
}
6 changes: 6 additions & 0 deletions apps/mobile/src/features/chats/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createNativeStackScreen } from "@react-navigation/native-stack";
import { pageRoute } from "../layout/route-options";
import { KinChatScreen } from "./KinChatScreen";
import { KinChatsScreen } from "./KinChatsScreen";
import { KinLevelsScreen } from "./KinLevelsScreen";
import { KinWaitingScreen } from "./KinWaitingScreen";

/** Kin's root-stack routes, spread into `Stack.tsx`. Kin files must never import `Stack.tsx`. */
Expand All @@ -23,4 +24,9 @@ export const KIN_SCREENS = {
linking: "kin/:environmentId/:orgId/chats/:chatId",
options: pageRoute({ duoMinimize: "onScrollUp" }),
}),
KinLevels: createNativeStackScreen({
screen: KinLevelsScreen,
linking: "kin/:environmentId/:orgId/levels/:agentId",
options: pageRoute({ title: "Levels" }),
}),
};
Loading