Skip to content

Commit efe015b

Browse files
authored
Merge pull request #842 from ksen0/pr-838-cherrypick
PR838 cherrypick into 2.0
2 parents 1b399f2 + c3d247e commit efe015b

File tree

4 files changed

+87
-53
lines changed

4 files changed

+87
-53
lines changed

src/api/OpenProcessing.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export type OpenProcessingCurationResponse = Array<{
2323
title: string;
2424
/** Description of sketch */
2525
description: string;
26+
instructions: string;
27+
mode: string;
28+
createdOn: string;
2629
userID: string;
2730
submittedOn: string;
2831
/** Author's name */
@@ -36,16 +39,19 @@ export type OpenProcessingCurationResponse = Array<{
3639
* @param limit max number of sketches to return
3740
* @returns sketches
3841
*/
39-
export const getCurationSketches = async (
42+
export const getCurationSketches = memoize(async (
4043
limit?: number,
4144
): Promise<OpenProcessingCurationResponse> => {
4245
const limitParam = limit ? `limit=${limit}` : "";
4346
const response = await fetch(
4447
`${openProcessingEndpoint}curation/${curationId}/sketches?${limitParam}`,
4548
);
49+
if(!response.ok){ //log error instead of throwing error to not cache result in memoize
50+
console.error('getCurationSketches', response.status, response.statusText)
51+
}
4652
const payload = await response.json();
4753
return payload as OpenProcessingCurationResponse;
48-
};
54+
});
4955

5056
/**
5157
* API Response from a call to the Sketch endpoint
@@ -69,26 +75,50 @@ export type OpenProcessingSketchResponse = {
6975

7076
/**
7177
* Get info about a specific sketch from the OpenProcessing API
78+
* First checks if the sketch is in the memoized curated sketches and returns the data if so,
79+
* Otherwise calls OpenProcessing API for this specific sketch
7280
*
7381
* https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#7cd344f6-6e87-426a-969b-2b4a79701dd1
7482
* @param id
7583
* @returns
7684
*/
77-
export const getSketch = memoize(async (
78-
id: string,
79-
): Promise<OpenProcessingSketchResponse> => {
85+
export const getSketch = memoize(
86+
async (id: string): Promise<OpenProcessingSketchResponse> => {
87+
// check for memoized sketch in curation sketches
88+
const curationSketches = await getCurationSketches();
89+
const memoizedSketch = curationSketches.find((el) => el.visualID === id);
90+
if (memoizedSketch) {
91+
return {
92+
...memoizedSketch,
93+
license: "",
94+
} as OpenProcessingSketchResponse;
95+
}
96+
97+
// check for sketch data in Open Processing API
8098
const response = await fetch(`${openProcessingEndpoint}sketch/${id}`);
99+
if (!response.ok) {
100+
//log error instead of throwing error to not cache result in memoize
101+
console.error("getSketch", id, response.status, response.statusText);
102+
}
81103
const payload = await response.json();
82104
return payload as OpenProcessingSketchResponse;
83105
});
84106

107+
/**
108+
* Note: this currently calls `/api/sketch/:id/code`
109+
* But only uses the width and height properties from this call
110+
* Width and height should instead be added to properties for `/api/sketch/:id` or `api/curation/:curationId/sketches` instead
111+
*/
85112
export const getSketchSize = memoize(async (id: string) => {
86113
const sketch = await getSketch(id)
87114
if (sketch.mode !== 'p5js') {
88115
return { width: undefined, height: undefined };
89116
}
90117

91118
const response = await fetch(`${openProcessingEndpoint}sketch/${id}/code`);
119+
if(!response.ok){ //log error instead of throwing error to not cache result in memoize
120+
console.error('getSketchSize', id, response.status, response.statusText)
121+
}
92122
const payload = await response.json();
93123

94124
for (const tab of payload) {

src/layouts/SketchLayout.astro

+48-46
Original file line numberDiff line numberDiff line change
@@ -21,76 +21,83 @@ interface Props {
2121
}
2222
2323
const { sketchId, authorName } = Astro.props;
24-
const sketchInfo = await getSketch(sketchId);
24+
const { title, createdOn, instructions } = await getSketch(sketchId);
2525
2626
const currentLocale = getCurrentLocale(Astro.url.pathname);
2727
const t = await getUiTranslator(currentLocale);
28-
const dateString = new Date(sketchInfo.createdOn).toLocaleDateString(
29-
currentLocale,
30-
{
31-
year: "numeric",
32-
month: "long",
33-
day: "numeric",
34-
}
35-
);
28+
const dateString = new Date(createdOn).toLocaleDateString(currentLocale, {
29+
year: "numeric",
30+
month: "long",
31+
day: "numeric",
32+
});
3633
3734
setJumpToState(null);
3835
const moreSketches = await getRandomCurationSketches(4);
39-
const featuredImageURL = makeThumbnailUrl(sketchInfo.visualID);
36+
const featuredImageURL = makeThumbnailUrl(sketchId);
4037
41-
let { width, height } = await getSketchSize(sketchInfo.visualID);
38+
let { width, height } = await getSketchSize(sketchId);
4239
let heightOverWidth = 1 / 1.5;
4340
if (width && height) {
4441
// Account for OP header bar
4542
height += 50;
4643
heightOverWidth = height / width;
4744
}
4845
49-
const iframeTitle = `OpenProcessing Sketch: ${sketchInfo.title} by ${authorName}`;
46+
const iframeTitle = `OpenProcessing Sketch: ${title} by ${authorName}`;
5047
---
5148

5249
<Head
53-
title={sketchInfo.title}
50+
title={title}
5451
locale={currentLocale}
5552
featuredImageSrc={featuredImageURL}
56-
description={sketchInfo.instructions}
53+
description={instructions}
5754
/>
5855

5956
<BaseLayout
60-
title={sketchInfo.title}
57+
title={title}
6158
titleAuthor={authorName}
6259
subtitle={dateString}
6360
variant="item"
6461
topic={"community"}
6562
>
6663
<div class="max-w-[770px]">
67-
<div style={{
68-
position: 'relative',
69-
width: '100%',
70-
paddingBottom: `${(heightOverWidth * 100).toFixed(4)}%`,
71-
}}>
72-
{width ? (
73-
<ScalingIframe
74-
client:load
75-
src={makeSketchEmbedUrl(sketchInfo.visualID)}
76-
width={width}
77-
height={height}
78-
title={iframeTitle}
79-
/>
80-
) : (
81-
<iframe
82-
src={makeSketchEmbedUrl(sketchInfo.visualID)}
83-
width="100%"
84-
height="100%"
85-
style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}
86-
title={iframeTitle}
87-
/>
88-
)}
64+
<div
65+
style={{
66+
position: "relative",
67+
width: "100%",
68+
paddingBottom: `${(heightOverWidth * 100).toFixed(4)}%`,
69+
}}
70+
>
71+
{
72+
width ? (
73+
<ScalingIframe
74+
client:load
75+
src={makeSketchEmbedUrl(sketchId)}
76+
width={width}
77+
height={height}
78+
title={iframeTitle}
79+
/>
80+
) : (
81+
<iframe
82+
src={makeSketchEmbedUrl(sketchId)}
83+
width="100%"
84+
height="100%"
85+
style={{
86+
position: "absolute",
87+
top: 0,
88+
left: 0,
89+
right: 0,
90+
bottom: 0,
91+
}}
92+
title={iframeTitle}
93+
/>
94+
)
95+
}
8996
</div>
9097
<div class="py-md grid gap-y-sm md:gap-y-md">
9198
<LinkButton
9299
variant="code"
93-
url={`${makeSketchLinkUrl(sketchInfo.visualID)}/#code`}
100+
url={`${makeSketchLinkUrl(sketchId)}/#code`}
94101
class="min-w-[184px] lg:min-w-[220px]">{t("Show Code")}</LinkButton
95102
>
96103
<LinkButton
@@ -100,16 +107,11 @@ const iframeTitle = `OpenProcessing Sketch: ${sketchInfo.title} by ${authorName}
100107
>
101108
</div>
102109

103-
{
104-
sketchInfo.instructions && (
105-
<p class="text-md my-sm md:my-lg">{sketchInfo.instructions}</p>
106-
)
107-
}
110+
{instructions && <p class="text-md my-sm md:my-lg">{instructions}</p>}
108111

109112
<p class="text-xs md:text-base mb-3xl">
110-
This <a
111-
class="text-type-magenta"
112-
href={makeSketchLinkUrl(sketchInfo.visualID)}>sketch</a
113+
This <a class="text-type-magenta" href={makeSketchLinkUrl(sketchId)}
114+
>sketch</a
113115
> is ported from the <a
114116
class="text-type-magenta"
115117
href="https://openprocessing.org">OpenProcessing</a

src/pages/[locale]/community.astro

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import CommunityLayout from "@layouts/CommunityLayout.astro";
55
import { getCollectionInLocaleWithFallbacks } from "@pages/_utils";
66
77
export const getStaticPaths = async () => {
8-
const sketches = await getCurationSketches(10);
8+
const allSketches = await getCurationSketches();
9+
const sketches = allSketches.slice(0, 10);
910
return await Promise.all(
1011
nonDefaultSupportedLocales.map(async (locale) => {
1112
const libraries = await getCollectionInLocaleWithFallbacks(

src/pages/community.astro

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { getCurationSketches } from "../api/OpenProcessing";
33
import CommunityLayout from "../layouts/CommunityLayout.astro";
44
import { getCollectionInDefaultLocale } from "./_utils";
55
6-
const sketches = await getCurationSketches(10);
6+
const allSketches = await getCurationSketches(10);
7+
const sketches = allSketches.slice(0, 10);
78
const libraries = await getCollectionInDefaultLocale("libraries");
89
const pastEvents = await getCollectionInDefaultLocale("events");
910
---

0 commit comments

Comments
 (0)