mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: add auth, hosts, & keychains ownership
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { createStore, useStore } from "zustand";
|
||||
import { persist, createJSONStorage } from "zustand/middleware";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
|
||||
type AppServer = {
|
||||
name?: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
type AppStore = {
|
||||
servers: AppServer[];
|
||||
curServerIdx?: number | null;
|
||||
};
|
||||
|
||||
const appStore = createStore(
|
||||
persist<AppStore>(
|
||||
() => ({
|
||||
servers: [],
|
||||
curServerIdx: null,
|
||||
}),
|
||||
{
|
||||
name: "vaulterm:app",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
export function addServer(srv: AppServer, setActive?: boolean) {
|
||||
const curServers = appStore.getState().servers;
|
||||
const isExist = curServers.findIndex((s) => s.url === srv.url);
|
||||
|
||||
if (isExist >= 0) {
|
||||
setActiveServer(isExist);
|
||||
return;
|
||||
}
|
||||
|
||||
appStore.setState((state) => ({
|
||||
servers: [...state.servers, srv],
|
||||
curServerIdx: setActive ? state.servers.length : state.curServerIdx,
|
||||
}));
|
||||
}
|
||||
|
||||
export function removeServer(idx: number) {
|
||||
appStore.setState((state) => ({
|
||||
servers: state.servers.filter((_, i) => i !== idx),
|
||||
curServerIdx: state.curServerIdx === idx ? null : state.curServerIdx,
|
||||
}));
|
||||
}
|
||||
|
||||
export function setActiveServer(idx: number) {
|
||||
appStore.setState({ curServerIdx: idx });
|
||||
}
|
||||
|
||||
export const useAppStore = () => {
|
||||
const state = useStore(appStore);
|
||||
const curServer =
|
||||
state.curServerIdx != null ? state.servers[state.curServerIdx] : null;
|
||||
|
||||
return { ...state, curServer };
|
||||
};
|
||||
|
||||
export default appStore;
|
||||
@@ -12,7 +12,7 @@ const authStore = createStore(
|
||||
token: null,
|
||||
}),
|
||||
{
|
||||
name: "auth",
|
||||
name: "vaulterm:auth",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -38,6 +38,9 @@ export const useTermSession = create(
|
||||
set({ curSession: idx });
|
||||
},
|
||||
}),
|
||||
{ name: "term-sessions", storage: createJSONStorage(() => AsyncStorage) }
|
||||
{
|
||||
name: "vaulterm:term-sessions",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
@@ -20,7 +20,7 @@ const useThemeStore = create(
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "theme",
|
||||
name: "vaulterm:theme",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user