| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 11 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 12 | import {loadable} from 'jotai/utils'; |
| b69ab31 | | | 13 | import {tryJsonParse} from 'shared/utils'; |
| b69ab31 | | | 14 | import serverAPI from '../ClientToServerAPI'; |
| b69ab31 | | | 15 | import {tracker} from '../analytics'; |
| b69ab31 | | | 16 | import {codeReviewProvider} from '../codeReview/CodeReviewInfo'; |
| b69ab31 | | | 17 | import {T} from '../i18n'; |
| b69ab31 | | | 18 | import {atomFamilyWeak} from '../jotaiUtils'; |
| b69ab31 | | | 19 | import {uncommittedChangesWithPreviews} from '../previews'; |
| b69ab31 | | | 20 | import {commitByHash} from '../serverAPIState'; |
| b69ab31 | | | 21 | import {commitInfoViewCurrentCommits, commitMode} from './CommitInfoState'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | import './SuggestedReviewers.css'; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | const MAX_VISIBLE_RECENT_REVIEWERS = 3; |
| b69ab31 | | | 26 | const RECENT_REVIEWERS_STORAGE_KEY = 'ISL_RECENT_REVIEWERS'; |
| b69ab31 | | | 27 | /** |
| b69ab31 | | | 28 | * Half-life for frecency decay in days. After this many days, |
| b69ab31 | | | 29 | * the recency multiplier is halved. |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | const FRECENCY_HALF_LIFE_DAYS = 14; |
| b69ab31 | | | 32 | /** |
| b69ab31 | | | 33 | * Maximum age in days before a reviewer is pruned from storage. |
| b69ab31 | | | 34 | * Reviewers not used within this period are removed to prevent |
| b69ab31 | | | 35 | * unbounded localStorage growth. |
| b69ab31 | | | 36 | */ |
| b69ab31 | | | 37 | const MAX_REVIEWER_AGE_DAYS = 90; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | type ReviewerData = {count: number; lastUsed: number}; |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | /** |
| b69ab31 | | | 42 | * Frecency-based recent reviewers, persisted to localStorage. |
| b69ab31 | | | 43 | * Combines frequency (how often used) with recency (how recently used) |
| b69ab31 | | | 44 | * using exponential decay. More recent usage has higher weight. |
| b69ab31 | | | 45 | */ |
| b69ab31 | | | 46 | class RecentReviewers { |
| b69ab31 | | | 47 | private recent: Map<string, ReviewerData>; |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | constructor() { |
| b69ab31 | | | 50 | try { |
| b69ab31 | | | 51 | const stored = tryJsonParse( |
| b69ab31 | | | 52 | localStorage.getItem(RECENT_REVIEWERS_STORAGE_KEY) ?? '[]', |
| b69ab31 | | | 53 | ) as Array<[string, number | ReviewerData]> | null; |
| b69ab31 | | | 54 | this.recent = new Map(); |
| b69ab31 | | | 55 | const maxAge = MAX_REVIEWER_AGE_DAYS * 24 * 60 * 60 * 1000; |
| b69ab31 | | | 56 | const now = Date.now(); |
| b69ab31 | | | 57 | let needsPersist = false; |
| b69ab31 | | | 58 | if (stored) { |
| b69ab31 | | | 59 | for (const [key, value] of stored) { |
| b69ab31 | | | 60 | if (typeof value === 'number') { |
| b69ab31 | | | 61 | // Migrate from old format (count only) to new format |
| b69ab31 | | | 62 | this.recent.set(key, {count: value, lastUsed: now}); |
| b69ab31 | | | 63 | needsPersist = true; |
| b69ab31 | | | 64 | } else if (now - value.lastUsed <= maxAge) { |
| b69ab31 | | | 65 | // Only keep reviewers used within MAX_REVIEWER_AGE_DAYS |
| b69ab31 | | | 66 | this.recent.set(key, value); |
| b69ab31 | | | 67 | } else { |
| b69ab31 | | | 68 | needsPersist = true; |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | } |
| b69ab31 | | | 72 | if (needsPersist) { |
| b69ab31 | | | 73 | this.persist(); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | } catch { |
| b69ab31 | | | 76 | this.recent = new Map(); |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | } |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | private persist() { |
| b69ab31 | | | 81 | try { |
| b69ab31 | | | 82 | localStorage.setItem( |
| b69ab31 | | | 83 | RECENT_REVIEWERS_STORAGE_KEY, |
| b69ab31 | | | 84 | JSON.stringify([...this.recent.entries()]), |
| b69ab31 | | | 85 | ); |
| b69ab31 | | | 86 | } catch {} |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | /** |
| b69ab31 | | | 90 | * Calculate frecency score for a reviewer. |
| b69ab31 | | | 91 | * Score = count * recencyMultiplier, where recencyMultiplier |
| b69ab31 | | | 92 | * decays exponentially based on time since last use. |
| b69ab31 | | | 93 | */ |
| b69ab31 | | | 94 | private getFrecencyScore(data: ReviewerData): number { |
| b69ab31 | | | 95 | const daysSinceLastUse = (Date.now() - data.lastUsed) / (1000 * 60 * 60 * 24); |
| b69ab31 | | | 96 | const recencyMultiplier = Math.pow(0.5, daysSinceLastUse / FRECENCY_HALF_LIFE_DAYS); |
| b69ab31 | | | 97 | return data.count * recencyMultiplier; |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | public useReviewer(reviewer: string) { |
| b69ab31 | | | 101 | const existing = this.recent.get(reviewer); |
| b69ab31 | | | 102 | this.recent.set(reviewer, { |
| b69ab31 | | | 103 | count: (existing?.count ?? 0) + 1, |
| b69ab31 | | | 104 | lastUsed: Date.now(), |
| b69ab31 | | | 105 | }); |
| b69ab31 | | | 106 | this.persist(); |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | public getRecent(): Array<string> { |
| b69ab31 | | | 110 | return [...this.recent.entries()] |
| b69ab31 | | | 111 | .map(([name, data]) => ({name, score: this.getFrecencyScore(data)})) |
| b69ab31 | | | 112 | .sort((a, b) => b.score - a.score) |
| b69ab31 | | | 113 | .slice(0, MAX_VISIBLE_RECENT_REVIEWERS) |
| b69ab31 | | | 114 | .map(({name}) => name); |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | } |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | export const recentReviewers = new RecentReviewers(); |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | /** |
| b69ab31 | | | 121 | * Since we use a selector to fetch suggestions, it will attempt to refetch |
| b69ab31 | | | 122 | * when any dependency (uncommitted changes, list of changed files) changes. |
| b69ab31 | | | 123 | * While technically suggestions could change if any edited path changes, |
| b69ab31 | | | 124 | * the UI flickers way to much. So let's cache the result within some time window. |
| b69ab31 | | | 125 | * using a time window ensures we don't overcache (for example, |
| b69ab31 | | | 126 | * in commit mode, where two commits may have totally different changes.) |
| b69ab31 | | | 127 | */ |
| b69ab31 | | | 128 | const cachedSuggestions = new Map<string, {lastFetch: number; reviewers: Array<string>}>(); |
| b69ab31 | | | 129 | const MAX_SUGGESTION_CACHE_AGE = 2 * 60 * 1000; |
| b69ab31 | | | 130 | const suggestedReviewersForCommit = atomFamilyWeak((hashOrHead: string | 'head' | undefined) => { |
| b69ab31 | | | 131 | return loadable( |
| b69ab31 | | | 132 | atom(get => { |
| b69ab31 | | | 133 | if (hashOrHead == null) { |
| b69ab31 | | | 134 | return []; |
| b69ab31 | | | 135 | } |
| b69ab31 | | | 136 | const context = { |
| b69ab31 | | | 137 | paths: [] as Array<string>, |
| b69ab31 | | | 138 | }; |
| b69ab31 | | | 139 | const cached = cachedSuggestions.get(hashOrHead); |
| b69ab31 | | | 140 | if (cached) { |
| b69ab31 | | | 141 | if (Date.now() - cached.lastFetch < MAX_SUGGESTION_CACHE_AGE) { |
| b69ab31 | | | 142 | return cached.reviewers; |
| b69ab31 | | | 143 | } |
| b69ab31 | | | 144 | } |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | if (hashOrHead === 'head') { |
| b69ab31 | | | 147 | const uncommittedChanges = get(uncommittedChangesWithPreviews); |
| b69ab31 | | | 148 | context.paths.push(...uncommittedChanges.slice(0, 10).map(change => change.path)); |
| b69ab31 | | | 149 | } else { |
| b69ab31 | | | 150 | const commit = get(commitByHash(hashOrHead)); |
| b69ab31 | | | 151 | if (commit?.isDot) { |
| b69ab31 | | | 152 | const uncommittedChanges = get(uncommittedChangesWithPreviews); |
| b69ab31 | | | 153 | context.paths.push(...uncommittedChanges.slice(0, 10).map(change => change.path)); |
| b69ab31 | | | 154 | } |
| b69ab31 | | | 155 | context.paths.push(...(commit?.filePathsSample.slice(0, 10) ?? [])); |
| b69ab31 | | | 156 | } |
| b69ab31 | | | 157 | |
| b69ab31 | | | 158 | return tracker.operation('GetSuggestedReviewers', 'FetchError', undefined, async () => { |
| b69ab31 | | | 159 | serverAPI.postMessage({ |
| b69ab31 | | | 160 | type: 'getSuggestedReviewers', |
| b69ab31 | | | 161 | key: hashOrHead, |
| b69ab31 | | | 162 | context, |
| b69ab31 | | | 163 | }); |
| b69ab31 | | | 164 | |
| b69ab31 | | | 165 | const response = await serverAPI.nextMessageMatching( |
| b69ab31 | | | 166 | 'gotSuggestedReviewers', |
| b69ab31 | | | 167 | message => message.key === hashOrHead, |
| b69ab31 | | | 168 | ); |
| b69ab31 | | | 169 | cachedSuggestions.set(hashOrHead, {lastFetch: Date.now(), reviewers: response.reviewers}); |
| b69ab31 | | | 170 | return response.reviewers; |
| b69ab31 | | | 171 | }); |
| b69ab31 | | | 172 | }), |
| b69ab31 | | | 173 | ); |
| b69ab31 | | | 174 | }); |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | export function SuggestedReviewers({ |
| b69ab31 | | | 177 | existingReviewers, |
| b69ab31 | | | 178 | addReviewer, |
| b69ab31 | | | 179 | }: { |
| b69ab31 | | | 180 | existingReviewers: Array<string>; |
| b69ab31 | | | 181 | addReviewer: (value: string) => unknown; |
| b69ab31 | | | 182 | }) { |
| b69ab31 | | | 183 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 184 | const recent = recentReviewers.getRecent().filter(s => !existingReviewers.includes(s)); |
| b69ab31 | | | 185 | const mode = useAtomValue(commitMode); |
| b69ab31 | | | 186 | const currentCommitInfoViewCommit = useAtomValue(commitInfoViewCurrentCommits); |
| b69ab31 | | | 187 | const currentCommit = currentCommitInfoViewCommit?.[0]; // assume we only have one commit |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | const key = currentCommit?.isDot && mode === 'commit' ? 'head' : (currentCommit?.hash ?? ''); |
| b69ab31 | | | 190 | const suggestedReviewers = useAtomValue(suggestedReviewersForCommit(key)); |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | const filteredSuggestions = ( |
| b69ab31 | | | 193 | suggestedReviewers.state === 'hasData' ? suggestedReviewers.data : [] |
| b69ab31 | | | 194 | ).filter(s => !existingReviewers.includes(s)); |
| b69ab31 | | | 195 | |
| b69ab31 | | | 196 | return ( |
| b69ab31 | | | 197 | <div className="suggested-reviewers" data-testid="suggested-reviewers"> |
| b69ab31 | | | 198 | {recent.length > 0 ? ( |
| b69ab31 | | | 199 | <div data-testid="recent-reviewers-list"> |
| b69ab31 | | | 200 | <div className="suggestion-header"> |
| b69ab31 | | | 201 | <T>Recent</T> |
| b69ab31 | | | 202 | </div> |
| b69ab31 | | | 203 | <div className="suggestions"> |
| b69ab31 | | | 204 | {recent.map(s => ( |
| b69ab31 | | | 205 | <Suggestion |
| b69ab31 | | | 206 | key={s} |
| b69ab31 | | | 207 | onClick={() => { |
| b69ab31 | | | 208 | addReviewer(s); |
| b69ab31 | | | 209 | tracker.track('AcceptSuggestedReviewer', {extras: {type: 'recent'}}); |
| b69ab31 | | | 210 | }}> |
| b69ab31 | | | 211 | {s} |
| b69ab31 | | | 212 | </Suggestion> |
| b69ab31 | | | 213 | ))} |
| b69ab31 | | | 214 | </div> |
| b69ab31 | | | 215 | </div> |
| b69ab31 | | | 216 | ) : null} |
| b69ab31 | | | 217 | {provider?.supportsSuggestedReviewers && |
| b69ab31 | | | 218 | (filteredSuggestions == null || filteredSuggestions.length > 0) ? ( |
| b69ab31 | | | 219 | <div data-testid="suggested-reviewers-list"> |
| b69ab31 | | | 220 | <div className="suggestion-header"> |
| b69ab31 | | | 221 | <T>Suggested</T> |
| b69ab31 | | | 222 | </div> |
| b69ab31 | | | 223 | <div className="suggestions"> |
| b69ab31 | | | 224 | {suggestedReviewers.state === 'loading' && ( |
| b69ab31 | | | 225 | <div className="suggestions-loading"> |
| b69ab31 | | | 226 | <Icon icon="loading" /> |
| b69ab31 | | | 227 | </div> |
| b69ab31 | | | 228 | )} |
| b69ab31 | | | 229 | {filteredSuggestions?.map(s => ( |
| b69ab31 | | | 230 | <Suggestion |
| b69ab31 | | | 231 | key={s} |
| b69ab31 | | | 232 | onClick={() => { |
| b69ab31 | | | 233 | addReviewer(s); |
| b69ab31 | | | 234 | tracker.track('AcceptSuggestedReviewer', {extras: {type: 'suggested'}}); |
| b69ab31 | | | 235 | }}> |
| b69ab31 | | | 236 | {s} |
| b69ab31 | | | 237 | </Suggestion> |
| b69ab31 | | | 238 | )) ?? null} |
| b69ab31 | | | 239 | </div> |
| b69ab31 | | | 240 | </div> |
| b69ab31 | | | 241 | ) : null} |
| b69ab31 | | | 242 | </div> |
| b69ab31 | | | 243 | ); |
| b69ab31 | | | 244 | } |
| b69ab31 | | | 245 | |
| b69ab31 | | | 246 | function Suggestion({children, onClick}: {children: ReactNode; onClick: () => unknown}) { |
| b69ab31 | | | 247 | return ( |
| b69ab31 | | | 248 | <button className="suggestion token" onClick={onClick}> |
| b69ab31 | | | 249 | {children} |
| b69ab31 | | | 250 | </button> |
| b69ab31 | | | 251 | ); |
| b69ab31 | | | 252 | } |