diff --git a/dev/ItemList.tsx b/dev/ItemList.tsx index 06176d3..14f2b22 100644 --- a/dev/ItemList.tsx +++ b/dev/ItemList.tsx @@ -5,49 +5,64 @@ export type ListItem = { label: string; isActive: boolean }; export type ItemHandler = (item: ListItem, index: number) => void; -export type Direction = undefined | "asc" | "desc"; +export type Direction = "default" | "asc" | "desc"; export function useSort( items: Array, -): (direction?: Direction) => Array { - const itemsRef = [...items]; +): [ + (direction?: Direction) => Array, + (newItems: Array) => void, +] { + let itemsRef = items; - return (direction) => { - if (!direction) { - return [...itemsRef]; - } - const newItems = [...itemsRef]; - - newItems.sort((a, b) => { - const labelA = a.label.toLocaleLowerCase(); - const labelB = b.label.toLocaleLowerCase(); - - if (labelA < labelB) { - return direction === "asc" ? -1 : 1; + return [ + (direction = "default") => { + if (direction === "default") { + return [...itemsRef]; } + const newItems = [...itemsRef]; - if (labelA > labelB) { - return direction === "asc" ? 1 : -1; - } + newItems.sort((a, b) => { + const labelA = a.label.toLocaleLowerCase(); + const labelB = b.label.toLocaleLowerCase(); - return 0; - }); + if (labelA < labelB) { + return direction === "asc" ? -1 : 1; + } - return newItems; - }; + if (labelA > labelB) { + return direction === "asc" ? 1 : -1; + } + + return 0; + }); + + return newItems; + }, + (newItems) => { + itemsRef = newItems; + }, + ]; } -export function useDirection(initialDirection?: Direction): () => Direction { +export function useDirection( + initialDirection: Direction = "default", +): (forcedDirection?: Direction) => Direction { let direction: Direction = initialDirection; - return () => { + return (forcedDirection) => { + if (forcedDirection) { + direction = forcedDirection; + return direction; + } + switch (direction) { case "asc": { direction = "desc"; break; } case "desc": { - direction = undefined; + direction = "default"; break; } default: { diff --git a/dev/RemoteItemList.css b/dev/RemoteItemList.css index c580c65..f6a4498 100644 --- a/dev/RemoteItemList.css +++ b/dev/RemoteItemList.css @@ -29,7 +29,7 @@ border-bottom: 1px solid var(--c-fg-3); } -.remote-item-list-label { +.remote-item-list-name { padding-left: var(--sp-1); cursor: pointer; display: flex; @@ -48,3 +48,9 @@ height: 0.875em; } } + +.remote-item-list-name.hide { + svg { + rotate: 0.75turn; + } +} diff --git a/dev/RemoteItemList.tsx b/dev/RemoteItemList.tsx index b394ce6..0d33976 100644 --- a/dev/RemoteItemList.tsx +++ b/dev/RemoteItemList.tsx @@ -1,6 +1,8 @@ import "./RemoteItemList.css"; -import { type Component, createSignal } from "solid-js"; +import { type Component, createSignal, Match, Show, Switch } from "solid-js"; import { + type Direction, + type ItemHandler, ItemList, type ListItem, useDirection, @@ -15,45 +17,62 @@ import { } from "#/dev/icons/index.ts"; export const RemoteItemList: Component<{ - fetchItems: () => Promise>; -}> = () => { - const [items, setItems] = createSignal>([ - { - label: "foo", - isActive: false, - }, - { - label: "bar", - isActive: false, - }, - { - label: "lol", - isActive: true, - }, - ]); - const getSorted = useSort(items()); + name: string; + fetch: () => Promise>; + handler: ItemHandler; +}> = (props) => { + const [showList, setShowList] = createSignal(true); + const [items, setItems] = createSignal>([]); + const [direction, setDirection] = createSignal("default"); + const [sorted, setSorted] = useSort([]); const toggleDirection = useDirection(); + const fetchItems = () => { + props.fetch().then((items) => { + setSorted(items); + setItems(sorted(direction())); + }); + }; + fetchItems(); + return (
-
- Items +
setShowList(!showList())} + > + {props.name}
-
- + + +
); }; diff --git a/dev/icons/ArrowNarrowDown.tsx b/dev/icons/ArrowNarrowDown.tsx index 721343c..06cf356 100644 --- a/dev/icons/ArrowNarrowDown.tsx +++ b/dev/icons/ArrowNarrowDown.tsx @@ -1,4 +1,4 @@ -export const ArrowNarrowDownIcon = () => { +export const ArrowNarrowDownIcon = () => ( { stroke-width={2} d="M12 5v14m4-4l-4 4m-4-4l4 4" /> - ; -}; + +); diff --git a/dev/main.tsx b/dev/main.tsx index 8520a83..3f6d6a2 100644 --- a/dev/main.tsx +++ b/dev/main.tsx @@ -172,7 +172,24 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({ - []} /> + [ + { + label: "foo", + isActive: false, + }, + { + label: "bar", + isActive: false, + }, + { + label: "lol", + isActive: true, + }, + ]} + />