import { __ } from '@wordpress/i18n' import React, { useState } from 'react' import { REST_BASES } from '../../utils/restAPI' import { SNIPPET_SCOPE_DESCRIPTIONS } from '../../utils/snippets/snippets' import { buildUrl } from '../../utils/urls' import { useRestAPI } from '../../hooks/useRestAPI' import { InsightsChart, TotalsInsightsChart } from './InsightsCharts' import type { InsightChartPreferencesSchema, InsightsChartEntry, InsightsChartViews, InsightsConfigurableChartKey, InsightsSummary } from '../../types/Insights' import type { SnippetCodeScope, SnippetType } from '../../types/Snippet' export const DEFAULT_INSIGHTS_CHART_VIEWS: InsightsChartViews = window.CODE_SNIPPETS?.insightsChartViews ?? { type: 'bar', activation: 'pie', conditions: 'pie', location: 'bar', tags: 'bar' } export const INSIGHTS_TYPE_COLORS: Readonly> = { php: '#2271b1', html: '#cd4510', css: '#9b59b6', js: '#f7d67a', cond: '#22826f' } export const INSIGHTS_ACTIVATION_COLORS: Readonly> = { active: '#118822', inactive: '#cd4510' } export const INSIGHTS_CONDITION_COLORS: Readonly> = { with: '#22826f', without: '#9b59b6' } export const INSIGHTS_LOCATION_COLORS: Readonly> = { 'global': '#ff9800', 'admin': '#03c7d2', 'front-end': '#d46f4d', 'single-use': '#00bcd4', 'content': '#2271b1', 'head-content': '#5865f2', 'body-content': '#3b5998', 'footer-content': '#9b59b6', 'admin-css': '#22826f', 'site-css': '#cd4510', 'site-head-js': '#f7d67a', 'site-footer-js': '#2b71a3' } interface StaticChartProps { summary: InsightsSummary } interface ConfigurableChartProps extends StaticChartProps { view: InsightsChartViews[Chart] setView: (view: InsightsChartViews[Chart]) => void } const SnippetTypeChart: React.FC> = ({ summary, view, setView }) => [type, { ...entry, url: buildUrl(window.CODE_SNIPPETS?.urls.manage, { subpage: 'snippets', type }) }]) )} colors={INSIGHTS_TYPE_COLORS} views={['pie', 'bar']} view={view} setView={setView} /> const ActivationStatusChart: React.FC> = ({ summary, view, setView }) => const ConditionUsageChart: React.FC> = ({ summary, view, setView }) => const LocationChart = ({ summary, view, setView }: ConfigurableChartProps<'location'>) => { const entries: Record = Object.fromEntries( Object.entries(summary.locationCounts).map(([scope, count]) => [scope, { label: SNIPPET_SCOPE_DESCRIPTIONS[scope as SnippetCodeScope], count }]) ) return ( ) } const TagsChart: React.FC> = ({ summary, view, setView }) => [tag, { ...entry, url: buildUrl(window.CODE_SNIPPETS?.urls.manage, { tag }) }]) )} views={['bar', 'cloud']} view={view} setView={setView} /> export interface InsightsDashboardProps { summary: InsightsSummary } export const InsightsDashboard: React.FC = ({ summary }) => { const { api } = useRestAPI() const [chartViews, setChartViews] = useState(DEFAULT_INSIGHTS_CHART_VIEWS) const updateChartView = (chart: Chart, view: InsightsChartViews[Chart]) => { const views: InsightsChartViews = { ...chartViews, [chart]: view } setChartViews(views) api.post(REST_BASES.preferences.insights, { views }) .catch((error: unknown) => { setChartViews(currentViews => currentViews === views ? chartViews : currentViews) console.error(error) }) } return <>

{__('Insights', 'code-snippets')}


updateChartView('type', view)} /> updateChartView('activation', view)} /> updateChartView('conditions', view)} /> updateChartView('location', view)} /> updateChartView('tags', view)} />
}