useUiSettings.ts 1.1 KB

1234567891011121314151617181920212223242526
  1. import { useQuery } from '@tanstack/react-query';
  2. import { api, type UiSettings } from '../api/client';
  3. import { useAuth } from '../contexts/AuthContext';
  4. /**
  5. * The install settings a page needs to render, for any signed-in user.
  6. *
  7. * GET /settings needs settings:read, and a user without it got a 403 that left
  8. * every value at its fallback. Such a user reads the subset served without
  9. * that permission instead; everyone else keeps the full settings, under the
  10. * same query key as before.
  11. */
  12. export function useUiSettings(options: { enabled?: boolean; staleTime?: number } = {}) {
  13. const { hasPermission } = useAuth();
  14. const readsAll = hasPermission('settings:read');
  15. return useQuery<UiSettings>({
  16. queryKey: readsAll ? ['settings'] : ['ui-settings'],
  17. queryFn: readsAll ? () => api.getSettings() : () => api.getUiSettings(),
  18. ...options,
  19. });
  20. }
  21. /** Whether G-code snippets are set up, from either form of the settings. */
  22. export function hasGcodeSnippets(settings: UiSettings | undefined): boolean {
  23. return !!(settings?.gcode_snippets || settings?.has_gcode_snippets);
  24. }