mirror of
https://github.com/zhom/donutbrowser.git
synced 2026-10-19 22:17:28 +02:00
feat: profile templates
This commit is contained in:
1 parent
3f5b573ead
commit
2cc91ed250
29 files changed
+2430
-62
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
/** A list the backend owns, loaded by `command` and reloaded on `event`. */
|
||||
export function useConfigList<T>(command: string, event: string): T[] {
|
||||
const [items, setItems] = useState<T[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
let unlisten: (() => void) | undefined;
|
||||
const load = async () => {
|
||||
try {
|
||||
const next = await invoke<T[]>(command);
|
||||
if (mounted) setItems(next);
|
||||
} catch (error) {
|
||||
console.error(`Failed to load ${command}:`, error);
|
||||
}
|
||||
};
|
||||
void load();
|
||||
void listen(event, () => {
|
||||
void load();
|
||||
}).then((stop) => {
|
||||
if (mounted) unlisten = stop;
|
||||
else stop();
|
||||
});
|
||||
return () => {
|
||||
mounted = false;
|
||||
unlisten?.();
|
||||
};
|
||||
}, [command, event]);
|
||||
|
||||
return items;
|
||||
}
|
||||
@@ -1,35 +1,10 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useConfigList } from "@/hooks/use-config-list";
|
||||
import type { ProfileStatus } from "@/types";
|
||||
|
||||
/** Every profile status, in order, kept current by `profile-statuses-changed`. */
|
||||
export function useProfileStatuses(): ProfileStatus[] {
|
||||
const [statuses, setStatuses] = useState<ProfileStatus[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
let unlisten: (() => void) | undefined;
|
||||
const load = async () => {
|
||||
try {
|
||||
const next = await invoke<ProfileStatus[]>("list_profile_statuses");
|
||||
if (mounted) setStatuses(next);
|
||||
} catch (error) {
|
||||
console.error("Failed to load profile statuses:", error);
|
||||
}
|
||||
};
|
||||
void load();
|
||||
void listen("profile-statuses-changed", () => {
|
||||
void load();
|
||||
}).then((stop) => {
|
||||
if (mounted) unlisten = stop;
|
||||
else stop();
|
||||
});
|
||||
return () => {
|
||||
mounted = false;
|
||||
unlisten?.();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return statuses;
|
||||
return useConfigList<ProfileStatus>(
|
||||
"list_profile_statuses",
|
||||
"profile-statuses-changed",
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { useConfigList } from "@/hooks/use-config-list";
|
||||
import type { ProfileTemplate } from "@/types";
|
||||
|
||||
/** Every profile template, in order, kept current by `profile-templates-changed`. */
|
||||
export function useProfileTemplates(): ProfileTemplate[] {
|
||||
return useConfigList<ProfileTemplate>(
|
||||
"list_profile_templates",
|
||||
"profile-templates-changed",
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user