|
| 1 | +import React, { useCallback } from 'react'; |
| 2 | +import type { ConventionalLabel, ConventionalDecoration } from '@plannotator/ui/types'; |
| 3 | + |
| 4 | +/** Semantic tone — maps to theme CSS variables, not arbitrary hex */ |
| 5 | +type SemanticTone = 'danger' | 'warn' | 'success' | 'info' | 'neutral'; |
| 6 | + |
| 7 | +export interface LabelDef { |
| 8 | + label: ConventionalLabel; |
| 9 | + display: string; |
| 10 | + tone: SemanticTone; |
| 11 | + /** Whether the blocking/non-blocking toggle shows in the picker for this label */ |
| 12 | + showBlockingToggle: boolean; |
| 13 | + hint: string; |
| 14 | +} |
| 15 | + |
| 16 | +export const CONVENTIONAL_LABELS: LabelDef[] = [ |
| 17 | + // Everyday — the three you reach for on 90%+ of comments |
| 18 | + { label: 'suggestion', display: 'suggestion', tone: 'info', showBlockingToggle: true, hint: 'Proposes an improvement' }, |
| 19 | + { label: 'nitpick', display: 'nit', tone: 'neutral', showBlockingToggle: false, hint: 'Trivial, preference-based' }, |
| 20 | + { label: 'question', display: 'question', tone: 'info', showBlockingToggle: true, hint: 'Seeking clarification' }, |
| 21 | + // High-signal |
| 22 | + { label: 'issue', display: 'issue', tone: 'danger', showBlockingToggle: true, hint: 'A problem that needs addressing' }, |
| 23 | + { label: 'praise', display: 'praise', tone: 'success', showBlockingToggle: false, hint: 'Highlight something positive' }, |
| 24 | + // Soft / meta |
| 25 | + { label: 'thought', display: 'thought', tone: 'neutral', showBlockingToggle: false, hint: 'An idea, not a request' }, |
| 26 | + { label: 'note', display: 'note', tone: 'neutral', showBlockingToggle: false, hint: 'Informational, no action needed' }, |
| 27 | + // Process |
| 28 | + { label: 'todo', display: 'todo', tone: 'warn', showBlockingToggle: true, hint: 'Small, necessary change' }, |
| 29 | + { label: 'chore', display: 'chore', tone: 'warn', showBlockingToggle: true, hint: 'Process task (CI, changelog, etc.)' }, |
| 30 | +]; |
| 31 | + |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | +// Picker |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | + |
| 36 | +/** Resolve which labels to show based on user config (null = all defaults; empty array = user cleared all) */ |
| 37 | +export function getEnabledLabels(configJson: string | null): LabelDef[] { |
| 38 | + if (!configJson) return CONVENTIONAL_LABELS; |
| 39 | + try { |
| 40 | + const parsed = JSON.parse(configJson) as Array<Record<string, unknown>>; |
| 41 | + if (!Array.isArray(parsed)) return CONVENTIONAL_LABELS; |
| 42 | + return parsed.map(cfg => { |
| 43 | + const builtIn = CONVENTIONAL_LABELS.find(l => l.label === cfg.label); |
| 44 | + return { |
| 45 | + label: cfg.label as ConventionalLabel, |
| 46 | + display: cfg.display as string, |
| 47 | + tone: builtIn?.tone || 'neutral', |
| 48 | + showBlockingToggle: cfg.blocking === true || cfg.blocking === 'true', |
| 49 | + hint: builtIn?.hint || (cfg.display as string), |
| 50 | + }; |
| 51 | + }); |
| 52 | + } catch { |
| 53 | + return CONVENTIONAL_LABELS; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +interface ConventionalLabelPickerProps { |
| 58 | + selected: ConventionalLabel | null; |
| 59 | + decorations: ConventionalDecoration[]; |
| 60 | + onSelect: (label: ConventionalLabel | null) => void; |
| 61 | + onDecorationsChange: (decorations: ConventionalDecoration[]) => void; |
| 62 | + /** Filtered label list from config (defaults to all) */ |
| 63 | + enabledLabels?: LabelDef[]; |
| 64 | +} |
| 65 | + |
| 66 | +export const ConventionalLabelPicker: React.FC<ConventionalLabelPickerProps> = ({ |
| 67 | + selected, |
| 68 | + decorations, |
| 69 | + onSelect, |
| 70 | + onDecorationsChange, |
| 71 | + enabledLabels = CONVENTIONAL_LABELS, |
| 72 | +}) => { |
| 73 | + const handleLabelClick = useCallback((label: ConventionalLabel) => { |
| 74 | + if (selected === label) { |
| 75 | + onSelect(null); |
| 76 | + onDecorationsChange([]); |
| 77 | + } else { |
| 78 | + onSelect(label); |
| 79 | + const def = enabledLabels.find(l => l.label === label); |
| 80 | + // If blocking toggle is enabled for this label, default to non-blocking |
| 81 | + if (def?.showBlockingToggle) { |
| 82 | + onDecorationsChange(['non-blocking']); |
| 83 | + } else { |
| 84 | + onDecorationsChange([]); |
| 85 | + } |
| 86 | + } |
| 87 | + }, [selected, enabledLabels, onSelect, onDecorationsChange]); |
| 88 | + |
| 89 | + const activeDef = selected ? enabledLabels.find(l => l.label === selected) : undefined; |
| 90 | + const isBlocking = decorations.includes('blocking'); |
| 91 | + const showToggle = activeDef?.showBlockingToggle ?? false; |
| 92 | + |
| 93 | + const toggleBlocking = useCallback(() => { |
| 94 | + const cleaned = decorations.filter(d => d !== 'blocking' && d !== 'non-blocking'); |
| 95 | + onDecorationsChange([...cleaned, isBlocking ? 'non-blocking' : 'blocking']); |
| 96 | + }, [decorations, isBlocking, onDecorationsChange]); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className="cc-picker"> |
| 100 | + <div className="cc-row"> |
| 101 | + {enabledLabels.map((def, idx) => { |
| 102 | + const isActive = selected === def.label; |
| 103 | + return ( |
| 104 | + <button |
| 105 | + key={`${idx}-${def.label}`} |
| 106 | + type="button" |
| 107 | + className={`cc-tag cc-tone-${def.tone}${isActive ? ' active' : ''}`} |
| 108 | + onClick={() => handleLabelClick(def.label)} |
| 109 | + title={def.hint} |
| 110 | + > |
| 111 | + {def.display} |
| 112 | + </button> |
| 113 | + ); |
| 114 | + })} |
| 115 | + |
| 116 | + {/* Blocking toggle — shown when enabled for this label */} |
| 117 | + {showToggle && ( |
| 118 | + <button |
| 119 | + type="button" |
| 120 | + className={`cc-blocking-toggle ${isBlocking ? 'is-blocking' : ''}`} |
| 121 | + onClick={toggleBlocking} |
| 122 | + title={isBlocking ? 'Must resolve before merge' : 'Optional, not blocking'} |
| 123 | + > |
| 124 | + <span className="cc-toggle-track"> |
| 125 | + <span className="cc-toggle-thumb" /> |
| 126 | + </span> |
| 127 | + {isBlocking ? 'blocking' : 'non-blocking'} |
| 128 | + </button> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +// --------------------------------------------------------------------------- |
| 136 | +// Badge (inline annotation header) |
| 137 | +// --------------------------------------------------------------------------- |
| 138 | + |
| 139 | +export const ConventionalLabelBadge: React.FC<{ |
| 140 | + label: ConventionalLabel; |
| 141 | + decorations?: ConventionalDecoration[]; |
| 142 | +}> = ({ label, decorations }) => { |
| 143 | + const def = CONVENTIONAL_LABELS.find(l => l.label === label); |
| 144 | + // Fall back gracefully for custom labels not in the built-in list |
| 145 | + const tone = def?.tone || 'neutral'; |
| 146 | + const display = def?.display || label; |
| 147 | + |
| 148 | + const isBlocking = decorations?.includes('blocking'); |
| 149 | + const hasDecoration = isBlocking || decorations?.includes('non-blocking'); |
| 150 | + |
| 151 | + return ( |
| 152 | + <span className={`cc-inline-badge cc-tone-${tone}`}> |
| 153 | + {display} |
| 154 | + {hasDecoration && ( |
| 155 | + <span className={`cc-inline-dec${isBlocking ? ' cc-inline-dec-blocking' : ''}`}> |
| 156 | + {isBlocking ? 'blocking' : 'non-blocking'} |
| 157 | + </span> |
| 158 | + )} |
| 159 | + </span> |
| 160 | + ); |
| 161 | +}; |
0 commit comments