Skip to content

Commit 0024d70

Browse files
committed
refactor: move standard library to yamllibrary
1 parent 8cd6373 commit 0024d70

File tree

8 files changed

+26
-254
lines changed

8 files changed

+26
-254
lines changed

src/components/shared/FavoriteComponentToggle.tsx

Lines changed: 3 additions & 25 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,43 +142,22 @@ 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),
150150
[component, checkIfUserComponent],
151151
);
152152

153-
const flatComponentList = useMemo(
154-
() => (componentLibrary ? flattenFolders(componentLibrary) : []),
155-
[componentLibrary],
156-
);
157-
158153
const { data: isInLibrary } = useSuspenseQuery({
159154
queryKey: ["component", "flags", component.digest],
160155
queryFn: async () => {
161156
if (!componentLibrary) return false;
162157

163158
if (isUserComponent) return true;
164159

165-
for (const c of flatComponentList) {
166-
if (component.name === "Chicago Taxi Trips dataset") {
167-
console.log(c.name, c.digest, component.digest);
168-
}
169-
170-
if (c.name && c.name !== component.name) {
171-
// micro optimization to skip components with different names
172-
continue;
173-
}
174-
175-
const digest = c.digest ?? (await hydrateComponentReference(c))?.digest;
176-
177-
if (digest === component.digest) {
178-
return true;
179-
}
180-
}
181-
182-
return false;
160+
return componentLibrary.hasComponent(component);
183161
},
184162
staleTime: 10 * MINUTES,
185163
});

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 & 65 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,
@@ -69,7 +65,6 @@ type AvailableComponentLibraries =
6965
| string;
7066

7167
type ComponentLibraryContextType = {
72-
componentLibrary: ComponentLibrary | undefined;
7368
userComponentsFolder: ComponentFolder | undefined;
7469
usedComponentsFolder: ComponentFolder;
7570
isLoading: boolean;
@@ -136,6 +131,10 @@ function useComponentLibraryRegistry() {
136131
/**
137132
* In future we will have other library types, including "standard_library", "favorite_components", "used_components", etc.
138133
*/
134+
[
135+
"standard_components",
136+
new YamlFileLibrary("Standard library", COMPONENT_LIBRARY_URL),
137+
],
139138
]),
140139
[queryClient],
141140
);
@@ -187,7 +186,6 @@ export const ComponentLibraryProvider = ({
187186
const { getComponentLibraryObject, existingComponentLibraries } =
188187
useComponentLibraryRegistry();
189188

190-
const [componentLibrary, setComponentLibrary] = useState<ComponentLibrary>();
191189
const [userComponentsFolder, setUserComponentsFolder] =
192190
useState<ComponentFolder>();
193191

@@ -196,17 +194,6 @@ export const ComponentLibraryProvider = ({
196194
const [newComponent, setNewComponent] =
197195
useState<HydratedComponentReference | null>(null);
198196

199-
// Fetch main component library
200-
const {
201-
data: rawComponentLibrary,
202-
isLoading: isLibraryLoading,
203-
error: libraryError,
204-
refetch: refetchLibrary,
205-
} = useQuery({
206-
queryKey: ["componentLibrary"],
207-
queryFn: fetchAndStoreComponentLibrary,
208-
});
209-
210197
// Fetch user components
211198
const {
212199
data: rawUserComponentsFolder,
@@ -227,14 +214,6 @@ export const ComponentLibraryProvider = ({
227214
);
228215

229216
// Methods
230-
const refreshComponentLibrary = useCallback(async () => {
231-
const { data: updatedLibrary } = await refetchLibrary();
232-
233-
if (updatedLibrary) {
234-
setComponentLibrary(updatedLibrary);
235-
}
236-
}, [refetchLibrary]);
237-
238217
const refreshUserComponents = useCallback(async () => {
239218
const { data: updatedUserComponents } = await refetchUserComponents();
240219

@@ -273,15 +252,7 @@ export const ComponentLibraryProvider = ({
273252
},
274253
};
275254

276-
if (componentLibrary) {
277-
const uniqueComponents = filterToUniqueByDigest(
278-
flattenFolders(componentLibrary),
279-
);
280-
281-
result.components.standard = uniqueComponents.filter(
282-
(c) => c.spec && componentMatchesSearch(c.spec, search, filters),
283-
);
284-
}
255+
// classic search is not supported for now
285256

286257
if (userComponentsFolder) {
287258
const uniqueComponents = filterToUniqueByDigest(
@@ -303,13 +274,13 @@ export const ComponentLibraryProvider = ({
303274

304275
return result;
305276
},
306-
[componentLibrary, userComponentsFolder, usedComponentsFolder],
277+
[userComponentsFolder, usedComponentsFolder],
307278
);
308279

309280
const internalAddComponentToLibrary = useCallback(
310281
async (hydratedComponent: HydratedComponentReference) => {
311282
await importComponent(hydratedComponent);
312-
await refreshComponentLibrary();
283+
313284
await refreshUserComponents();
314285
setNewComponent(null);
315286
setExistingComponent(null);
@@ -322,7 +293,7 @@ export const ComponentLibraryProvider = ({
322293
}),
323294
);
324295
},
325-
[refreshComponentLibrary, refreshUserComponents, importComponent],
296+
[refreshUserComponents, importComponent],
326297
);
327298

328299
const handleImportComponent = useCallback(
@@ -341,12 +312,7 @@ export const ComponentLibraryProvider = ({
341312
console.error("Error importing component:", error);
342313
}
343314
},
344-
[
345-
newComponent,
346-
refreshComponentLibrary,
347-
refreshUserComponents,
348-
importComponent,
349-
],
315+
[newComponent, refreshUserComponents, importComponent],
350316
);
351317

352318
const addToComponentLibraryWithDuplicateCheck = useCallback(
@@ -376,12 +342,7 @@ export const ComponentLibraryProvider = ({
376342
console.error("Error adding component to library:", error);
377343
}
378344
},
379-
[
380-
userComponentsFolder,
381-
refreshComponentLibrary,
382-
refreshUserComponents,
383-
importComponent,
384-
],
345+
[userComponentsFolder, refreshUserComponents, importComponent],
385346
);
386347

387348
const addToComponentLibrary = useCallback(
@@ -420,7 +381,6 @@ export const ComponentLibraryProvider = ({
420381
USER_COMPONENTS_LIST_NAME,
421382
component.name,
422383
).then(async () => {
423-
await refreshComponentLibrary();
424384
await refreshUserComponents();
425385
});
426386
} else {
@@ -432,7 +392,7 @@ export const ComponentLibraryProvider = ({
432392
console.error("Error deleting component:", error);
433393
}
434394
},
435-
[refreshComponentLibrary, refreshUserComponents],
395+
[refreshUserComponents],
436396
);
437397

438398
const handleCloseDuplicationDialog = useCallback(() => {
@@ -451,14 +411,6 @@ export const ComponentLibraryProvider = ({
451411
[currentSearchFilter, searchComponentLibrary],
452412
);
453413

454-
useEffect(() => {
455-
if (!rawComponentLibrary) {
456-
setComponentLibrary(undefined);
457-
return;
458-
}
459-
setComponentLibrary(rawComponentLibrary);
460-
}, [rawComponentLibrary]);
461-
462414
useEffect(() => {
463415
if (!rawUserComponentsFolder) {
464416
setUserComponentsFolder(undefined);
@@ -476,12 +428,11 @@ export const ComponentLibraryProvider = ({
476428
[],
477429
);
478430

479-
const isLoading = isLibraryLoading || isUserComponentsLoading;
480-
const error = libraryError || userComponentsError;
431+
const isLoading = isUserComponentsLoading;
432+
const error = userComponentsError;
481433

482434
const value = useMemo(
483435
() => ({
484-
componentLibrary,
485436
userComponentsFolder,
486437
usedComponentsFolder,
487438
isLoading,
@@ -495,7 +446,6 @@ export const ComponentLibraryProvider = ({
495446
checkIfUserComponent,
496447
}),
497448
[
498-
componentLibrary,
499449
userComponentsFolder,
500450
usedComponentsFolder,
501451
isLoading,

0 commit comments

Comments
 (0)