From 10dd3ac03a975e927311078149d07e1760c32e57 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 30 Jun 2023 22:47:12 +0800 Subject: [PATCH] Update --- components/TaskFilterBar.tsx | 97 ++++++++++++++++++++++++++++++++++++ islands/TaskManager.tsx | 49 ++++++++++-------- static/common.css | 19 ------- 3 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 components/TaskFilterBar.tsx diff --git a/components/TaskFilterBar.tsx b/components/TaskFilterBar.tsx new file mode 100644 index 0000000..d720600 --- /dev/null +++ b/components/TaskFilterBar.tsx @@ -0,0 +1,97 @@ +// @ts-nocheck Chips +import { Component } from "preact"; +import { Ref, useRef } from "preact/hooks"; +import Chips from "preact-material-components/Chips"; +import t from "../server/i18n.ts"; + +export enum TaskStatusFlag { + None = 0, + Running = 1 << 0, + Waiting = 1 << 1, + Failed = 1 << 2, + Finished = 1 << 3, + All = ~(~0 << 4), +} + +type Props = { + value: TaskStatusFlag; + set_value: (v: TaskStatusFlag) => void; +}; + +export class TaskFilterBar extends Component { + ref: Ref | undefined; + get is_all() { + return this.props.value === TaskStatusFlag.All; + } + get is_failed() { + return (this.props.value & TaskStatusFlag.Failed) !== 0; + } + get is_finished() { + return (this.props.value & TaskStatusFlag.Finished) !== 0; + } + get is_running() { + return (this.props.value & TaskStatusFlag.Running) !== 0; + } + get is_waiting() { + return (this.props.value & TaskStatusFlag.Waiting) !== 0; + } + get value() { + if (this.ref && this.ref.current) { + const com = this.ref.current.MDComponent; + if (com) { + return com.chips.slice(1).reduce( + (p, v, i) => v.selected ? p | 1 << i : p, + 0, + ); + } + } + return TaskStatusFlag.None; + } + render() { + this.ref = useRef(null); + const onClick = () => { + const co = () => this.props.set_value(this.value); + setTimeout(co, 0); + }; + return ( + + { + const co = () => { + if (this.ref && this.ref.current) { + const com = this.ref.current.MDComponent; + if (com) { + com.chips.slice(1).forEach((v) => { + v.selected = com.chips[0].selected; + }); + } + } + this.props.set_value(this.value); + }; + setTimeout(co, 0); + }} + > + + {t("task.all")} + + + + {t("task.running")} + + + + {t("task.waiting")} + + + + {t("task.failed")} + + + + {t("task.finished")} + + + ); + } +} diff --git a/islands/TaskManager.tsx b/islands/TaskManager.tsx index 4c6daed..e02024c 100644 --- a/islands/TaskManager.tsx +++ b/islands/TaskManager.tsx @@ -1,5 +1,5 @@ import { Component, ContextType } from "preact"; -import { useEffect, useRef } from "preact/hooks"; +import { useEffect, useRef, useState } from "preact/hooks"; import { signal } from "@preact/signals"; import { GlobalCtx } from "../components/GlobalContext.tsx"; import { TaskDetail, TaskStatus } from "../task.ts"; @@ -11,12 +11,20 @@ import Fab from "preact-material-components/Fab"; import Icon from "preact-material-components/Icon"; import { set_state } from "../server/state.ts"; import t from "../server/i18n.ts"; +import { TaskFilterBar, TaskStatusFlag } from "../components/TaskFilterBar.tsx"; export type TaskManagerProps = { base: string; show: boolean; }; +function map_taskstatus(s: TaskStatus) { + if (s === TaskStatus.Wait) return TaskStatusFlag.Waiting; + else if (s === TaskStatus.Running) return TaskStatusFlag.Running; + else if (s === TaskStatus.Finished) return TaskStatusFlag.Finished; + return TaskStatusFlag.None; +} + const tasks = signal(new Map()); const task_list = signal(new Array()); export const task_ws = signal(undefined); @@ -165,6 +173,7 @@ export default class TaskManager extends Component { }); }, []); if (!this.props.show) return null; + const [flags, set_flags] = useState(TaskStatusFlag.All); return (
{ add
-
-

count

-

{t("task.all")}

-
-
-

count

-

{t("task.running")}

-
-
-

count

-

{t("task.waiting")}

-
-
-

count

-

{t("task.failed")}

-
-
-

count

-

{t("task.finished")}

-
+
-
+
+ {task_list.value.map((k) => { + const t = tasks.value.get(k); + if (t) { + if (!(flags & map_taskstatus(t.status))) { + return null; + } + return ; + } else { + return
; + } + })}
); diff --git a/static/common.css b/static/common.css index c82aa67..a6fdc76 100644 --- a/static/common.css +++ b/static/common.css @@ -172,25 +172,6 @@ body { height: 64px; } -.task_amounts>div { - flex: auto; - text-align: center; - display: flex; - flex-direction: column; - justify-content: center; - align-items: center; - color: aliceblue; -} - -.task_amounts>div:first-child { - border-radius: 8px 0 0 8px; - background-color: black; -} - -.task_amounts>div:last-child { - border-radius: 0 8px 8px 0 -} - .btn-success { background-color: #28a745 }