Skip to content

Commit 42f5069

Browse files
committed
refactor: move favorite libs to indexdb library
1 parent 55d86e6 commit 42f5069

File tree

11 files changed

+126
-257
lines changed

11 files changed

+126
-257
lines changed

src/components/shared/Dialogs/ComponentDuplicateDialog.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ const createMockComponentLibraryContext = (
4040
componentLibrary: undefined,
4141
userComponentsFolder,
4242
usedComponentsFolder: { name: "Used", components: [] },
43-
favoritesFolder: { name: "Favorites", components: [] },
4443
isLoading: false,
4544
error: null,
4645
existingComponentLibraries: undefined,
4746
searchResult: null,
4847
searchComponentLibrary: vi.fn(),
4948
addToComponentLibrary: vi.fn(),
5049
removeFromComponentLibrary: vi.fn(),
51-
setComponentFavorite: vi.fn(),
5250
checkIfUserComponent: vi.fn().mockReturnValue(false),
5351
getComponentLibrary: vi.fn(),
5452
};

src/components/shared/FavoriteComponentToggle.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import { Spinner } from "@/components/ui/spinner";
1414
import { useGuaranteedHydrateComponentReference } from "@/hooks/useHydrateComponentReference";
1515
import { cn } from "@/lib/utils";
1616
import { useComponentLibrary } from "@/providers/ComponentLibraryProvider";
17-
import {
18-
flattenFolders,
19-
isFavoriteComponent,
20-
} from "@/providers/ComponentLibraryProvider/componentLibrary";
17+
import { flattenFolders } from "@/providers/ComponentLibraryProvider/componentLibrary";
2118
import { hydrateComponentReference } from "@/services/componentService";
2219
import { type ComponentReference } from "@/utils/componentSpec";
2320
import { MINUTES } from "@/utils/constants";
@@ -108,17 +105,26 @@ const FavoriteToggleButton = withSuspenseWrapper(
108105
({ component }: { component: ComponentReference }) => {
109106
const queryClient = useQueryClient();
110107

111-
const { setComponentFavorite } = useComponentLibrary();
108+
const { getComponentLibrary } = useComponentLibrary();
109+
const favoriteComponentsLibrary = getComponentLibrary(
110+
"favorite_components",
111+
);
112112
const hydratedComponent = useGuaranteedHydrateComponentReference(component);
113113

114114
const { data: isFavorited } = useSuspenseQuery({
115115
queryKey: favoriteComponentKey(hydratedComponent),
116-
queryFn: async () => isFavoriteComponent(hydratedComponent),
116+
queryFn: async () =>
117+
favoriteComponentsLibrary.hasComponent(hydratedComponent),
117118
});
118119

119120
const { mutate: setFavorite } = useMutation({
120-
mutationFn: async () =>
121-
setComponentFavorite(hydratedComponent, !isFavorited),
121+
mutationFn: async () => {
122+
if (isFavorited) {
123+
await favoriteComponentsLibrary.removeComponent(hydratedComponent);
124+
} else {
125+
await favoriteComponentsLibrary.addComponent(hydratedComponent);
126+
}
127+
},
122128
onSuccess: () => {
123129
queryClient.invalidateQueries({
124130
queryKey: favoriteComponentKey(hydratedComponent),

src/components/shared/ReactFlow/FlowSidebar/sections/GraphComponents.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const GraphComponents = ({ isOpen }: { isOpen: boolean }) => {
3939
const remoteComponentLibrarySearchEnabled = useBetaFlagValue(
4040
"remote-component-library-search",
4141
);
42-
4342
const { updateSearchFilter, currentSearchFilter } = useForcedSearchContext();
4443

4544
const handleSearchChange = (e: ChangeEvent<HTMLInputElement>) => {
@@ -121,20 +120,20 @@ function ComponentLibrarySection() {
121120
const remoteComponentLibrarySearchEnabled = useBetaFlagValue(
122121
"remote-component-library-search",
123122
);
124-
125123
const githubComponentLibraryEnabled = useBetaFlagValue(
126124
"github-component-library",
127125
);
128126

129127
const { getComponentLibrary, existingComponentLibraries } =
130128
useComponentLibrary();
131129

130+
const favoriteComponentsLibrary = getComponentLibrary("favorite_components");
131+
132132
const { updateSearchFilter } = useForcedSearchContext();
133133
const {
134134
componentLibrary,
135135
usedComponentsFolder,
136136
userComponentsFolder,
137-
favoritesFolder,
138137
isLoading,
139138
error,
140139
searchResult,
@@ -165,9 +164,6 @@ function ComponentLibrarySection() {
165164
usedComponentsFolder?.components &&
166165
usedComponentsFolder.components.length > 0;
167166

168-
const hasFavouriteComponents =
169-
favoritesFolder?.components && favoritesFolder.components.length > 0;
170-
171167
const hasUserComponents =
172168
userComponentsFolder?.components &&
173169
userComponentsFolder.components.length > 0;
@@ -184,13 +180,13 @@ function ComponentLibrarySection() {
184180
icon="LayoutGrid"
185181
/>
186182
)}
187-
{hasFavouriteComponents && (
188-
<FolderItem
189-
key="favorite-components-folder"
190-
folder={favoritesFolder}
191-
icon="Star"
192-
/>
193-
)}
183+
184+
<LibraryFolderItem
185+
key="favorite-components-folder-v2"
186+
library={favoriteComponentsLibrary}
187+
icon="Star"
188+
/>
189+
194190
{hasUserComponents && (
195191
<FolderItem
196192
key="my-components-folder"

src/providers/ComponentLibraryProvider/ComponentLibraryProvider.test.tsx

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,9 @@ const mockImportComponent = vi.mocked(componentStore.importComponent);
6767
const mockDeleteComponentFileFromList = vi.mocked(
6868
componentStore.deleteComponentFileFromList,
6969
);
70-
const mockUpdateComponentRefInList = vi.mocked(
71-
componentStore.updateComponentRefInList,
72-
);
73-
const mockGetComponentByUrl = vi.mocked(localforage.getComponentByUrl);
7470
const mockGetUserComponentByName = vi.mocked(
7571
localforage.getUserComponentByName,
7672
);
77-
const mockSaveComponent = vi.mocked(localforage.saveComponent);
7873
const mockGetComponentName = vi.mocked(getComponentName.getComponentName);
7974

8075
describe("ComponentLibraryProvider - Component Management", () => {
@@ -537,82 +532,4 @@ describe("ComponentLibraryProvider - Component Management", () => {
537532
expect(isUserComponent).toBe(false);
538533
});
539534
});
540-
541-
describe("Component Favoriting", () => {
542-
it("should set component as favorite for user components", async () => {
543-
const userComponent: ComponentReference = {
544-
name: "user-component",
545-
digest: "user-digest",
546-
spec: mockComponentSpec,
547-
text: "user yaml content",
548-
};
549-
550-
mockUpdateComponentRefInList.mockResolvedValue({
551-
componentRef: userComponent,
552-
name: "user-component",
553-
data: new ArrayBuffer(0),
554-
creationTime: new Date(),
555-
modificationTime: new Date(),
556-
} as any);
557-
558-
const { result } = renderHook(() => useComponentLibrary(), {
559-
wrapper: createWrapper,
560-
});
561-
562-
await waitFor(() => {
563-
expect(result.current.isLoading).toBe(false);
564-
});
565-
566-
await act(async () => {
567-
await result.current.setComponentFavorite(userComponent, true);
568-
});
569-
570-
expect(mockUpdateComponentRefInList).toHaveBeenCalledWith(
571-
USER_COMPONENTS_LIST_NAME,
572-
expect.objectContaining({ favorited: true }),
573-
"user-component",
574-
);
575-
});
576-
577-
it("should set component as favorite for standard components", async () => {
578-
const standardComponent: ComponentReference = {
579-
name: "standard-component",
580-
digest: "standard-digest",
581-
url: "https://example.com/standard.yaml",
582-
spec: mockComponentSpec,
583-
};
584-
585-
const mockStoredComponent = {
586-
id: "stored-1",
587-
url: "https://example.com/standard.yaml",
588-
data: "stored yaml",
589-
createdAt: Date.now(),
590-
updatedAt: Date.now(),
591-
favorited: false,
592-
};
593-
594-
mockGetComponentByUrl.mockResolvedValue(mockStoredComponent);
595-
mockSaveComponent.mockResolvedValue(mockStoredComponent as any);
596-
597-
const { result } = renderHook(() => useComponentLibrary(), {
598-
wrapper: createWrapper,
599-
});
600-
601-
await waitFor(() => {
602-
expect(result.current.isLoading).toBe(false);
603-
});
604-
605-
await act(async () => {
606-
await result.current.setComponentFavorite(standardComponent, true);
607-
});
608-
609-
expect(mockGetComponentByUrl).toHaveBeenCalledWith(
610-
"https://example.com/standard.yaml",
611-
);
612-
expect(mockSaveComponent).toHaveBeenCalledWith({
613-
...mockStoredComponent,
614-
favorited: true,
615-
});
616-
});
617-
});
618535
});

0 commit comments

Comments
 (0)