Skip to content

Commit 3a20178

Browse files
committed
refactor: move checkLibraryContainsComponent out of ComponentLibraryProvider
1 parent cc2648a commit 3a20178

File tree

4 files changed

+65
-60
lines changed

4 files changed

+65
-60
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const createMockComponentLibraryContext = (
5050
removeFromComponentLibrary: vi.fn(),
5151
setComponentFavorite: vi.fn(),
5252
checkIfUserComponent: vi.fn().mockReturnValue(false),
53-
checkLibraryContainsComponent: vi.fn().mockReturnValue(false),
5453
getComponentLibrary: vi.fn(),
5554
};
5655
};

src/components/shared/FavoriteComponentToggle.tsx

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ 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 { isFavoriteComponent } from "@/providers/ComponentLibraryProvider/componentLibrary";
17+
import {
18+
flattenFolders,
19+
isFavoriteComponent,
20+
} from "@/providers/ComponentLibraryProvider/componentLibrary";
1821
import { hydrateComponentReference } from "@/services/componentService";
19-
import type { ComponentReference } from "@/utils/componentSpec";
22+
import { type ComponentReference } from "@/utils/componentSpec";
23+
import { MINUTES } from "@/utils/constants";
2024
import { getComponentName } from "@/utils/getComponentName";
2125

2226
import { withSuspenseWrapper } from "./SuspenseWrapper";
@@ -132,30 +136,63 @@ const FavoriteToggleButton = withSuspenseWrapper(
132136
),
133137
);
134138

135-
export const ComponentFavoriteToggle = ({
139+
const useComponentFlags = (component: ComponentReference) => {
140+
const { checkIfUserComponent, componentLibrary } = useComponentLibrary();
141+
142+
const isUserComponent = useMemo(
143+
() => checkIfUserComponent(component),
144+
[component, checkIfUserComponent],
145+
);
146+
147+
const flatComponentList = useMemo(
148+
() => (componentLibrary ? flattenFolders(componentLibrary) : []),
149+
[componentLibrary],
150+
);
151+
152+
const { data: isInLibrary } = useSuspenseQuery({
153+
queryKey: ["component", "flags", component.digest],
154+
queryFn: async () => {
155+
if (!componentLibrary) return false;
156+
157+
if (isUserComponent) return true;
158+
159+
for (const c of flatComponentList) {
160+
if (component.name === "Chicago Taxi Trips dataset") {
161+
console.log(c.name, c.digest, component.digest);
162+
}
163+
164+
if (c.name && c.name !== component.name) {
165+
// micro optimization to skip components with different names
166+
continue;
167+
}
168+
169+
const digest = c.digest ?? (await hydrateComponentReference(c))?.digest;
170+
171+
if (digest === component.digest) {
172+
return true;
173+
}
174+
}
175+
176+
return false;
177+
},
178+
staleTime: 10 * MINUTES,
179+
});
180+
181+
return { isInLibrary, isUserComponent };
182+
};
183+
184+
const ComponentFavoriteToggleInternal = ({
136185
component,
137186
hideDelete = false,
138187
}: ComponentFavoriteToggleProps) => {
139-
const {
140-
addToComponentLibrary,
141-
removeFromComponentLibrary,
142-
checkIfUserComponent,
143-
checkLibraryContainsComponent,
144-
} = useComponentLibrary();
188+
const { addToComponentLibrary, removeFromComponentLibrary } =
189+
useComponentLibrary();
145190

146191
const [isOpen, setIsOpen] = useState(false);
147192

148193
const { spec, url } = component;
149194

150-
const isUserComponent = useMemo(
151-
() => checkIfUserComponent(component),
152-
[component, checkIfUserComponent],
153-
);
154-
155-
const isInLibrary = useMemo(
156-
() => checkLibraryContainsComponent(component),
157-
[component, checkLibraryContainsComponent],
158-
);
195+
const { isInLibrary, isUserComponent } = useComponentFlags(component);
159196

160197
const displayName = useMemo(
161198
() => getComponentName({ spec, url }),
@@ -228,3 +265,13 @@ export const ComponentFavoriteToggle = ({
228265
</>
229266
);
230267
};
268+
269+
export const ComponentFavoriteToggle = withSuspenseWrapper(
270+
ComponentFavoriteToggleInternal,
271+
() => <Spinner size={10} />,
272+
() => (
273+
<IconStateButton disabled>
274+
<Icon name="Star" />
275+
</IconStateButton>
276+
),
277+
);

src/providers/ComponentLibraryProvider/ComponentLibraryProvider.test.tsx

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -536,29 +536,6 @@ describe("ComponentLibraryProvider - Component Management", () => {
536536
result.current.checkIfUserComponent(standardComponent);
537537
expect(isUserComponent).toBe(false);
538538
});
539-
540-
it("should correctly check if library contains component", async () => {
541-
const libraryComponent: ComponentReference = {
542-
name: "library-component",
543-
digest: "library-digest",
544-
spec: mockComponentSpec,
545-
};
546-
547-
mockFlattenFolders.mockReturnValue([libraryComponent]);
548-
mockFilterToUniqueByDigest.mockReturnValue([libraryComponent]);
549-
550-
const { result } = renderHook(() => useComponentLibrary(), {
551-
wrapper: createWrapper,
552-
});
553-
554-
await waitFor(() => {
555-
expect(result.current.isLoading).toBe(false);
556-
});
557-
558-
const containsComponent =
559-
result.current.checkLibraryContainsComponent(libraryComponent);
560-
expect(containsComponent).toBe(true);
561-
});
562539
});
563540

564541
describe("Component Favoriting", () => {

src/providers/ComponentLibraryProvider/ComponentLibraryProvider.tsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ type ComponentLibraryContextType = {
8989
favorited: boolean,
9090
) => void;
9191
checkIfUserComponent: (component: ComponentReference) => boolean;
92-
checkLibraryContainsComponent: (component: ComponentReference) => boolean;
9392

9493
getComponentLibrary: (libraryName: AvailableComponentLibraries) => Library;
9594
};
@@ -340,21 +339,6 @@ export const ComponentLibraryProvider = ({
340339
[userComponentsFolder],
341340
);
342341

343-
const checkLibraryContainsComponent = useCallback(
344-
(component: ComponentReference) => {
345-
if (!componentLibrary) return false;
346-
347-
if (checkIfUserComponent(component)) return true;
348-
349-
const uniqueComponents = filterToUniqueByDigest(
350-
flattenFolders(componentLibrary),
351-
);
352-
353-
return uniqueComponents.some((c) => c.digest === component.digest);
354-
},
355-
[componentLibrary, checkIfUserComponent],
356-
);
357-
358342
/**
359343
* Local component library search
360344
*/
@@ -592,7 +576,6 @@ export const ComponentLibraryProvider = ({
592576
removeFromComponentLibrary,
593577
setComponentFavorite,
594578
checkIfUserComponent,
595-
checkLibraryContainsComponent,
596579
}),
597580
[
598581
componentLibrary,
@@ -609,7 +592,6 @@ export const ComponentLibraryProvider = ({
609592
removeFromComponentLibrary,
610593
setComponentFavorite,
611594
checkIfUserComponent,
612-
checkLibraryContainsComponent,
613595
],
614596
);
615597

0 commit comments

Comments
 (0)