render placeholder when processing assets

This commit is contained in:
Roland Osborne 2022-05-02 11:53:18 -07:00
parent 6f4fb0f331
commit e64490f994
4 changed files with 58 additions and 35 deletions

View File

@ -1,4 +1,5 @@
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Skeleton } from 'antd';
import { CarouselWrapper } from './Carousel.styled'; import { CarouselWrapper } from './Carousel.styled';
import { RightOutlined, LeftOutlined, CloseOutlined } from '@ant-design/icons'; import { RightOutlined, LeftOutlined, CloseOutlined } from '@ant-design/icons';
import ReactResizeDetector from 'react-resize-detector'; import ReactResizeDetector from 'react-resize-detector';
@ -6,7 +7,7 @@ import ReactResizeDetector from 'react-resize-detector';
import login from '../login.png'; import login from '../login.png';
import test from '../test.png'; import test from '../test.png';
export function Carousel({ items, itemRenderer, itemRemove }) { export function Carousel({ ready, items, itemRenderer, itemRemove }) {
const [slots, setSlots] = useState([]); const [slots, setSlots] = useState([]);
const [carouselRef, setCarouselRef] = useState(false); const [carouselRef, setCarouselRef] = useState(false);
const [itemIndex, setItemIndex] = useState(0); const [itemIndex, setItemIndex] = useState(0);
@ -81,6 +82,7 @@ export function Carousel({ items, itemRenderer, itemRemove }) {
useEffect(() => { useEffect(() => {
let assets = []; let assets = [];
if (ready) {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
assets.push(( assets.push((
<ReactResizeDetector handleWidth={true} handleHeight={false}> <ReactResizeDetector handleWidth={true} handleHeight={false}>
@ -107,11 +109,12 @@ export function Carousel({ items, itemRenderer, itemRemove }) {
setItemIndex(0); setItemIndex(0);
} }
} }
}
setSlots(assets); setSlots(assets);
setScroll(); setScroll();
setArrows(); setArrows();
}, [items]); }, [ready, items]);
const onRefSet = (r) => { const onRefSet = (r) => {
if (r != null) { if (r != null) {
@ -120,6 +123,20 @@ export function Carousel({ items, itemRenderer, itemRemove }) {
} }
} }
if (!ready) {
return (
<CarouselWrapper>
<div class="carousel">
<Skeleton.Image style={{ height: 128 }} />
</div>
<div class="arrows">
<div class="arrow" onClick={onRight}><RightOutlined style={{ visibility: scrollRight }} /></div>
<div class="arrow" onClick={onLeft}><LeftOutlined style={{ visibility: scrollLeft }} /></div>
</div>
</CarouselWrapper>
)
}
if (slots.length != 0) { if (slots.length != 0) {
return ( return (
<CarouselWrapper> <CarouselWrapper>

View File

@ -85,7 +85,7 @@ export function AddTopic() {
<AddTopicWrapper> <AddTopicWrapper>
<div class="container noselect"> <div class="container noselect">
<div class="carousel"> <div class="carousel">
<Carousel items={state.assets} itemRenderer={renderItem} itemRemove={removeItem} /> <Carousel ready={true} items={state.assets} itemRenderer={renderItem} itemRemove={removeItem} />
</div> </div>
<div class="input"> <div class="input">
<Input.TextArea placeholder="Message" autoSize={{ minRows: 2, maxRows: 6 }} onKeyPress={onKey} <Input.TextArea placeholder="Message" autoSize={{ minRows: 2, maxRows: 6 }} onKeyPress={onKey}

View File

@ -35,7 +35,7 @@ export function TopicItem({ topic }) {
<CommentOutlined /> <CommentOutlined />
</div> </div>
</div> </div>
<Carousel items={state.assets} itemRenderer={renderAsset} /> <Carousel ready={state.ready} items={state.assets} itemRenderer={renderAsset} />
<div class="message">{ state.message?.text }</div> <div class="message">{ state.message?.text }</div>
</div> </div>
</TopicItemWrapper> </TopicItemWrapper>

View File

@ -13,8 +13,7 @@ export function useTopicItem(topic) {
imageUrl: null, imageUrl: null,
message: null, message: null,
created: null, created: null,
status: null, ready: false,
transform: null,
assets: [], assets: [],
}); });
@ -28,17 +27,24 @@ export function useTopicItem(topic) {
useEffect(() => { useEffect(() => {
if (!topic?.data) {
console.log("invalid topic:", topic);
return;
}
const { status, transform, data } = topic.data.topicDetail; const { status, transform, data } = topic.data.topicDetail;
let message; let message;
let ready = false;
let assets = []; let assets = [];
if (status === 'confirmed') { if (status === 'confirmed') {
try { try {
message = JSON.parse(data); message = JSON.parse(data);
if (transform === 'complete') {
if (message.assets) { if (message.assets) {
assets = message.assets; assets = message.assets;
delete message.assets; delete message.assets;
} }
if (transform === 'complete') {
ready = true;
} }
} }
catch(err) { catch(err) {
@ -50,11 +56,11 @@ export function useTopicItem(topic) {
const { guid, created } = topic.data.topicDetail; const { guid, created } = topic.data.topicDetail;
if (profile.state.profile.guid == guid) { if (profile.state.profile.guid == guid) {
const { name, handle, imageUrl } = profile.actions.getProfile(); const { name, handle, imageUrl } = profile.actions.getProfile();
updateState({ name, handle, imageUrl, status, message, transform, assets, created }); updateState({ name, handle, imageUrl, status, message, transform, assets, ready, created });
} }
else { else {
const { name, handle, imageUrl } = card.actions.getCardProfileByGuid(guid); const { name, handle, imageUrl } = card.actions.getCardProfileByGuid(guid);
updateState({ name, handle, imageUrl, status, message, transform, assets, created }); updateState({ name, handle, imageUrl, status, message, transform, assets, ready, created });
} }
} }
}, [profile, card, conversation, topic]); }, [profile, card, conversation, topic]);