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

File handling cleanup #3240

Merged
merged 3 commits into from
Nov 25, 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
14 changes: 8 additions & 6 deletions web/src/app/admin/connectors/[connector]/AddConnectorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export default function AddConnector({
// State for managing credentials and files
const [currentCredential, setCurrentCredential] =
useState<Credential<any> | null>(null);
const [selectedFiles, setSelectedFiles] = useState<File[]>([]);
const [createConnectorToggle, setCreateConnectorToggle] = useState(false);

// Fetch credentials data
Expand Down Expand Up @@ -258,6 +257,13 @@ export default function AddConnector({
refreshFreq: (refreshFreq ?? defaultRefreshFreqMinutes) * 60,
};

// File-specific handling
const selectedFiles = Array.isArray(values.file_locations)
? values.file_locations
: values.file_locations
? [values.file_locations]
: [];

// Google sites-specific handling
if (connector == "google_sites") {
const response = await submitGoogleSite(
Expand All @@ -276,13 +282,11 @@ export default function AddConnector({
}
return;
}

// File-specific handling
if (connector == "file" && selectedFiles.length > 0) {
if (connector == "file") {
const response = await submitFiles(
selectedFiles,
setPopup,
setSelectedFiles,
name,
access_type,
groups
Expand Down Expand Up @@ -427,8 +431,6 @@ export default function AddConnector({
<DynamicConnectionForm
values={formikProps.values}
config={configuration}
setSelectedFiles={setSelectedFiles}
selectedFiles={selectedFiles}
connector={connector}
currentCredential={
currentCredential ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ interface FileInputProps {
label: string;
optional?: boolean;
description?: string;
isZip?: boolean;
}

export default function FileInput({
name,
label,
optional = false,
description,
isZip = false, // Default to false for multiple file uploads
}: FileInputProps) {
const [field, meta, helpers] = useField(name);

Expand All @@ -28,10 +30,22 @@ export default function FileInput({
</label>
{description && <CredentialSubText>{description}</CredentialSubText>}
<FileUpload
selectedFiles={field.value ? [field.value] : []}
selectedFiles={
Array.isArray(field.value)
? field.value
: field.value
? [field.value]
: []
}
setSelectedFiles={(files: File[]) => {
helpers.setValue(files[0] || null);
if (isZip) {
helpers.setValue(files[0] || null);
} else {
helpers.setValue(files);
}
}}
multiple={!isZip} // Allow multiple files if not a zip
accept={isZip ? ".zip" : undefined} // Only accept zip files if isZip is true
/>
{meta.touched && meta.error && (
<div className="text-red-500 text-sm mt-1">{meta.error}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ import { RenderField } from "./FieldRendering";

export interface DynamicConnectionFormProps {
config: ConnectionConfiguration;
selectedFiles: File[];
setSelectedFiles: Dispatch<SetStateAction<File[]>>;
values: any;
connector: ConfigurableSources;
currentCredential: Credential<any> | null;
}

const DynamicConnectionForm: FC<DynamicConnectionFormProps> = ({
config,
selectedFiles,
setSelectedFiles,
values,
connector,
currentCredential,
Expand All @@ -48,8 +44,6 @@ const DynamicConnectionForm: FC<DynamicConnectionFormProps> = ({
key={field.name}
field={field}
values={values}
selectedFiles={selectedFiles}
setSelectedFiles={setSelectedFiles}
connector={connector}
currentCredential={currentCredential}
/>
Expand All @@ -73,8 +67,6 @@ const DynamicConnectionForm: FC<DynamicConnectionFormProps> = ({
key={field.name}
field={field}
values={values}
selectedFiles={selectedFiles}
setSelectedFiles={setSelectedFiles}
connector={connector}
currentCredential={currentCredential}
/>
Expand Down
21 changes: 2 additions & 19 deletions web/src/app/admin/connectors/[connector]/pages/FieldRendering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ import {
interface TabsFieldProps {
tabField: TabOption;
values: any;
selectedFiles: File[];
setSelectedFiles: Dispatch<SetStateAction<File[]>>;
connector: ConfigurableSources;
currentCredential: Credential<any> | null;
}

const TabsField: FC<TabsFieldProps> = ({
tabField,
values,
selectedFiles,
setSelectedFiles,
connector,
currentCredential,
}) => {
Expand Down Expand Up @@ -101,8 +97,6 @@ const TabsField: FC<TabsFieldProps> = ({
key={subField.name}
field={subField}
values={values}
selectedFiles={selectedFiles}
setSelectedFiles={setSelectedFiles}
connector={connector}
currentCredential={currentCredential}
/>
Expand All @@ -119,17 +113,13 @@ const TabsField: FC<TabsFieldProps> = ({
interface RenderFieldProps {
field: any;
values: any;
selectedFiles: File[];
setSelectedFiles: Dispatch<SetStateAction<File[]>>;
connector: ConfigurableSources;
currentCredential: Credential<any> | null;
}

export const RenderField: FC<RenderFieldProps> = ({
field,
values,
selectedFiles,
setSelectedFiles,
connector,
currentCredential,
}) => {
Expand All @@ -147,8 +137,6 @@ export const RenderField: FC<RenderFieldProps> = ({
<TabsField
tabField={field}
values={values}
selectedFiles={selectedFiles}
setSelectedFiles={setSelectedFiles}
connector={connector}
currentCredential={currentCredential}
/>
Expand All @@ -157,15 +145,10 @@ export const RenderField: FC<RenderFieldProps> = ({

const fieldContent = (
<>
{field.type === "file" ? (
<FileUpload
name={field.name}
selectedFiles={selectedFiles}
setSelectedFiles={setSelectedFiles}
/>
) : field.type === "zip" ? (
{field.type === "zip" || field.type === "file" ? (
<FileInput
name={field.name}
isZip={field.type === "zip"}
label={label}
optional={field.optional}
description={description}
Expand Down
2 changes: 0 additions & 2 deletions web/src/app/admin/connectors/[connector]/pages/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AccessType } from "@/lib/types";
export const submitFiles = async (
selectedFiles: File[],
setPopup: (popup: PopupSpec) => void,
setSelectedFiles: (files: File[]) => void,
name: string,
access_type: string,
groups?: number[]
Expand Down Expand Up @@ -102,7 +101,6 @@ export const submitFiles = async (
return false;
}

setSelectedFiles([]);
setPopup({
type: "success",
message: "Successfully uploaded files!",
Expand Down
19 changes: 15 additions & 4 deletions web/src/components/admin/connectors/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ interface FileUploadProps {
setSelectedFiles: (files: File[]) => void;
message?: string;
name?: string;
multiple?: boolean;
accept?: string;
}

export const FileUpload: FC<FileUploadProps> = ({
name,
selectedFiles,
setSelectedFiles,
message,
multiple = true,
accept,
}) => {
const [dragActive, setDragActive] = useState(false);
const { setFieldValue } = useFormikContext();
Expand All @@ -23,14 +27,17 @@ export const FileUpload: FC<FileUploadProps> = ({
<div>
<Dropzone
onDrop={(acceptedFiles) => {
setSelectedFiles(acceptedFiles);
const filesToSet = multiple ? acceptedFiles : [acceptedFiles[0]];
setSelectedFiles(filesToSet);
setDragActive(false);
if (name) {
setFieldValue(name, acceptedFiles);
setFieldValue(name, multiple ? filesToSet : filesToSet[0]);
}
}}
onDragLeave={() => setDragActive(false)}
onDragEnter={() => setDragActive(true)}
multiple={multiple}
accept={accept ? { [accept]: [] } : undefined}
>
{({ getRootProps, getInputProps }) => (
<section>
Expand All @@ -45,7 +52,9 @@ export const FileUpload: FC<FileUploadProps> = ({
<input {...getInputProps()} />
<b className="text-emphasis">
{message ||
"Drag and drop some files here, or click to select files"}
`Drag and drop ${
multiple ? "some files" : "a file"
} here, or click to select ${multiple ? "files" : "a file"}`}
</b>
</div>
</section>
Expand All @@ -54,7 +63,9 @@ export const FileUpload: FC<FileUploadProps> = ({

{selectedFiles.length > 0 && (
<div className="mt-4">
<h2 className="text-sm font-bold">Selected Files</h2>
<h2 className="text-sm font-bold">
Selected File{multiple ? "s" : ""}
</h2>
<ul>
{selectedFiles.map((file) => (
<div key={file.name} className="flex">
Expand Down
16 changes: 4 additions & 12 deletions web/src/lib/connectors/connectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Credential } from "@/lib/connectors/credentials"; // Import Credential

export function isLoadState(connector_name: string): boolean {
// TODO: centralize connector metadata like this somewhere instead of hardcoding it here
const loadStateConnectors = ["web", "xenforo"];
const loadStateConnectors = ["web", "xenforo", "file"];
if (loadStateConnectors.includes(connector_name)) {
return true;
}
Expand Down Expand Up @@ -78,11 +78,6 @@ export interface FileOption extends Option {
default?: string;
}

export interface ZipOption extends Option {
type: "zip";
default?: string;
}

export interface StringTabOption extends Option {
type: "string_tab";
default?: string;
Expand All @@ -101,7 +96,6 @@ export interface TabOption extends Option {
| NumberOption
| SelectOption
| FileOption
| ZipOption
| StringTabOption
)[];
}[];
Expand All @@ -118,7 +112,6 @@ export interface ConnectionConfiguration {
| NumberOption
| SelectOption
| FileOption
| ZipOption
| TabOption
)[];
advanced_values: (
Expand All @@ -128,7 +121,6 @@ export interface ConnectionConfiguration {
| NumberOption
| SelectOption
| FileOption
| ZipOption
| TabOption
)[];
overrideDefaultFreq?: number;
Expand Down Expand Up @@ -750,10 +742,10 @@ For example, specifying .*-support.* as a "channel" will cause the connector to
description: "Configure Google Sites connector",
values: [
{
type: "zip",
type: "file",
query: "Enter the zip path:",
label: "Zip Path",
name: "zip_path",
label: "File Locations",
name: "file_locations",
optional: false,
description:
"Upload a zip file containing the HTML of your Google Site",
Expand Down
Loading