[GUI] Add Alerts

This commit is contained in:
Ben C 2023-04-01 21:40:01 -04:00
parent ef405bac05
commit e8d9d5d189
9 changed files with 66 additions and 5 deletions

View File

@ -1,10 +1,12 @@
use log::debug;
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use crate::mods::{LocalMod, ModWarning};
use anyhow::Result;
/// Represents an alert gotten from the database.
#[typeshare]
#[derive(Serialize, Deserialize)]
pub struct Alert {
pub enabled: bool,

View File

@ -2,6 +2,7 @@ use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use owmods_core::alerts::{fetch_alert, Alert};
use owmods_core::config::Config;
use owmods_core::constants::OWML_UNIQUE_NAME;
use owmods_core::db::{LocalDatabase, RemoteDatabase};
@ -648,3 +649,9 @@ pub async fn db_has_issues(state: tauri::State<'_, State>) -> Result<bool, Strin
let has_errors = local_db.active().any(|m| !m.errors.is_empty());
Ok(has_errors)
}
#[tauri::command]
pub async fn get_alert(state: tauri::State<'_, State>) -> Result<Alert, String> {
let config = state.config.read().await;
fetch_alert(&config.alert_url).await.map_err(e_to_str)
}

View File

@ -98,7 +98,8 @@ fn main() -> Result<(), Box<dyn Error>> {
export_mods,
import_mods,
fix_mod_deps,
db_has_issues
db_has_issues,
get_alert
])
.run(tauri::generate_context!())
.expect("Error while running tauri application.");

View File

@ -1,6 +1,14 @@
import { LoadState, useTauri } from "@hooks";
import { invoke } from "@tauri-apps/api";
import { Config, GuiConfig, OWMLConfig, RemoteMod, GameMessage, UnsafeLocalMod } from "@types";
import {
Config,
GuiConfig,
OWMLConfig,
RemoteMod,
GameMessage,
UnsafeLocalMod,
Alert
} from "@types";
type CommandInfo<P, R> = [P, R];
type GetCommand<V> = CommandInfo<Record<string, never>, V>;
@ -61,7 +69,8 @@ const commandInfo = {
exportMods: $<ActionCommand<{ path: string }>>("export_mods"),
importMods: $<ActionCommand<{ path: string }>>("import_mods"),
fixDeps: $<ActionCommand<{ uniqueName: string }>>("fix_mod_deps"),
checkDBForIssues: $<GetCommand<boolean>>("db_has_issues")
checkDBForIssues: $<GetCommand<boolean>>("db_has_issues"),
getAlert: $<GetCommand<Alert>>("get_alert")
};
type Command = keyof typeof commandInfo;

View File

@ -11,6 +11,7 @@ import { commands, hooks } from "@commands";
import { useTheme } from "@hooks";
import { Theme } from "@types";
import CenteredSpinner from "./common/CenteredSpinner";
import AlertBar from "./alerts/AlertBar";
startConsoleLogListen();
@ -70,6 +71,7 @@ const App = () => {
<main className="container">
<OwmlSetupModal open={openOwmlSetup} />
<header>
<AlertBar />
<Nav />
</header>
<Tabs />

View File

@ -0,0 +1,21 @@
import { hooks } from "@commands";
import { memo } from "react";
const AlertBar = memo(() => {
const [status, alert, err] = hooks.getAlert("CONFIG_RELOAD");
if (status === "Loading") {
return <></>;
} else if (status === "Error") {
console.error(err);
return <></>;
} else {
if (alert!.enabled) {
return <span className="alert-row">{alert!.message}</span>;
} else {
return <></>;
}
}
});
export default AlertBar;

View File

@ -15,8 +15,6 @@ const LogList = memo((props: LogListProps) => {
const logLen = props.logLines.length;
const logSizes = useRef<Record<number, number>>({});
console.debug(logSizes);
const reportSize = useCallback((i: number, l: number, size: number) => {
logSizes.current[l] = size;
listRef.current?.resetAfterIndex(i);

View File

@ -55,3 +55,17 @@ details[role="list"] a:hover {
details[role="list"] a svg {
margin-right: $margin;
}
span.alert-row {
z-index: 100;
position: fixed;
left: 0;
right: 0;
top: 0;
display: block;
text-align: center;
width: 100%;
padding: $margin-sm;
margin-top: 0;
background-color: var(--accent-bg);
}

View File

@ -2,6 +2,13 @@
Generated by typeshare 1.2.0
*/
/** Represents an alert gotten from the database. */
export interface Alert {
enabled: boolean;
severity?: string;
message?: string;
}
/** Represents the core config, contains critical info needed by the core API */
export interface Config {
owmlPath: string;