mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
/**
|
|
* Static survey dataset access.
|
|
*
|
|
* The microsite ships fully static. All survey numbers come from the committed
|
|
* JSON generated by scripts/generate-state-of-startups-data.ts — no live
|
|
* Supabase at build or runtime. Every chart, stat, cross-tab, channel-mix, and
|
|
* cohort toggle resolves to one pre-aggregated "distribution" looked up by the
|
|
* canonical (year, column, aggregation, filters) key from survey-key.ts.
|
|
*/
|
|
|
|
import datasetJson from '../../../data/surveys/state-of-startups-data.json'
|
|
import type { SurveyYear } from '../components/year-context'
|
|
import { surveyKey, type Aggregation, type SurveyFilters } from './survey-key'
|
|
|
|
export { surveyKey, mergeFilters } from './survey-key'
|
|
export type { Aggregation, FilterValue, SurveyFilters } from './survey-key'
|
|
|
|
export interface DistributionRow {
|
|
label: string
|
|
count: number
|
|
}
|
|
|
|
export interface Distribution {
|
|
rows: DistributionRow[]
|
|
respondentCount: number
|
|
}
|
|
|
|
/** One bar in a chart: label + rounded percentage. */
|
|
export interface ChartDataItem {
|
|
label: string
|
|
value: number
|
|
}
|
|
|
|
interface SurveyDataset {
|
|
meta: { generatedFrom: string; years: Record<string, number>; slices: number }
|
|
distributions: Record<string, Distribution>
|
|
}
|
|
|
|
const dataset = datasetJson as SurveyDataset
|
|
const DISTRIBUTIONS = dataset.distributions
|
|
|
|
export const surveyMeta = dataset.meta
|
|
|
|
/** Returns the pre-aggregated distribution, or undefined when the column did
|
|
* not exist that year (e.g. a new-in-2026 question viewed at 2025). */
|
|
export function getDistribution(
|
|
year: SurveyYear,
|
|
column: string,
|
|
aggregation: Aggregation,
|
|
filters?: SurveyFilters
|
|
): Distribution | undefined {
|
|
return DISTRIBUTIONS[surveyKey(year, column, aggregation, filters)]
|
|
}
|
|
|
|
/** Sums the matching target rows over the respondent denominator. Returns null
|
|
* when there is no data for the year or the target option is absent. */
|
|
export function distributionPercent(
|
|
dist: Distribution | undefined,
|
|
target: string | readonly string[]
|
|
): number | null {
|
|
if (!dist || dist.respondentCount <= 0) return null
|
|
const targets = Array.isArray(target) ? target : [target]
|
|
let count = 0
|
|
let matched = 0
|
|
for (const row of dist.rows) {
|
|
if (targets.includes(row.label)) {
|
|
count += row.count
|
|
matched += 1
|
|
}
|
|
}
|
|
if (matched === 0) return null
|
|
return Math.round((count / dist.respondentCount) * 100)
|
|
}
|
|
|
|
/** Maps a distribution to chart bars (respondent-count denominator), already
|
|
* sorted desc by the generator. Optionally caps the number of bars. */
|
|
export function distributionToBars(
|
|
dist: Distribution | undefined,
|
|
maxBars?: number
|
|
): ChartDataItem[] {
|
|
if (!dist || dist.respondentCount <= 0) return []
|
|
const bars = dist.rows.map((row) => ({
|
|
label: row.label,
|
|
value: Math.round((row.count / dist.respondentCount) * 100),
|
|
}))
|
|
return maxBars ? bars.slice(0, maxBars) : bars
|
|
}
|