🐛 fix: Add loaded state to node, server, and subscribe stores for better loading management

This commit is contained in:
web 2025-09-17 07:26:56 -07:00
parent 41f06bfe54
commit 13dce0c20b
3 changed files with 36 additions and 8 deletions

View File

@ -9,6 +9,8 @@ interface NodeState {
// Loading states
loading: boolean;
loadingTags: boolean;
loaded: boolean;
loadedTags: boolean;
// Actions
fetchNodes: () => Promise<void>;
@ -30,6 +32,8 @@ export const useNodeStore = create<NodeState>((set, get) => ({
tags: [],
loading: false,
loadingTags: false,
loaded: false,
loadedTags: false,
// Actions
fetchNodes: async () => {
@ -38,9 +42,13 @@ export const useNodeStore = create<NodeState>((set, get) => ({
set({ loading: true });
try {
const { data } = await filterNodeList({ page: 1, size: 999999999 });
set({ nodes: data?.data?.list || [] });
set({
nodes: data?.data?.list || [],
loaded: true,
});
} catch (error) {
// Handle error silently
set({ loaded: true });
} finally {
set({ loading: false });
}
@ -52,9 +60,13 @@ export const useNodeStore = create<NodeState>((set, get) => ({
set({ loadingTags: true });
try {
const { data } = await queryNodeTag();
set({ tags: data?.data?.tags || [] });
set({
tags: data?.data?.tags || [],
loadedTags: true,
});
} catch (error) {
// Handle error silently
set({ loadedTags: true });
} finally {
set({ loadingTags: false });
}
@ -104,10 +116,10 @@ export const useNode = () => {
const store = useNodeStore();
// Auto-fetch nodes and tags
if (store.nodes.length === 0 && !store.loading) {
if (!store.loaded && !store.loading) {
store.fetchNodes();
}
if (store.tags.length === 0 && !store.loadingTags) {
if (!store.loadedTags && !store.loadingTags) {
store.fetchTags();
}
@ -116,6 +128,8 @@ export const useNode = () => {
tags: store.tags,
loading: store.loading,
loadingTags: store.loadingTags,
loaded: store.loaded,
loadedTags: store.loadedTags,
fetchNodes: store.fetchNodes,
fetchTags: store.fetchTags,
getNodeById: store.getNodeById,

View File

@ -7,6 +7,7 @@ interface ServerState {
// Loading states
loading: boolean;
loaded: boolean;
// Actions
fetchServers: () => Promise<void>;
@ -24,6 +25,7 @@ export const useServerStore = create<ServerState>((set, get) => ({
// Initial state
servers: [],
loading: false,
loaded: false,
// Actions
fetchServers: async () => {
@ -32,9 +34,13 @@ export const useServerStore = create<ServerState>((set, get) => ({
set({ loading: true });
try {
const { data } = await filterServerList({ page: 1, size: 999999999 });
set({ servers: data?.data?.list || [] });
set({
servers: data?.data?.list || [],
loaded: true,
});
} catch (error) {
// Handle error silently
set({ loaded: true });
} finally {
set({ loading: false });
}
@ -84,13 +90,14 @@ export const useServer = () => {
const store = useServerStore();
// Auto-fetch servers
if (store.servers.length === 0 && !store.loading) {
if (!store.loaded && !store.loading) {
store.fetchServers();
}
return {
servers: store.servers,
loading: store.loading,
loaded: store.loaded,
fetchServers: store.fetchServers,
getServerById: store.getServerById,
getServerName: store.getServerName,

View File

@ -7,6 +7,7 @@ interface SubscribeState {
// Loading states
loading: boolean;
loaded: boolean;
// Actions
fetchSubscribes: () => Promise<void>;
@ -20,6 +21,7 @@ export const useSubscribeStore = create<SubscribeState>((set, get) => ({
// Initial state
subscribes: [],
loading: false,
loaded: false,
// Actions
fetchSubscribes: async () => {
@ -28,9 +30,13 @@ export const useSubscribeStore = create<SubscribeState>((set, get) => ({
set({ loading: true });
try {
const { data } = await getSubscribeList({ page: 1, size: 999999999 });
set({ subscribes: data?.data?.list || [] });
set({
subscribes: data?.data?.list || [],
loaded: true,
});
} catch (error) {
// Handle error silently
set({ loaded: true });
} finally {
set({ loading: false });
}
@ -52,13 +58,14 @@ export const useSubscribe = () => {
const store = useSubscribeStore();
// Auto-fetch subscribes
if (store.subscribes.length === 0 && !store.loading) {
if (!store.loaded && !store.loading) {
store.fetchSubscribes();
}
return {
subscribes: store.subscribes,
loading: store.loading,
loaded: store.loaded,
fetchSubscribes: store.fetchSubscribes,
getSubscribeName: store.getSubscribeName,
getSubscribeById: store.getSubscribeById,