From b99aa22a73427d95f0f9d79055debb5a38454a5b Mon Sep 17 00:00:00 2001 From: Jeff Emmett <46964190+Jeff-Emmett@users.noreply.github.com> Date: Sun, 11 Aug 2024 01:13:11 -0400 Subject: [PATCH] poll for impox updates --- src/components/Inbox.tsx | 95 ++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/src/components/Inbox.tsx b/src/components/Inbox.tsx index 1035c77..43f6fce 100644 --- a/src/components/Inbox.tsx +++ b/src/components/Inbox.tsx @@ -1,43 +1,72 @@ import { createShapeId, Editor, Tldraw, TLGeoShape, TLShapePartial } from "@tldraw/tldraw"; +import { useEffect, useRef } from "react"; export function Inbox() { + const editorRef = useRef(null); + + const updateEmails = async (editor: Editor) => { + try { + const response = await fetch('https://jeffemmett-canvas.web.val.run', { + method: 'GET', + }); + const messages = await response.json(); + + for (let i = 0; i < messages.length; i++) { + const message = messages[i]; + const messageId = message.id; + const parsedEmailName = message.from.match(/^([^<]+)/)?.[1]?.trim() || message.from.match(/[^<@]+(?=@)/)?.[0] || message.from; + const messageText = `from: ${parsedEmailName}\nsubject: ${message.subject}\n\n${message.text}` + const shapeWidth = 500 + const shapeHeight = 300 + const spacing = 50 + const shape: TLShapePartial = { + id: createShapeId(), + type: 'geo', + x: shapeWidth * (i % 5) + spacing * (i % 5), + y: shapeHeight * Math.floor(i / 5) + spacing * Math.floor(i / 5), + props: { + w: shapeWidth, + h: shapeHeight, + text: messageText, + align:'start', + verticalAlign:'start' + }, + meta: { + id: messageId + } + } + let found = false; + for (const s of editor.getCurrentPageShapes()) { + if (s.meta.id === messageId) { + found = true; + break; + } + } + if (!found) { + editor.createShape(shape) + } + } + } catch (error) { + console.error('Error fetching data:', error); + } + }; + + useEffect(() => { + const intervalId = setInterval(() => { + if (editorRef.current) { + updateEmails(editorRef.current); + } + }, 5*1000); + + return () => clearInterval(intervalId); + }, []); + return (
{ - (async () => { - try { - const response = await fetch('https://jeffemmett-canvas.web.val.run', { - method: 'GET', - }); - const messages = await response.json(); - - for (let i = 0; i < messages.length; i++) { - const message = messages[i]; - const parsedEmailName = message.from.match(/^([^<]+)/)?.[1]?.trim() || message.from.match(/[^<@]+(?=@)/)?.[0] || message.from; - const messageText = `from: ${parsedEmailName}\nsubject: ${message.subject}\n\n${message.text}` - const shapeWidth = 500 - const shapeHeight = 300 - const spacing = 50 - const shape: TLShapePartial = { - id: createShapeId(), - type: 'geo', - x: shapeWidth * (i % 5) + spacing * (i % 5), - y: shapeHeight * Math.floor(i / 5) + spacing * Math.floor(i / 5), - props: { - w: shapeWidth, - h: shapeHeight, - text: messageText, - align:'start', - verticalAlign:'start' - } - } - editor.createShape(shape) - } - } catch (error) { - console.error('Error fetching data:', error); - } - })(); + editorRef.current = editor; + updateEmails(editor); }} />