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
1 change: 1 addition & 0 deletions apps/mobile/src/features/chats/state/kinClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const refreshKinAccessMe = kin.refreshAccessMe;
export const refreshKinAgents = kin.refreshKinAgents;
export const refreshKinBriefs = kin.refreshKinBriefs;
export const refreshKinHost = kin.refreshKinHost;
export const refreshKinCacheDays = kin.refreshKinCacheDays;

const approvalRead = createKinRpcCommand(connectionAtomRuntime, {
label: "kin:approval:read",
Expand Down
87 changes: 87 additions & 0 deletions apps/mobile/src/features/usage/UsageKinCacheLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useAtomValue } from "@effect/atom-react";
import {
KIN_CACHE_DAILY_COPY,
kinCacheDayLine,
kinCacheLabel,
} from "@t3tools/client-runtime/kin/kinCacheDaily";
import type { EnvironmentId } from "@t3tools/contracts";
import * as Option from "effect/Option";
import { AsyncResult } from "effect/reactivity";
import { View } from "react-native";

import { AppText as Text } from "../../components/ui/AppText";
import { Button } from "../../components/ui/Button";
import { kinClient, refreshKinCacheDays } from "../chats/state/kinClient";

const LINE = "text-sm leading-5 text-foreground-muted";

/** Kin's cache line for each selected host whose owner reads it here; nothing for anyone else. */
export function UsageKinCacheLines(props: {
readonly environments: readonly {
readonly environmentId: EnvironmentId;
readonly label: string;
}[];
}) {
return props.environments.map((environment) => (
<KinCacheGate
key={environment.environmentId}
environmentId={environment.environmentId}
label={props.environments.length > 1 ? environment.label : null}
/>
));
}

function KinCacheGate(props: {
readonly environmentId: EnvironmentId;
readonly label: string | null;
}) {
const allowed = useAtomValue(kinClient.cacheDaily.permissionAtom(props.environmentId));
return allowed ? <KinCacheLines {...props} /> : null;
}

function DayLine(props: { readonly label: string; readonly body: string }) {
return (
<Text className={LINE}>
<Text className="text-sm leading-5 text-foreground">{props.label}:</Text> {props.body}
</Text>
);
}

function KinCacheLines(props: {
readonly environmentId: EnvironmentId;
readonly label: string | null;
}) {
const result = useAtomValue(
kinClient.cacheDays({ environmentId: props.environmentId, input: {} }),
);
const days = Option.getOrNull(AsyncResult.value(result));
const failed = days === null && AsyncResult.isFailure(result);
return (
<View className="gap-1">
{props.label === null ? null : (
<Text className="text-sm leading-5 font-medium text-foreground">{props.label}</Text>
)}
{days !== null ? (
[kinCacheDayLine("today", days.day), kinCacheDayLine("yesterday", days.before)].map(
(line) => <DayLine key={line.label} label={line.label} body={line.body} />,
)
) : failed ? (
<View className="flex-row flex-wrap items-center gap-3">
<Text className={LINE}>{KIN_CACHE_DAILY_COPY.failed}</Text>
<Button
tone="secondary"
size="compact"
label={KIN_CACHE_DAILY_COPY.retry}
onPress={() => refreshKinCacheDays(props.environmentId)}
/>
</View>
) : (
// The two lines' height, held while the first read is out, so the screen doesn't move.
<View accessibilityState={{ busy: true }} className="gap-1">
<DayLine label={kinCacheLabel("today")} body={KIN_CACHE_DAILY_COPY.reading} />
<DayLine label={kinCacheLabel("yesterday")} body={KIN_CACHE_DAILY_COPY.reading} />
</View>
)}
</View>
);
}
10 changes: 9 additions & 1 deletion apps/mobile/src/features/usage/UsageRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ import type { MessageResolver } from "../../i18n/messages";
import { SettingsScreen } from "../settings/components/SettingsScreen";
import { NativeWorkspaceTabActivityContext } from "../../native/v5-workspace-context";
import { useUsage, type EnvironmentUsageStatus } from "../../state/usage";
import { refreshKinCacheDays } from "../chats/state/kinClient";
import { SettingsSection } from "../settings/components/SettingsSection";
import { usageAxisDayLabel, usageAxisHourLabel } from "./usageAxisLabels";
import { UsageDailyChart } from "./UsageDailyChart";
import { UsageKinCacheLines } from "./UsageKinCacheLine";
import { toggleUsageEnvironment } from "./usageEnvironmentSelection";
import { useRefreshLimits } from "./UsageLimitsSection";
import { UsageLimitsSection } from "./UsageLimitsPooled";
Expand Down Expand Up @@ -261,12 +263,18 @@ function UsageScreen(props: {
showingLimits || canReadDiagnostics ? (
<RefreshControl
refreshing={showingLimits ? limits.refreshing : refreshingUsage}
onRefresh={showingLimits ? () => void limits.refresh() : refreshWindow}
onRefresh={() => {
for (const environment of selectedEnvironments)
refreshKinCacheDays(environment.environmentId);
if (showingLimits) void limits.refresh();
else refreshWindow();
}}
/>
) : undefined
}
>
<View className="w-full gap-6 self-center" style={{ maxWidth: MAX_CONTENT_WIDTH }}>
<UsageKinCacheLines environments={selectedEnvironments} />
{wide ? (
<View className="flex-row items-center gap-3">
{showingLimits ? (
Expand Down
Loading
Loading