Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags to examples and hide those marked as experimental #316

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 13 additions & 8 deletions apps/typegpu-docs/src/components/ExampleList.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to bring back categories someday, especially when the amount of examples is going to increase, so maybe we could filter the examples as they are being rendered?

        {exampleCategories.map((category) =>
          (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>
            );
          }),
        )}

Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import {
import { exampleCategories } from '../utils/examples/types';
import { ExampleLink } from './ExampleLink';

function ExampleList() {
function ExampleList({ excludeTags = [] }: { excludeTags?: string[] }) {
const filteredExamples = exampleCategories.flatMap((category) =>
(examplesByCategory[category.key] ?? []).filter(
(example) =>
!example.metadata.tags?.some((tag) => excludeTags.includes(tag)),
),
);

return (
<>
<nav className="flex flex-col flex-1 gap-7 py-4 overflow-y-auto min-w-64">
<ExampleLink key={PLAYGROUND_KEY} exampleKey={PLAYGROUND_KEY}>
Playground
</ExampleLink>
<hr />
{exampleCategories.map((category) =>
(examplesByCategory[category.key] ?? []).map((example) => (
<ExampleLink key={example.key} exampleKey={example.key}>
{example.metadata.title}
</ExampleLink>
)),
)}
{filteredExamples.map((example) => (
<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 @@ -111,6 +111,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 @@ -258,6 +259,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