diff options
| author | Elad Rosenheim <eladroz@users.noreply.github.com> | 2024-04-23 17:55:24 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 17:55:24 +0300 |
| commit | 7e40aa188979311e706154dfcf52e47975f99cde (patch) | |
| tree | 1368cc922524b5b1eb7eef4684fccab0fbc3628d /src/pages/blobs | |
| parent | 5396de306695226c939384598410761297739fef (diff) | |
Revalidation & Blobs pages, tweaks (#8)
* added blobs.astro page
* updated blobs
* updated index.astro and npm dependencies
* updated api blobs
* Revalidation example WIP
* Revalidation by tag example
* Update revalidation.astro
* Image CDN fixes
* Text fixes
* updated colors (#9)
* updated colors
* updated README.md
* updated Layout.astro bg color
* updated template colors
* Inline Inter font
* Inline noise.png
* Remove remaining "Astro.js"
---------
Co-authored-by: TomasBankauskas <tbdizainas@gmail.com>
Diffstat (limited to 'src/pages/blobs')
| -rw-r--r-- | src/pages/blobs/_components/NewShape.tsx | 60 | ||||
| -rw-r--r-- | src/pages/blobs/_components/ShapeEditor.tsx | 18 | ||||
| -rw-r--r-- | src/pages/blobs/_components/ShapePreview.tsx | 19 | ||||
| -rw-r--r-- | src/pages/blobs/_components/StoredShapes.tsx | 78 | ||||
| -rw-r--r-- | src/pages/blobs/index.astro | 18 |
5 files changed, 193 insertions, 0 deletions
diff --git a/src/pages/blobs/_components/NewShape.tsx b/src/pages/blobs/_components/NewShape.tsx new file mode 100644 index 0000000..05f9e6f --- /dev/null +++ b/src/pages/blobs/_components/NewShape.tsx @@ -0,0 +1,60 @@ +import { useState, useEffect } from 'react'; +import type { Dispatch, SetStateAction } from 'react'; +import ShapePreview from './ShapePreview.tsx'; +import { generateBlob } from '../../../utils'; +import type { BlobProps } from '../../../types.ts'; + +interface Props { + setLastMutationTime?: Dispatch<SetStateAction<number>>; +} + +export default function NewShape(props: Props) { + const { setLastMutationTime } = props; + const [blobData, setBlobData] = useState<BlobProps>(); + const [wasUploaded, setWasUploaded] = useState<boolean>(false); + + const randomizeBlob = () => { + setBlobData(generateBlob()); + setWasUploaded(false); + }; + + const uploadBlob = async () => { + const response = await fetch('/api/blobs', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(blobData.parameters) + }); + const data = await response.json(); + if (data.message) { + console.log(data.message); + } + setWasUploaded(true); + setLastMutationTime(Date.now()); + }; + + useEffect(() => { + if (!blobData) { + randomizeBlob(); + } + }, [blobData]); + + return ( + <> + <h2 className="mb-4 text-xl text-center sm:text-xl">New Random Shape</h2> + <div className="w-full bg-white rounded-lg mb-6"> + <div className="min-h-14 p-4 border-b border-neutral-200 text-neutral-900 text-center"> + {blobData && <span>{blobData.parameters?.name}</span>} + </div> + <div className="p-4 aspect-square text-primary">{blobData && <ShapePreview {...blobData} />}</div> + </div> + <div className="flex flex-wrap justify-center gap-4"> + <button className="btn btn-primary" onClick={randomizeBlob}> + Randomize + </button> + <button className="btn btn-primary" onClick={uploadBlob} disabled={wasUploaded || !blobData}> + Upload + </button> + </div> + </> + ); +} diff --git a/src/pages/blobs/_components/ShapeEditor.tsx b/src/pages/blobs/_components/ShapeEditor.tsx new file mode 100644 index 0000000..9a339cc --- /dev/null +++ b/src/pages/blobs/_components/ShapeEditor.tsx @@ -0,0 +1,18 @@ +import { useState } from 'react'; +import NewShape from './NewShape.tsx'; +import StoredShapes from './StoredShapes.tsx'; + +export default function ShapeEditor() { + const [lastMutationTime, setLastMutationTime] = useState<number>(null); + + return ( + <div className="flex flex-col gap-8 md:flex-row"> + <div className="flex-1"> + <NewShape setLastMutationTime={setLastMutationTime} /> + </div> + <div className="flex-1"> + <StoredShapes lastMutationTime={lastMutationTime} /> + </div> + </div> + ); +} diff --git a/src/pages/blobs/_components/ShapePreview.tsx b/src/pages/blobs/_components/ShapePreview.tsx new file mode 100644 index 0000000..a00d2bc --- /dev/null +++ b/src/pages/blobs/_components/ShapePreview.tsx @@ -0,0 +1,19 @@ +import { randomInt } from '../../../utils.ts'; +import type { BlobProps } from '../../../types.ts'; + +export default function ShapePreview(props: BlobProps) { + const { svgPath, parameters } = props; + const gradientId = `gradient-${randomInt(10_000_000, 100_000_000)}`; + + return ( + <svg viewBox={`0 0 ${parameters.size} ${parameters.size}`} xmlns="http://www.w3.org/2000/svg" width="100%"> + <defs> + <linearGradient id={gradientId} x1="0%" y1="0%" x2="0%" y2="100%"> + <stop offset="0%" style={{ stopColor: parameters.colors[0] }} /> + <stop offset="100%" style={{ stopColor: parameters.colors[1] }} /> + </linearGradient> + </defs> + <path d={svgPath} fill={`url(#${gradientId})`}></path> + </svg> + ); +} diff --git a/src/pages/blobs/_components/StoredShapes.tsx b/src/pages/blobs/_components/StoredShapes.tsx new file mode 100644 index 0000000..e870ef5 --- /dev/null +++ b/src/pages/blobs/_components/StoredShapes.tsx @@ -0,0 +1,78 @@ +import { useState, useEffect } from 'react'; +import ShapePreview from './ShapePreview.tsx'; +import { generateBlob } from '../../../utils'; +import type { BlobProps } from '../../../types.ts'; + +interface Props { + lastMutationTime: number; +} + +export default function StoredShapes(props: Props) { + const { lastMutationTime } = props; + const [keys, setKeys] = useState<string[]>([]); + const [selectedKey, setSelectedKey] = useState<string>(null); + const [previewData, setPreviewData] = useState<BlobProps>(null); + + const getBlobKeyList = async () => { + console.log('Fetching keys...'); + const response = await fetch('/api/blobs', { + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }); + const data = await response.json(); + if (data.keys) { + setKeys(data.keys); + } + }; + + const getBlobByKey = async (key: string) => { + setSelectedKey(key); + const params = new URLSearchParams({ key }); + const response = await fetch(`/api/blob/?${params}`, { + method: 'GET' + }); + const data = await response.json(); + if (data.blob) { + setPreviewData(generateBlob(data.blob)); + } + }; + + useEffect(() => { + getBlobKeyList(); + }, [lastMutationTime]); + + return ( + <> + <h2 className="mb-4 text-xl text-center sm:text-xl">Objects in Blob Store</h2> + <div className="w-full bg-white rounded-lg"> + <div className="min-h-14 p-4 text-center"> + {keys?.length ? ( + <div className="space-y-1"> + {keys.map((keyName) => ( + <button + key={keyName} + className={ + 'btn btn-ghost btn-sm btn-block text-neutral-900 font-normal' + + (selectedKey === keyName ? ' bg-base-content/20 pointer-events-none' : '') + } + onClick={() => { + getBlobByKey(keyName); + }} + > + {keyName} + </button> + ))} + </div> + ) : ( + <span className="text-neutral-900">Please upload some shapes!</span> + )} + </div> + {previewData && ( + <div className="p-4 aspect-square text-primary border-t border-neutral-200"> + <ShapePreview {...previewData} /> + </div> + )} + </div> + </> + ); +} diff --git a/src/pages/blobs/index.astro b/src/pages/blobs/index.astro new file mode 100644 index 0000000..5630d6b --- /dev/null +++ b/src/pages/blobs/index.astro @@ -0,0 +1,18 @@ +--- +import Layout from '../../layouts/Layout.astro'; +import Markdown from '../../components/Markdown.astro'; +import ShapeEditor from './_components/ShapeEditor.tsx'; + +const explainer = ` +[Netlify Blobs](https://docs.netlify.com/blobs/overview/) provides an object store for any kind of data, be it JSON, binary, +or [really](https://mk.gg/projects/chalkstream) anything else ([really!](https://mk.gg/projects/turbofan)). In this example, the blob store is used to **hold the data of user-generated random blobby shapes**. + +Below is an Astro island with a React component for editing, uploading and viewing blobs to the blob store 🙂. +`; +--- + +<Layout title="Blobs"> + <h1 class="mb-8">Blobs x Blobs</h1> + <Markdown content={explainer} class="mb-12" /> + <ShapeEditor client:load /> +</Layout> |
