Skip to content

Commit

Permalink
Merge branch 'feature-ui'
Browse files Browse the repository at this point in the history
  • Loading branch information
spaaaacccee committed Feb 26, 2024
2 parents 1e83c7e + 96d3405 commit 28d4b97
Show file tree
Hide file tree
Showing 29 changed files with 704 additions and 451 deletions.
6 changes: 3 additions & 3 deletions client/src/components/app-bar/FeaturePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ButtonProps, Typography as Type, useTheme } from "@mui/material";
import { Flex } from "components/generic/Flex";
import { Select } from "components/generic/Select";
import { Space } from "components/generic/Space";
import { find, map, startCase, truncate } from "lodash";
import { filter, find, map, startCase, truncate } from "lodash";
import { FeatureDescriptor } from "protocol/FeatureQuery";
import { ReactElement, ReactNode, cloneElement } from "react";
import { AccentColor, getShade, usePaper } from "theme";
Expand Down Expand Up @@ -57,8 +57,8 @@ export function FeaturePicker({
<FeaturePickerButton
{...props}
{...ButtonProps}
sx={_paper ? { ...paper(1), m: 0.5, px: 1.25, py: 0.5 } : {}}
disabled={!items?.length || disabled}
sx={_paper ? { ...paper(1), my: 0.5, px: 1.25, py: 0.5 } : {}}
disabled={!filter(items, (item) => !item.hidden)?.length || disabled}
icon={selected?.icon ? getIcon(selected.icon, selected.color) : icon}
arrow={arrow}
>
Expand Down
1 change: 1 addition & 0 deletions client/src/components/app-bar/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useLoadingState } from "slices/loading";
import { EditorProps } from "../Editor";
import { FeaturePicker } from "./FeaturePicker";
import { custom, uploadMap, uploadTrace } from "./upload";

function name(s: string) {
return s.split(".").shift();
}
Expand Down
94 changes: 52 additions & 42 deletions client/src/components/app-bar/upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import { find, startCase } from "lodash";
import { Feature, FeatureDescriptor } from "protocol/FeatureQuery";
import { UploadedTrace } from "slices/UIState";
import { parseYamlAsync } from "workers/async";

function ext(s: string) {
return s.split(".").pop();
}
function name(s: string) {
return s.split(".").shift();
}
import { name, ext } from "../../utils/path";
import { nanoid as id } from "nanoid";

const customId = "internal/custom";

Expand All @@ -25,7 +20,9 @@ export const custom = (
id: customId,
});

const FORMATS = ["json", "yaml", "yml"];
export const EXTENSIONS = ["json", "yaml", "yml"];

const FORMATS = EXTENSIONS.map((c) => `.trace.${c}`);

export type FileHandle<T> = {
file: File;
Expand All @@ -36,31 +33,40 @@ export async function uploadTrace(): Promise<
FileHandle<UploadedTrace | undefined> | undefined
> {
const f = await file({
accept: FORMATS.map((c) => `.trace.${c}`),
accept: FORMATS,
strict: true,
});
if (f) {
return {
file: f,
read: async () => {
if (FORMATS.includes(ext(f.name)!)) {
const content = await f.text();
const parsed = await parseYamlAsync(content);
return {
...custom(),
format: parsed?.format,
content: parsed,
name: startCase(name(f.name)),
type: customId,
};
} else {
throw new Error(`The format (${ext(f.name)}) is unsupported.`);
}
},
};
return readUploadedTrace(f);
}
}

export function readUploadedTrace(f: File) {
return {
file: f,
read: async () => {
if (isTraceFormat(f)) {
const content = await f.text();
const parsed = await parseYamlAsync(content);
return {
...custom(),
format: parsed?.format,
content: parsed,
name: startCase(name(f.name)),
type: customId,
key: id(),
};
} else {
throw new Error(`The format (${ext(f.name)}) is unsupported.`);
}
},
};
}

export function isTraceFormat(f: File) {
return !!find(FORMATS, (r) => f.name.endsWith(r));
}

export async function uploadMap(
accept: FeatureDescriptor[]
): Promise<
Expand All @@ -71,20 +77,24 @@ export async function uploadMap(
strict: true,
});
if (f) {
return {
file: f,
read: async () => {
if (find(accept, { id: ext(f.name) })) {
return {
...custom(),
format: ext(f.name),
content: await f.text(),
name: startCase(name(f.name)),
} as Feature & { format?: string };
} else {
throw new Error(`The format (${ext(f.name)}) is unsupported.`);
}
},
};
return readUploadedMap(f, accept);
}
}

export function readUploadedMap(f: File, accept: FeatureDescriptor[]) {
return {
file: f,
read: async () => {
if (find(accept, { id: ext(f.name) })) {
return {
...custom(),
format: ext(f.name),
content: await f.text(),
name: startCase(name(f.name)),
} as Feature & { format?: string };
} else {
throw new Error(`The format (${ext(f.name)}) is unsupported.`);
}
},
};
}
Loading

0 comments on commit 28d4b97

Please sign in to comment.