Skip to content

Commit 4478ee7

Browse files
committed
feat: add select all checkbox back
fix: overflowing container
1 parent 5904598 commit 4478ee7

3 files changed

Lines changed: 89 additions & 38 deletions

File tree

packages/slice-machine/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/ImportSlicesFromLibraryModal.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ function TabContent(args: TabContentProps) {
129129
const { children, selected } = args;
130130

131131
if (!selected) {
132-
return <Box display="none">{children}</Box>;
132+
return (
133+
<Box display="none" minHeight={0}>
134+
{children}
135+
</Box>
136+
);
133137
}
134138

135139
return (
136-
<Box display="flex" flexDirection="column" flexGrow={1}>
140+
<Box display="flex" flexDirection="column" flexGrow={1} minHeight={0}>
137141
{children}
138142
</Box>
139143
);
Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { Box, Button, ScrollArea, Text, TextInput } from "@prismicio/editor-ui";
2-
import { useEffect, useState } from "react";
1+
import {
2+
Box,
3+
Button,
4+
Checkbox,
5+
InlineLabel,
6+
ScrollArea,
7+
Text,
8+
TextInput,
9+
} from "@prismicio/editor-ui";
10+
import { useMemo, useState } from "react";
311

412
import { useImportSlicesFromGithub } from "./hooks/useImportSlicesFromGithub";
513
import { useReuseExistingSlicesContext } from "./ReuseExistingSlicesContext";
@@ -12,12 +20,30 @@ export function LibrarySlicesTab() {
1220
const { selectedLibrarySlices, toggleLibrarySlice, setAllLibrarySlices } =
1321
useReuseExistingSlicesContext();
1422

15-
useEffect(() => {
16-
if (slices.length === 0) return;
23+
const { allSelected, someSelected } = useMemo(() => {
24+
if (slices.length === 0) return { allSelected: false, someSelected: false };
25+
return {
26+
allSelected: slices.every((slice) =>
27+
selectedLibrarySlices.some((s) => s.model.id === slice.model.id),
28+
),
29+
someSelected: slices.some((slice) =>
30+
selectedLibrarySlices.some((s) => s.model.id === slice.model.id),
31+
),
32+
};
33+
}, [slices, selectedLibrarySlices]);
1734

18-
// Set all slices as selected by default
19-
setAllLibrarySlices(slices);
20-
}, [slices, setAllLibrarySlices]);
35+
const onSelectAll = (checked: boolean) => {
36+
if (checked) {
37+
setAllLibrarySlices(slices);
38+
} else {
39+
setAllLibrarySlices([]);
40+
}
41+
};
42+
43+
const onImport = async () => {
44+
const fetchedSlices = await handleImportFromGithub(githubUrl);
45+
setAllLibrarySlices(fetchedSlices);
46+
};
2147

2248
if (slices.length === 0) {
2349
return (
@@ -38,7 +64,7 @@ export function LibrarySlicesTab() {
3864
onValueChange={setGithubUrl}
3965
/>
4066
<Button
41-
onClick={() => void handleImportFromGithub(githubUrl)}
67+
onClick={() => void onImport()}
4268
disabled={!githubUrl.trim() || isLoadingSlices}
4369
loading={isLoadingSlices}
4470
color="purple"
@@ -51,31 +77,49 @@ export function LibrarySlicesTab() {
5177
);
5278
}
5379

80+
let selectAllLabel = "Select all slices";
81+
if (allSelected) {
82+
selectAllLabel = `Selected all slices (${selectedLibrarySlices.length})`;
83+
} else if (someSelected) {
84+
selectAllLabel = `${selectedLibrarySlices.length} of ${slices.length} selected`;
85+
}
86+
5487
return (
55-
<ScrollArea stableScrollbar={false}>
56-
<Box
57-
display="grid"
58-
gridTemplateColumns="1fr 1fr 1fr"
59-
gap={16}
60-
padding={16}
61-
>
62-
{slices.map((slice) => {
63-
const isSelected = selectedLibrarySlices.some(
64-
(s) => s.model.id === slice.model.id,
65-
);
66-
return (
67-
<SliceCard
68-
model={slice.model}
69-
thumbnailUrl={slice.thumbnailUrl}
70-
key={slice.model.id}
71-
selected={isSelected}
72-
onSelectedChange={() => {
73-
toggleLibrarySlice(slice);
74-
}}
75-
/>
76-
);
77-
})}
88+
<Box flexDirection="column" flexGrow={1} minHeight={0}>
89+
<Box padding={{ block: 12, inline: 16 }} alignItems="center" gap={8}>
90+
<InlineLabel value={selectAllLabel}>
91+
<Checkbox
92+
checked={allSelected}
93+
indeterminate={someSelected && !allSelected}
94+
onCheckedChange={onSelectAll}
95+
/>
96+
</InlineLabel>
7897
</Box>
79-
</ScrollArea>
98+
<ScrollArea stableScrollbar={false}>
99+
<Box
100+
display="grid"
101+
gridTemplateColumns="1fr 1fr 1fr"
102+
gap={16}
103+
padding={{ inline: 16, bottom: 16 }}
104+
>
105+
{slices.map((slice) => {
106+
const isSelected = selectedLibrarySlices.some(
107+
(s) => s.model.id === slice.model.id,
108+
);
109+
return (
110+
<SliceCard
111+
model={slice.model}
112+
thumbnailUrl={slice.thumbnailUrl}
113+
key={slice.model.id}
114+
selected={isSelected}
115+
onSelectedChange={() => {
116+
toggleLibrarySlice(slice);
117+
}}
118+
/>
119+
);
120+
})}
121+
</Box>
122+
</ScrollArea>
123+
</Box>
80124
);
81125
}

packages/slice-machine/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/hooks/useImportSlicesFromGithub.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function useImportSlicesFromGithub() {
2626
if (!owner || !repo) {
2727
toast.error("Invalid GitHub URL format");
2828
setIsLoadingSlices(false);
29-
return;
29+
throw new Error("Invalid GitHub URL format");
3030
}
3131

3232
const branch = await getDefaultBranch({ owner, repo });
@@ -43,14 +43,14 @@ export function useImportSlicesFromGithub() {
4343
}`,
4444
);
4545
setIsLoadingSlices(false);
46-
return;
46+
throw error;
4747
}
4848

4949
if (libraries.length === 0) {
5050
console.warn("No libraries were found in the SM config.");
5151
setIsLoadingSlices(false);
5252
toast.error("No libraries were found in the SM config.");
53-
return;
53+
throw new Error("No libraries were found in the SM config.");
5454
}
5555

5656
const fetchedSlices = await fetchSlicesFromLibraries({
@@ -63,14 +63,16 @@ export function useImportSlicesFromGithub() {
6363
if (fetchedSlices.length === 0) {
6464
toast.error("Error fetching slices from the repository");
6565
setIsLoadingSlices(false);
66-
return;
66+
throw new Error("No slices were found in the libraries.");
6767
}
6868

6969
setSlices(fetchedSlices);
7070
setIsLoadingSlices(false);
7171
toast.success(
7272
`Found ${fetchedSlices.length} slice(s) from ${libraries.length} library/libraries`,
7373
);
74+
75+
return fetchedSlices;
7476
} catch (error) {
7577
console.error("Error importing from GitHub:", error);
7678
toast.error(
@@ -79,6 +81,7 @@ export function useImportSlicesFromGithub() {
7981
}`,
8082
);
8183
setIsLoadingSlices(false);
84+
return [];
8285
}
8386
};
8487

0 commit comments

Comments
 (0)