Initial screen starter scaffold
This commit is contained in:
commit
d136b9dddc
49 files changed
+2966
No files matched your search
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "@dev-screen-starter/charts",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --noEmit",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"echarts": "^6.0.0",
|
||||
"react": "^19.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^19.1.16",
|
||||
"typescript": "^5.9.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import * as echarts from 'echarts'
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
type EChartProps = {
|
||||
option: echarts.EChartsOption
|
||||
loading?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function EChart({ option, loading = false, className = '' }: EChartProps) {
|
||||
const hostRef = useRef<HTMLDivElement | null>(null)
|
||||
const chartRef = useRef<echarts.ECharts | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!hostRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
chartRef.current = echarts.init(hostRef.current)
|
||||
const observer = new ResizeObserver(() => chartRef.current?.resize())
|
||||
observer.observe(hostRef.current)
|
||||
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
chartRef.current?.dispose()
|
||||
chartRef.current = null
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
chartRef.current?.setOption(option, true)
|
||||
}, [option])
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) {
|
||||
chartRef.current?.showLoading()
|
||||
return
|
||||
}
|
||||
|
||||
chartRef.current?.hideLoading()
|
||||
}, [loading])
|
||||
|
||||
return <div className={`chart-host ${className}`} ref={hostRef} />
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
export const baseChartTextStyle = {
|
||||
color: '#d7e4f2',
|
||||
fontFamily: 'Inter, ui-sans-serif, system-ui'
|
||||
}
|
||||
|
||||
export const withChartDefaults = (option: EChartsOption): EChartsOption => ({
|
||||
textStyle: baseChartTextStyle,
|
||||
grid: {
|
||||
top: 32,
|
||||
right: 24,
|
||||
bottom: 32,
|
||||
left: 48,
|
||||
containLabel: true
|
||||
},
|
||||
tooltip: {
|
||||
trigger: 'axis',
|
||||
backgroundColor: '#101722',
|
||||
borderColor: '#314154',
|
||||
textStyle: {
|
||||
color: '#d7e4f2'
|
||||
}
|
||||
},
|
||||
...option
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './EChart'
|
||||
export * from './chartTheme'
|
||||
export * from './optionFactories'
|
||||
@@ -0,0 +1,263 @@
|
||||
import type { EChartsOption } from 'echarts'
|
||||
import { withChartDefaults } from './chartTheme'
|
||||
|
||||
type DataPoint = {
|
||||
name: string
|
||||
value: number
|
||||
}
|
||||
|
||||
type AxisChartInput = {
|
||||
categories: string[]
|
||||
values: number[]
|
||||
name?: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
type MultiSeriesInput = {
|
||||
categories: string[]
|
||||
series: Array<{
|
||||
name: string
|
||||
values: number[]
|
||||
color?: string
|
||||
}>
|
||||
}
|
||||
|
||||
type PieChartInput = {
|
||||
data: DataPoint[]
|
||||
radius?: string | [string, string]
|
||||
}
|
||||
|
||||
type GaugeChartInput = {
|
||||
name?: string
|
||||
value: number
|
||||
min?: number
|
||||
max?: number
|
||||
unit?: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
type RadarChartInput = {
|
||||
indicators: Array<{
|
||||
name: string
|
||||
max: number
|
||||
}>
|
||||
values: number[]
|
||||
name?: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
const axisLine = { lineStyle: { color: '#35506c' } }
|
||||
const axisLabel = { color: '#9fb4cc' }
|
||||
const splitLine = { lineStyle: { color: '#173047' } }
|
||||
|
||||
export function createLineOption(input: AxisChartInput | MultiSeriesInput): EChartsOption {
|
||||
const series = 'series' in input
|
||||
? input.series.map((item) => ({
|
||||
name: item.name,
|
||||
type: 'line' as const,
|
||||
smooth: true,
|
||||
data: item.values,
|
||||
areaStyle: { color: `${item.color ?? '#63a4ff'}22` },
|
||||
lineStyle: { color: item.color ?? '#63a4ff', width: 3 },
|
||||
symbolSize: 8
|
||||
}))
|
||||
: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'line' as const,
|
||||
smooth: true,
|
||||
data: input.values,
|
||||
areaStyle: { color: `${input.color ?? '#63a4ff'}22` },
|
||||
lineStyle: { color: input.color ?? '#63a4ff', width: 3 },
|
||||
symbolSize: 8
|
||||
}
|
||||
]
|
||||
|
||||
return withChartDefaults({
|
||||
legend: 'series' in input ? { top: 0, textStyle: axisLabel } : undefined,
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: input.categories,
|
||||
axisLine,
|
||||
axisLabel
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
splitLine,
|
||||
axisLabel
|
||||
},
|
||||
series
|
||||
})
|
||||
}
|
||||
|
||||
export function createBarOption(input: AxisChartInput | MultiSeriesInput): EChartsOption {
|
||||
const series = 'series' in input
|
||||
? input.series.map((item) => ({
|
||||
name: item.name,
|
||||
type: 'bar' as const,
|
||||
data: item.values,
|
||||
barMaxWidth: 28,
|
||||
itemStyle: { color: item.color ?? '#63a4ff', borderRadius: [4, 4, 0, 0] }
|
||||
}))
|
||||
: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'bar' as const,
|
||||
data: input.values,
|
||||
barMaxWidth: 32,
|
||||
itemStyle: { color: input.color ?? '#63a4ff', borderRadius: [4, 4, 0, 0] }
|
||||
}
|
||||
]
|
||||
|
||||
return withChartDefaults({
|
||||
legend: 'series' in input ? { top: 0, textStyle: axisLabel } : undefined,
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: input.categories,
|
||||
axisLine,
|
||||
axisLabel
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
splitLine,
|
||||
axisLabel
|
||||
},
|
||||
series
|
||||
})
|
||||
}
|
||||
|
||||
export function createRankBarOption(input: AxisChartInput): EChartsOption {
|
||||
return withChartDefaults({
|
||||
grid: {
|
||||
top: 16,
|
||||
right: 24,
|
||||
bottom: 16,
|
||||
left: 80,
|
||||
containLabel: true
|
||||
},
|
||||
xAxis: {
|
||||
type: 'value',
|
||||
splitLine,
|
||||
axisLabel
|
||||
},
|
||||
yAxis: {
|
||||
type: 'category',
|
||||
data: input.categories,
|
||||
inverse: true,
|
||||
axisLine: { show: false },
|
||||
axisTick: { show: false },
|
||||
axisLabel
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'bar',
|
||||
data: input.values,
|
||||
barMaxWidth: 18,
|
||||
label: {
|
||||
show: true,
|
||||
position: 'right',
|
||||
color: '#d7e4f2'
|
||||
},
|
||||
itemStyle: { color: input.color ?? '#63a4ff', borderRadius: [0, 4, 4, 0] }
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
export function createPieOption(input: PieChartInput): EChartsOption {
|
||||
return withChartDefaults({
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
backgroundColor: '#101722',
|
||||
borderColor: '#314154',
|
||||
textStyle: { color: '#d7e4f2' }
|
||||
},
|
||||
legend: {
|
||||
bottom: 0,
|
||||
textStyle: axisLabel
|
||||
},
|
||||
series: [
|
||||
{
|
||||
type: 'pie',
|
||||
radius: input.radius ?? ['46%', '68%'],
|
||||
center: ['50%', '45%'],
|
||||
avoidLabelOverlap: true,
|
||||
label: { color: '#d7e4f2' },
|
||||
data: input.data
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
export function createGaugeOption(input: GaugeChartInput): EChartsOption {
|
||||
const max = input.max ?? 100
|
||||
|
||||
return {
|
||||
tooltip: {
|
||||
formatter: `{b}: {c}${input.unit ?? ''}`
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'gauge',
|
||||
min: input.min ?? 0,
|
||||
max,
|
||||
progress: {
|
||||
show: true,
|
||||
width: 14,
|
||||
itemStyle: { color: input.color ?? '#63a4ff' }
|
||||
},
|
||||
axisLine: {
|
||||
lineStyle: {
|
||||
width: 14,
|
||||
color: [[1, '#173047']]
|
||||
}
|
||||
},
|
||||
axisTick: { show: false },
|
||||
splitLine: { length: 8, lineStyle: { color: '#466886' } },
|
||||
axisLabel: { color: '#9fb4cc' },
|
||||
pointer: { width: 4 },
|
||||
detail: {
|
||||
formatter: `{value}${input.unit ?? ''}`,
|
||||
color: '#f0f7ff',
|
||||
fontSize: 32
|
||||
},
|
||||
data: [{ name: input.name, value: input.value }]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
export function createRadarOption(input: RadarChartInput): EChartsOption {
|
||||
return withChartDefaults({
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
backgroundColor: '#101722',
|
||||
borderColor: '#314154',
|
||||
textStyle: { color: '#d7e4f2' }
|
||||
},
|
||||
radar: {
|
||||
indicator: input.indicators,
|
||||
axisName: { color: '#9fb4cc' },
|
||||
splitLine: { lineStyle: { color: '#24425e' } },
|
||||
splitArea: { areaStyle: { color: ['rgba(36, 66, 94, 0.16)', 'rgba(36, 66, 94, 0.05)'] } },
|
||||
axisLine: { lineStyle: { color: '#24425e' } }
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'radar',
|
||||
data: [
|
||||
{
|
||||
name: input.name,
|
||||
value: input.values,
|
||||
areaStyle: { color: `${input.color ?? '#63a4ff'}33` },
|
||||
lineStyle: { color: input.color ?? '#63a4ff', width: 2 },
|
||||
itemStyle: { color: input.color ?? '#63a4ff' }
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@dev-screen-starter/debug",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --noEmit",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^19.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^19.1.16",
|
||||
"typescript": "^5.9.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
type DebugBadgeProps = {
|
||||
enabled?: boolean
|
||||
scale?: number
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
const defaultEnabled = typeof location !== 'undefined' && location.hostname === 'localhost'
|
||||
|
||||
export function DebugBadge({ enabled = defaultEnabled, scale, updatedAt }: DebugBadgeProps) {
|
||||
if (!enabled) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<aside className="debug-badge">
|
||||
{scale ? <span>scale {scale.toFixed(3)}</span> : null}
|
||||
{updatedAt ? <span>updated {new Date(updatedAt).toLocaleTimeString()}</span> : null}
|
||||
</aside>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './DebugBadge'
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@dev-screen-starter/screen-core",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --noEmit",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^19.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^19.1.16",
|
||||
"typescript": "^5.9.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
type MetricCardProps = {
|
||||
label: string
|
||||
value: string | number
|
||||
unit?: string
|
||||
tone?: 'blue' | 'green' | 'amber'
|
||||
}
|
||||
|
||||
export function MetricCard({ label, value, unit, tone = 'blue' }: MetricCardProps) {
|
||||
return (
|
||||
<div className={`metric-card tone-${tone}`}>
|
||||
<span>{label}</span>
|
||||
<strong>
|
||||
{value}
|
||||
{unit ? <small>{unit}</small> : null}
|
||||
</strong>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
type PanelProps = {
|
||||
title?: string
|
||||
className?: string
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
export function Panel({ title, className = '', children }: PanelProps) {
|
||||
return (
|
||||
<section className={`screen-panel ${className}`}>
|
||||
{title ? <header>{title}</header> : null}
|
||||
<div className="screen-panel-body">{children}</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { type CSSProperties, type ReactNode, useEffect, useMemo, useState } from 'react'
|
||||
import { calculateStageScale, type ScaleMode } from './scale'
|
||||
|
||||
type ScreenStageProps = {
|
||||
width?: number
|
||||
height?: number
|
||||
scaleMode?: ScaleMode
|
||||
background?: string
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
export function ScreenStage({ width = 1920, height = 1080, scaleMode = 'fit', background = '#061826', children }: ScreenStageProps) {
|
||||
const [viewport, setViewport] = useState({ width: window.innerWidth, height: window.innerHeight })
|
||||
|
||||
useEffect(() => {
|
||||
const onResize = () => setViewport({ width: window.innerWidth, height: window.innerHeight })
|
||||
window.addEventListener('resize', onResize)
|
||||
return () => window.removeEventListener('resize', onResize)
|
||||
}, [])
|
||||
|
||||
const scale = useMemo(() => calculateStageScale({ width, height, scaleMode }, viewport.width, viewport.height), [height, scaleMode, viewport.height, viewport.width, width])
|
||||
|
||||
const stageStyle: CSSProperties = {
|
||||
width,
|
||||
height,
|
||||
background,
|
||||
transform: `translate(-50%, -50%) scale(${scale.scaleX}, ${scale.scaleY})`
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="screen-viewport">
|
||||
<section className="screen-stage" style={stageStyle}>
|
||||
{children}
|
||||
</section>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from './scale'
|
||||
export * from './ScreenStage'
|
||||
export * from './Panel'
|
||||
export * from './MetricCard'
|
||||
@@ -0,0 +1,30 @@
|
||||
export type ScaleMode = 'fit' | 'fill' | 'stretch' | 'none'
|
||||
|
||||
export type ScreenSize = {
|
||||
width: number
|
||||
height: number
|
||||
scaleMode?: ScaleMode
|
||||
}
|
||||
|
||||
export type StageScale = {
|
||||
scaleX: number
|
||||
scaleY: number
|
||||
}
|
||||
|
||||
export function calculateStageScale(screen: ScreenSize, viewportWidth: number, viewportHeight: number): StageScale {
|
||||
const scaleMode = screen.scaleMode ?? 'fit'
|
||||
|
||||
if (scaleMode === 'none') {
|
||||
return { scaleX: 1, scaleY: 1 }
|
||||
}
|
||||
|
||||
const scaleX = viewportWidth / screen.width
|
||||
const scaleY = viewportHeight / screen.height
|
||||
|
||||
if (scaleMode === 'stretch') {
|
||||
return { scaleX, scaleY }
|
||||
}
|
||||
|
||||
const scale = scaleMode === 'fill' ? Math.max(scaleX, scaleY) : Math.min(scaleX, scaleY)
|
||||
return { scaleX: scale, scaleY: scale }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in new issue
Block a user