diff --git a/apps/mobile/src/features/chats/KinRulesScreen.tsx b/apps/mobile/src/features/chats/KinRulesScreen.tsx new file mode 100644 index 000000000000..f8e29c31bee6 --- /dev/null +++ b/apps/mobile/src/features/chats/KinRulesScreen.tsx @@ -0,0 +1,301 @@ +import { useAtomValue } from "@effect/atom-react"; +import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; +import { KIN_RETRY } from "@t3tools/client-runtime/kin/kinAnswers"; +import { KIN_THIS_PHONE_COPY } from "@t3tools/client-runtime/kin/kinDevice"; +import { + KIN_RULES_COPY, + KIN_RULES_NO_EDITS, + createKinRulesEditor, + kinRulesProblem, + kinRulesRead, + kinRulesView, + type KinRulesView, +} from "@t3tools/client-runtime/kin/kinRules"; +import { KIN_FORM_DISCARD } from "@t3tools/client-runtime/kin/kinWorkForms"; +import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; +import { EnvironmentId, KinOrgId, type KinMemoryRules } from "@t3tools/contracts"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import { AsyncResult, Atom } from "effect/reactivity"; +import * as Clipboard from "expo-clipboard"; +import { useState } from "react"; +import { View } from "react-native"; + +import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; +import { AppText as Text } from "../../components/ui/AppText"; +import { Button } from "../../components/ui/Button"; +import * as Dialog from "../../components/ui/Dialog"; +import { EmptyState } from "../../components/ui/EmptyState"; +import { ListSection } from "../../components/ui/ListSection"; +import { Notice } from "../../components/ui/Notice"; +import { appAtomRegistry } from "../../state/atom-registry"; +import { useAtomCommand } from "../../state/use-atom-command"; +import { SettingsRow } from "../settings/components/SettingsRow"; +import { + KinGateView, + KinWorkFrame, + KinWorkTextField, + useKinAskBeforeDiscard, +} from "./components/KinWorkPieces"; +import { kinClient } from "./state/kinClient"; +import { useKinGate } from "./state/useKinHost"; + +export type KinRulesParams = { readonly environmentId: string; readonly orgId: string }; + +interface KinRulesTarget { + readonly environmentId: EnvironmentId; + readonly orgId: KinOrgId; +} + +const decodeEnvironmentId = Schema.decodeUnknownOption(EnvironmentId); +const decodeOrgId = Schema.decodeUnknownOption(KinOrgId); + +type RulesResult = AsyncResult.AsyncResult; +// Read only by the owner's session; anyone else asks the host nothing. +const noRulesAtom: Atom.Atom = Atom.make(AsyncResult.initial()); + +/** Settings' way in, for the owner alone: absent for everyone else. */ +export function KinRulesSettingsRow(props: KinRulesTarget) { + const navigation = useNavigation(); + const allowed = useAtomValue(kinClient.memorySaveRules.permissionAtom(props.environmentId)); + if (!allowed) return null; + return ( + navigation.navigate("KinRules", props)} + /> + ); +} + +/** The standing rules every agent follows: the owner's alone, to read and to change. */ +export function KinRulesScreen({ route }: StaticScreenProps) { + const navigation = useNavigation(); + const environmentId = Option.getOrNull(decodeEnvironmentId(route.params.environmentId)); + const orgId = Option.getOrNull(decodeOrgId(route.params.orgId)); + if (environmentId === null || orgId === null) + return ( + navigation.goBack()}> + + + ); + return ; +} + +function KinRulesBody({ environmentId, orgId }: KinRulesTarget) { + const navigation = useNavigation(); + const gate = useKinGate( + kinClient.memorySaveRules.permissionStateAtom(environmentId), + environmentId, + ); + const rulesAtom = kinClient.memoryRules({ environmentId, input: { orgId } }); + const result = useAtomValue(gate === null ? rulesAtom : noRulesAtom); + const saveRules = useAtomCommand(kinClient.memorySaveRules, { reportFailure: false }); + const [edits, setEdits] = useState(KIN_RULES_NO_EDITS); + const [editor] = useState(() => + createKinRulesEditor({ + orgId, + device: "phone", + save: (input) => saveRules({ environmentId, input }), + update: setEdits, + refresh: () => appAtomRegistry.refresh(rulesAtom), + }), + ); + // What was last read stays up while it is read again, and the draft stays over it. + const read = kinRulesRead(result); + const view = read === null ? null : kinRulesView(read, edits); + // Kept while saving too: a save that fails or finds the rules changed leaves the draft here. + const guarded = view !== null && view.dirty; + useKinAskBeforeDiscard(guarded); + + const save = async (confirmed = false) => { + if (read === null || view === null) return; + const ask = view.ask; + if ((await editor.save(read, edits, confirmed)) !== "ask" || ask === null) return; + Dialog.alert(ask.title, ask.detail, [ + { text: KIN_FORM_DISCARD.keep, style: "cancel" }, + { + text: ask.action, + style: "destructive", + onPress: () => void save(true), + }, + ]); + }; + + return ( + navigation.goBack()} + {...(view === null + ? {} + : { + confirm: { + label: view.saving ? KIN_RULES_COPY.saving : view.saveLabel, + onPress: () => void save(), + loading: view.saving, + disabled: !view.canSave, + }, + })} + > + {gate !== null ? ( + + ) : read === null || view === null ? ( + AsyncResult.isFailure(result) ? ( + appAtomRegistry.refresh(rulesAtom)} /> + ) : ( + + + + ) + ) : ( + <> + + {view.empty ? KIN_RULES_COPY.empty : KIN_RULES_COPY.purpose} + + {view.replacing === null ? null : ( + + )} + {view.problem === null ? null : ( + navigation.navigate("KinThisPhone"), + }, + } + : {})} + /> + )} + {view.changed === null ? null : ( + editor.takeCurrent(read)} + /> + )} + + editor.type(view.held, text)} + /> + + {view.facts.length === 0 ? null : ( + + {view.facts.join("\n")} + + )} + + )} + + ); +} + +function KinRulesLoadFailed(props: { + readonly result: AsyncResult.Failure; + readonly onRetry: () => void; +}) { + const navigation = useNavigation(); + const problem = kinRulesProblem(squashAtomCommandFailure(props.result), "", "phone"); + return ( + } + : problem.kind === "pair_again" + ? { + action: ( +