Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ tauri-plugin-listener2 = { path = "plugins/listener2" }
tauri-plugin-local-llm = { path = "plugins/local-llm" }
tauri-plugin-local-stt = { path = "plugins/local-stt" }
tauri-plugin-misc = { path = "plugins/misc" }
tauri-plugin-network = { path = "plugins/network" }
tauri-plugin-notification = { path = "plugins/notification" }
tauri-plugin-permissions = { path = "plugins/permissions" }
tauri-plugin-sfx = { path = "plugins/sfx" }
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tauri-plugin-listener = { workspace = true }
tauri-plugin-listener2 = { workspace = true }
tauri-plugin-local-stt = { workspace = true }
tauri-plugin-misc = { workspace = true }
tauri-plugin-network = { workspace = true }
tauri-plugin-notification = { workspace = true }
tauri-plugin-opener = { workspace = true }
tauri-plugin-os = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
]
},
"misc:default",
"network:default",
"os:default",
"detect:default",
"permissions:default",
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub async fn main() {
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_misc::init())
.plugin(tauri_plugin_network::init())
.plugin(tauri_plugin_template::init())
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_detect::init())
Expand Down
77 changes: 77 additions & 0 deletions apps/desktop/src/contexts/network.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { createContext, useContext, useEffect, useState } from "react";

import { commands, events } from "@hypr/plugin-network";

interface NetworkContextValue {
isOnline: boolean;
}

const NetworkContext = createContext<NetworkContextValue | null>(null);

export const NetworkProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const [isOnline, setIsOnline] = useState(true);

useEffect(() => {
commands
.isOnline()
.then((result) => {
if (result.status === "ok") {
setIsOnline(result.data);
} else {
console.error("Failed to check network status:", result.error);
setIsOnline(false);
}
})
.catch((err) => {
console.error("Failed to check network status:", err);
setIsOnline(false);
});
}, []);

useEffect(() => {
let unlisten: (() => void) | undefined;
let cancelled = false;

events.networkEvent
.listen(({ payload }) => {
if (payload.type === "statusChanged") {
setIsOnline(payload.is_online);
}
})
.then((fn) => {
if (cancelled) {
fn();
} else {
unlisten = fn;
}
})
.catch((err) => {
console.error("Failed to setup network event listener:", err);
});

return () => {
cancelled = true;
unlisten?.();
};
}, []);

return (
<NetworkContext.Provider value={{ isOnline }}>
{children}
</NetworkContext.Provider>
);
};

export const useNetwork = () => {
const context = useContext(NetworkContext);

if (!context) {
throw new Error("'useNetwork' must be used within a 'NetworkProvider'");
}

return context;
};
1 change: 1 addition & 0 deletions plugins/network/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
29 changes: 29 additions & 0 deletions plugins/network/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "tauri-plugin-network"
version = "0.1.0"
authors = ["You"]
edition = "2021"
exclude = ["/js", "/node_modules"]
links = "tauri-plugin-network"
description = ""

[build-dependencies]
tauri-plugin = { workspace = true, features = ["build"] }

[dev-dependencies]
specta-typescript = { workspace = true }

[dependencies]
hypr-network = { workspace = true }

tauri = { workspace = true, features = ["specta", "test"] }
tauri-plugin-windows = { workspace = true }

specta = { workspace = true }
tauri-specta = { workspace = true, features = ["derive", "typescript"] }

serde = { workspace = true }
thiserror = { workspace = true }

tokio = { workspace = true, features = ["rt-multi-thread", "macros", "time"] }
tracing = { workspace = true }
5 changes: 5 additions & 0 deletions plugins/network/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const COMMANDS: &[&str] = &["is_online"];

fn main() {
tauri_plugin::Builder::new(COMMANDS).build();
}
95 changes: 95 additions & 0 deletions plugins/network/js/bindings.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// @ts-nocheck


// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.

/** user-defined commands **/


export const commands = {
async isOnline() : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("plugin:network|is_online") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
}
}

/** user-defined events **/


export const events = __makeEvents__<{
networkEvent: NetworkEvent
}>({
networkEvent: "plugin:network:network-event"
})

/** user-defined constants **/



/** user-defined types **/

export type NetworkEvent = { type: "statusChanged"; is_online: boolean }

/** tauri-specta globals **/

import {
invoke as TAURI_INVOKE,
Channel as TAURI_CHANNEL,
} from "@tauri-apps/api/core";
import * as TAURI_API_EVENT from "@tauri-apps/api/event";
import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow";

type __EventObj__<T> = {
listen: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
once: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: null extends T
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
};

export type Result<T, E> =
| { status: "ok"; data: T }
| { status: "error"; error: E };

function __makeEvents__<T extends Record<string, any>>(
mappings: Record<keyof T, string>,
) {
return new Proxy(
{} as unknown as {
[K in keyof T]: __EventObj__<T[K]> & {
(handle: __WebviewWindow__): __EventObj__<T[K]>;
};
},
{
get: (_, event) => {
const name = mappings[event as keyof T];

return new Proxy((() => {}) as any, {
apply: (_, __, [window]: [__WebviewWindow__]) => ({
listen: (arg: any) => window.listen(name, arg),
once: (arg: any) => window.once(name, arg),
emit: (arg: any) => window.emit(name, arg),
}),
get: (_, command: keyof __EventObj__<any>) => {
switch (command) {
case "listen":
return (arg: any) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg: any) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg: any) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}
1 change: 1 addition & 0 deletions plugins/network/js/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./bindings.gen";
11 changes: 11 additions & 0 deletions plugins/network/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@hypr/plugin-network",
"private": true,
"main": "./js/index.ts",
"scripts": {
"codegen": "cargo test -p tauri-plugin-network"
},
"dependencies": {
"@tauri-apps/api": "^2.9.0"
}
}
13 changes: 13 additions & 0 deletions plugins/network/permissions/autogenerated/commands/is_online.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!

"$schema" = "../../schemas/schema.json"

[[permission]]
identifier = "allow-is-online"
description = "Enables the is_online command without any pre-configured scope."
commands.allow = ["is_online"]

[[permission]]
identifier = "deny-is-online"
description = "Denies the is_online command without any pre-configured scope."
commands.deny = ["is_online"]
43 changes: 43 additions & 0 deletions plugins/network/permissions/autogenerated/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Default Permission

Default permissions for the plugin

#### This default permission set includes the following:

- `allow-is-online`

## Permission Table

<table>
<tr>
<th>Identifier</th>
<th>Description</th>
</tr>


<tr>
<td>

`network:allow-is-online`

</td>
<td>

Enables the is_online command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`network:deny-is-online`

</td>
<td>

Denies the is_online command without any pre-configured scope.

</td>
</tr>
</table>
5 changes: 5 additions & 0 deletions plugins/network/permissions/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[default]
description = "Default permissions for the plugin"
permissions = [
"allow-is-online",
]
Loading