|
| 1 | +import type { DocFullViewProps } from "@/components/safeDoc-react/DocView"; |
| 2 | +import { DEFAULT_COLOR, type CardV1 } from "@/docs/card"; |
| 3 | +import { CardDisplayBack } from "./CardDisplayBack"; |
| 4 | +import { useOnAllReady } from "@/hooks/useOnAllReady"; |
| 5 | +import { CardDisplayFront } from "./CardDisplayFront"; |
| 6 | +import { useRef, useState } from "react"; |
| 7 | +import { svgToImage } from "@/utils/print"; |
| 8 | +import { Canvas, useFrame } from "@react-three/fiber"; |
| 9 | +import { Image } from "@react-three/drei"; |
| 10 | +import { CARD_SIZE } from "../BusinessCardSvg"; |
| 11 | +import * as THREE from "three"; |
| 12 | +import { ColorInfo } from "./ColorInfo"; |
| 13 | +import { Logo } from "@/components/Logo"; |
| 14 | +import "./CardFullView.css"; |
| 15 | +import { ICON_WHITE_BG } from "@/docs/card/icon-white-bg"; |
| 16 | +import { ICON_RATIOS } from "@/docs/card/icon-ratios"; |
| 17 | +import clsx from "clsx"; |
| 18 | +import { isArabic } from "@/components/utils/isArabic"; |
| 19 | + |
| 20 | +export type CardFullViewProps = DocFullViewProps<typeof CardV1>; |
| 21 | +const scale = [CARD_SIZE.normal.width / 200, CARD_SIZE.normal.height / 200] as [ |
| 22 | + number, |
| 23 | + number, |
| 24 | +]; |
| 25 | + |
| 26 | +function DreiView({ front, back }: { front: string; back: string }) { |
| 27 | + const fRef = useRef<THREE.Mesh>(null); |
| 28 | + const bRef = useRef<THREE.Mesh>(null); |
| 29 | + useFrame(() => { |
| 30 | + if (!fRef.current || !bRef.current) return; |
| 31 | + const angleD = Math.round((Date.now() / 12000) * 1000) % 1000; |
| 32 | + const angle = (angleD / 1000) * 2 * Math.PI; |
| 33 | + bRef.current.setRotationFromEuler(new THREE.Euler(0, angle, 0)); |
| 34 | + fRef.current.setRotationFromEuler(new THREE.Euler(0, angle + Math.PI, 0)); |
| 35 | + }); |
| 36 | + return ( |
| 37 | + <> |
| 38 | + <Image ref={fRef} url={front} scale={scale} /> |
| 39 | + <Image ref={bRef} url={back} scale={scale} /> |
| 40 | + </> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +function RotatingCardView(props: CardFullViewProps) { |
| 45 | + const front = useRef<SVGSVGElement>(null); |
| 46 | + const back = useRef<SVGSVGElement>(null); |
| 47 | + const [frontUrl, setFrontUrl] = useState<string>(); |
| 48 | + const [backUrl, setBackUrl] = useState<string>(); |
| 49 | + const [useFrontReady, useBackReady] = useOnAllReady(() => { |
| 50 | + props.onReady?.(); |
| 51 | + Promise.all([ |
| 52 | + svgToImage(front.current!).then((img) => |
| 53 | + setFrontUrl(URL.createObjectURL(img)), |
| 54 | + ), |
| 55 | + svgToImage(back.current!).then((img) => |
| 56 | + setBackUrl(URL.createObjectURL(img)), |
| 57 | + ), |
| 58 | + ]).catch((error) => { |
| 59 | + console.log(error); |
| 60 | + }); |
| 61 | + }, 2); |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <div |
| 65 | + className="fullcard--drei-loader" |
| 66 | + style={{ width: CARD_SIZE.normal.width }} |
| 67 | + > |
| 68 | + {frontUrl ? null : ( |
| 69 | + <CardDisplayFront ref={front} {...props} onReady={useFrontReady} /> |
| 70 | + )} |
| 71 | + {backUrl ? null : ( |
| 72 | + <CardDisplayBack ref={back} {...props} onReady={useBackReady} /> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + {frontUrl && backUrl ? ( |
| 76 | + <Canvas style={{ height: "50vh", width: "100cqw", gridColumn: "full" }}> |
| 77 | + <DreiView front={frontUrl} back={backUrl} /> |
| 78 | + </Canvas> |
| 79 | + ) : null} |
| 80 | + </> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +const CardDesignView = ({ data }: CardFullViewProps) => { |
| 85 | + const colorInfo = ColorInfo[data.color] ?? ColorInfo[DEFAULT_COLOR]; |
| 86 | + const colorStyle = { backgroundColor: colorInfo.bg, color: colorInfo.fg }; |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <header style={colorStyle} className="full"> |
| 90 | + <Logo src={colorInfo.logo} /> |
| 91 | + </header> |
| 92 | + <section style={colorStyle} className="full fullcard"> |
| 93 | + <menu></menu> |
| 94 | + <h1> |
| 95 | + <ruby> |
| 96 | + {data.callname} |
| 97 | + <rt>{data.callname_kana}</rt> |
| 98 | + </ruby> |
| 99 | + </h1> |
| 100 | + </section> |
| 101 | + <section className="text"> |
| 102 | + <div |
| 103 | + className={clsx("name", { |
| 104 | + arabic: isArabic(data.surname) || isArabic(data.firstname), |
| 105 | + })} |
| 106 | + > |
| 107 | + <strong> |
| 108 | + <ruby> |
| 109 | + {data.surname} |
| 110 | + <rt>{data.surname_kana}</rt> |
| 111 | + </ruby> |
| 112 | + </strong>{" "} |
| 113 | + <ruby> |
| 114 | + {data.firstname} |
| 115 | + <rt>{data.firstname_kana}</rt> |
| 116 | + </ruby> |
| 117 | + </div> |
| 118 | + <div className="subtitle">{data.subtitle}</div> |
| 119 | + {data.email ? ( |
| 120 | + <div className="email"> |
| 121 | + <a href={`mailto:${data.email}`}>{data.email}</a> |
| 122 | + </div> |
| 123 | + ) : null} |
| 124 | + {data.url ? ( |
| 125 | + <div className="url"> |
| 126 | + <a href={data.url}>{data.url}</a> |
| 127 | + </div> |
| 128 | + ) : null} |
| 129 | + </section> |
| 130 | + </> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +export const CardFullView = (props: CardFullViewProps) => { |
| 135 | + const icons = [ |
| 136 | + props.data.bottom1, |
| 137 | + props.data.bottom2, |
| 138 | + props.data.bottom3, |
| 139 | + props.data.bottom4, |
| 140 | + props.data.bottom5, |
| 141 | + ].filter(Boolean) as string[]; |
| 142 | + return ( |
| 143 | + <div className="fullcard"> |
| 144 | + <CardDesignView {...props} /> |
| 145 | + <RotatingCardView {...props} /> |
| 146 | + {icons.length > 0 || props.data.description ? ( |
| 147 | + <footer className="fullcard"> |
| 148 | + <div> |
| 149 | + {icons.map((value) => { |
| 150 | + const border = |
| 151 | + ICON_WHITE_BG[value as keyof typeof ICON_WHITE_BG] ?? false; |
| 152 | + const height = 20; |
| 153 | + const width = |
| 154 | + ICON_RATIOS[value as keyof typeof ICON_RATIOS] * height; |
| 155 | + return ( |
| 156 | + <img |
| 157 | + key={value} |
| 158 | + className={clsx({ border, icon: true })} |
| 159 | + src={`/svg/${value}.svg`} |
| 160 | + width={width} |
| 161 | + height={height} |
| 162 | + /> |
| 163 | + ); |
| 164 | + })} |
| 165 | + {props.data.description} |
| 166 | + </div> |
| 167 | + </footer> |
| 168 | + ) : null} |
| 169 | + </div> |
| 170 | + ); |
| 171 | +}; |
0 commit comments