functioning profiles

This commit is contained in:
ari committed 2022-08-31 15:15:30 -04:00
1 parent 8985312dd2
commit d890c1aa64
17 files changed
+136 -78

No files matched your search

+4 -6
View File
@@ -3,13 +3,13 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from src.automations.manager import AutomationManager
from src.env import API_PREFIX, ORIGIN, ROOT_FOLDER
from src.hass_config.loader import HassConfig
from .routes import automations, details, ping, socket
def make_app(automation_mgr: AutomationManager) -> FastAPI:
def make_app(hass_config: HassConfig) -> FastAPI:
app = FastAPI()
has_web_folder = (ROOT_FOLDER / "web").exists()
if has_web_folder:
@@ -33,11 +33,9 @@ def make_app(automation_mgr: AutomationManager) -> FastAPI:
)
app.include_router(ping.router, prefix="/ping")
app.include_router(
automations.make_automation_router(automation_mgr), prefix=f"{API_PREFIX}/automations"
)
app.include_router(
details.make_details_router(automation_mgr.hass_config), prefix=f"{API_PREFIX}/details"
automations.make_automation_router(hass_config), prefix=f"{API_PREFIX}/automations"
)
app.include_router(details.make_details_router(hass_config), prefix=f"{API_PREFIX}/details")
app.include_router(socket.router, prefix=f"{API_PREFIX}/socket")
@app.get("/")
+7 -1
View File
@@ -5,13 +5,15 @@ from src.automations.errors import FailedDeletion
from src.automations.manager import AutomationManager
from src.automations.types import Automation, ExtenededAutomation
from src.errors import ErrorSet
from src.hass_config.loader import HassConfig
def make_automation_router(automations: AutomationManager) -> APIRouter:
def make_automation_router(hass_config: HassConfig) -> APIRouter:
router = APIRouter()
@router.post("/list")
def list_autos(body: ListParams) -> ListData[ExtenededAutomation]:
automations = AutomationManager(hass_config)
try:
automations.reload()
return ListData(
@@ -27,18 +29,22 @@ def make_automation_router(automations: AutomationManager) -> APIRouter:
@router.post("/item")
def insert_auto(body: Automation):
automations = AutomationManager(hass_config)
automations.create(body)
@router.put("/item")
def update_auto(body: ExtenededAutomation):
automations = AutomationManager(hass_config)
automations.update(body)
@router.post("/item/tags")
def update_tags(body: UpdateTags):
automations = AutomationManager(hass_config)
automations.update_tags(body.automation_id, body.tags)
@router.delete("/item")
def delete_auto(body: ExtenededAutomation):
automations = AutomationManager(hass_config)
try:
automations.delete(body)
except FailedDeletion as err:
+1 -1
View File
@@ -3,4 +3,4 @@ from src.automations.manager import AutomationManager
from src.hass_config import find_hass_config
hass_config = find_hass_config()
app = make_app(AutomationManager(hass_config))
app = make_app(hass_config)
-1
View File
@@ -16,7 +16,6 @@ from .errors import (
from .loader import (
get_base_automation_key,
load_and_iter_automations,
load_automation_path,
)
from .tags import TagManager
from .types import Automation, ExtenededAutomation
+1 -1
View File
@@ -19,4 +19,4 @@ class BaseTestCase(TestCase):
self.hass_config,
self.automation_loader,
) = get_example_automation_loader(config_to_copy=self.source_config_path)
self.client = TestClient(make_app(self.automation_loader))
self.client = TestClient(make_app(self.hass_config))
+10 -4
View File
@@ -23,10 +23,16 @@ export const useAPIService = (
// initial load of autos
useEffect(() => {
autoManager.reload();
userProfileManager.reload();
// eslint-disable-next-line
}, []);
if (state.automations.ready === undefined) {
autoManager.reload();
}
}, [state.automations.ready, autoManager]);
// initial load of profile
useEffect(() => {
if (state.userProfile.ready === undefined) {
userProfileManager.reload();
}
}, [state.userProfile.ready, userProfileManager]);
return {
state: {
+1 -1
View File
@@ -15,7 +15,7 @@ const baseURL = new URL(
);
export const wsURL = baseURL + "socket";
export const remoteAutoAPI = makeRemoteAPI(baseURL + "automations");
export const detailsAPI = makeRemoteAPI(baseURL + "details/");
export const detailsAPI = makeRemoteAPI(baseURL + "details");
export const useConnectedApiService = () =>
useAPIService(makeAutomationAPI(remoteAutoAPI), makeProfileAPI(detailsAPI));
export const useMockApiService = (
+4 -3
View File
@@ -19,7 +19,8 @@ export type APIResponse<D> =
ok: false;
error: string;
};
export type Errorable<D> =
export type Waitable<D> =
| { ready: undefined }
| ({ ready: true } & APIResponse<D>)
| {
ready: false;
@@ -31,8 +32,8 @@ export interface ListData<D> {
}
export interface ApiState {
automations: Errorable<ListData<AutomationData>>;
userProfile: Errorable<UserProfile>;
automations: Waitable<ListData<AutomationData>>;
userProfile: Waitable<UserProfile>;
}
export type UserProfile = {
+12 -3
View File
@@ -6,8 +6,8 @@ import { UserProfile } from "apiService/types";
export const useDefaultApiState = () =>
useState<ApiState>({
automations: { ready: false },
userProfile: { ready: false },
automations: { ready: undefined },
userProfile: { ready: undefined },
});
export const makeReloadAutomations = (
@@ -76,6 +76,15 @@ export const makeProfileManager = (
userProfileAPI
.update(data)
.then(() => reload())
.catch(() => reload()),
.catch((err) =>
setState({
...state,
userProfile: {
ok: false,
error: String(err),
ready: true,
},
})
),
};
};
@@ -9,12 +9,15 @@ import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
import SelectIcon from "@mui/icons-material/HighlightAltSharp";
import RunIcon from "@mui/icons-material/RunCircleOutlined";
import Tooltip from "@mui/material/Tooltip";
import Box from "@mui/material/Box";
import Chip from "@mui/material/Chip";
import { InputTextView } from "components/Inputs/InputTextView";
import { ButtonIcon } from "components/Icons/ButtonIcons";
import { AutomationManagerAuto } from "../types";
import { Tags } from "./Tags";
import { TagDB } from "../TagDB";
import { Chip, Tooltip } from "@mui/material";
import { useLang } from "lang";
export type MetadataBoxProps = AutomationManagerAuto & {
@@ -34,7 +37,7 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
? langStore.get(`AUTOMATION_STATE_${props.state}`)
: props.state;
return (
<div
<Box
className={[
"automation-manager--metadatabox",
props.isSelected ? "selected" : "",
@@ -79,15 +82,18 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
/>
</Tooltip>
</span>
<Tooltip title={`ID=${props.id}, EntityID=${props.entityId}`}>
<InputTextView
className="title"
placeholder={langStore.get("TITLE")}
value={props.title}
onChange={props.onTitleUpdate}
disabled={props.isNew}
/>
</Tooltip>
<InputTextView
titleSlug="AUTOMATION_METADATA_TOOLTIP_SIMPLE"
titleParams={{
id: props.id,
entityId: props.entityId,
}}
className="title"
placeholder={langStore.get("TITLE")}
value={props.title}
onChange={props.onTitleUpdate}
disabled={props.isNew}
/>
<InputTextView
className="description"
placeholder={langStore.get("DESCRIPTION")}
@@ -106,24 +112,24 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
</div>
<div className="buttons">
<ButtonIcon
titleSlug="SELECT_AUTOMATION"
className="select"
icon={<SelectIcon />}
onClick={props.onSelect}
title={langStore.get("SELECT_AUTOMATION")}
/>
<ButtonIcon
titleSlug="TRIGGER_AUTOMATION"
className="run"
icon={<RunIcon />}
onClick={props.onRun}
title={langStore.get("TRIGGER_AUTOMATION")}
/>
<ButtonIcon
titleSlug="DELETE_AUTOMATION"
className="delete"
icon={<DeleteForeverIcon />}
onClick={props.onDelete}
title={langStore.get("DELETE_AUTOMATION")}
/>
</div>
</div>
</Box>
);
};
@@ -40,4 +40,11 @@
.bottom-bar .settings svg {
height: 0.5em;
width: 0.5em;
}
.user-profile {
width: 100%;
height: 100%;
min-width: 10em;
min-height: 10em;
}
+1 -1
View File
@@ -34,7 +34,7 @@ export const BottomBar: FC<BottomBarProps> = ({ api }) => {
<span>version: {version}</span>
<Button className="settings" onClick={() => setOpen(!isOpen)}>
<SettingsSuggestOutlinedIcon />
settings
{lang.get("USER_PRERENCES")}
</Button>
</div>
);
+25 -13
View File
@@ -1,6 +1,8 @@
import "./ButtonIcons.css";
import Tooltip from "@mui/material/Tooltip";
import { FC, ReactNode } from "react";
import MuiIconButton, { IconButtonTypeMap } from "@mui/material/IconButton";
import IconButton, { IconButtonTypeMap } from "@mui/material/IconButton";
import { useLang } from "lang";
export type ButtonIconProps = {
icon: ReactNode;
@@ -10,21 +12,31 @@ export type ButtonIconProps = {
disabled?: boolean;
color?: IconButtonTypeMap["props"]["color"];
borderless?: boolean;
titleSlug?: string;
titleParams?: Record<string, string>;
};
export const ButtonIcon: FC<ButtonIconProps> = (props) => {
const langStore = useLang();
return (
<MuiIconButton
className={[
"icon-button",
props.className ?? "",
props.borderless ? "borderless" : "",
].join(" ")}
onClick={props.onClick}
title={props.title}
color={props.color}
disabled={props.disabled}
<Tooltip
title={
props.titleSlug ? langStore.get(props.titleSlug, props.titleParams) : ""
}
describeChild
>
{props.icon}
</MuiIconButton>
<IconButton
className={[
"icon-button",
props.className ?? "",
props.borderless ? "borderless" : "",
].join(" ")}
onClick={props.onClick}
title={props.title}
color={props.color}
disabled={props.disabled}
>
{props.icon}
</IconButton>
</Tooltip>
);
};
+32 -17
View File
@@ -4,6 +4,8 @@ import { FC, PropsWithChildren, useState } from "react";
import CheckCircleOutlineOutlinedIcon from "@mui/icons-material/CheckCircleOutlineOutlined";
import ModeEditOutlineOutlinedIcon from "@mui/icons-material/ModeEditOutlineOutlined";
import TextField from "@mui/material/TextField";
import Tooltip from "@mui/material/Tooltip";
import { useLang } from "lang";
export type InputTextViewProps = {
className?: string;
@@ -12,11 +14,15 @@ export type InputTextViewProps = {
initialViewMode?: boolean;
placeholder?: string;
disabled?: boolean;
borderless?: boolean;
titleSlug?: string;
titleParams?: Record<string, string>;
};
export const InputTextView: FC<PropsWithChildren<InputTextViewProps>> = (
props
) => {
const langStore = useLang();
const [viewMode, setViewMode] = useState(props.initialViewMode ?? true);
const [text, setText] = useState(props.value);
const className = [
@@ -27,24 +33,33 @@ export const InputTextView: FC<PropsWithChildren<InputTextViewProps>> = (
if (viewMode) {
return (
<div
className={className}
onClick={() => !props.disabled && setViewMode(false)}
<Tooltip
title={
props.titleSlug
? langStore.get(props.titleSlug, props.titleParams)
: ""
}
describeChild
>
{!!props.value ? (
<div className="input-text-view--text text-ellipsis">
<span>{props.value}</span>
</div>
) : (
<div className="input-text-view--text input-text-view--placeholder text-ellipsis">
<span>{props.placeholder}</span>
</div>
)}
{!props.disabled && (
<ModeEditOutlineOutlinedIcon className="input-text-view--edit" />
)}
{props.children}
</div>
<div
className={className}
onClick={() => !props.disabled && setViewMode(false)}
>
{!!props.value ? (
<div className="input-text-view--text text-ellipsis">
<span>{props.value}</span>
</div>
) : (
<div className="input-text-view--text input-text-view--placeholder text-ellipsis">
<span>{props.placeholder}</span>
</div>
)}
{!props.disabled && (
<ModeEditOutlineOutlinedIcon className="input-text-view--edit" />
)}
{props.children}
</div>
</Tooltip>
);
} else {
return (
+5 -11
View File
@@ -1,24 +1,18 @@
import React from "react";
import ReactDOM from "react-dom";
import "./styles/root.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
ReactDOM.render(
import { createRoot } from "react-dom/client";
const container = document.getElementById("root");
const root = createRoot(container!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
</React.StrictMode>
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.info))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals(console.info);
// import { createRoot } from 'react-dom/client';
// const container = document.getElementById('root');
// const root = createRoot(container!);
// root.render(<React.StrictMode>
// <App />
// </React.StrictMode>);
+3
View File
@@ -77,6 +77,8 @@
"THEME": "Theme",
"LANGUAGE": "Language",
"USER_PRERENCES": "Settings",
"SELECT_AUTOMATION": "Select automation",
"TRIGGER_AUTOMATION": "Trigger automation",
"DELETE_AUTOMATION": "Delete automation",
@@ -91,6 +93,7 @@
"MISSING_TEXT": "MISSING TEXT -- {{langId}}",
"AUTOMATION_METADATA_TOOLTIP_TEXT": "This automation is located in the file {{source_file}}. It's currently '{{state}}', it's entity id is '{{entityId}}' and it's automation id is '{{id}}'",
"AUTOMATION_METADATA_TOOLTIP_SIMPLE": "ID={{id}}, EntityID={{entityId}}",
"ERROR_FAILED_TO_SAVE": "Failed to save automation \"{{aid}}\" contents - {{contents}} -- not found in `/config`",
"ERROR_INVALID_AUTOMATION_STATE": "Invalid state",
+2
View File
@@ -80,6 +80,8 @@
"THEME": "Thème",
"LANGUAGE": "Langue",
"USER_PRERENCES": "Préférences de l'utilisateur",
"SELECT_AUTOMATION": "Sélectionnez l'automatisation",
"TRIGGER_AUTOMATION": "Déclencher l'automatisation",
"DELETE_AUTOMATION": "Supprimer l'automatisation",