From 7890c216ffa5736c9f7b949108cc8e17875b98a9 Mon Sep 17 00:00:00 2001 From: listlessbird <124798751+listlessbird@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:41:55 +0530 Subject: [PATCH 1/2] feat: Kindle control panel reports the device and queues commands --- apps/mobile/src/Stack.tsx | 28 ++- .../src/features/kindle/KindleScreen.tsx | 207 ++++++++++++++++++ .../src/features/kindle/kindleCopy.test.ts | 85 +++++++ apps/mobile/src/features/kindle/kindleCopy.ts | 109 +++++++++ .../src/features/library/libraryCopy.ts | 27 ++- apps/mobile/src/features/sending/book.ts | 7 +- .../src/features/sending/sender.test.ts | 11 +- .../src/features/settings/SettingsScreen.tsx | 19 +- apps/mobile/src/lib/mailboxClient.ts | 21 ++ apps/mobile/src/lib/units.ts | 3 + apps/mobile/src/state/connection.test.ts | 9 +- apps/mobile/src/state/kindle.test.ts | 102 +++++++++ apps/mobile/src/state/kindle.ts | 50 +++++ apps/worker/package.json | 2 +- apps/worker/src/Api.ts | 132 ++++++++++- apps/worker/src/Mailbox.ts | 114 +++++++++- apps/worker/src/Worker.ts | 29 ++- apps/worker/test/Mailbox.test.ts | 157 ++++++++++++- pnpm-lock.yaml | 9 +- 19 files changed, 1066 insertions(+), 55 deletions(-) create mode 100644 apps/mobile/src/features/kindle/KindleScreen.tsx create mode 100644 apps/mobile/src/features/kindle/kindleCopy.test.ts create mode 100644 apps/mobile/src/features/kindle/kindleCopy.ts create mode 100644 apps/mobile/src/state/kindle.test.ts create mode 100644 apps/mobile/src/state/kindle.ts diff --git a/apps/mobile/src/Stack.tsx b/apps/mobile/src/Stack.tsx index 94e2c43..b27e807 100644 --- a/apps/mobile/src/Stack.tsx +++ b/apps/mobile/src/Stack.tsx @@ -10,27 +10,36 @@ import { createNativeStackNavigator } from "@react-navigation/native-stack"; import { useAtomValue } from "@effect/atom-react"; import * as Option from "effect/Option"; import { AsyncResult } from "effect/reactivity"; +import { View } from "react-native"; import { IconButton } from "./components/ui/IconButton"; import { ConnectScreen } from "./features/connect/ConnectScreen"; import { EnterDetailsScreen } from "./features/connect/EnterDetailsScreen"; import { ScanPairingCodeScreen } from "./features/connect/ScanPairingCodeScreen"; import { ComponentCatalogScreen } from "./features/development/ComponentCatalogScreen"; +import { KindleScreen } from "./features/kindle/KindleScreen"; import { BookScreen } from "./features/library/BookScreen"; import { LibraryScreen } from "./features/library/LibraryScreen"; import { useAppearance } from "./features/settings/appearance/AppearanceProvider"; import { SettingsScreen } from "./features/settings/SettingsScreen"; import { connectionAtom } from "./state/connection"; -function SettingsButton() { +function LibraryHeaderButtons() { const navigation = useNavigation(); return ( - navigation.navigate("Settings")} - /> + + navigation.navigate("Kindle")} + /> + navigation.navigate("Settings")} + /> + ); } @@ -64,13 +73,18 @@ const RootStack = createNativeStackNavigator({ Library: { screen: LibraryScreen, linking: "", - options: { title: "Dropcap", headerRight: SettingsButton }, + options: { title: "Dropcap", headerRight: LibraryHeaderButtons }, }, Book: { screen: BookScreen, linking: "book/:id", options: { title: "" }, }, + Kindle: { + screen: KindleScreen, + linking: "kindle", + options: { title: "Kindle" }, + }, Settings: { screen: SettingsScreen, linking: "settings", diff --git a/apps/mobile/src/features/kindle/KindleScreen.tsx b/apps/mobile/src/features/kindle/KindleScreen.tsx new file mode 100644 index 0000000..9062928 --- /dev/null +++ b/apps/mobile/src/features/kindle/KindleScreen.tsx @@ -0,0 +1,207 @@ +import { useAtom, useAtomRefresh, useAtomValue } from "@effect/atom-react"; +import { + Command, + type BookPath, + type CommandId, + type Device, + type QueuedCommand, +} from "@kindle-drop/worker/Api"; +import { useFocusEffect } from "@react-navigation/native"; +import type * as DateTime from "effect/DateTime"; +import * as Option from "effect/Option"; +import { AsyncResult } from "effect/reactivity"; +import { ScrollView, View } from "react-native"; +import { useSafeAreaInsets } from "react-native-safe-area-context"; + +import { Button } from "../../components/ui/Button"; +import * as Dialog from "../../components/ui/Dialog"; +import { EmptyState } from "../../components/ui/EmptyState"; +import { ErrorBanner } from "../../components/ui/ErrorBanner"; +import { ListActionRow } from "../../components/ui/ListActionRow"; +import { ListRowLayout } from "../../components/ui/ListRowLayout"; +import { ListSection } from "../../components/ui/ListSection"; +import { Notice } from "../../components/ui/Notice"; +import { booksState } from "../../state/books"; +import { nowAtom } from "../../state/clock"; +import { kindleState } from "../../state/kindle"; +import { statusError } from "../library/libraryCopy"; +import { + commandError, + deviceSummary, + failures, + libraryEntries, + rescanPending, + type LibraryEntry, +} from "./kindleCopy"; + +function DeviceSection(props: { readonly device: Device; readonly now: DateTime.Utc }) { + const { reported, battery, free, firmware } = deviceSummary(props.device, props.now); + + return ( + + + {Option.isSome(battery) ? : null} + {Option.isSome(free) ? : null} + {Option.isSome(firmware) ? : null} + + ); +} + +function FailureNotice(props: { readonly id: CommandId; readonly text: string }) { + const [dismissed, dismiss] = useAtom(kindleState.dismissAtom(props.id)); + + return ( + dismiss()} + /> + ); +} + +function BookEntryRow(props: { readonly entry: LibraryEntry; readonly onDelete: () => void }) { + const { book, deleting } = props.entry; + + return Option.match(deleting, { + onNone: () => ( + + Dialog.confirm({ + title: `Delete ${book.title || "this book"}?`, + message: + "It's removed from your Kindle, with your place in it, the next time your Kindle wakes.", + confirmText: "Delete", + cancelText: "Cancel", + destructive: true, + onConfirm: props.onDelete, + }) + } + /> + ), + onSome: ({ id }) => , + }); +} + +function PendingDeleteRow(props: { readonly id: CommandId; readonly title: string }) { + const [cancelled, cancel] = useAtom(kindleState.dismissAtom(props.id)); + + return ( + + Dialog.confirm({ + title: `Keep ${props.title}?`, + message: "Your Kindle won't delete it.", + confirmText: "Keep", + cancelText: "Cancel", + onConfirm: () => cancel(), + }) + } + /> + ); +} + +function Report(props: { + readonly device: Device; + readonly commands: ReadonlyArray; + readonly now: DateTime.Utc; + readonly onDelete: (path: BookPath) => void; +}) { + const entries = libraryEntries(props.device.books, props.commands); + + return ( + <> + + + + {entries.length === 0 ? ( + + ) : ( + entries.map((entry) => ( + props.onDelete(entry.book.path)} + /> + )) + )} + + + ); +} + +/** The Kindle as of its last wake, and what to run on its next one. */ +export function KindleScreen() { + const insets = useSafeAreaInsets(); + const status = useAtomValue(booksState.kindleStatusAtom); + const now = useAtomValue(nowAtom); + const refreshStatus = useAtomRefresh(booksState.kindleStatusAtom); + const refreshNow = useAtomRefresh(nowAtom); + const [queued, queue] = useAtom(kindleState.queueAtom); + + useFocusEffect(() => { + refreshNow(); + refreshStatus(); + }); + + // A failed refresh keeps showing the last answer the mailbox gave. + const last = AsyncResult.value(status); + const error = status.waiting ? undefined : statusError(status); + const queueError = commandError(queued); + + if (Option.isNone(last) && !error) return ; + + const commands = Option.match(last, { onNone: () => [], onSome: (value) => value.commands }); + const device = Option.flatMap(last, (value) => value.device); + const rescanning = rescanPending(commands); + + return ( + + + {error ? : null} + {queueError ? : null} + + {failures(commands).map(({ id, text }) => ( + + ))} + + {Option.isSome(device) ? ( + queue(Command.cases.DeleteBook.make({ path }))} + /> + ) : ( + + )} + +