MCPcopy Create free account
hub / github.com/SKaplanOfficial/Raycast-PromptLab / useFiles

Function useFiles

src/hooks/useFiles.ts:144–223  ·  view source on GitHub ↗
(options: CommandOptions)

Source from the content-addressed store, hash-verified

142}
143
144export const useFiles = (options: CommandOptions) => {
145 const [selectedFiles, setSelectedFiles] = useState<{ paths: string[]; csv: string }>();
146 const [fileContents, setFileContents] = useState<{ [key: string]: string; contents: string }>();
147 const [isLoading, setIsLoading] = useState<boolean>(true);
148 const [error, setError] = useState<number>();
149 const { models } = useModels();
150 const { advancedSettings, revalidateAdvancedSettings } = useAdvancedSettings();
151
152 const loadSelection = async () => {
153 if (!options.minNumFiles)
154 return {
155 paths: [],
156 csv: "",
157 };
158
159 const validExtensions = (options.acceptedFileExtensions || []).map((ext) => ext.toLowerCase());
160
161 setIsLoading(true);
162 const rawSelection = await ScriptRunner.SelectedFiles();
163
164 // Remove invalid files and directories from the selection
165 const selection = rawSelection.paths.reduce(
166 (acc, filepath) => {
167 if (
168 filepath.trim().length > 0 &&
169 (validExtensions.length == 0 || validExtensions.includes(path.extname(filepath).slice(1).toLowerCase()))
170 ) {
171 return { paths: [...acc.paths, filepath], csv: acc.csv + "," + filepath };
172 }
173 return acc;
174 },
175 { paths: [] as string[], csv: "" },
176 );
177
178 maxCharacters = maxCharacters / selection.paths.length;
179 setSelectedFiles(selection);
180 return selection;
181 };
182
183 const loadFileContents = async (selection: { paths: string[]; csv: string }) => {
184 // Raise an error if too few files are selected
185 if (selection.paths.length < (options.minNumFiles || 0)) {
186 setError(ERRORTYPE.MIN_SELECTION_NOT_MET);
187 setIsLoading(false);
188 return;
189 }
190
191 await revalidateAdvancedSettings();
192
193 let model = options.model ? models.find((m) => m.name == options.model) : undefined;
194 if (!model) model = models.find((m) => m.isDefault) || models[0];
195 const maxCharacters = model ? parseInt(model.lengthLimit) : 3000;
196
197 const fileData: { [key: string]: string; contents: string } = {
198 contents: "",
199 };
200 for (const [index, filepath] of selection.paths.entries()) {
201 // Init. file contents with file name as header

Callers 1

CommandResponseFunction · 0.90

Calls 3

useModelsFunction · 0.90
useAdvancedSettingsFunction · 0.90
revalidateFunction · 0.70

Tested by

no test coverage detected