Skip to content

Commit

Permalink
New compare tool to build 2d gallery grid
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkennedy22 committed May 19, 2024
1 parent 890db09 commit 245c688
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 10 deletions.
12 changes: 9 additions & 3 deletions public/editor/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@ import { TopBar } from "./TopBar";
import { MainFace } from "./MainFace";
import { EditJsonModal } from "./EditJsonModal";
import { Credits } from "./Credits";
import { CompareFaceModal } from "./CompareFaceModal";

const App = () => {
const modalDisclosure = useDisclosure();
const uploadModalDisclosure = useDisclosure();
const compareModalDisclosure = useDisclosure();

return (
<NextUIProvider>
<TopBar />
<div className="flex flex-col-reverse md:flex-row items-center md:items-start pt-16 gap-2 mx-2">
<FeatureGallery />
<div className="md:sticky md:top-16 flex-shrink-0 w-[280px] lg:w-[350px] xl:w-[400px] flex flex-col h-full">
<MainFace modalDisclosure={modalDisclosure} />
<MainFace
uploadModalDisclosure={uploadModalDisclosure}
compareModalDisclosure={compareModalDisclosure}
/>

<div className="hidden md:block mt-6">
<Credits />
</div>
</div>
</div>
<EditJsonModal modalDisclosure={modalDisclosure} />
<EditJsonModal modalDisclosure={uploadModalDisclosure} />
<CompareFaceModal modalDisclosure={compareModalDisclosure} />
<div className="md:hidden mb-2 mx-2">
<Credits />
</div>
Expand Down
185 changes: 185 additions & 0 deletions public/editor/CompareFaceModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { useState } from "react";
import override from "../../src/override";
import { useStateStore } from "./stateStore";
import {
Modal,
ModalContent,
ModalHeader,
ModalBody,
type useDisclosure,
Select,
SelectItem,
Divider,
} from "@nextui-org/react";
import { getOverrideListForItem } from "./overrideList";
import { Face } from "../../src/Face";
import { deepCopy, getFromDict, setToDict } from "./utils";

const CompareFaceGrid = ({
featureA,
featureB,
}: {
featureA: string;
featureB: string;
}) => {
const { gallerySectionConfigList, faceConfig, setFaceStore } =
useStateStore();
const featureAConfigItem = gallerySectionConfigList.find(
(gallerySectionConfig) => gallerySectionConfig.key === featureA,
);
const featureBConfigItem = gallerySectionConfigList.find(
(gallerySectionConfig) => gallerySectionConfig.key === featureB,
);

if (!featureAConfigItem || !featureBConfigItem) {
return null;
}

let featureAOverrideList = getOverrideListForItem(featureAConfigItem);
let featureBOverrideList = getOverrideListForItem(featureBConfigItem);

if (featureBOverrideList.length > featureAOverrideList.length) {
[featureAOverrideList, featureBOverrideList] = [
featureBOverrideList,
featureAOverrideList,
];
}

const columns = featureBOverrideList.length;

const gridStyle = {
display: "grid",
gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))`,
gap: "1rem",
};

return (
<div
className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"
style={gridStyle}
>
{featureAOverrideList.flatMap((featureAOverride, indexA) =>
featureBOverrideList.map((featureBOverride, indexB) => {
const faceConfigCopy = deepCopy(faceConfig);
let faceOverride = {};

Check failure on line 64 in public/editor/CompareFaceModal.tsx

View workflow job for this annotation

GitHub Actions / build

'faceOverride' is never reassigned. Use 'const' instead
setToDict(faceOverride, featureA, featureAOverride.value);
setToDict(faceOverride, featureB, featureBOverride.value);
override(faceConfigCopy, faceOverride);
const selected =
getFromDict(faceConfig, featureA) === featureAOverride.value &&
getFromDict(faceConfig, featureB) === featureBOverride.value;

const faceIndex = indexA * columns + indexB;
return (
<div
key={faceIndex}
className={`rounded-lg cursor-pointer hover:bg-slate-100 hover:border-slate-500 border-2 border-inherit flex justify-center active:scale-90 transition-transform ease-in-out${selected ? " bg-slate-200 hover:border-slate-500" : ""}`}
onClick={() => {
setFaceStore(faceConfigCopy);
}}
>
<Face face={faceConfigCopy} style={{ width: "150px" }} lazy />
</div>
);
}),
)}
</div>
);
};

export const CompareFaceModal = ({
modalDisclosure,
}: {
modalDisclosure: ReturnType<typeof useDisclosure>;
}) => {
const { isOpen, onOpenChange } = modalDisclosure;

const { gallerySectionConfigList } = useStateStore();

const [firstSelectInvalid, setFirstSelectInvalid] = useState(true);
const [secondSelectInvalid, setSecondSelectInvalid] = useState(true);

const [firstSelectChoice, setFirstSelectChoice] = useState<string>("");
const [secondSelectChoice, setSecondSelectChoice] = useState<string>("");

return (
<Modal
shadow="md"
size="5xl"
// className='min-w-2/3'
// style={{ width: '90%', maxWidth: '90%' }}
isOpen={isOpen}
onOpenChange={onOpenChange}
scrollBehavior="inside"
>
<ModalContent>
{(_) => (
<>
<ModalHeader>Compare face configs</ModalHeader>
<ModalBody className="overflow-y-visible">
<div className="flex justify-start gap-4 h-full">
<Select
label="First feature"
className="max-w-xs"
isInvalid={firstSelectInvalid}
selectedKeys={[firstSelectChoice]}
onChange={(e) => {
setFirstSelectChoice(e.target.value);
setFirstSelectInvalid(false);
}}
>
{gallerySectionConfigList.map((gallerySectionConfig) => (
<SelectItem
key={gallerySectionConfig.key}
value={gallerySectionConfig.key}
isDisabled={
gallerySectionConfig.key === secondSelectChoice
}
>
{gallerySectionConfig.text}
</SelectItem>
))}
</Select>
<Select
label="Second feature"
className="max-w-xs"
isInvalid={secondSelectInvalid}
selectedKeys={[secondSelectChoice]}
onChange={(e) => {
setSecondSelectChoice(e.target.value);
setSecondSelectInvalid(false);
}}
>
{gallerySectionConfigList.map((gallerySectionConfig) => (
<SelectItem
key={gallerySectionConfig.key}
value={gallerySectionConfig.key}
isDisabled={
gallerySectionConfig.key === firstSelectChoice
}
>
{gallerySectionConfig.text}
</SelectItem>
))}
</Select>
</div>
</ModalBody>
<Divider />
{!(firstSelectInvalid || secondSelectInvalid) ? (
<ModalBody className="">
<CompareFaceGrid
featureA={firstSelectChoice}
featureB={secondSelectChoice}
/>
</ModalBody>
) : (
<ModalBody className="">
<p>Please select two different features</p>
</ModalBody>
)}
</>
)}
</ModalContent>
</Modal>
);
};
29 changes: 22 additions & 7 deletions public/editor/MainFace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UploadSimple,
LinkSimple,
CaretDown,
MagnifyingGlass,
} from "@phosphor-icons/react";
import {
Button,
Expand All @@ -28,14 +29,17 @@ const copyStringToClipboard = async (str: string) => {

const MainFaceActionBar = ({
faceRef,
modalDisclosure,
uploadModalDisclosure,
compareModalDisclosure,
}: {
faceRef: RefObject<HTMLDivElement>;
modalDisclosure: ReturnType<typeof useDisclosure>;
uploadModalDisclosure: ReturnType<typeof useDisclosure>;
compareModalDisclosure: ReturnType<typeof useDisclosure>;
}) => {
const { faceConfig } = useStateStore();

const { onOpen } = modalDisclosure;
const { onOpen: onUploadOpen } = uploadModalDisclosure;
const { onOpen: onCompareOpen } = compareModalDisclosure;

const dropdownConfig = [
{
Expand Down Expand Up @@ -97,7 +101,12 @@ const MainFaceActionBar = ({
{
groupName: "Upload",
groupIcon: <UploadSimple size={24} />,
baseAction: onOpen,
baseAction: onUploadOpen,
},
{
groupName: "Compare",
groupIcon: <MagnifyingGlass size={24} />,
baseAction: onCompareOpen,
},
];

Expand Down Expand Up @@ -157,17 +166,23 @@ const MainFaceActionBar = ({
};

export const MainFace = ({
modalDisclosure,
uploadModalDisclosure,
compareModalDisclosure,
}: {
modalDisclosure: ReturnType<typeof useDisclosure>;
uploadModalDisclosure: ReturnType<typeof useDisclosure>;
compareModalDisclosure: ReturnType<typeof useDisclosure>;
}) => {
const { faceConfig } = useStateStore();
const ref = useRef<HTMLDivElement>(null);

return (
<div className="border-5 border-slate-800 rounded-lg shadow-medium">
<Face face={faceConfig} style={{ width: "400px" }} ref={ref} />
<MainFaceActionBar modalDisclosure={modalDisclosure} faceRef={ref} />
<MainFaceActionBar
uploadModalDisclosure={uploadModalDisclosure}
compareModalDisclosure={compareModalDisclosure}
faceRef={ref}
/>
</div>
);
};

0 comments on commit 245c688

Please sign in to comment.