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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
import { create } from "zustand";
import {
getCollections,
getCollection,
searchIcons,
type IconSet,
type CollectionInfo,
type IconSearchResult,
} from "@/lib/iconify-api";
import { EditorCore } from "@/core";
import { buildStickerElement } from "@/lib/timeline/element-utils";
import { STICKER_CATEGORY_CONFIG } from "@/constants/stickers-constants";
import type { StickerCategory } from "@/types/stickers";
type ViewMode = "search" | "browse" | "collection";
interface StickersStore {
searchQuery: string;
selectedCategory: StickerCategory;
selectedCollection: string | null;
viewMode: ViewMode;
collections: Record<string, IconSet>;
currentCollection: CollectionInfo | null;
searchResults: IconSearchResult | null;
recentStickers: string[];
isLoadingCollections: boolean;
isLoadingCollection: boolean;
isSearching: boolean;
addingSticker: string | null;
setSearchQuery: ({ query }: { query: string }) => void;
setSelectedCategory: ({ category }: { category: StickerCategory }) => void;
setSelectedCollection: ({
collection,
}: {
collection: string | null;
}) => void;
setViewMode: ({ mode }: { mode: ViewMode }) => void;
loadCollections: () => Promise<void>;
loadCollection: ({ prefix }: { prefix: string }) => Promise<void>;
searchStickers: ({ query }: { query: string }) => Promise<void>;
addStickerToTimeline: ({ iconName }: { iconName: string }) => void;
addToRecentStickers: ({ iconName }: { iconName: string }) => void;
clearRecentStickers: () => void;
}
const MAX_RECENT_STICKERS = 50;
export const useStickersStore = create<StickersStore>((set, get) => ({
searchQuery: "",
selectedCategory: "all",
selectedCollection: null,
viewMode: "browse",
collections: {},
currentCollection: null,
searchResults: null,
recentStickers: [],
isLoadingCollections: false,
isLoadingCollection: false,
isSearching: false,
addingSticker: null,
setSearchQuery: ({ query }) => set({ searchQuery: query }),
setSelectedCategory: ({ category }) =>
set({
selectedCategory: category,
viewMode: "browse",
selectedCollection: null,
currentCollection: null,
}),
setSelectedCollection: ({ collection }) => {
set({
selectedCollection: collection,
viewMode: collection ? "collection" : "browse",
currentCollection: null,
});
if (collection) {
get().loadCollection({ prefix: collection });
}
},
setViewMode: ({ mode }) => set({ viewMode: mode }),
loadCollections: async () => {
set({ isLoadingCollections: true });
try {
const collections = await getCollections();
set({ collections });
} catch (error) {
console.error("Failed to load collections:", error);
} finally {
set({ isLoadingCollections: false });
}
},
loadCollection: async ({ prefix }: { prefix: string }) => {
set({ isLoadingCollection: true });
try {
const collection = await getCollection(prefix);
set({ currentCollection: collection });
} catch (error) {
console.error(`Failed to load collection ${prefix}:`, error);
set({ currentCollection: null });
} finally {
set({ isLoadingCollection: false });
}
},
searchStickers: async ({ query }: { query: string }) => {
if (!query.trim()) {
set({ searchResults: null, viewMode: "browse" });
return;
}
const { selectedCategory } = get();
set({ isSearching: true, viewMode: "search" });
try {
const category = STICKER_CATEGORY_CONFIG[selectedCategory];
const results = await searchIcons(query, 100, undefined, category);
set({ searchResults: results });
} catch (error) {
console.error("Search failed:", error);
set({ searchResults: null });
} finally {
set({ isSearching: false });
}
},
addStickerToTimeline: ({ iconName }: { iconName: string }) => {
set({ addingSticker: iconName });
try {
const editor = EditorCore.getInstance();
const currentTime = editor.playback.getCurrentTime();
const tracks = editor.timeline.getTracks();
const stickerTrack = tracks.find((t) => t.type === "sticker");
let trackId: string;
if (stickerTrack) {
trackId = stickerTrack.id;
} else {
trackId = editor.timeline.addTrack({ type: "sticker" });
}
const element = buildStickerElement({ iconName, startTime: currentTime });
editor.timeline.insertElement({
placement: { mode: "explicit", trackId },
element,
});
get().addToRecentStickers({ iconName });
} finally {
set({ addingSticker: null });
}
},
addToRecentStickers: ({ iconName }: { iconName: string }) => {
set((state) => {
const recent = [
iconName,
...state.recentStickers.filter((s) => s !== iconName),
];
return {
recentStickers: recent.slice(0, MAX_RECENT_STICKERS),
};
});
},
clearRecentStickers: () => set({ recentStickers: [] }),
}));
|