Skip to content

Commit 57753e2

Browse files
authored
Modify git commit modal (#9607)
1 parent f96bcd7 commit 57753e2

20 files changed

+497
-427
lines changed

packages/insomnia/src/common/get-workspace-label.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
1-
import { isDesign, isEnvironment, isMcp, isMockServer, type Workspace } from '../models/workspace';
1+
import type { IconProp } from '@fortawesome/fontawesome-svg-core';
2+
3+
import { isDesign, isEnvironment, isMcp, isMockServer, type Workspace, type WorkspaceScope } from '../models/workspace';
24
import { strings } from './strings';
35

6+
export type ProjectScopeKeys = WorkspaceScope | 'unsynced';
7+
8+
export const scopeToLabelMap: Record<
9+
ProjectScopeKeys,
10+
'Document' | 'Collection' | 'Mock Server' | 'Unsynced' | 'Environment' | 'MCP Client'
11+
> = {
12+
'design': 'Document',
13+
'collection': 'Collection',
14+
'mock-server': 'Mock Server',
15+
'unsynced': 'Unsynced',
16+
'environment': 'Environment',
17+
'mcp': 'MCP Client',
18+
};
19+
20+
export const scopeToIconMap: Record<ProjectScopeKeys, IconProp> = {
21+
'design': 'file',
22+
'collection': 'bars',
23+
'mock-server': 'server',
24+
'unsynced': 'cloud-download',
25+
'environment': 'code',
26+
'mcp': ['fac', 'mcp'] as unknown as IconProp,
27+
};
28+
29+
export const scopeToBgColorMap: Record<ProjectScopeKeys, string> = {
30+
'design': 'bg-(--color-info)',
31+
'collection': 'bg-(--color-surprise)',
32+
'mock-server': 'bg-(--color-warning)',
33+
'unsynced': 'bg-(--hl-md)',
34+
'environment': 'bg-(--color-font)',
35+
'mcp': 'bg-(--color-danger)',
36+
};
37+
38+
export const scopeToTextColorMap: Record<ProjectScopeKeys, string> = {
39+
'design': 'text-(--color-font-info)',
40+
'collection': 'text-(--color-font-surprise)',
41+
'mock-server': 'text-(--color-font-warning)',
42+
'unsynced': 'text-(--color-font)',
43+
'environment': 'text-(--color-bg)',
44+
'mcp': 'text-(--color-font-danger)',
45+
};
46+
447
export const getWorkspaceLabel = (workspace: Workspace) => {
548
if (isDesign(workspace)) {
649
return strings.document;

packages/insomnia/src/common/significant-diff-detection.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path';
22

3+
import { diffLines } from 'diff';
34
import { parse } from 'yaml';
45

56
import { normalizeScripts } from '~/common/insomnia-schema-migrations/v5.1';
@@ -181,3 +182,85 @@ export function hasSignificantChanges(
181182
return originalContent !== modifiedContent;
182183
}
183184
}
185+
186+
/**
187+
* Find lines that represent "system changes" - lines that were modified (not added/deleted)
188+
* and contain the "modified" property.
189+
*
190+
* A "modification" is detected by finding adjacent removed and added chunks in the diff.
191+
* For each such pair, we check if any line contains "modified" and mark those lines.
192+
*/
193+
export function findSystemChangeLines(
194+
original: string,
195+
modified: string,
196+
): {
197+
originalLines: number[];
198+
modifiedLines: number[];
199+
} {
200+
const originalLines: number[] = [];
201+
const modifiedLines: number[] = [];
202+
203+
try {
204+
const changes = diffLines(original, modified);
205+
206+
let originalLineNumber = 1;
207+
let modifiedLineNumber = 1;
208+
209+
for (let i = 0; i < changes.length; i++) {
210+
const change = changes[i];
211+
const lines = change.value.split('\n');
212+
// diffLines includes trailing newline, so remove empty last element
213+
if (lines[lines.length - 1] === '') {
214+
lines.pop();
215+
}
216+
const lineCount = lines.length;
217+
218+
if (!change.added && !change.removed) {
219+
// Unchanged lines - advance both line counters
220+
originalLineNumber += lineCount;
221+
modifiedLineNumber += lineCount;
222+
} else if (change.removed && !change.added) {
223+
// Check if next change is an addition (which would make this a modification)
224+
const nextChange = changes[i + 1];
225+
if (nextChange && nextChange.added && !nextChange.removed) {
226+
// This is a modification: removed lines followed by added lines
227+
const addedLines = nextChange.value.split('\n');
228+
if (addedLines[addedLines.length - 1] === '') {
229+
addedLines.pop();
230+
}
231+
232+
// Check each line pair for "modified" property
233+
// For modifications, we consider lines that contain "modified:" in either version
234+
lines.forEach((originalLine, j) => {
235+
if (originalLine && /\bmodified\b/.test(originalLine)) {
236+
originalLines.push(originalLineNumber + j);
237+
}
238+
});
239+
240+
addedLines.forEach((modifiedLine, j) => {
241+
if (modifiedLine && /\bmodified\b/.test(modifiedLine)) {
242+
modifiedLines.push(modifiedLineNumber + j);
243+
}
244+
});
245+
246+
// Advance original line counter for removed lines
247+
originalLineNumber += lineCount;
248+
// Advance modified line counter for added lines
249+
modifiedLineNumber += addedLines.length;
250+
// Skip the next change since we've processed it
251+
i++;
252+
} else {
253+
// Pure deletion - just advance original line counter
254+
originalLineNumber += lineCount;
255+
}
256+
} else if (change.added && !change.removed) {
257+
// Pure addition - just advance modified line counter
258+
modifiedLineNumber += lineCount;
259+
}
260+
}
261+
} catch (error) {
262+
console.error('Error finding system change lines:', error);
263+
}
264+
265+
return { originalLines, modifiedLines };
266+
}

packages/insomnia/src/main/git-service.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,16 +2144,22 @@ export const unstageChangesAction = async ({
21442144
}
21452145
};
21462146

2147-
function getPreviewItemName(previewDiffItem: { before: string; after: string }) {
2147+
function getPreviewItemNameAndScope(previewDiffItem: { before: string; after: string }) {
21482148
let prevName = '';
21492149
let nextName = '';
21502150

2151+
let prevScope: WorkspaceScope = 'collection';
2152+
let nextScope: WorkspaceScope = 'collection';
2153+
21512154
try {
21522155
const prev = parse(previewDiffItem.before);
21532156

21542157
if ((prev && 'fileName' in prev) || 'name' in prev) {
21552158
prevName = prev.fileName || prev.name;
21562159
}
2160+
if ('type' in prev) {
2161+
prevScope = insomniaSchemaTypeToScope(prev.type);
2162+
}
21572163
} catch {
21582164
// Nothing to do
21592165
}
@@ -2163,11 +2169,17 @@ function getPreviewItemName(previewDiffItem: { before: string; after: string })
21632169
if ((next && 'fileName' in next) || 'name' in next) {
21642170
nextName = next.fileName || next.name;
21652171
}
2172+
if ('type' in next) {
2173+
nextScope = insomniaSchemaTypeToScope(next.type);
2174+
}
21662175
} catch {
21672176
// Nothing to do
21682177
}
21692178

2170-
return nextName || prevName;
2179+
return {
2180+
name: nextName || prevName,
2181+
scope: nextScope || prevScope,
2182+
};
21712183
}
21722184

21732185
export type GitDiffResult =
@@ -2177,6 +2189,9 @@ export type GitDiffResult =
21772189
before: string;
21782190
after: string;
21792191
};
2192+
filepath: string;
2193+
scope: WorkspaceScope;
2194+
staged: boolean;
21802195
}
21812196
| {
21822197
errors: string[];
@@ -2207,9 +2222,14 @@ export const diffFileLoader = async ({
22072222
after: fileStatus.workdir,
22082223
};
22092224

2225+
const { name, scope } = getPreviewItemNameAndScope(diff);
2226+
22102227
return {
2211-
name: getPreviewItemName(diff) || filepath,
2228+
name: name || filepath,
22122229
diff,
2230+
filepath,
2231+
scope,
2232+
staged,
22132233
};
22142234
} catch (e) {
22152235
const errorMessage = e instanceof Error ? e.message : 'Error while unstaging changes';

packages/insomnia/src/routes/organization.$organizationId.project.$projectId._index.tsx

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getAppWebsiteBaseURL,
3333
} from '~/common/constants';
3434
import { database } from '~/common/database';
35+
import { scopeToBgColorMap, scopeToIconMap, scopeToLabelMap, scopeToTextColorMap } from '~/common/get-workspace-label';
3536
import { fuzzyMatchAll, isNotNullOrUndefined } from '~/common/misc';
3637
import { descendingNumberSort, sortMethodMap } from '~/common/sorting';
3738
import * as models from '~/models';
@@ -85,46 +86,6 @@ import { trackTempProjectOpened } from '~/ui/temp-segment-tracking';
8586
import { isPrimaryClickModifier } from '~/ui/utils';
8687
import { invariant } from '~/utils/invariant';
8788

88-
export type ProjectScopeKeys = WorkspaceScope | 'unsynced';
89-
export const scopeToLabelMap: Record<
90-
ProjectScopeKeys,
91-
'Document' | 'Collection' | 'Mock Server' | 'Unsynced' | 'Environment' | 'MCP Client'
92-
> = {
93-
'design': 'Document',
94-
'collection': 'Collection',
95-
'mock-server': 'Mock Server',
96-
'unsynced': 'Unsynced',
97-
'environment': 'Environment',
98-
'mcp': 'MCP Client',
99-
};
100-
101-
export const scopeToIconMap: Record<ProjectScopeKeys, IconProp> = {
102-
'design': 'file',
103-
'collection': 'bars',
104-
'mock-server': 'server',
105-
'unsynced': 'cloud-download',
106-
'environment': 'code',
107-
'mcp': ['fac', 'mcp'] as unknown as IconProp,
108-
};
109-
110-
export const scopeToBgColorMap: Record<ProjectScopeKeys, string> = {
111-
'design': 'bg-(--color-info)',
112-
'collection': 'bg-(--color-surprise)',
113-
'mock-server': 'bg-(--color-warning)',
114-
'unsynced': 'bg-(--hl-md)',
115-
'environment': 'bg-(--color-font)',
116-
'mcp': 'bg-(--color-danger)',
117-
};
118-
119-
export const scopeToTextColorMap: Record<ProjectScopeKeys, string> = {
120-
'design': 'text-(--color-font-info)',
121-
'collection': 'text-(--color-font-surprise)',
122-
'mock-server': 'text-(--color-font-warning)',
123-
'unsynced': 'text-(--color-font)',
124-
'environment': 'text-(--color-bg)',
125-
'mcp': 'text-(--color-font-danger)',
126-
};
127-
12889
export interface InsomniaFile {
12990
id: string;
13091
name: string;

packages/insomnia/src/routes/organization.$organizationId.project.$projectId.list-workspaces.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { href } from 'react-router';
22

33
import { parseApiSpec, type ParsedApiSpec } from '~/common/api-specs';
44
import { database } from '~/common/database';
5+
import { scopeToLabelMap } from '~/common/get-workspace-label';
56
import { isNotNullOrUndefined } from '~/common/misc';
67
import { descendingNumberSort } from '~/common/sorting';
78
import * as models from '~/models';
@@ -16,7 +17,7 @@ import { invariant } from '~/utils/invariant';
1617
import { createFetcherLoadHook } from '~/utils/router';
1718

1819
import type { Route } from './+types/organization.$organizationId.project.$projectId.list-workspaces';
19-
import { type InsomniaFile, scopeToLabelMap } from './organization.$organizationId.project.$projectId._index';
20+
import { type InsomniaFile } from './organization.$organizationId.project.$projectId._index';
2021

2122
async function getAllLocalFiles({ projectId }: { projectId: string }) {
2223
const projectWorkspaces = await models.workspace.findByParentId(projectId);

0 commit comments

Comments
 (0)