-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConversationListControlModule.ts
More file actions
121 lines (109 loc) · 4.01 KB
/
ConversationListControlModule.ts
File metadata and controls
121 lines (109 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// src/controls/modules/ConversationListControlModule.ts
// FULL FILE
import React from "react";
import { type ControlModule } from "@/types/litechat/control";
import { type LiteChatModApi } from "@/types/litechat/modding";
import { ConversationListControlComponent } from "@/controls/components/conversation-list/ConversationListControlComponent";
import { ConversationListIconRenderer } from "@/controls/components/conversation-list/IconRenderer";
import { useConversationStore } from "@/store/conversation.store";
import { useProjectStore } from "@/store/project.store";
import { conversationEvent } from "@/types/litechat/events/conversation.events";
import { projectEvent } from "@/types/litechat/events/project.events";
export class ConversationListControlModule implements ControlModule {
readonly id = "core-conversation-list";
private unregisterCallback: (() => void) | null = null;
private eventUnsubscribers: (() => void)[] = [];
public isLoading = true;
private conversationsLoading = true;
private projectsLoading = true;
private notifyComponentUpdate: (() => void) | null = null;
async initialize(modApi: LiteChatModApi): Promise<void> {
this.conversationsLoading = useConversationStore.getState().isLoading;
this.projectsLoading = useProjectStore.getState().isLoading;
this.updateCombinedLoadingState();
modApi.emit(conversationEvent.loadConversationsRequest, undefined);
modApi.emit(projectEvent.loadProjectsRequest, undefined);
const unsubConversations = modApi.on(
conversationEvent.conversationsLoaded,
() => {
this.conversationsLoading = false;
this.updateCombinedLoadingState();
}
);
const unsubProjects = modApi.on(projectEvent.loaded, () => {
this.projectsLoading = false;
this.updateCombinedLoadingState();
});
const unsubConvLoading = modApi.on(
conversationEvent.loadingStateChanged,
(payload) => {
if (payload.isLoading !== this.conversationsLoading) {
this.conversationsLoading = payload.isLoading;
this.updateCombinedLoadingState();
}
}
);
const unsubProjLoading = modApi.on(
projectEvent.loadingStateChanged,
(payload) => {
if (payload.isLoading !== this.projectsLoading) {
this.projectsLoading = payload.isLoading;
this.updateCombinedLoadingState();
}
}
);
this.eventUnsubscribers.push(
unsubConversations,
unsubProjects,
unsubConvLoading,
unsubProjLoading
);
}
private updateCombinedLoadingState() {
const newLoadingState = this.conversationsLoading || this.projectsLoading;
if (this.isLoading !== newLoadingState) {
this.isLoading = newLoadingState;
this.notifyComponentUpdate?.();
}
}
public setIsLoading = (loading: boolean) => {
// This might be called externally, but internal state is driven by store events
if (this.isLoading !== loading) {
this.isLoading = loading;
this.notifyComponentUpdate?.();
}
};
public setNotifyCallback = (cb: (() => void) | null) => {
this.notifyComponentUpdate = cb;
};
register(modApi: LiteChatModApi): void {
if (this.unregisterCallback) {
console.warn(
`[${this.id}] Module "${this.id}" already registered. Skipping.`
);
return;
}
this.unregisterCallback = modApi.registerChatControl({
id: this.id,
panel: "sidebar",
status: () => (this.isLoading ? "loading" : "ready"),
renderer: () =>
React.createElement(ConversationListControlComponent, {
module: this,
}),
iconRenderer: () =>
React.createElement(ConversationListIconRenderer, { module: this }),
show: () => true,
});
}
destroy(): void {
this.eventUnsubscribers.forEach((unsub) => unsub());
this.eventUnsubscribers = [];
if (this.unregisterCallback) {
this.unregisterCallback();
this.unregisterCallback = null;
}
this.notifyComponentUpdate = null;
console.log(`[${this.id}] Destroyed.`);
}
}