-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseHttpRequestWorkspace.ts
More file actions
155 lines (141 loc) · 4.32 KB
/
useHttpRequestWorkspace.ts
File metadata and controls
155 lines (141 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { TreqClient } from '@t-req/sdk/client';
import { type Accessor, createEffect, createMemo, createSignal, on } from 'solid-js';
import {
useRequestBodyDraftController,
useRequestHeaderDraftController,
useRequestParseDetails
} from '../components/request-workspace';
import type { WorkspaceRequest } from '../sdk';
import type { WorkspaceStore } from '../stores/workspace';
interface UseHttpRequestWorkspaceInput {
path: Accessor<string>;
client: Accessor<TreqClient | null>;
workspace: WorkspaceStore;
}
// Structured API with domain grouping
export interface HttpRequestWorkspaceState {
// Core request selection state
selection: {
index: Accessor<number>;
setIndex: (index: number) => void;
selected: Accessor<WorkspaceRequest | undefined>;
hasSelection: Accessor<boolean>;
};
// All requests in current file
requests: {
all: Accessor<WorkspaceRequest[]>;
count: Accessor<number>;
hasRequests: Accessor<boolean>;
};
// Draft controllers for editing
drafts: {
parse: ReturnType<typeof useRequestParseDetails>;
header: ReturnType<typeof useRequestHeaderDraftController>;
body: ReturnType<typeof useRequestBodyDraftController>;
};
// Actions/operations
actions: {
reset: () => void;
};
}
export function useHttpRequestWorkspace(
input: UseHttpRequestWorkspaceInput
): HttpRequestWorkspaceState {
const [selectedRequestIndex, setSelectedRequestIndex] = createSignal(0);
const requests = createMemo<WorkspaceRequest[]>(() => {
const path = input.path();
return input.workspace.requestsByPath()[path] ?? [];
});
const selectedRequest = createMemo<WorkspaceRequest | undefined>(() => {
const allRequests = requests();
if (allRequests.length === 0) {
return undefined;
}
return allRequests[selectedRequestIndex()];
});
const hasRequests = () => requests().length > 0;
const requestCount = () => requests().length;
const hasSelection = () => selectedRequest() !== undefined;
const parseDetails = useRequestParseDetails({
client: input.client,
path: input.path,
requestIndex: () => selectedRequest()?.index
});
const headerDraft = useRequestHeaderDraftController({
path: input.path,
selectedRequest,
sourceHeaders: parseDetails.headers,
sourceUrl: () => selectedRequest()?.url,
getFileContent: () => {
const path = input.path();
return input.workspace.fileContents()[path]?.content;
},
setFileContent: (content: string) => {
const path = input.path();
input.workspace.updateFileContent(path, content);
},
saveFile: (path: string) => input.workspace.saveFile(path),
reloadRequests: (path: string) => input.workspace.loadRequests(path),
refetchRequestDetails: parseDetails.refetch
});
const bodyDraft = useRequestBodyDraftController({
path: input.path,
selectedRequest,
sourceBody: parseDetails.bodySummary,
requestDiagnostics: parseDetails.diagnostics,
getFileContent: () => {
const path = input.path();
return input.workspace.fileContents()[path]?.content;
},
setFileContent: (content: string) => {
const path = input.path();
input.workspace.updateFileContent(path, content);
},
saveFile: (path: string) => input.workspace.saveFile(path),
reloadRequests: (path: string) => input.workspace.loadRequests(path),
refetchRequestDetails: parseDetails.refetch
});
// Keep selected index valid as request lists change after edits/saves.
const validateAndAdjustIndex = () => {
const totalRequests = requests().length;
if (totalRequests === 0) {
if (selectedRequestIndex() !== 0) {
setSelectedRequestIndex(0);
}
return;
}
if (selectedRequestIndex() >= totalRequests) {
setSelectedRequestIndex(totalRequests - 1);
}
};
createEffect(
on(
() => requests().length,
() => validateAndAdjustIndex()
)
);
const reset = () => {
setSelectedRequestIndex(0);
};
return {
selection: {
index: selectedRequestIndex,
setIndex: setSelectedRequestIndex,
selected: selectedRequest,
hasSelection
},
requests: {
all: requests,
count: requestCount,
hasRequests
},
drafts: {
parse: parseDetails,
header: headerDraft,
body: bodyDraft
},
actions: {
reset
}
};
}