repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import { useState, useCallback } from 'react'; |
| #2 | import axios from 'axios'; |
| #3 | import { useDispatch, useSelector } from 'react-redux'; |
| #4 | import { AppDispatch, RootState } from '@/store/store'; |
| #5 | import { |
| #6 | App, |
| #7 | AppDetails, |
| #8 | AppMemory, |
| #9 | AccessedMemory, |
| #10 | setAppsSuccess, |
| #11 | setAppsError, |
| #12 | setAppsLoading, |
| #13 | setSelectedAppLoading, |
| #14 | setSelectedAppDetails, |
| #15 | setCreatedMemoriesLoading, |
| #16 | setCreatedMemoriesSuccess, |
| #17 | setCreatedMemoriesError, |
| #18 | setAccessedMemoriesLoading, |
| #19 | setAccessedMemoriesSuccess, |
| #20 | setAccessedMemoriesError, |
| #21 | setSelectedAppError, |
| #22 | } from '@/store/appsSlice'; |
| #23 | |
| #24 | interface ApiResponse { |
| #25 | total: number; |
| #26 | page: number; |
| #27 | page_size: number; |
| #28 | apps: App[]; |
| #29 | } |
| #30 | |
| #31 | interface MemoriesResponse { |
| #32 | total: number; |
| #33 | page: number; |
| #34 | page_size: number; |
| #35 | memories: AppMemory[]; |
| #36 | } |
| #37 | |
| #38 | interface AccessedMemoriesResponse { |
| #39 | total: number; |
| #40 | page: number; |
| #41 | page_size: number; |
| #42 | memories: AccessedMemory[]; |
| #43 | } |
| #44 | |
| #45 | interface FetchAppsParams { |
| #46 | name?: string; |
| #47 | is_active?: boolean; |
| #48 | sort_by?: 'name' | 'memories' | 'memories_accessed'; |
| #49 | sort_direction?: 'asc' | 'desc'; |
| #50 | page?: number; |
| #51 | page_size?: number; |
| #52 | } |
| #53 | |
| #54 | interface UseAppsApiReturn { |
| #55 | fetchApps: (params?: FetchAppsParams) => Promise<{ apps: App[], total: number }>; |
| #56 | fetchAppDetails: (appId: string) => Promise<void>; |
| #57 | fetchAppMemories: (appId: string, page?: number, pageSize?: number) => Promise<void>; |
| #58 | fetchAppAccessedMemories: (appId: string, page?: number, pageSize?: number) => Promise<void>; |
| #59 | updateAppDetails: (appId: string, details: { is_active: boolean }) => Promise<void>; |
| #60 | isLoading: boolean; |
| #61 | error: string | null; |
| #62 | } |
| #63 | |
| #64 | export const useAppsApi = (): UseAppsApiReturn => { |
| #65 | const [isLoading, setIsLoading] = useState<boolean>(false); |
| #66 | const [error, setError] = useState<string | null>(null); |
| #67 | const dispatch = useDispatch<AppDispatch>(); |
| #68 | const user_id = useSelector((state: RootState) => state.profile.userId); |
| #69 | |
| #70 | const URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8765"; |
| #71 | |
| #72 | const fetchApps = useCallback(async (params: FetchAppsParams = {}): Promise<{ apps: App[], total: number }> => { |
| #73 | const { |
| #74 | name, |
| #75 | is_active, |
| #76 | sort_by = 'name', |
| #77 | sort_direction = 'asc', |
| #78 | page = 1, |
| #79 | page_size = 10 |
| #80 | } = params; |
| #81 | |
| #82 | setIsLoading(true); |
| #83 | dispatch(setAppsLoading()); |
| #84 | try { |
| #85 | const queryParams = new URLSearchParams({ |
| #86 | page: String(page), |
| #87 | page_size: String(page_size) |
| #88 | }); |
| #89 | |
| #90 | if (name) queryParams.append('name', name); |
| #91 | if (is_active !== undefined) queryParams.append('is_active', String(is_active)); |
| #92 | if (sort_by) queryParams.append('sort_by', sort_by); |
| #93 | if (sort_direction) queryParams.append('sort_direction', sort_direction); |
| #94 | |
| #95 | const response = await axios.get<ApiResponse>( |
| #96 | `${URL}/api/v1/apps/?${queryParams.toString()}` |
| #97 | ); |
| #98 | |
| #99 | setIsLoading(false); |
| #100 | dispatch(setAppsSuccess(response.data.apps)); |
| #101 | return { |
| #102 | apps: response.data.apps, |
| #103 | total: response.data.total |
| #104 | }; |
| #105 | } catch (err: any) { |
| #106 | const errorMessage = err.message || 'Failed to fetch apps'; |
| #107 | setError(errorMessage); |
| #108 | dispatch(setAppsError(errorMessage)); |
| #109 | setIsLoading(false); |
| #110 | throw new Error(errorMessage); |
| #111 | } |
| #112 | }, [dispatch]); |
| #113 | |
| #114 | const fetchAppDetails = useCallback(async (appId: string): Promise<void> => { |
| #115 | setIsLoading(true); |
| #116 | dispatch(setSelectedAppLoading()); |
| #117 | try { |
| #118 | const response = await axios.get<AppDetails>( |
| #119 | `${URL}/api/v1/apps/${appId}` |
| #120 | ); |
| #121 | dispatch(setSelectedAppDetails(response.data)); |
| #122 | setIsLoading(false); |
| #123 | } catch (err: any) { |
| #124 | const errorMessage = err.message || 'Failed to fetch app details'; |
| #125 | dispatch(setSelectedAppError(errorMessage)); |
| #126 | setError(errorMessage); |
| #127 | setIsLoading(false); |
| #128 | throw new Error(errorMessage); |
| #129 | } |
| #130 | }, [dispatch]); |
| #131 | |
| #132 | const fetchAppMemories = useCallback(async (appId: string, page: number = 1, pageSize: number = 10): Promise<void> => { |
| #133 | setIsLoading(true); |
| #134 | dispatch(setCreatedMemoriesLoading()); |
| #135 | try { |
| #136 | const response = await axios.get<MemoriesResponse>( |
| #137 | `${URL}/api/v1/apps/${appId}/memories?page=${page}&page_size=${pageSize}` |
| #138 | ); |
| #139 | dispatch(setCreatedMemoriesSuccess({ |
| #140 | items: response.data.memories, |
| #141 | total: response.data.total, |
| #142 | page: response.data.page, |
| #143 | })); |
| #144 | setIsLoading(false); |
| #145 | } catch (err: any) { |
| #146 | const errorMessage = err.message || 'Failed to fetch app memories'; |
| #147 | dispatch(setCreatedMemoriesError(errorMessage)); |
| #148 | setError(errorMessage); |
| #149 | setIsLoading(false); |
| #150 | } |
| #151 | }, [dispatch]); |
| #152 | |
| #153 | const fetchAppAccessedMemories = useCallback(async (appId: string, page: number = 1, pageSize: number = 10): Promise<void> => { |
| #154 | setIsLoading(true); |
| #155 | dispatch(setAccessedMemoriesLoading()); |
| #156 | try { |
| #157 | const response = await axios.get<AccessedMemoriesResponse>( |
| #158 | `${URL}/api/v1/apps/${appId}/accessed?page=${page}&page_size=${pageSize}` |
| #159 | ); |
| #160 | dispatch(setAccessedMemoriesSuccess({ |
| #161 | items: response.data.memories, |
| #162 | total: response.data.total, |
| #163 | page: response.data.page, |
| #164 | })); |
| #165 | setIsLoading(false); |
| #166 | } catch (err: any) { |
| #167 | const errorMessage = err.message || 'Failed to fetch accessed memories'; |
| #168 | dispatch(setAccessedMemoriesError(errorMessage)); |
| #169 | setError(errorMessage); |
| #170 | setIsLoading(false); |
| #171 | } |
| #172 | }, [dispatch]); |
| #173 | |
| #174 | const updateAppDetails = async (appId: string, details: { is_active: boolean }) => { |
| #175 | setIsLoading(true); |
| #176 | try { |
| #177 | const response = await axios.put( |
| #178 | `${URL}/api/v1/apps/${appId}?is_active=${details.is_active}` |
| #179 | ); |
| #180 | setIsLoading(false); |
| #181 | return response.data; |
| #182 | } catch (error) { |
| #183 | console.error("Failed to update app details:", error); |
| #184 | setIsLoading(false); |
| #185 | throw error; |
| #186 | } |
| #187 | }; |
| #188 | |
| #189 | return { |
| #190 | fetchApps, |
| #191 | fetchAppDetails, |
| #192 | fetchAppMemories, |
| #193 | fetchAppAccessedMemories, |
| #194 | updateAppDetails, |
| #195 | isLoading, |
| #196 | error |
| #197 | }; |
| #198 | }; |
| #199 |