-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from mykhailodanilenko/feature/recent-backends
Add recent instances view
- Loading branch information
Showing
9 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./useCategoryScroll"; | ||
export * from "./useViewHistory"; | ||
export * from "./useDeviceCapabilities"; | ||
export * from "./useRecentInstances"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
import { readFromAsyncStorage, writeToAsyncStorage } from "../utils"; | ||
import { STORAGE } from "../types"; | ||
|
||
export const useRecentInstances = () => { | ||
const queryClient = useQueryClient(); | ||
const { data: recentInstances } = useQuery({ | ||
queryKey: ["recentInstances"], | ||
queryFn: async () => { | ||
const instances: string[] | undefined = await readFromAsyncStorage(STORAGE.RECENT_INSTANCES); | ||
|
||
return instances || []; | ||
}, | ||
select: (data) => { | ||
return data.slice(0, 50); | ||
}, | ||
}); | ||
|
||
const { mutateAsync: updateRecentInstances } = useMutation({ | ||
mutationFn: async (data: string[]) => { | ||
await writeToAsyncStorage(STORAGE.RECENT_INSTANCES, data); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: ["recentInstances"], | ||
}); | ||
}, | ||
}); | ||
|
||
const addRecentInstance = async (instance: string) => { | ||
const currentInstances = queryClient.getQueryData<string[]>(["recentInstances"]); | ||
|
||
await updateRecentInstances(Array.from(new Set([instance, ...(currentInstances || [])]))); | ||
}; | ||
|
||
const clearRecentInstances = async () => { | ||
await updateRecentInstances([]); | ||
}; | ||
|
||
return { recentInstances, addRecentInstance, clearRecentInstances }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters