Skip to content

Commit 6e695a7

Browse files
drew-harrisskeptrunedev
authored andcommitted
feat: magicbox
1 parent 93e2b96 commit 6e695a7

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

frontends/dashboard/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"typecheck": "tsc --noEmit"
1414
},
1515
"dependencies": {
16+
"cva": "npm:class-variance-authority",
1617
"@nozbe/microfuzz": "^1.0.0",
1718
"@sentry/browser": "^7.110.0",
1819
"@solidjs/router": "^0.10.1",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { CreateQueryResult } from "@tanstack/solid-query";
2+
import { cva, VariantProps } from "cva";
3+
import { children, createMemo, JSX } from "solid-js";
4+
import { Show } from "solid-js";
5+
6+
interface MagicBoxProps<D extends CreateQueryResult>
7+
extends VariantProps<typeof container> {
8+
class?: string;
9+
fallback?: JSX.Element;
10+
id?: string;
11+
heightKey?: string;
12+
query: D;
13+
children: (data: NonNullable<D["data"]>) => JSX.Element;
14+
}
15+
16+
const container = cva([], {
17+
variants: {
18+
styled: {
19+
true: "bg-white border border-neutral-300 p-4 shadow-sm",
20+
false: "",
21+
},
22+
},
23+
defaultVariants: {
24+
styled: true,
25+
},
26+
});
27+
28+
export const MagicBox = <D extends CreateQueryResult>(
29+
props: MagicBoxProps<D>,
30+
) => {
31+
const children = createMemo(() => {
32+
return props.children(props.query.data as NonNullable<D["data"]>);
33+
});
34+
35+
const skeletonHeight = createMemo(() => {
36+
if (props.heightKey) {
37+
if (props.query.status === "success") {
38+
console.log("saving height");
39+
// save height of div to local storage
40+
const height = document.getElementById(`skeleton-${props.heightKey}`)
41+
?.clientHeight;
42+
if (height) {
43+
console.log("saving height", height);
44+
localStorage.setItem(
45+
`skeleton-${props.heightKey}`,
46+
height.toString(),
47+
);
48+
}
49+
} else {
50+
// get height from local storage
51+
const height = localStorage.getItem(`skeleton-${props.heightKey}`);
52+
if (height) {
53+
console.log("restoring height", height);
54+
return `${height}px`;
55+
} else {
56+
return "auto";
57+
}
58+
}
59+
} else {
60+
return "auto";
61+
}
62+
});
63+
64+
return (
65+
<div
66+
style={{ height: skeletonHeight() }}
67+
id={`skeleton-${props.heightKey}`}
68+
class={container({ ...props, class: props.class })}
69+
>
70+
<Show
71+
fallback={props.fallback || <div>Loading...</div>}
72+
when={props.query.data}
73+
>
74+
{children()}
75+
</Show>
76+
</div>
77+
);
78+
};

frontends/dashboard/src/layouts/NavbarLayout.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { JSX, useContext } from "solid-js";
2-
import { UserContext } from "../contexts/UserContext";
3-
import { A, useNavigate } from "@solidjs/router";
1+
import { JSX } from "solid-js";
2+
import { A } from "@solidjs/router";
43
import { BiRegularLinkExternal } from "solid-icons/bi";
54

65
interface NavbarLayoutProps {
@@ -42,7 +41,7 @@ export const NavbarLayout = (props: NavbarLayoutProps) => {
4241
</a>
4342
</div>
4443
</div>
45-
<div class="flex grow flex-col overflow-scroll bg-purple-200">
44+
<div class="flex grow flex-col overflow-scroll bg-neutral-100">
4645
{props.children}
4746
</div>
4847
</div>
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1-
import { useContext } from "solid-js";
1+
import { For, useContext } from "solid-js";
22
import { DatasetContext } from "../../contexts/DatasetContext";
3+
import { createQuery } from "@tanstack/solid-query";
4+
import { MagicBox } from "../../components/MagicBox";
5+
6+
const slowFetchRandomNumber = (delay: number): Promise<number[]> => {
7+
return new Promise((resolve) => {
8+
setTimeout(() => {
9+
resolve([23, 23, 24]);
10+
}, delay);
11+
});
12+
};
313

414
export const DatasetHomepage = () => {
515
const { datasetId } = useContext(DatasetContext);
16+
17+
const numQuery = createQuery(() => ({
18+
queryKey: ["randomNumber"],
19+
queryFn: () => slowFetchRandomNumber(2000),
20+
}));
21+
622
return (
7-
<div>
23+
<div class="p-4">
824
<div>Dataset Homepage</div>
925
<div class="m-3 bg-orange-700">ID: {datasetId}</div>
26+
<MagicBox heightKey="big-tes" query={numQuery}>
27+
{(d) => (
28+
<div>
29+
testing
30+
<For each={d}>
31+
{(num) => <div class="m-3 bg-orange-700">{num}</div>}
32+
</For>
33+
</div>
34+
)}
35+
</MagicBox>
1036
</div>
1137
);
1238
};

0 commit comments

Comments
 (0)