| 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 {atom} from 'jotai'; |
| b69ab31 | | | 9 | import {writeAtom} from '../jotaiUtils'; |
| b69ab31 | | | 10 | import platform from '../platform'; |
| b69ab31 | | | 11 | import {registerDisposable} from '../utils'; |
| b69ab31 | | | 12 | import type {CodeReviewIssue, CodeReviewProgressStatus} from './types'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | /** |
| b69ab31 | | | 15 | * Atom that stores the current status of the AI code review. |
| b69ab31 | | | 16 | */ |
| b69ab31 | | | 17 | export const codeReviewStatusAtom = atom<CodeReviewProgressStatus | null>(null); |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | /** |
| b69ab31 | | | 20 | * Atom that stores comments for the current review. |
| b69ab31 | | | 21 | */ |
| b69ab31 | | | 22 | export const firstPassCommentData = atom<CodeReviewIssue[]>([]); |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | export const firstPassCommentDataCount = atom(get => get(firstPassCommentData).length); |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | export const firstPassCommentError = atom<Error | undefined>(undefined); |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | /** |
| b69ab31 | | | 29 | * Derived atom that maps comments by file path. |
| b69ab31 | | | 30 | * The resulting object has file paths as keys and arrays of CodeReviewIssue as values. |
| b69ab31 | | | 31 | */ |
| b69ab31 | | | 32 | export const commentsByFilePathAtom = atom(get => { |
| b69ab31 | | | 33 | const comments = get(firstPassCommentData); |
| b69ab31 | | | 34 | return comments.reduce<Record<string, CodeReviewIssue[]>>((acc, comment) => { |
| b69ab31 | | | 35 | if (!acc[comment.filepath]) { |
| b69ab31 | | | 36 | acc[comment.filepath] = []; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | acc[comment.filepath].push(comment); |
| b69ab31 | | | 39 | return acc; |
| b69ab31 | | | 40 | }, {}); |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | registerDisposable( |
| b69ab31 | | | 44 | firstPassCommentData, |
| b69ab31 | | | 45 | platform.aiCodeReview?.onDidChangeAIReviewComments(comments => { |
| b69ab31 | | | 46 | writeAtom(firstPassCommentData, comments); |
| b69ab31 | | | 47 | }) ?? {dispose: () => {}}, |
| b69ab31 | | | 48 | import.meta.hot, |
| b69ab31 | | | 49 | ); |