Skip to content

Commit

Permalink
Add useGafaelfawrUser hook
Browse files Browse the repository at this point in the history
This hook leverages swr to get a user's data from Gafaelfawr's
/auth/api/v1/user-info endpoint.

New dependencies:

- swr
- unfetch as a polyfill for fetch; this is needed because squared isn't
  inside a Next.js project that bakes the polyfill in for us.
  • Loading branch information
jonathansick committed Aug 14, 2023
1 parent 7bf9e11 commit b765732
Show file tree
Hide file tree
Showing 6 changed files with 670 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-sheep-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lsst-sqre/squared': minor
---

Add a useGafaelfawrUser hook.
10 changes: 6 additions & 4 deletions packages/squared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"dist/**"
],
"scripts": {
"build": "tsup src/index.tsx --format esm,cjs --dts --external react",
"dev": "tsup src/index.tsx --format esm,cjs --watch --dts --external react",
"build": "tsup src/index.ts --format esm,cjs --dts --external react",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts --external react",
"lint": "eslint \"src/**/*.ts*\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"storybook": "storybook dev -p 6006",
Expand All @@ -25,11 +25,13 @@
},
"dependencies": {
"@fontsource/source-sans-pro": "^4.5.11",
"@lsst-sqre/rubin-style-dictionary": "workspace:*",
"@lsst-sqre/global-css": "workspace:*",
"@lsst-sqre/rubin-style-dictionary": "workspace:*",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"styled-components": "^5.3.6"
"styled-components": "^5.3.6",
"swr": "^2.2.1",
"unfetch": "^5.0.0"
},
"devDependencies": {
"@lsst-sqre/eslint-config": "workspace:*",
Expand Down
58 changes: 58 additions & 0 deletions packages/squared/src/hooks/useGafaelfawrUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import useSWR, { Fetcher } from 'swr';
import fetch from 'unfetch';

type GafaelfawrGroup = {
name: string;
id?: number;
};

type GafaelfawrApiQuota = {
[key: string]: number;
};

type GafaelfawrNotebookQuota = {
cpu: number;
memory: number;
};

type GafaelfawrQuota = {
api: GafaelfawrApiQuota;
notebook: GafaelfawrNotebookQuota;
};

type GafaelfawrUser = {
username: string;
name?: string;
email?: string;
uid?: number;
gid?: number;
groups?: GafaelfawrGroup[];
quota?: GafaelfawrQuota;
};

const fetcher: Fetcher<GafaelfawrUser, string> = (url: string) =>
fetch(url).then((res) => res.json());

/**
* A React hook for getting data from Gafaelfawr's `/auth/user-info` endpoint
* and establishing in general whether the user is logged in.
*/
const useGafaelfawrUser = () => {
const { data, error, isLoading, isValidating } = useSWR(
'/auth/api/v1/user-info',
fetcher
);

const isLoggedIn = !error && data && data.hasOwnProperty('username');

return {
user: data,
isLoading,
isValidating,
isLoggedIn,
error,
};
};

export default useGafaelfawrUser;
export type { GafaelfawrUser };
8 changes: 8 additions & 0 deletions packages/squared/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Components */
export { Button, type ButtonProps } from './Button';

/* Hooks */
export {
default as useGafaelfawrUser,
type GafaelfawrUser,
} from './hooks/useGafaelfawrUser';
3 changes: 0 additions & 3 deletions packages/squared/src/index.tsx

This file was deleted.

Loading

0 comments on commit b765732

Please sign in to comment.