Skip to content

Commit

Permalink
Add tags to examples and hide those marked as experimental (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
reczkok authored Sep 16, 2024
1 parent b13f8b6 commit d29f064
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 23 deletions.
4 changes: 3 additions & 1 deletion apps/typegpu-docs/src/components/ExampleLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { menuShownAtom } from '../utils/examples/menuShownAtom';
import ExampleList from './ExampleList';
import ExamplePage from './ExamplePage';

const isDev = import.meta.env.DEV;

export function ExampleLayout() {
const menuShown = useAtomValue(menuShownAtom);

Expand Down Expand Up @@ -49,7 +51,7 @@ function SideMenu() {

<hr />

<ExampleList />
<ExampleList excludeTags={isDev ? [] : ['experimental']} />

<div className="flex justify-between text-tameplum-800 text-xs">
<div>&copy; 2024 Software Mansion S.A.</div>
Expand Down
20 changes: 14 additions & 6 deletions apps/typegpu-docs/src/components/ExampleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { examplesByCategory } from '../utils/examples/exampleContent';
import { exampleCategories } from '../utils/examples/types';
import { ExampleLink } from './ExampleLink';

function ExampleList() {
function ExampleList({ excludeTags = [] }: { excludeTags?: string[] }) {
return (
<>
<nav className="flex flex-col flex-1 gap-7 py-4 overflow-y-auto min-w-64">
Expand All @@ -11,11 +11,19 @@ function ExampleList() {
</ExampleLink>
<hr /> */}
{exampleCategories.map((category) =>
(examplesByCategory[category.key] ?? []).map((example) => (
<ExampleLink key={example.key} exampleKey={example.key}>
{example.metadata.title}
</ExampleLink>
)),
(examplesByCategory[category.key] ?? []).map((example) => {
if (
example.metadata.tags?.some((tag) => excludeTags.includes(tag))
) {
return null;
}

return (
<ExampleLink key={example.key} exampleKey={example.key}>
{example.metadata.title}
</ExampleLink>
);
}),
)}
</nav>
</>
Expand Down
15 changes: 11 additions & 4 deletions apps/typegpu-docs/src/components/ExampleView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Props = {
function useExample(
exampleCode: string,
setSnackbarText: (text: string | undefined) => void,
tags?: string[],
) {
const exampleRef = useRef<ExampleState | null>(null);
const setExampleControlParams = useSetAtom(exampleControlsAtom);
Expand All @@ -38,7 +39,7 @@ function useExample(
let cancelled = false;
setSnackbarText(undefined);

executeExample(exampleCode, createLayout)
executeExample(exampleCode, createLayout, tags)
.then((example) => {
if (cancelled) {
// Another instance was started in the meantime.
Expand Down Expand Up @@ -67,7 +68,13 @@ function useExample(
exampleRef.current?.dispose();
cancelled = true;
};
}, [exampleCode, createLayout, setSnackbarText, setExampleControlParams]);
}, [
exampleCode,
createLayout,
setSnackbarText,
setExampleControlParams,
tags,
]);

return {
def,
Expand All @@ -76,7 +83,7 @@ function useExample(
}

export function ExampleView({ example, isPlayground = false }: Props) {
const { code: initialCode } = example;
const { code: initialCode, metadata } = example;
const [code, setCode] = useState(initialCode);
const [snackbarText, setSnackbarText] = useState<string | undefined>();
const setCurrentExample = useSetAtom(currentExampleAtom);
Expand Down Expand Up @@ -107,7 +114,7 @@ export function ExampleView({ example, isPlayground = false }: Props) {
setCodeDebouncer.call(newCode);
});

const { def, setRef } = useExample(code, setSnackbarText);
const { def, setRef } = useExample(code, setSnackbarText, metadata.tags);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Matrix Multiplication",
"category": "algorithms"
"category": "algorithms",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Image Blur",
"category": "image-processing"
"category": "image-processing",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Camera Thresholding",
"category": "image-processing"
"category": "image-processing",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Chroma Keying",
"category": "image-processing"
"category": "image-processing",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Box Raytracing",
"category": "rendering"
"category": "rendering",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Gradient Tiles",
"category": "simple"
"category": "simple",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Increment (TGSL)",
"category": "simple"
"category": "simple",
"tags": ["experimental"]
}
*/

Expand Down
3 changes: 2 additions & 1 deletion apps/typegpu-docs/src/content/examples/simple/increment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Increment",
"category": "simple"
"category": "simple",
"tags": ["experimental"]
}
*/

Expand Down
3 changes: 2 additions & 1 deletion apps/typegpu-docs/src/content/examples/simulation/boids.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Boids",
"category": "simulation"
"category": "simulation",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Fluid (double-buffering)",
"category": "simulation"
"category": "simulation",
"tags": ["experimental"]
}
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
{
"title": "Fluid (with atomics)",
"category": "simulation"
"category": "simulation",
"tags": ["experimental"]
}
*/

Expand Down
6 changes: 6 additions & 0 deletions apps/typegpu-docs/src/utils/examples/exampleRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function tsToJs(code: string): string {
export async function executeExample(
exampleCode: string,
createLayout: () => LayoutInstance,
tags?: string[],
): Promise<ExampleState> {
const cleanupCallbacks: (() => unknown)[] = [];

Expand Down Expand Up @@ -298,6 +299,11 @@ export async function executeExample(
return await import('typegpu/experimental');
}
if (moduleKey === 'typegpu/experimental') {
if (!tags?.includes('experimental')) {
throw new Error(
'Examples not labeled as experimental cannot import experimental modules.',
);
}
return await import('typegpu/experimental');
}
if (moduleKey === 'typegpu/data') {
Expand Down
1 change: 1 addition & 0 deletions apps/typegpu-docs/src/utils/examples/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type ExampleMetadata = z.infer<typeof ExampleMetadata>;
export const ExampleMetadata = z.object({
title: z.string(),
category: z.string(),
tags: z.array(z.string()).optional(),
});

export const exampleCategories = [
Expand Down
1 change: 0 additions & 1 deletion packages/typegpu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const tgpu = {
Uniform,
Storage,
Vertex,

createBuffer,
read,
write,
Expand Down

0 comments on commit d29f064

Please sign in to comment.