feat(chat): implement pull-to-refresh functionality and pagination for chat history
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import {
|
||||
applyOlderHistoryLoadedOutput,
|
||||
applyHistoryLoadedOutput,
|
||||
applyNetworkHistoryLoadedOutput,
|
||||
canLoadMoreHistory,
|
||||
} from "../helper/history";
|
||||
import {
|
||||
countLockedHistoryMessages,
|
||||
shouldPromptUnlockHistory,
|
||||
} from "../helper/unlock";
|
||||
import type { ChatState } from "../chat-state";
|
||||
import { baseChatMachineSetup } from "./setup";
|
||||
|
||||
const applyLocalHistoryLoadedAction = baseChatMachineSetup.assign(
|
||||
@@ -49,6 +52,30 @@ const markHistoryLoadFailedAction = baseChatMachineSetup.assign({
|
||||
historyLoaded: true,
|
||||
});
|
||||
|
||||
const markLoadMoreHistoryStartedAction = baseChatMachineSetup.assign({
|
||||
isLoadingMoreHistory: true,
|
||||
});
|
||||
|
||||
const requestOlderHistoryPageAction = baseChatMachineSetup.sendTo(
|
||||
"loadMoreHistory",
|
||||
({ context }) => ({
|
||||
type: "LoadMoreHistoryPageRequested",
|
||||
offset: context.nextHistoryOffset,
|
||||
limit: context.historyLimit,
|
||||
}),
|
||||
);
|
||||
|
||||
const applyOlderHistoryLoadedAction = baseChatMachineSetup.assign(
|
||||
({ context, event }) => {
|
||||
if (event.type !== "ChatOlderHistoryLoaded") return {};
|
||||
return applyOlderHistoryLoadedOutput(context, event.output);
|
||||
},
|
||||
);
|
||||
|
||||
const markOlderHistoryLoadFailedAction = baseChatMachineSetup.assign({
|
||||
isLoadingMoreHistory: false,
|
||||
});
|
||||
|
||||
export const historyMachineSetup = baseChatMachineSetup.extend({
|
||||
actions: {
|
||||
applyLocalHistoryLoaded: applyLocalHistoryLoadedAction,
|
||||
@@ -58,9 +85,27 @@ export const historyMachineSetup = baseChatMachineSetup.extend({
|
||||
showUnlockHistoryPromptFromNetwork:
|
||||
showUnlockHistoryPromptFromNetworkAction,
|
||||
markHistoryLoadFailed: markHistoryLoadFailedAction,
|
||||
markLoadMoreHistoryStarted: markLoadMoreHistoryStartedAction,
|
||||
requestOlderHistoryPage: requestOlderHistoryPageAction,
|
||||
applyOlderHistoryLoaded: applyOlderHistoryLoadedAction,
|
||||
markOlderHistoryLoadFailed: markOlderHistoryLoadFailedAction,
|
||||
},
|
||||
});
|
||||
|
||||
export const historyPaginationTransitions = {
|
||||
ChatLoadMoreHistoryRequested: {
|
||||
guard: ({ context }: { context: ChatState }) =>
|
||||
canLoadMoreHistory(context),
|
||||
actions: ["markLoadMoreHistoryStarted", "requestOlderHistoryPage"],
|
||||
},
|
||||
ChatOlderHistoryLoaded: {
|
||||
actions: "applyOlderHistoryLoaded",
|
||||
},
|
||||
ChatOlderHistoryLoadFailed: {
|
||||
actions: "markOlderHistoryLoadFailed",
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const guestInitializingState = historyMachineSetup.createStateConfig({
|
||||
on: {
|
||||
ChatLocalHistoryLoaded: {
|
||||
|
||||
Reference in New Issue
Block a user