You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-explore.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { useGlobalPublicStore } from '@/context/global-public-context'
  2. import { AccessMode } from '@/models/access-control'
  3. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
  4. import { fetchInstalledAppList, getAppAccessModeByAppId, uninstallApp, updatePinStatus } from './explore'
  5. import { fetchAppMeta, fetchAppParams } from './share'
  6. const NAME_SPACE = 'explore'
  7. export const useGetInstalledApps = () => {
  8. return useQuery({
  9. queryKey: [NAME_SPACE, 'installedApps'],
  10. queryFn: () => {
  11. return fetchInstalledAppList()
  12. },
  13. })
  14. }
  15. export const useUninstallApp = () => {
  16. const client = useQueryClient()
  17. return useMutation({
  18. mutationKey: [NAME_SPACE, 'uninstallApp'],
  19. mutationFn: (appId: string) => uninstallApp(appId),
  20. onSuccess: () => {
  21. client.invalidateQueries({ queryKey: [NAME_SPACE, 'installedApps'] })
  22. },
  23. })
  24. }
  25. export const useUpdateAppPinStatus = () => {
  26. const client = useQueryClient()
  27. return useMutation({
  28. mutationKey: [NAME_SPACE, 'updateAppPinStatus'],
  29. mutationFn: ({ appId, isPinned }: { appId: string; isPinned: boolean }) => updatePinStatus(appId, isPinned),
  30. onSuccess: () => {
  31. client.invalidateQueries({ queryKey: [NAME_SPACE, 'installedApps'] })
  32. },
  33. })
  34. }
  35. export const useGetInstalledAppAccessModeByAppId = (appId: string | null) => {
  36. const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
  37. return useQuery({
  38. queryKey: [NAME_SPACE, 'appAccessMode', appId],
  39. queryFn: () => {
  40. if (systemFeatures.webapp_auth.enabled === false) {
  41. return {
  42. accessMode: AccessMode.PUBLIC,
  43. }
  44. }
  45. if (!appId || appId.length === 0)
  46. return Promise.reject(new Error('App code is required to get access mode'))
  47. return getAppAccessModeByAppId(appId)
  48. },
  49. enabled: !!appId,
  50. })
  51. }
  52. export const useGetInstalledAppParams = (appId: string | null) => {
  53. return useQuery({
  54. queryKey: [NAME_SPACE, 'appParams', appId],
  55. queryFn: () => {
  56. if (!appId || appId.length === 0)
  57. return Promise.reject(new Error('App ID is required to get app params'))
  58. return fetchAppParams(true, appId)
  59. },
  60. enabled: !!appId,
  61. })
  62. }
  63. export const useGetInstalledAppMeta = (appId: string | null) => {
  64. return useQuery({
  65. queryKey: [NAME_SPACE, 'appMeta', appId],
  66. queryFn: () => {
  67. if (!appId || appId.length === 0)
  68. return Promise.reject(new Error('App ID is required to get app meta'))
  69. return fetchAppMeta(true, appId)
  70. },
  71. enabled: !!appId,
  72. })
  73. }