|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import Tooltip from "@material-ui/core/Tooltip"; |
| 3 | +import { |
| 4 | + EK_CREATE_ICON, |
| 5 | + EK_ICON_DRAG_AND_DROPPED, |
| 6 | +} from "@core/constant/ek.constant"; |
| 7 | +import CircularProgress from "@material-ui/core/CircularProgress"; |
| 8 | +import { Draggable } from "@plugin-sdk/draggable"; |
| 9 | +import { assistant as analytics } from "@analytics.bridged.xyz/internal"; |
| 10 | +import styled from "@emotion/styled"; |
| 11 | +import { IconMeta, Icon, loadSvg, makeIconUrl, useIcons } from "./resources"; |
| 12 | + |
| 13 | +type IconItemProps = Icon & { onClick: () => void }; |
| 14 | + |
| 15 | +export function IconItem({ onClick, ...props }: IconItemProps) { |
| 16 | + const { package: _package, name, variant } = props; |
| 17 | + const [loading, setLoading] = useState(false); |
| 18 | + const [loaded, setLoaded] = useState(false); |
| 19 | + const [downloading, setDownloading] = useState<boolean>(false); |
| 20 | + |
| 21 | + const _aid = name + " " + variant; // id for analytics |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + setTimeout(() => { |
| 25 | + setLoading(!loaded); |
| 26 | + }, 5); |
| 27 | + }, [loaded]); |
| 28 | + |
| 29 | + const _onUserLoadIconToCanvas = () => { |
| 30 | + // ANALYTICS |
| 31 | + analytics.event_load_icon({ |
| 32 | + icon_name: _aid, |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + async function loadData() { |
| 37 | + _onUserLoadIconToCanvas(); |
| 38 | + try { |
| 39 | + setDownloading(true); |
| 40 | + const svg = await loadSvg(props, { |
| 41 | + disable_cache: true, |
| 42 | + }); |
| 43 | + const data = { |
| 44 | + key: name, |
| 45 | + svg: svg, |
| 46 | + config: { |
| 47 | + name: props.name, |
| 48 | + variant: props.variant, |
| 49 | + size: props.size, |
| 50 | + package: props.package, |
| 51 | + }, |
| 52 | + }; |
| 53 | + return data; |
| 54 | + } catch (_) { |
| 55 | + throw _; |
| 56 | + } finally { |
| 57 | + setDownloading(false); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const onclick = () => { |
| 62 | + onClick(); |
| 63 | + _onUserLoadIconToCanvas(); |
| 64 | + loadData().then((d) => { |
| 65 | + parent.postMessage( |
| 66 | + { |
| 67 | + pluginMessage: { |
| 68 | + type: EK_CREATE_ICON, |
| 69 | + data: d, |
| 70 | + }, |
| 71 | + }, |
| 72 | + "*" |
| 73 | + ); |
| 74 | + }); |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Draggable customDataLoader={loadData} eventKey={EK_ICON_DRAG_AND_DROPPED}> |
| 79 | + <Tooltip |
| 80 | + title={`${name} (${variant ?? "default"}) (${_package})`} |
| 81 | + placement="top" |
| 82 | + PopperProps={{ |
| 83 | + popperOptions: { |
| 84 | + modifiers: { |
| 85 | + offset: { |
| 86 | + offset: "-1px, -20px", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }} |
| 91 | + > |
| 92 | + <IconButton onClick={onclick} disabled={downloading}> |
| 93 | + {downloading ? ( |
| 94 | + <CircularProgress size={24} /> |
| 95 | + ) : ( |
| 96 | + <svg |
| 97 | + width="24" |
| 98 | + height="24" |
| 99 | + style={{ |
| 100 | + borderRadius: 4, |
| 101 | + background: loading ? "rgba(0, 0, 0, 0.1)" : "none", |
| 102 | + transition: "background 0.3s", |
| 103 | + }} |
| 104 | + > |
| 105 | + <image |
| 106 | + className="unicons" |
| 107 | + xlinkHref={makeIconUrl(props)} |
| 108 | + onLoad={() => setLoaded(true)} |
| 109 | + width="24" |
| 110 | + height="24" |
| 111 | + /> |
| 112 | + </svg> |
| 113 | + )} |
| 114 | + </IconButton> |
| 115 | + </Tooltip> |
| 116 | + </Draggable> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +const IconButton = styled.button` |
| 121 | + background-color: #fff; |
| 122 | + border: none; |
| 123 | + height: 48px !important; |
| 124 | + width: 48px; |
| 125 | + display: flex; |
| 126 | + align-items: center; |
| 127 | + justify-content: center; |
| 128 | +
|
| 129 | + &:hover { |
| 130 | + background-color: #eeeeee; |
| 131 | + border-radius: 8px; |
| 132 | + display: flex; |
| 133 | + align-items: center; |
| 134 | + justify-content: center; |
| 135 | + transition: 0.25s; |
| 136 | + } |
| 137 | +`; |
0 commit comments