1.5 KB50 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import {atom} from 'jotai';
9import {writeAtom} from '../jotaiUtils';
10import platform from '../platform';
11import {registerDisposable} from '../utils';
12import type {CodeReviewIssue, CodeReviewProgressStatus} from './types';
13
14/**
15 * Atom that stores the current status of the AI code review.
16 */
17export const codeReviewStatusAtom = atom<CodeReviewProgressStatus | null>(null);
18
19/**
20 * Atom that stores comments for the current review.
21 */
22export const firstPassCommentData = atom<CodeReviewIssue[]>([]);
23
24export const firstPassCommentDataCount = atom(get => get(firstPassCommentData).length);
25
26export const firstPassCommentError = atom<Error | undefined>(undefined);
27
28/**
29 * Derived atom that maps comments by file path.
30 * The resulting object has file paths as keys and arrays of CodeReviewIssue as values.
31 */
32export const commentsByFilePathAtom = atom(get => {
33 const comments = get(firstPassCommentData);
34 return comments.reduce<Record<string, CodeReviewIssue[]>>((acc, comment) => {
35 if (!acc[comment.filepath]) {
36 acc[comment.filepath] = [];
37 }
38 acc[comment.filepath].push(comment);
39 return acc;
40 }, {});
41});
42
43registerDisposable(
44 firstPassCommentData,
45 platform.aiCodeReview?.onDidChangeAIReviewComments(comments => {
46 writeAtom(firstPassCommentData, comments);
47 }) ?? {dispose: () => {}},
48 import.meta.hot,
49);
50