mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
refactor(plugins): share the plugin user settings, OAuth and activity logic between desktop and phone
The declared field form with its save, the actions and their confirm state, the OAuth connect and disconnect and the activity log now live in usePluginUserSettings, usePluginOAuth and usePluginActivity. Each shell keeps its own form, confirm dialog or sheet and picker.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { pluginsApi } from '../../api/client';
|
||||
import { useTranslation } from '../../i18n';
|
||||
|
||||
export interface ActivityRow {
|
||||
ts: string;
|
||||
plugin_id: string;
|
||||
plugin_name: string | null;
|
||||
method: string;
|
||||
resource: string | null;
|
||||
code: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The signed-in user's own plugin activity log behind the desktop panel and the
|
||||
* phone card: loaded on mount and on refresh, newest first. Fail-safe: a failed
|
||||
* load leaves an empty list, never an error.
|
||||
*/
|
||||
export function usePluginActivity() {
|
||||
const { locale } = useTranslation();
|
||||
const [rows, setRows] = useState<ActivityRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const load = () => {
|
||||
setLoading(true);
|
||||
pluginsApi
|
||||
.myActivity()
|
||||
.then((r) => setRows(r.activity))
|
||||
.catch(() => setRows([]))
|
||||
.finally(() => setLoading(false));
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, []);
|
||||
|
||||
const fmtWhen = (ts: string): string => {
|
||||
const d = new Date(ts);
|
||||
return Number.isNaN(d.getTime()) ? ts : d.toLocaleString(locale);
|
||||
};
|
||||
|
||||
return { rows, loading, load, fmtWhen };
|
||||
}
|
||||
Reference in New Issue
Block a user