Skip to content

Commit 8bd9c3d

Browse files
committed
refactor: move standard library to yamllibrary
1 parent c3f9da6 commit 8bd9c3d

File tree

8 files changed

+27
-267
lines changed

8 files changed

+27
-267
lines changed

src/components/shared/FavoriteComponentToggle.tsx

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ 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 { flattenFolders } from "@/providers/ComponentLibraryProvider/componentLibrary";
1817
import { hydrateComponentReference } from "@/services/componentService";
1918
import { type ComponentReference } from "@/utils/componentSpec";
2019
import { MINUTES } from "@/utils/constants";
@@ -143,38 +142,20 @@ const FavoriteToggleButton = withSuspenseWrapper(
143142
);
144143

145144
const useComponentFlags = (component: ComponentReference) => {
146-
const { checkIfUserComponent, componentLibrary } = useComponentLibrary();
145+
const { checkIfUserComponent, getComponentLibrary } = useComponentLibrary();
146+
const componentLibrary = getComponentLibrary("standard_components");
147147

148148
const isUserComponent = useMemo(
149149
() => checkIfUserComponent(component),
150-
[checkIfUserComponent, component],
151-
);
152-
153-
const flatComponentList = useMemo(
154-
() => (componentLibrary ? flattenFolders(componentLibrary) : []),
155-
[componentLibrary],
150+
[checkIfUserComponent],
156151
);
157152

158153
const { data: isInLibrary } = useSuspenseQuery({
159154
queryKey: ["component", "flags", component.digest],
160155
queryFn: async () => {
161156
if (!componentLibrary) return false;
162157

163-
for (const c of flatComponentList) {
164-
if (c.name && c.name !== component.name) {
165-
// micro optimization to skip components with different names
166-
continue;
167-
}
168-
169-
const hydratedComponent = await hydrateComponentReference(c);
170-
const digest = c.digest ?? hydratedComponent?.digest;
171-
172-
if (digest === component.digest) {
173-
return true;
174-
}
175-
}
176-
177-
return false;
158+
return componentLibrary.hasComponent(component);
178159
},
179160
staleTime: 10 * MINUTES,
180161
});

src/components/shared/ReactFlow/FlowSidebar/components/LibraryStates.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,3 @@ export const LoadingState = () => (
1212
export const ErrorState = ({ message }: { message: string }) => (
1313
<SidebarMenuItem className="text-red-500">Error: {message}</SidebarMenuItem>
1414
);
15-
16-
export const EmptyState = () => (
17-
<SidebarMenuItem>No components found</SidebarMenuItem>
18-
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { default as FolderItem } from "./FolderItem";
22
export { default as ImportComponent } from "./ImportComponent";
3-
export { EmptyState, ErrorState, LoadingState } from "./LibraryStates";
3+
export { ErrorState, LoadingState } from "./LibraryStates";
44
export { default as SearchInput } from "./SearchInput";
55
export { default as SearchResults } from "./SearchResults";

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { useForcedSearchContext } from "@/providers/ComponentLibraryProvider/For
2222
import type { UIComponentFolder } from "@/types/componentLibrary";
2323

2424
import {
25-
EmptyState,
2625
ErrorState,
2726
FolderItem,
2827
ImportComponent,
@@ -131,14 +130,15 @@ function ComponentLibrarySection() {
131130

132131
const { updateSearchFilter } = useForcedSearchContext();
133132
const {
134-
componentLibrary,
135133
usedComponentsFolder,
136134
userComponentsFolder,
137135
isLoading,
138136
error,
139137
searchResult,
140138
} = useComponentLibrary();
141139

140+
const standardComponentsLibrary = getComponentLibrary("standard_components");
141+
142142
const handleFiltersChange = (filters: string[]) => {
143143
updateSearchFilter({
144144
filters,
@@ -147,7 +147,6 @@ function ComponentLibrarySection() {
147147

148148
if (isLoading) return <LoadingState />;
149149
if (error) return <ErrorState message={(error as Error).message} />;
150-
if (!componentLibrary) return <EmptyState />;
151150

152151
if (!remoteComponentLibrarySearchEnabled && searchResult) {
153152
// If there's a search result, use the SearchResults component
@@ -210,15 +209,9 @@ function ComponentLibrarySection() {
210209
icon="Cable"
211210
/>
212211
<Separator />
213-
<FolderItem
214-
key="standard-library-folder"
215-
folder={
216-
{
217-
name: "Standard library",
218-
components: [],
219-
folders: componentLibrary.folders,
220-
} as UIComponentFolder
221-
}
212+
<LibraryFolderItem
213+
key="standard-library-folder-v2"
214+
library={standardComponentsLibrary}
222215
icon="Folder"
223216
/>
224217
{githubComponentLibraryEnabled && (

src/providers/ComponentLibraryProvider/ComponentLibraryProvider.test.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@ vi.mock("./componentLibrary");
4141

4242
// Import mocked modules
4343
import * as componentLibraryUtils from "@/providers/ComponentLibraryProvider/componentLibrary";
44-
import * as componentService from "@/services/componentService";
4544
import * as componentStore from "@/utils/componentStore";
4645
import * as getComponentName from "@/utils/getComponentName";
4746
import * as localforage from "@/utils/localforage";
4847

4948
// Mock implementations
50-
const mockFetchAndStoreComponentLibrary = vi.mocked(
51-
componentService.fetchAndStoreComponentLibrary,
52-
);
49+
5350
const mockFetchUserComponents = vi.mocked(
5451
componentLibraryUtils.fetchUserComponents,
5552
);
@@ -134,7 +131,6 @@ describe("ComponentLibraryProvider - Component Management", () => {
134131
componentDuplicateDialogProps.handleImportComponent = undefined;
135132

136133
// Setup default mock implementations
137-
mockFetchAndStoreComponentLibrary.mockResolvedValue(mockComponentLibrary);
138134
mockFetchUserComponents.mockResolvedValue(mockUserComponentsFolder);
139135
mockFetchUsedComponents.mockReturnValue({
140136
name: "Used Components",

src/providers/ComponentLibraryProvider/ComponentLibraryProvider.tsx

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ import {
1515
isYamlLibraryConfiguration,
1616
} from "@/components/shared/GitHubLibrary/types";
1717
import {
18-
fetchAndStoreComponentLibrary,
18+
COMPONENT_LIBRARY_URL,
1919
hydrateComponentReference,
2020
} from "@/services/componentService";
21-
import type {
22-
ComponentFolder,
23-
ComponentLibrary,
24-
SearchResult,
25-
} from "@/types/componentLibrary";
21+
import type { ComponentFolder, SearchResult } from "@/types/componentLibrary";
2622
import type {
2723
ComponentReference,
2824
HydratedComponentReference,
@@ -75,7 +71,6 @@ type AvailableComponentLibraries =
7571
| string;
7672

7773
type ComponentLibraryContextType = {
78-
componentLibrary: ComponentLibrary | undefined;
7974
userComponentsFolder: ComponentFolder | undefined;
8075
usedComponentsFolder: ComponentFolder;
8176
isLoading: boolean;
@@ -142,6 +137,10 @@ function useComponentLibraryRegistry() {
142137
/**
143138
* In future we will have other library types, including "standard_library", "favorite_components", "used_components", etc.
144139
*/
140+
[
141+
"standard_components",
142+
new YamlFileLibrary("Standard library", COMPONENT_LIBRARY_URL),
143+
],
145144
]),
146145
[queryClient],
147146
);
@@ -193,7 +192,6 @@ export const ComponentLibraryProvider = ({
193192
const { getComponentLibraryObject, existingComponentLibraries } =
194193
useComponentLibraryRegistry();
195194

196-
const [componentLibrary, setComponentLibrary] = useState<ComponentLibrary>();
197195
const [userComponentsFolder, setUserComponentsFolder] =
198196
useState<ComponentFolder>();
199197

@@ -202,17 +200,6 @@ export const ComponentLibraryProvider = ({
202200
const [newComponent, setNewComponent] =
203201
useState<HydratedComponentReference | null>(null);
204202

205-
// Fetch main component library
206-
const {
207-
data: rawComponentLibrary,
208-
isLoading: isLibraryLoading,
209-
error: libraryError,
210-
refetch: refetchLibrary,
211-
} = useQuery({
212-
queryKey: ["componentLibrary"],
213-
queryFn: fetchAndStoreComponentLibrary,
214-
});
215-
216203
// Fetch user components
217204
const {
218205
data: rawUserComponentsFolder,
@@ -233,14 +220,6 @@ export const ComponentLibraryProvider = ({
233220
);
234221

235222
// Methods
236-
const refreshComponentLibrary = useCallback(async () => {
237-
const { data: updatedLibrary } = await refetchLibrary();
238-
239-
if (updatedLibrary) {
240-
setComponentLibrary(updatedLibrary);
241-
}
242-
}, [refetchLibrary]);
243-
244223
const refreshUserComponents = useCallback(async () => {
245224
const { data: updatedUserComponents } = await refetchUserComponents();
246225

@@ -300,30 +279,7 @@ export const ComponentLibraryProvider = ({
300279
},
301280
};
302281

303-
if (componentLibrary) {
304-
const uniqueComponents = filterToUniqueByDigest(
305-
flattenFolders(componentLibrary),
306-
);
307-
308-
if (!hasNameFilter && filtersSet.size > 1) {
309-
// we need specs
310-
const hydratedComponents = await Promise.all(
311-
uniqueComponents.map(async (c) => {
312-
if (!c.spec) {
313-
return await hydrateComponentReference(c);
314-
}
315-
return c;
316-
}),
317-
);
318-
319-
result.components.standard = hydratedComponents
320-
.filter((c) => c !== null)
321-
.filter(componentMatches);
322-
} else {
323-
result.components.standard =
324-
uniqueComponents.filter(componentMatches);
325-
}
326-
}
282+
// classic search is not supported for now
327283

328284
if (userComponentsFolder) {
329285
const uniqueComponents = filterToUniqueByDigest(
@@ -341,13 +297,13 @@ export const ComponentLibraryProvider = ({
341297

342298
return result;
343299
},
344-
[componentLibrary, userComponentsFolder, usedComponentsFolder],
300+
[userComponentsFolder, usedComponentsFolder],
345301
);
346302

347303
const internalAddComponentToLibrary = useCallback(
348304
async (hydratedComponent: HydratedComponentReference) => {
349305
await importComponent(hydratedComponent);
350-
await refreshComponentLibrary();
306+
351307
await refreshUserComponents();
352308
setNewComponent(null);
353309
setExistingComponent(null);
@@ -360,7 +316,7 @@ export const ComponentLibraryProvider = ({
360316
}),
361317
);
362318
},
363-
[refreshComponentLibrary, refreshUserComponents, importComponent],
319+
[refreshUserComponents, importComponent],
364320
);
365321

366322
const handleImportComponent = useCallback(
@@ -379,12 +335,7 @@ export const ComponentLibraryProvider = ({
379335
console.error("Error importing component:", error);
380336
}
381337
},
382-
[
383-
newComponent,
384-
refreshComponentLibrary,
385-
refreshUserComponents,
386-
importComponent,
387-
],
338+
[newComponent, refreshUserComponents, importComponent],
388339
);
389340

390341
const addToComponentLibraryWithDuplicateCheck = useCallback(
@@ -414,12 +365,7 @@ export const ComponentLibraryProvider = ({
414365
console.error("Error adding component to library:", error);
415366
}
416367
},
417-
[
418-
userComponentsFolder,
419-
refreshComponentLibrary,
420-
refreshUserComponents,
421-
importComponent,
422-
],
368+
[userComponentsFolder, refreshUserComponents, importComponent],
423369
);
424370

425371
const addToComponentLibrary = useCallback(
@@ -458,7 +404,6 @@ export const ComponentLibraryProvider = ({
458404
USER_COMPONENTS_LIST_NAME,
459405
component.name,
460406
).then(async () => {
461-
await refreshComponentLibrary();
462407
await refreshUserComponents();
463408
});
464409
} else {
@@ -470,7 +415,7 @@ export const ComponentLibraryProvider = ({
470415
console.error("Error deleting component:", error);
471416
}
472417
},
473-
[refreshComponentLibrary, refreshUserComponents],
418+
[refreshUserComponents],
474419
);
475420

476421
const handleCloseDuplicationDialog = useCallback(() => {
@@ -497,14 +442,6 @@ export const ComponentLibraryProvider = ({
497442
};
498443
}, [currentSearchFilter, searchComponentLibrary]);
499444

500-
useEffect(() => {
501-
if (!rawComponentLibrary) {
502-
setComponentLibrary(undefined);
503-
return;
504-
}
505-
setComponentLibrary(rawComponentLibrary);
506-
}, [rawComponentLibrary]);
507-
508445
useEffect(() => {
509446
if (!rawUserComponentsFolder) {
510447
setUserComponentsFolder(undefined);
@@ -522,12 +459,11 @@ export const ComponentLibraryProvider = ({
522459
[],
523460
);
524461

525-
const isLoading = isLibraryLoading || isUserComponentsLoading;
526-
const error = libraryError || userComponentsError;
462+
const isLoading = isUserComponentsLoading;
463+
const error = userComponentsError;
527464

528465
const value = useMemo(
529466
() => ({
530-
componentLibrary,
531467
userComponentsFolder,
532468
usedComponentsFolder,
533469
isLoading,
@@ -541,7 +477,6 @@ export const ComponentLibraryProvider = ({
541477
checkIfUserComponent,
542478
}),
543479
[
544-
componentLibrary,
545480
userComponentsFolder,
546481
usedComponentsFolder,
547482
isLoading,

0 commit comments

Comments
 (0)