Please revisit this thing

This commit is contained in:
kolaflash
2025-12-29 18:56:04 +01:00
parent 74d22d425d
commit 398f88ff94
5 changed files with 111 additions and 54 deletions
+40 -25
View File
@@ -5,49 +5,64 @@ export type ListItem = { label: string; isActive: boolean };
export type ItemHandler = (item: ListItem, index: number) => void; export type ItemHandler = (item: ListItem, index: number) => void;
export type Direction = undefined | "asc" | "desc"; export type Direction = "default" | "asc" | "desc";
export function useSort( export function useSort(
items: Array<ListItem>, items: Array<ListItem>,
): (direction?: Direction) => Array<ListItem> { ): [
const itemsRef = [...items]; (direction?: Direction) => Array<ListItem>,
(newItems: Array<ListItem>) => void,
] {
let itemsRef = items;
return (direction) => { return [
if (!direction) { (direction = "default") => {
return [...itemsRef]; if (direction === "default") {
} 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;
} }
const newItems = [...itemsRef];
if (labelA > labelB) { newItems.sort((a, b) => {
return direction === "asc" ? 1 : -1; 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; let direction: Direction = initialDirection;
return () => { return (forcedDirection) => {
if (forcedDirection) {
direction = forcedDirection;
return direction;
}
switch (direction) { switch (direction) {
case "asc": { case "asc": {
direction = "desc"; direction = "desc";
break; break;
} }
case "desc": { case "desc": {
direction = undefined; direction = "default";
break; break;
} }
default: { default: {
+7 -1
View File
@@ -29,7 +29,7 @@
border-bottom: 1px solid var(--c-fg-3); border-bottom: 1px solid var(--c-fg-3);
} }
.remote-item-list-label { .remote-item-list-name {
padding-left: var(--sp-1); padding-left: var(--sp-1);
cursor: pointer; cursor: pointer;
display: flex; display: flex;
@@ -48,3 +48,9 @@
height: 0.875em; height: 0.875em;
} }
} }
.remote-item-list-name.hide {
svg {
rotate: 0.75turn;
}
}
+43 -24
View File
@@ -1,6 +1,8 @@
import "./RemoteItemList.css"; import "./RemoteItemList.css";
import { type Component, createSignal } from "solid-js"; import { type Component, createSignal, Match, Show, Switch } from "solid-js";
import { import {
type Direction,
type ItemHandler,
ItemList, ItemList,
type ListItem, type ListItem,
useDirection, useDirection,
@@ -15,45 +17,62 @@ import {
} from "#/dev/icons/index.ts"; } from "#/dev/icons/index.ts";
export const RemoteItemList: Component<{ export const RemoteItemList: Component<{
fetchItems: () => Promise<Array<ListItem>>; name: string;
}> = () => { fetch: () => Promise<Array<ListItem>>;
const [items, setItems] = createSignal<Array<ListItem>>([ handler: ItemHandler;
{ }> = (props) => {
label: "foo", const [showList, setShowList] = createSignal(true);
isActive: false, const [items, setItems] = createSignal<Array<ListItem>>([]);
}, const [direction, setDirection] = createSignal<Direction>("default");
{ const [sorted, setSorted] = useSort([]);
label: "bar",
isActive: false,
},
{
label: "lol",
isActive: true,
},
]);
const getSorted = useSort(items());
const toggleDirection = useDirection(); const toggleDirection = useDirection();
const fetchItems = () => {
props.fetch().then((items) => {
setSorted(items);
setItems(sorted(direction()));
});
};
fetchItems();
return ( return (
<div class="remote-item-list"> <div class="remote-item-list">
<div class="remote-item-list-header"> <div class="remote-item-list-header">
<div class="remote-item-list-label" role="button"> <div
Items class="remote-item-list-name"
classList={{
hide: !showList(),
}}
role="button"
onClick={() => setShowList(!showList())}
>
{props.name}
<ChevronDownIcon /> <ChevronDownIcon />
</div> </div>
<button <button
type="button" type="button"
onClick={() => { onClick={() => {
setItems(getSorted(toggleDirection())); const direction = toggleDirection();
setDirection(direction);
setItems(sorted(direction));
}} }}
> >
<ArrowsSortIcon /> <Switch fallback={<ArrowsSortIcon />}>
<Match when={direction() === "asc"}>
<ArrowNarrowUpIcon />
</Match>
<Match when={direction() === "desc"}>
<ArrowNarrowDownIcon />
</Match>
</Switch>
</button> </button>
<button type="button"> <button type="button" onClick={fetchItems}>
<ReloadIcon /> <ReloadIcon />
</button> </button>
</div> </div>
<ItemList items={items} handler={console.log} /> <Show when={showList()}>
<ItemList items={items} handler={props.handler} />
</Show>
</div> </div>
); );
}; };
+3 -3
View File
@@ -1,4 +1,4 @@
export const ArrowNarrowDownIcon = () => { export const ArrowNarrowDownIcon = () => (
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
width={24} width={24}
@@ -13,5 +13,5 @@ export const ArrowNarrowDownIcon = () => {
stroke-width={2} stroke-width={2}
d="M12 5v14m4-4l-4 4m-4-4l4 4" d="M12 5v14m4-4l-4 4m-4-4l4 4"
/> />
</svg>; </svg>
}; );
+18 -1
View File
@@ -172,7 +172,24 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({
<FileBrowser root={scenes} handler={setCurrentScene} /> <FileBrowser root={scenes} handler={setCurrentScene} />
<FileBrowser root={entities} handler={setActiveEntity} /> <FileBrowser root={entities} handler={setActiveEntity} />
<RemoteItemList fetchItems={async () => []} /> <RemoteItemList
name={"Sample list"}
handler={console.log}
fetch={async () => [
{
label: "foo",
isActive: false,
},
{
label: "bar",
isActive: false,
},
{
label: "lol",
isActive: true,
},
]}
/>
</div> </div>
<div class="tool-bar-resize" onMouseDown={startResizing}></div> <div class="tool-bar-resize" onMouseDown={startResizing}></div>
</div> </div>