Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(patch) #139

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
import { errorSlice } from "../features/Errors/errorsSlice";
import { warningSlice } from "../features/Errors/warningSlice";
import { pagesSlice } from "../features/Pages/pagesSlice";
import { openFilesSlice } from "../features/OpenFiles/openFilesSlice";
import mergeInitialState from "redux-persist/lib/stateReconciler/autoMergeLevel2";
import { listenerMiddleware } from "./middleware";

Expand Down Expand Up @@ -79,7 +78,6 @@ const rootReducer = combineSlices(
errorSlice,
warningSlice,
pagesSlice,
openFilesSlice,
);

const rootPersistConfig = {
Expand Down
21 changes: 21 additions & 0 deletions src/components/Markdown/Markdown.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@
.list {
padding-inline-start: var(--space-6);
}

.patch_title {
padding-left: var(--space-2);
padding-right: var(--space-2);
}

.patch_title,
.patch_title::before,
.patch_title::after {
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
border-bottom-width: 0;
}

.patch_title + pre {
margin-top: 0;
}

.patch_title + pre pre {
margin-top: 0;
}
121 changes: 48 additions & 73 deletions src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ import {
Strong,
Button,
Flex,
Box,
Card,
} from "@radix-ui/themes";
import rehypeKatex from "rehype-katex";
import remarkMath from "remark-math";
import "katex/dist/katex.min.css";
import { diffApi, isDetailMessage } from "../../services/refact";
import {
useConfig,
useDiffApplyMutation,
useEventsBusForIDE,
} from "../../hooks";
import { selectOpenFiles } from "../../features/OpenFiles/openFilesSlice";
import { useEventsBusForIDE } from "../../hooks";
import { useSelector } from "react-redux";
import { ErrorCallout, DiffWarningCallout } from "../Callout";
import {
selectIsStreaming,
selectIsWaiting,
selectMessages,
} from "../../features/Chat";
import { TruncateLeft } from "../Text";

export type MarkdownProps = Pick<
React.ComponentProps<typeof ReactMarkdown>,
Expand All @@ -51,10 +47,13 @@ export type MarkdownProps = Pick<
> & { canHavePins?: boolean };

const usePinActions = () => {
const { diffPreview, startFileAnimation, stopFileAnimation } =
useEventsBusForIDE();
const { onSubmit, result: _result } = useDiffApplyMutation();
const openFiles = useSelector(selectOpenFiles);
const {
diffPreview,
startFileAnimation,
stopFileAnimation,
openFile,
writeResultsToFile,
} = useEventsBusForIDE();
const messages = useSelector(selectMessages);
const isStreaming = useSelector(selectIsStreaming);
const isWaiting = useSelector(selectIsWaiting);
Expand Down Expand Up @@ -90,11 +89,8 @@ const usePinActions = () => {
})
.then((patch) => {
stopFileAnimation(fileName);
if (patch.chunks.length === 0) {
setErrorMessage({ type: "warning", text: "No Chunks to show." });
} else {
diffPreview(patch);
}
// TODO: might work with patch results?
diffPreview(patch);
})
.catch((error: Error | { data: { detail: string } }) => {
stopFileAnimation(fileName);
Expand Down Expand Up @@ -130,28 +126,7 @@ const usePinActions = () => {
})
.then((patch) => {
stopFileAnimation(fileName);
const files = patch.results.reduce<string[]>((acc, cur) => {
const { file_name_add, file_name_delete, file_name_edit } = cur;
if (file_name_add) acc.push(file_name_add);
if (file_name_delete) acc.push(file_name_delete);
if (file_name_edit) acc.push(file_name_edit);
return acc;
}, []);

if (files.length === 0) {
setErrorMessage({ type: "warning", text: "No chunks to apply" });
return;
}

const fileIsOpen = files.some((file) => openFiles.includes(file));

if (fileIsOpen) {
diffPreview(patch);
} else {
const chunks = patch.chunks;
const toApply = chunks.map(() => true);
void onSubmit({ chunks, toApply });
}
writeResultsToFile(patch.results);
})
.catch((error: Error | { data: { detail: string } }) => {
stopFileAnimation(fileName);
Expand All @@ -169,13 +144,11 @@ const usePinActions = () => {
});
},
[
diffPreview,
getPatch,
messages,
onSubmit,
openFiles,
startFileAnimation,
stopFileAnimation,
writeResultsToFile,
],
);

Expand All @@ -186,54 +159,56 @@ const usePinActions = () => {
handleApply,
resetErrorMessage,
disable,
openFile,
};
};

const MaybePinButton: React.FC<{
key?: Key | null;
children?: React.ReactNode;
}> = ({ children }) => {
const { host } = useConfig();

const isPin = typeof children === "string" && children.startsWith("📍");

const { handleApply, handleShow, errorMessage, resetErrorMessage, disable } =
usePinActions();
const {
handleApply,
handleShow,
errorMessage,
resetErrorMessage,
disable,
openFile,
} = usePinActions();

if (isPin) {
const [cmd, ticket, filePath] = children.split(" ");
const [_cmd, _ticket, filePath, ..._rest] = children.split(" ");
return (
<Box>
<Flex my="2" gap="2" wrap="wrap-reverse">
<Text
as="p"
wrap="wrap"
style={{ lineBreak: "anywhere", wordBreak: "break-all" }}
>
{cmd} {ticket}{" "}
{host !== "web" || import.meta.env.MODE === "development" ? (
<Link
wrap="wrap"
href=""
// TODO: button that looks like a link
// disabled={disable}
onClick={(event) => {
event.preventDefault();
handleShow(children);
}}
>
{filePath}
</Link>
) : (
filePath
)}
</Text>
<Card className={styles.patch_title} size="1" variant="surface" mt="4">
<Flex gap="2" py="2" pl="2">
<TruncateLeft>
<Link
href=""
title="Open file"
onClick={(event) => {
event.preventDefault();
openFile({ file_name: filePath });
}}
>
{filePath}
</Link>
</TruncateLeft>{" "}
<Flex gap="2" justify="end" ml="auto">
<Button
size="1"
onClick={() => handleShow(children)}
disabled={disable}
title={`Show: ${children}`}
>
Show
</Button>
<Button
size="1"
onClick={() => handleApply(children)}
disabled={disable}
title="Apply patch"
title={`Apply: ${children}`}
>
Apply
</Button>
Expand All @@ -251,7 +226,7 @@ const MaybePinButton: React.FC<{
message={errorMessage.text}
/>
)}
</Box>
</Card>
);
}

Expand Down
5 changes: 1 addition & 4 deletions src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import type { FIMDebugState } from "../hooks";
// import { rootReducer } from "../app/store";
export { updateConfig, type Config } from "../features/Config/configSlice";
export { type FileInfo, setFileInfo } from "../features/Chat/activeFile";
export {
setOpenFiles,
type OpenFilesState,
} from "../features/OpenFiles/openFilesSlice";
export {
type Snippet,
setSelectedSnippet,
Expand Down Expand Up @@ -53,6 +49,7 @@ export {
ideOpenChatInNewTab,
ideAnimateFileStart,
ideAnimateFileStop,
ideWriteResultsToFile,
} from "../hooks/useEventBusForIDE";

export const fim = {
Expand Down
25 changes: 0 additions & 25 deletions src/features/OpenFiles/openFilesSlice.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/hooks/useEventBusForApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useConfig } from "./useConfig";
import { updateConfig } from "../features/Config/configSlice";
import { setFileInfo } from "../features/Chat/activeFile";
import { setSelectedSnippet } from "../features/Chat/selectedSnippet";
import { setOpenFiles } from "../features/OpenFiles/openFilesSlice";
import { newChatAction } from "../features/Chat/Thread/actions";
import {
isPageInHistory,
Expand Down Expand Up @@ -40,10 +39,6 @@ export function useEventBusForApp() {
dispatch(newChatAction(event.data.payload));
}

if (setOpenFiles.match(event.data)) {
dispatch(event.data);
}

if (resetDiffApi.match(event.data)) {
dispatch(diffApi.util.resetApiState());
}
Expand Down
22 changes: 21 additions & 1 deletion src/hooks/useEventBusForIDE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import {
HostSettings,
SetupHost,
} from "../events/setup";
import type { DiffPreviewResponse } from "../services/refact";
import type { DiffPreviewResponse, PatchResult } from "../services/refact";

export const ideDiffPasteBackAction = createAction<string>("ide/diffPasteBack");

export const ideDiffPreviewAction =
createAction<DiffPreviewResponse>("ide/diffPreview");

export const ideOpenSettingsAction = createAction("ide/openSettings");

export const ideNewFileAction = createAction<string>("ide/newFile");

export const ideOpenHotKeys = createAction("ide/openHotKeys");

export type OpenFilePayload = {
file_name: string;
line?: number;
};
export const ideOpenFile = createAction<OpenFilePayload>("ide/openFile");

export const ideOpenChatInNewTab = createAction<ChatThread>(
"ide/openChatInNewTab",
);
Expand All @@ -29,6 +36,10 @@ export const ideAnimateFileStart = createAction<string>(

export const ideAnimateFileStop = createAction<string>("ide/animateFile/stop");

export const ideWriteResultsToFile = createAction<PatchResult[]>(
"ide/writeResultsToFile",
);

import { pathApi } from "../services/refact/path";

export const useEventsBusForIDE = () => {
Expand Down Expand Up @@ -126,6 +137,14 @@ export const useEventsBusForIDE = () => {
[postMessage],
);

const writeResultsToFile = useCallback(
(results: PatchResult[]) => {
const action = ideWriteResultsToFile(results);
postMessage(action);
},
[postMessage],
);

const [getCustomizationPath] = pathApi.useLazyCustomizationPathQuery();
const [getPrivacyPath] = pathApi.useLazyPrivacyPathQuery();
const [getBringYourOwnKeyPath] = pathApi.useLazyBringYourOwnKeyPathQuery();
Expand Down Expand Up @@ -169,5 +188,6 @@ export const useEventsBusForIDE = () => {
// canPaste,
stopFileAnimation,
startFileAnimation,
writeResultsToFile,
};
};
2 changes: 1 addition & 1 deletion src/services/refact/diffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function isPatchState(json: unknown): json is PatchState {
return true;
}

type PatchResult = {
export type PatchResult = {
file_text: string;
file_name_edit: string | null;
file_name_delete: string | null;
Expand Down
Loading