-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathselectors.ts
74 lines (58 loc) · 2.2 KB
/
selectors.ts
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
import { t } from 'i18next';
import { ChatTopic, ChatTopicSummary, GroupedTopic } from '@/types/topic';
import { groupTopicsByTime } from '@/utils/client/topic';
import { ChatStoreState } from '../../initialState';
const currentTopics = (s: ChatStoreState): ChatTopic[] | undefined => s.topicMaps[s.activeId];
const currentActiveTopic = (s: ChatStoreState): ChatTopic | undefined => {
return currentTopics(s)?.find((topic) => topic.id === s.activeTopicId);
};
const searchTopics = (s: ChatStoreState): ChatTopic[] => s.searchTopics;
const displayTopics = (s: ChatStoreState): ChatTopic[] | undefined =>
s.isSearchingTopic ? searchTopics(s) : currentTopics(s);
const currentFavTopics = (s: ChatStoreState): ChatTopic[] =>
currentTopics(s)?.filter((s) => s.favorite) || [];
const currentUnFavTopics = (s: ChatStoreState): ChatTopic[] =>
currentTopics(s)?.filter((s) => !s.favorite) || [];
const currentTopicLength = (s: ChatStoreState): number => currentTopics(s)?.length || 0;
const getTopicById =
(id: string) =>
(s: ChatStoreState): ChatTopic | undefined =>
currentTopics(s)?.find((topic) => topic.id === id);
const currentActiveTopicSummary = (s: ChatStoreState): ChatTopicSummary | undefined => {
const activeTopic = currentActiveTopic(s);
if (!activeTopic) return undefined;
return {
content: activeTopic.historySummary || '',
model: activeTopic.metadata?.model || '',
provider: activeTopic.metadata?.provider || '',
};
};
const isCreatingTopic = (s: ChatStoreState) => s.creatingTopic;
const groupedTopicsSelector = (s: ChatStoreState): GroupedTopic[] => {
const topics = displayTopics(s);
if (!topics) return [];
const favTopics = currentFavTopics(s);
const unfavTopics = currentUnFavTopics(s);
return favTopics.length > 0
? [
{
children: favTopics,
id: 'favorite',
title: t('favorite', { ns: 'topic' }),
},
...groupTopicsByTime(unfavTopics),
]
: groupTopicsByTime(topics);
};
export const topicSelectors = {
currentActiveTopic,
currentActiveTopicSummary,
currentTopicLength,
currentTopics,
currentUnFavTopics,
displayTopics,
getTopicById,
groupedTopicsSelector,
isCreatingTopic,
searchTopics,
};