diff options
| author | Elad Rosenheim <eladroz@users.noreply.github.com> | 2024-04-23 19:48:09 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 19:48:09 +0300 |
| commit | 6eb143cda881ba4dd26ab84bd85673f0fe94c1b8 (patch) | |
| tree | d946a3b4b567f0ee2590c9b45d3e2673db89626f /src/pages | |
| parent | e028782c14356a5efaaa72b01cf34c30a4357df6 (diff) | |
Support disabling uploads + fonts fix (#11)
* Inline Netlify logo, preload fonts
* Support disabling blobs upload + fonts fix
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/api/blobs.ts | 29 | ||||
| -rw-r--r-- | src/pages/blobs/_components/NewShape.tsx | 4 | ||||
| -rw-r--r-- | src/pages/blobs/index.astro | 15 |
3 files changed, 38 insertions, 10 deletions
diff --git a/src/pages/api/blobs.ts b/src/pages/api/blobs.ts index d11aa58..6c2901b 100644 --- a/src/pages/api/blobs.ts +++ b/src/pages/api/blobs.ts @@ -1,9 +1,12 @@ import type { APIRoute } from 'astro'; import { getStore } from '@netlify/blobs'; +import { uploadDisabled } from '../../utils'; export const prerender = false; export const POST: APIRoute = async ({ request }) => { + if (uploadDisabled) throw new Error('Sorry, uploads are disabled'); + const parameters = await request.json(); const blobStore = getStore('shapes'); const key = parameters.name; @@ -16,12 +19,22 @@ export const POST: APIRoute = async ({ request }) => { }; export const GET: APIRoute = async ({ request }) => { - const blobStore = getStore({ name: 'shapes', consistency: 'strong' }); - const data = await blobStore.list(); - const keys = data.blobs.map(({ key }) => key); - return new Response( - JSON.stringify({ - keys - }) - ); + try { + const blobStore = getStore({ name: 'shapes', consistency: 'strong' }); + const data = await blobStore.list(); + const keys = data.blobs.map(({ key }) => key); + return new Response( + JSON.stringify({ + keys + }) + ); + } catch (e) { + console.error(e); + return new Response( + JSON.stringify({ + keys: [], + error: 'Failed listing blobs' + }) + ); + } }; diff --git a/src/pages/blobs/_components/NewShape.tsx b/src/pages/blobs/_components/NewShape.tsx index 05f9e6f..a141177 100644 --- a/src/pages/blobs/_components/NewShape.tsx +++ b/src/pages/blobs/_components/NewShape.tsx @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react'; import type { Dispatch, SetStateAction } from 'react'; import ShapePreview from './ShapePreview.tsx'; -import { generateBlob } from '../../../utils'; +import { generateBlob, uploadDisabled } from '../../../utils'; import type { BlobProps } from '../../../types.ts'; interface Props { @@ -51,7 +51,7 @@ export default function NewShape(props: Props) { <button className="btn btn-primary" onClick={randomizeBlob}> Randomize </button> - <button className="btn btn-primary" onClick={uploadBlob} disabled={wasUploaded || !blobData}> + <button className="btn btn-primary" onClick={uploadBlob} disabled={uploadDisabled || wasUploaded || !blobData}> Upload </button> </div> diff --git a/src/pages/blobs/index.astro b/src/pages/blobs/index.astro index 5630d6b..11aa98a 100644 --- a/src/pages/blobs/index.astro +++ b/src/pages/blobs/index.astro @@ -2,6 +2,8 @@ import Layout from '../../layouts/Layout.astro'; import Markdown from '../../components/Markdown.astro'; import ShapeEditor from './_components/ShapeEditor.tsx'; +import { uploadDisabled } from '../../utils'; +import ContextAlert from '../../components/ContextAlert.astro'; const explainer = ` [Netlify Blobs](https://docs.netlify.com/blobs/overview/) provides an object store for any kind of data, be it JSON, binary, @@ -9,9 +11,22 @@ or [really](https://mk.gg/projects/chalkstream) anything else ([really!](https:/ Below is an Astro island with a React component for editing, uploading and viewing blobs to the blob store 🙂. `; + +const uploadDisabledText = ` +User uploads are disabled in this site. To run your own and try it out: +<a href="https://app.netlify.com/start/deploy?repository=https://github.com/netlify-templates/astro-platform-starter"> +<img src="https://www.netlify.com/img/deploy/button.svg" style="display: inline;" alt="Deploy to Netlify" /> +</a> +`; --- <Layout title="Blobs"> + <ContextAlert + addedChecksFunction={() => { + return uploadDisabled ? uploadDisabledText : null; + }} + class="mb-6" + /> <h1 class="mb-8">Blobs x Blobs</h1> <Markdown content={explainer} class="mb-12" /> <ShapeEditor client:load /> |
