Skip to content

Commit

Permalink
refactor(ui): disable TS null assertion and update the code that use …
Browse files Browse the repository at this point in the history
…it (#2312)
  • Loading branch information
skamril authored Jan 27, 2025
1 parent a31f9c1 commit cbe394d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions webapp/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default [
rules: {
...reactHookPlugin.configs.recommended.rules,
"@typescript-eslint/array-type": ["error", { default: "array-simple" }],
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-restricted-imports": [
"error",
{
Expand Down
11 changes: 8 additions & 3 deletions webapp/src/components/App/Singlestudy/FreezeStudy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function FreezeStudy({ studyId }: FreezeStudyProps) {
setBlockingTasks(
tasks.map((task) => ({
id: task.id,
type: task.type!,
type: task.type,
})),
);

Expand Down Expand Up @@ -143,10 +143,15 @@ function FreezeStudy({ studyId }: FreezeStudyProps) {

function forceUpdate(taskId: BlockingTask["id"]) {
getTask({ id: taskId }).then((task) => {
// Normally all blocking tasks have a type
if (!task.type) {
return;
}

const payload = {
id: task.id,
message: task.result?.message || "",
type: task.type!,
type: task.type,
};
if (task.status === TaskStatus.Running) {
if (typeof task.progress === "number") {
Expand Down Expand Up @@ -179,7 +184,7 @@ function FreezeStudy({ studyId }: FreezeStudyProps) {
unsubscribeWsChannels();
window.clearInterval(intervalId);
};
}, [studyId]);
}, [blockingTasksRef, studyId]);

return (
<Backdrop open={blockingTasks.length > 0} sx={{ position: "absolute" }}>
Expand Down
27 changes: 12 additions & 15 deletions webapp/src/components/App/Singlestudy/explore/Debug/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Debug() {
// Allow to keep expanded items when the tree is reloaded with `reloadTreeData`
const [expandedItems, setExpandedItems] = useState<string[]>([]);
const [searchParams, setSearchParams] = useSearchParams();
const pathInUrl = searchParams.get("path");
const path = searchParams.get("path");

const res = usePromiseWithSnackbarError(
async () => {
Expand All @@ -60,17 +60,18 @@ function Debug() {
const firstChildName = Object.keys(res.data ?? {})[0];
const firstChildTreeData = R.path<TreeData>([firstChildName], res.data);

const pathInUrlParts = pathInUrl?.split("/");
const urlPathTreeData = pathInUrlParts ? R.path<TreeData>(pathInUrlParts, res.data) : null;
const pathSegments = path?.split("/");
const filename = pathSegments ? R.last(pathSegments) : null;
const treeData = pathSegments ? R.path<TreeData>(pathSegments, res.data) : null;

let fileInfo: FileInfo | null = null;

if (urlPathTreeData) {
if (path && filename && treeData) {
fileInfo = {
fileType: getFileType(urlPathTreeData),
treeData: urlPathTreeData,
filename: R.last(pathInUrlParts!)!,
filePath: pathInUrl!,
fileType: getFileType(treeData),
treeData,
filename,
filePath: path,
};
} else if (firstChildTreeData) {
fileInfo = {
Expand All @@ -81,15 +82,11 @@ function Debug() {
};
}

if (fileInfo) {
setSelectedFile(fileInfo);
} else {
setSelectedFile(null);
}
}, [res.data, pathInUrl]);
setSelectedFile(fileInfo);
}, [res.data, path]);

useUpdateEffect(() => {
if (selectedFile?.filePath !== pathInUrl) {
if (selectedFile?.filePath !== path) {
setSearchParams({ path: selectedFile?.filePath || "" });
}
}, [selectedFile?.filePath]);
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/App/Singlestudy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function SingleStudy(props: Props) {
) : (
<HomeView study={study} tree={tree} />
)}
<FreezeStudy studyId={studyId!} />
{studyId && <FreezeStudy studyId={studyId} />}
</Box>
{openCommands && studyId && (
<CommandDrawer
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/components/common/buttons/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ function DownloadButton<OptionValue extends string>(props: DownloadButtonProps<O

try {
if (formatOptions) {
await onClick?.(format!);
if (!format) {
throw new Error("No format selected");
}
await onClick?.(format);
} else {
await onClick?.();
}
Expand Down
8 changes: 5 additions & 3 deletions webapp/src/services/api/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
*/

import client from "../client";
import type { GetTaskParams, GetTasksParams, TaskDTO } from "./types";
import type { GetTaskParams, GetTasksParams, TaskDTO, TTaskStatus, TTaskType } from "./types";

export async function getTasks(params: GetTasksParams) {
const { data } = await client.post<TaskDTO[]>("/v1/tasks", {
export async function getTasks<TStatus extends TTaskStatus, TType extends TTaskType | undefined>(
params: GetTasksParams<TStatus, TType>,
) {
const { data } = await client.post<Array<TaskDTO<TStatus, TType>>>("/v1/tasks", {
status: params.status,
type: params.type,
name: params.name,
Expand Down
22 changes: 16 additions & 6 deletions webapp/src/services/api/tasks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export type TTaskStatus = O.UnionOf<typeof TaskStatus>;

export type TTaskType = O.UnionOf<typeof TaskType>;

export interface TaskDTO extends IdentityDTO<string> {
status: TTaskStatus;
type?: TTaskType;
interface BaseTaskDTO<
TStatus extends TTaskStatus = TTaskStatus,
TType extends TTaskType = TTaskType,
> extends IdentityDTO<string> {
status: TStatus;
type?: TType;
owner?: number;
ref_id?: string;
creation_date_utc: string;
Expand All @@ -39,9 +42,16 @@ export interface TaskDTO extends IdentityDTO<string> {
}>;
}

export interface GetTasksParams {
status?: TTaskStatus[];
type?: TTaskType[];
export type TaskDTO<
TStatus extends TTaskStatus = TTaskStatus,
TType extends TTaskType | undefined = undefined,
> = TType extends TTaskType
? O.Required<BaseTaskDTO<TStatus, TType>, "type">
: BaseTaskDTO<TStatus>;

export interface GetTasksParams<TStatus extends TTaskStatus, TType extends TTaskType | undefined> {
status?: TStatus[];
type?: TType[];
name?: string;
studyId?: StudyMetadata["id"];
fromCreationDateUtc?: number;
Expand Down

0 comments on commit cbe394d

Please sign in to comment.