Compare commits

...

120 Commits

Author SHA1 Message Date
Szilárd Dóró
19818e2b59 Merge pull request #1777 from nhost/changeset-release/main
chore: update versions
2023-03-27 12:03:16 +02:00
github-actions[bot]
b3eeec82ef chore: update versions 2023-03-27 09:38:55 +00:00
Szilárd Dóró
34ff254696 Merge pull request #1782 from nhost/renovate/sharp-0.x
fix(deps): update dependency sharp to ^0.32.0
2023-03-27 11:37:33 +02:00
Szilárd Dóró
1c4806bf51 chore: add changeset 2023-03-27 11:17:41 +02:00
renovate[bot]
2fb82ec97d fix(deps): update dependency sharp to ^0.32.0 2023-03-27 07:50:45 +00:00
Szilárd Dóró
0c994a9651 Merge pull request #1779 from nhost/renovate/pnpm-find-workspace-dir-6.x
fix(deps): update dependency @pnpm/find-workspace-dir to v6
2023-03-27 09:48:14 +02:00
Szilárd Dóró
4713cecfc2 chore: add changeset 2023-03-27 09:26:44 +02:00
renovate[bot]
f79eebadbf fix(deps): update dependency @pnpm/find-workspace-dir to v6 2023-03-24 21:30:22 +00:00
Szilárd Dóró
ac174b5e51 Merge pull request #1780 from nhost/chore/vercel-preview-fetcher 2023-03-24 17:07:43 +01:00
Szilárd Dóró
dc9ddfc9ae chore(ci): adjust preview fetcher 2023-03-24 16:30:29 +01:00
Szilárd Dóró
3bdd9f570c Merge pull request #1773 from nhost/chore/dashboard-delete-table-tests
chore(dashboard): tests for table deletion
2023-03-24 15:52:25 +01:00
Szilárd Dóró
94477be998 Merge pull request #1778 from nhost/chore/fetch-preview-url
chore: use dynamic test URL
2023-03-24 15:51:47 +01:00
Szilárd Dóró
568577e8ca Merge pull request #1774 from nhost/renovate/docusaurus-monorepo
fix(deps): update docusaurus monorepo to v2.4.0
2023-03-24 15:38:23 +01:00
Szilárd Dóró
e93b06ab8f chore: add changeset 2023-03-24 15:37:08 +01:00
Szilárd Dóró
c75bf46ba1 fix: fetch valid previews only 2023-03-24 15:24:36 +01:00
Szilárd Dóró
63a1fd09b5 fix: use correct Vercel token 2023-03-24 15:09:42 +01:00
Szilárd Dóró
630d44ad6e fix: use staging project ID 2023-03-24 14:55:26 +01:00
Szilárd Dóró
d7db521974 chore: use dynamic test URL 2023-03-24 14:16:05 +01:00
renovate[bot]
90e4053f0a fix(deps): update docusaurus monorepo to v2.4.0 2023-03-24 09:57:19 +00:00
Szilárd Dóró
8e9d5d1b38 Merge pull request #1775 from nhost/fix/storage-sdk-tests
chore(hasura-storage-js): improve presignedUrl test
2023-03-24 10:54:51 +01:00
Szilárd Dóró
43c86fef14 chore(hasura-storage-js): improve presignedUrl test 2023-03-24 10:25:18 +01:00
Szilárd Dóró
6b97340cf4 fix: remove test.only call 2023-03-23 16:14:49 +01:00
Szilárd Dóró
1605756362 chore: add tests for table deletion 2023-03-23 16:05:21 +01:00
Szilárd Dóró
6437544384 Merge pull request #1771 from nhost/changeset-release/main
chore: update versions
2023-03-23 14:20:16 +01:00
github-actions[bot]
b4dcd1996d chore: update versions 2023-03-23 13:01:48 +00:00
Szilárd Dóró
7fb73dbb1b Merge pull request #1770 from nhost/fix/subscription-errors
fix(apollo): retry subscriptions on error
2023-03-23 14:00:11 +01:00
Szilárd Dóró
a66b11d245 Merge pull request #1769 from st3phan/patch-1
Fix import in docs for SignedIn component
2023-03-23 13:23:35 +01:00
Szilárd Dóró
912ed76c64 fix: potential subscription fix 2023-03-23 12:30:14 +01:00
Szilárd Dóró
b47c0d1af7 Merge pull request #1765 from nhost/chore/dashboard-db-tests
chore(dashboard): tests for table creation
2023-03-23 09:36:27 +01:00
Stephan van Opstal
b97ab2be2f Fix import in docs 2023-03-22 21:46:58 +01:00
Szilárd Dóró
f12cb666ff fix: remove test.only call 2023-03-22 15:42:05 +01:00
Szilárd Dóró
c3b2b1cd02 chore: add remaining table creation tests 2023-03-22 15:40:39 +01:00
Szilárd Dóró
c0b71102d4 chore: add foreign key constraint test 2023-03-22 15:32:18 +01:00
Szilárd Dóró
5f68ae95c4 chore: add extra database UI tests 2023-03-22 15:22:49 +01:00
Szilárd Dóró
2d1b7bb292 chore: restructure tests, add basic table creation test 2023-03-22 14:57:33 +01:00
Szilárd Dóró
ee154d4eca Merge pull request #1764 from nhost/changeset-release/main
chore: update versions
2023-03-22 14:21:18 +01:00
github-actions[bot]
58ef9bbe02 chore: update versions 2023-03-22 12:49:02 +00:00
Szilárd Dóró
f3f35beefd Merge pull request #1758 from nhost/renovate/turbo-1.x
chore(deps): update dependency turbo to v1.8.5
2023-03-22 13:47:44 +01:00
Szilárd Dóró
d31330e6c0 Merge pull request #1762 from nhost/renovate/react-error-boundary-4.x
fix(deps): update dependency react-error-boundary to v4
2023-03-22 13:47:30 +01:00
Szilárd Dóró
c3dda79d95 Merge branch 'renovate/react-error-boundary-4.x' of https://github.com/nhost/nhost into renovate/react-error-boundary-4.x 2023-03-22 13:11:13 +01:00
Szilárd Dóró
7c1273725d chore: add changeset 2023-03-22 13:11:01 +01:00
renovate[bot]
70be0e1ab4 fix(deps): update dependency react-error-boundary to v4 2023-03-22 12:08:14 +00:00
renovate[bot]
4f5870cfd7 chore(deps): update dependency turbo to v1.8.5 2023-03-22 12:07:47 +00:00
Szilárd Dóró
623607476e Merge branch 'main' into renovate/react-error-boundary-4.x 2023-03-22 13:07:31 +01:00
Szilárd Dóró
1e232713d9 Merge pull request #1763 from nhost/chore/improve-e2e
chore(tests): improve E2E tests
2023-03-22 13:05:43 +01:00
Szilárd Dóró
1ed647c4e9 fix: fix lint and test jobs 2023-03-22 12:50:12 +01:00
Szilárd Dóró
b666a173b1 fix: use correct name for the build script 2023-03-22 12:15:48 +01:00
Szilárd Dóró
caba147b32 chore: add changeset 2023-03-22 12:07:44 +01:00
Szilárd Dóró
ca365fc8e7 cleanup cypress 2023-03-22 12:06:46 +01:00
Szilárd Dóró
d88cdedb26 migrated file upload e2e tests 2023-03-22 11:39:17 +01:00
Szilárd Dóró
1de08cecaf migrate password change e2e tests 2023-03-22 11:21:38 +01:00
Szilárd Dóró
47bb997036 migrate email change e2e tests 2023-03-22 11:15:20 +01:00
Szilárd Dóró
4e4d962f30 migrate apollo e2e tests 2023-03-22 11:05:46 +01:00
Szilárd Dóró
883fb82c77 migrate sign-in tests 2023-03-22 10:46:11 +01:00
Szilárd Dóró
c9f5634ac2 chore: migrate sign in e2e tests 2023-03-21 18:25:19 +01:00
renovate[bot]
6ee9a589fb fix(deps): update dependency react-error-boundary to v4 2023-03-21 17:21:51 +00:00
Szilárd Dóró
e2d733cf34 chore: migrate passwordless e2e tests 2023-03-21 16:45:19 +01:00
Szilárd Dóró
a0d7327c8d chore: migrate sign up tests 2023-03-21 16:33:56 +01:00
Szilárd Dóró
c7c8a20334 chore: port some tests to playwright 2023-03-21 16:10:37 +01:00
Szilárd Dóró
cca8de5805 Merge pull request #1760 from nhost/fix/dashboard-e2e-workflow
fix dashboard e2e workflow
2023-03-21 13:42:10 +01:00
Szilárd Dóró
8c065c42d6 fix: remove dashboard-e2e job 2023-03-21 13:19:31 +01:00
Szilárd Dóró
210af3a3e8 merge ci and e2e workflows 2023-03-21 12:52:01 +01:00
Szilárd Dóró
fbb12a8079 update workflow triggers 2023-03-21 12:44:37 +01:00
Szilárd Dóró
77692ac40e remove unnecessary job from e2e workflow 2023-03-21 11:57:49 +01:00
Szilárd Dóró
2c2a42a8e8 fix test script 2023-03-21 11:51:22 +01:00
Szilárd Dóró
a8466798a3 Merge pull request #1761 from nhost/changeset-release/main
chore: update versions
2023-03-21 11:47:58 +01:00
Szilárd Dóró
a45c0970bb pin playwright version 2023-03-21 11:46:30 +01:00
Szilárd Dóró
9bf30a1ccc fix e2e script 2023-03-21 11:35:12 +01:00
github-actions[bot]
99d3d82c72 chore: update versions 2023-03-21 10:23:53 +00:00
Szilárd Dóró
43acb3fb50 Merge pull request #1757 from nhost/renovate/hookform-resolvers-3.x
fix(deps): update dependency @hookform/resolvers to v3
2023-03-21 11:22:15 +01:00
Szilárd Dóró
ba9ef13ba3 update workflow 2023-03-21 11:21:28 +01:00
Szilárd Dóró
cea507a271 fix: install browsers before e2e tests 2023-03-21 11:07:24 +01:00
Szilárd Dóró
9130ab1230 chore(deps): bump yup and @hookform/resolvers 2023-03-21 10:58:44 +01:00
Szilárd Dóró
27acdd6f56 fix: add missing env vars 2023-03-21 10:43:55 +01:00
Szilárd Dóró
dcdacd73ec fix: fix dashboard e2e workflow 2023-03-21 10:24:10 +01:00
Szilárd Dóró
9c9966a30f Merge pull request #1759 from diecknet/patch-1
fix DevDependencies parameter for @types/express
2023-03-21 10:22:50 +01:00
Andreas Dieckmann
5a23e7a0a8 fix DevDependencies parameter for @types/express
it's `-D` not `-d`
2023-03-20 22:18:40 +01:00
renovate[bot]
47500fac39 fix(deps): update dependency @hookform/resolvers to v3 2023-03-20 18:24:31 +00:00
Szilárd Dóró
cbbf53c05b Merge pull request #1756 from nhost/feat/playwright
chore(dashboard): prepare E2E testing framework
2023-03-20 16:02:27 +01:00
Szilárd Dóró
11bd011860 fix: correct CI workflows 2023-03-20 15:02:36 +01:00
Szilárd Dóró
e3c0c47777 fix: correct tests 2023-03-20 14:03:27 +01:00
Szilárd Dóró
d825404b54 Merge branch 'main' into feat/playwright 2023-03-20 13:45:17 +01:00
Szilárd Dóró
d46d77ee71 Merge pull request #1751 from Glenas7/docs/google-oauth-guide
Added Javascript origins step to docs
2023-03-20 12:20:35 +01:00
Szilárd Dóró
a292482705 Update docs/docs/authentication/sign-in-methods/4-google.mdx 2023-03-20 12:20:27 +01:00
Szilárd Dóró
8a4ca41172 Merge pull request #1754 from nhost/changeset-release/main
chore: update versions
2023-03-20 11:33:21 +01:00
github-actions[bot]
fd3ce98600 chore: update versions 2023-03-20 10:08:17 +00:00
Szilárd Dóró
04f36a0491 Merge pull request #1669 from nhost/new-create-app-mutation
feat(dashboard): Limit Free Projects
2023-03-20 11:05:30 +01:00
Glenas7
eca9e551e8 Added Javascript origins step 2023-03-18 21:30:46 +01:00
Szilárd Dóró
7cce8652e7 chore: update response message for pausing 2023-03-17 12:20:16 +01:00
Szilárd Dóró
f2e2323801 fix: refresh list when deleting app 2023-03-17 12:09:41 +01:00
Szilárd Dóró
4e16de6db2 chore: cleanup, improve error messages 2023-03-17 12:01:11 +01:00
Szilárd Dóró
3d8067ff7b fix: show pausing only for free projects
- improve project list
2023-03-17 09:44:02 +01:00
Szilárd Dóró
0fa4b428a9 chore: change function to string 2023-03-16 15:04:13 +01:00
Szilárd Dóró
8c5864340e fix: fix build error 2023-03-16 14:57:25 +01:00
Szilárd Dóró
c131100af9 chore: fetch free and live apps separately 2023-03-16 14:52:35 +01:00
Szilárd Dóró
e363fef8cf fix: refetch projects after delete/pause 2023-03-16 13:11:28 +01:00
Szilárd Dóró
d8072101c8 feat: added pause section to settings 2023-03-16 13:03:11 +01:00
Szilárd Dóró
afbba531a1 Merge branch 'main' into new-create-app-mutation 2023-03-16 10:28:02 +01:00
Szilárd Dóró
a9e9fc4305 chore: extend tests 2023-03-10 16:57:21 +01:00
Szilárd Dóró
c547b490e5 chore: improved overview tests 2023-03-10 11:46:12 +01:00
Szilárd Dóró
4f4449b855 Merge remote-tracking branch 'origin/main' into feat/playwright 2023-03-10 11:28:13 +01:00
Johan Eliasson
ae19105302 cleanup 2023-03-02 21:32:34 +01:00
Johan Eliasson
730a482598 optimization 2023-03-02 21:25:43 +01:00
Johan Eliasson
253dd235ca added changeset 2023-03-01 09:43:00 +01:00
Johan Eliasson
991e8f2d15 removed unused code 2023-02-28 19:57:51 +01:00
Johan Eliasson
e500e87022 review fixes 2023-02-28 19:15:25 +01:00
Johan Eliasson
c684d0307b Update dashboard/src/utils/CONSTANTS.ts
Co-authored-by: Szilárd Dóró <doroszilard@icloud.com>
2023-02-28 16:59:35 +01:00
Johan Eliasson
2d657b9c29 styled 2023-02-28 13:42:22 +01:00
Johan Eliasson
f46d96bafc query fix 2023-02-27 17:33:26 +01:00
Johan Eliasson
8261743bd3 show warning if max free projects has been created by the user already 2023-02-27 10:44:52 +01:00
Johan Eliasson
34cf1d79a0 readability 2023-02-26 15:01:07 +01:00
Johan Eliasson
9d4542b3db revert back 2023-02-26 14:51:14 +01:00
Johan Eliasson
bb5dbdf5a3 small cleanup 2023-02-26 14:49:44 +01:00
Johan Eliasson
2801b03bf4 removed unused code 2023-02-26 09:57:46 +01:00
Johan Eliasson
8298d458d5 cleanup 2023-02-26 09:56:58 +01:00
Johan Eliasson
6e9b941b89 handle slug server side 2023-02-26 09:54:00 +01:00
Johan Eliasson
5dd25941e5 update 2023-02-26 09:25:40 +01:00
Szilárd Dóró
cfcb97b8ee chore(actions): update workflow 2023-02-22 11:04:59 +01:00
Szilárd Dóró
a1ffad77eb chore(dashboard): move sign in to global setup
- add test skeletons for the Overview
2023-02-20 17:19:46 +01:00
Szilárd Dóró
de4d59da99 feat(dashboard): add Playwright to the dashboard 2023-02-20 15:44:45 +01:00
116 changed files with 3464 additions and 2199 deletions

View File

@@ -23,9 +23,7 @@ runs:
- uses: actions/cache@v3
id: pnpm-cache
with:
path: |
${{ steps.pnpm-cache-dir.outputs.dir }}
~/.cache/Cypress
path: ${{ steps.pnpm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('pnpm-lock.yaml') }}
restore-keys: ${{ runner.os }}-node-
- name: Use Node.js 16

View File

@@ -19,6 +19,11 @@ env:
NEXT_PUBLIC_ENV: dev
NEXT_TELEMETRY_DISABLED: 1
NEXT_PUBLIC_NHOST_BACKEND_URL: http://localhost:1337
NHOST_TEST_DASHBOARD_URL: ${{ vars.NHOST_TEST_DASHBOARD_URL }}
NHOST_TEST_WORKSPACE_NAME: ${{ vars.NHOST_TEST_WORKSPACE_NAME }}
NHOST_TEST_PROJECT_NAME: ${{ vars.NHOST_TEST_PROJECT_NAME }}
NHOST_TEST_USER_EMAIL: ${{ secrets.NHOST_TEST_USER_EMAIL }}
NHOST_TEST_USER_PASSWORD: ${{ secrets.NHOST_TEST_USER_PASSWORD }}
jobs:
build:
@@ -60,47 +65,6 @@ jobs:
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
e2e:
name: 'e2e (${{ matrix.package.path }})'
needs: build
if: ${{ needs.build.outputs.matrix != '[]' && needs.build.outputs.matrix != '' }}
strategy:
# * Don't cancel other matrices when one fails
fail-fast: false
matrix:
package: ${{ fromJson(needs.build.outputs.matrix) }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# * Install Node and dependencies. Package dependencies won't be downloaded again as they have been cached by the `build` job.
- name: Install Node and dependencies
uses: ./.github/actions/install-dependencies
with:
TURBO_TOKEN: ${{ env.TURBO_TOKEN }}
TURBO_TEAM: ${{ env.TURBO_TEAM }}
# * Install Nhost CLI if a `nhost/config.yaml` file is found
- name: Install Nhost CLI
if: hashFiles(format('{0}/nhost/config.yaml', matrix.package.path)) != ''
uses: ./.github/actions/nhost-cli
# * Run the `ci` script of the current package of the matrix. Dependencies build is cached by Turborepo
- name: Run e2e test
run: pnpm --filter="${{ matrix.package.name }}" run e2e
- id: file-name
if: ${{ failure() }}
name: Tranform package name into a valid file name
run: |
PACKAGE_FILE_NAME=$(echo "${{ matrix.package.name }}" | sed 's/@//g; s/\//-/g')
echo "fileName=$PACKAGE_FILE_NAME" >> $GITHUB_OUTPUT
# * Run this step only if the previous step failed, and some Cypress screenshots/videos exist
- name: Upload Cypress videos and screenshots
if: ${{ failure() && hashFiles(format('{0}/cypress/screenshots/**', matrix.package.path), format('{0}/cypress/videos/**', matrix.package.path)) != ''}}
uses: actions/upload-artifact@v3
with:
name: cypress-${{ steps.file-name.outputs.fileName }}
path: |
${{format('{0}/cypress/screenshots/**', matrix.package.path)}}
${{format('{0}/cypress/videos/**', matrix.package.path)}}
unit:
name: Unit tests
needs: build
@@ -141,3 +105,57 @@ jobs:
# * Run every `lint` script in the workspace . Dependencies build is cached by Turborepo
- name: Lint
run: pnpm run lint:all
e2e:
name: 'E2E (Package: ${{ matrix.package.path }})'
needs: build
if: ${{ needs.build.outputs.matrix != '[]' && needs.build.outputs.matrix != '' }}
strategy:
# * Don't cancel other matrices when one fails
fail-fast: false
matrix:
package: ${{ fromJson(needs.build.outputs.matrix) }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# * Install Node and dependencies. Package dependencies won't be downloaded again as they have been cached by the `build` job.
- name: Install Node and dependencies
uses: ./.github/actions/install-dependencies
with:
TURBO_TOKEN: ${{ env.TURBO_TOKEN }}
TURBO_TEAM: ${{ env.TURBO_TEAM }}
# * Install Nhost CLI if a `nhost/config.yaml` file is found
- name: Install Nhost CLI
if: hashFiles(format('{0}/nhost/config.yaml', matrix.package.path)) != ''
uses: ./.github/actions/nhost-cli
- name: Fetch Dashboard Preview URL
id: fetch-dashboard-preview-url
uses: zentered/vercel-preview-url@v1.1.9
if: github.ref_name != 'main'
env:
VERCEL_TOKEN: ${{ secrets.DASHBOARD_VERCEL_DEPLOY_TOKEN }}
GITHUB_REF: ${{ github.ref_name }}
GITHUB_REPOSITORY: ${{ github.repository }}
with:
vercel_team_id: ${{ secrets.DASHBOARD_VERCEL_TEAM_ID }}
vercel_project_id: ${{ secrets.DASHBOARD_STAGING_VERCEL_PROJECT_ID }}
vercel_state: BUILDING,READY,INITIALIZING
- name: Set Dashboard Preview URL
if: steps.fetch-dashboard-preview-url.outputs.preview_url != ''
run: echo "NHOST_TEST_DASHBOARD_URL=https://${{ steps.fetch-dashboard-preview-url.outputs.preview_url }}" >> $GITHUB_ENV
# * Run the `ci` script of the current package of the matrix. Dependencies build is cached by Turborepo
- name: Run e2e tests
run: pnpm --filter="${{ matrix.package.name }}" run e2e
- id: file-name
if: ${{ failure() }}
name: Transform package name into a valid file name
run: |
PACKAGE_FILE_NAME=$(echo "${{ matrix.package.name }}" | sed 's/@//g; s/\//-/g')
echo "fileName=$PACKAGE_FILE_NAME" >> $GITHUB_OUTPUT
# * Run this step only if the previous step failed, and Playwright generated a report
- name: Upload Playwright Report
if: ${{ failure() && hashFiles(format('{0}/playwright-report/**', matrix.package.path)) != ''}}
uses: actions/upload-artifact@v3
with:
name: playwright-${{ steps.file-name.outputs.fileName }}
path: ${{format('{0}/playwright-report/**', matrix.package.path)}}

View File

@@ -9,6 +9,7 @@ env:
NEXT_PUBLIC_ENV: dev
NEXT_TELEMETRY_DISABLED: 1
NEXT_PUBLIC_NHOST_BACKEND_URL: http://localhost:1337
jobs:
build:
name: Build

View File

@@ -23,8 +23,8 @@ module.exports = {
'e2e/**/*.ts',
'e2e/**/*.d.ts'
],
plugins: ['@typescript-eslint', 'cypress'],
extends: ['plugin:cypress/recommended'],
plugins: ['@typescript-eslint'],
extends: [],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'

View File

@@ -49,4 +49,8 @@ tailwind.json
.idea
# Do not ignore Logs page
!src/**/logs*
!src/**/logs*
/test-results/
/playwright-report/
/playwright/.cache/
storageState.json

View File

@@ -51,7 +51,7 @@ export const decorators = [
(Story) => (
<NhostApolloProvider
fetchPolicy="cache-first"
graphqlUrl="http://localhost:1337/v1/graphql"
graphqlUrl="https://local.graphql.nhost.run/v1"
>
<Story />
</NhostApolloProvider>

View File

@@ -1,5 +1,40 @@
# @nhost/dashboard
## 0.13.10
### Patch Changes
- e93b06ab: fix(dashboard): remove left margin from workspace list on mobile
- 1c4806bf: chore(deps): bump `sharp` to 0.32.0
- @nhost/react-apollo@5.0.14
- @nhost/nextjs@1.13.18
## 0.13.9
### Patch Changes
- 912ed76c: chore(dashboard): bump `@apollo/client` to 3.7.10
- Updated dependencies [912ed76c]
- @nhost/react-apollo@5.0.13
## 0.13.8
### Patch Changes
- 7c127372: chore(dashboard): bump `react-error-boundary` to v4
## 0.13.7
### Patch Changes
- 9130ab12: chore(dashboard): bump `yup` to v1 and `@hookform/resolvers` to v3
## 0.13.6
### Patch Changes
- 253dd235: using new mutation to create projects + refactor Create Project page.
## 0.13.5
### Patch Changes

View File

@@ -111,3 +111,21 @@ pnpm storybook
| `@typescript-eslint/consistent-type-imports` | Enforces `import type { Type } from 'module'` syntax. It prevents false positive circular dependency errors. |
| `@typescript-eslint/naming-convention` | Enforces a consistent naming convention. |
| `no-restricted-imports` | Enforces absolute imports and consistent import paths for components from `src/components/ui` folder. |
### End-to-End Tests
End-to-end tests are written using [Playwright](https://playwright.dev/). To run the tests, run the following command:
```bash
pnpm e2e
```
Most of the tests require access to the Nhost test user. To run these tests, you need to set the following environment variables in `.env.test`:
```
NHOST_TEST_DASHBOARD_URL=<test_dashboard_url>
NHOST_TEST_USER_EMAIL=<test_user_email>
NHOST_TEST_USER_PASSWORD=<test_user_password>
NHOST_TEST_WORKSPACE_NAME=<test_workspace_name>
NHOST_TEST_PROJECT_NAME=<test_project_name>
```

View File

@@ -0,0 +1,93 @@
import {
TEST_PROJECT_NAME,
TEST_PROJECT_SLUG,
TEST_WORKSPACE_SLUG,
} from '@/e2e/env';
import { openProject } from '@/e2e/utils';
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
let page: Page;
test.describe.configure({ mode: 'serial' });
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto('/');
await openProject({
page,
projectName: TEST_PROJECT_NAME,
workspaceSlug: TEST_WORKSPACE_SLUG,
projectSlug: TEST_PROJECT_SLUG,
});
await page
.getByRole('navigation', { name: /main navigation/i })
.getByRole('link', { name: /auth/i })
.click();
await page.waitForURL(`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/users`);
});
test.afterAll(async () => {
await page.close();
});
test('should create a user', async () => {
await expect(
page.getByRole('heading', { name: /there are no users yet/i }),
).toBeVisible();
await page
.getByRole('button', { name: /create user/i })
.first()
.click();
await expect(page.getByRole('dialog')).toBeVisible();
await expect(
page.getByRole('heading', { name: /create user/i }),
).toBeVisible();
await page
.getByRole('textbox', { name: /email/i })
.fill('testuser@example.com');
await page.getByRole('textbox', { name: /password/i }).fill('test.password');
await page.getByRole('button', { name: /create/i, exact: true }).click();
await expect(page.getByRole('dialog')).not.toBeVisible();
await expect(
page.getByRole('button', { name: /view testuser@example.com/i }),
).toBeVisible();
});
test('should delete a user', async () => {
await expect(
page.getByRole('button', { name: /view testuser@example.com/i }),
).toBeVisible();
await page
.getByRole('button', { name: /more options for testuser@example.com/i })
.click();
await page.getByRole('menuitem', { name: /delete user/i }).click();
await expect(page.getByRole('dialog')).toBeVisible();
await expect(
page.getByRole('heading', { name: /delete user/i }),
).toBeVisible();
await expect(
page.getByText(
/are you sure you want to delete the "testuser@example.com" user?/i,
),
).toBeVisible();
await page.getByRole('button', { name: /delete/i, exact: true }).click();
await expect(page.getByRole('dialog')).not.toBeVisible();
await expect(
page.getByRole('heading', { name: /there are no users yet/i }),
).toBeVisible();
});

View File

@@ -0,0 +1,276 @@
import {
TEST_PROJECT_NAME,
TEST_PROJECT_SLUG,
TEST_WORKSPACE_SLUG,
} from '@/e2e/env';
import { openProject, prepareTable } from '@/e2e/utils';
import { faker } from '@faker-js/faker';
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
let page: Page;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto('/');
await openProject({
page,
projectName: TEST_PROJECT_NAME,
workspaceSlug: TEST_WORKSPACE_SLUG,
projectSlug: TEST_PROJECT_SLUG,
});
await page
.getByRole('navigation', { name: /main navigation/i })
.getByRole('link', { name: /database/i })
.click();
});
test.afterAll(async () => {
await page.close();
});
test('should create a simple table', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const tableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text' },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${tableName}`,
);
await expect(
page.getByRole('link', { name: tableName, exact: true }),
).toBeVisible();
});
test('should create a table with unique constraints', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const tableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text', unique: true },
{ name: 'isbn', type: 'text', unique: true },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${tableName}`,
);
await expect(
page.getByRole('link', { name: tableName, exact: true }),
).toBeVisible();
});
test('should create a table with nullable columns', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const tableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text', nullable: true },
{ name: 'description', type: 'text', nullable: true },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${tableName}`,
);
await expect(
page.getByRole('link', { name: tableName, exact: true }),
).toBeVisible();
});
test('should create a table with an identity column', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const tableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'int4' },
{ name: 'title', type: 'text', nullable: true },
{ name: 'description', type: 'text', nullable: true },
],
});
await page.getByRole('button', { name: /identity/i }).click();
await page.getByRole('option', { name: /id/i }).click();
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${tableName}`,
);
await expect(
page.getByRole('link', { name: tableName, exact: true }),
).toBeVisible();
});
test('should create table with foreign key constraint', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const firstTableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: firstTableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'name', type: 'text' },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${firstTableName}`,
);
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const secondTableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: secondTableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text' },
{ name: 'author_id', type: 'uuid' },
],
});
await page.getByRole('button', { name: /add foreign key/i }).click();
// select column in current table
await page
.getByRole('button', { name: /column/i })
.first()
.click();
await page.getByRole('option', { name: /author_id/i }).click();
// select reference schema
await page.getByRole('button', { name: /schema/i }).click();
await page.getByRole('option', { name: /public/i }).click();
// select reference table
await page.getByRole('button', { name: /table/i }).click();
await page.getByRole('option', { name: firstTableName, exact: true }).click();
// select reference column
await page
.getByRole('button', { name: /column/i })
.nth(1)
.click();
await page.getByRole('option', { name: /id/i }).click();
await page.getByRole('button', { name: /add/i }).click();
await expect(
page.getByText(`public.${firstTableName}.id`, { exact: true }),
).toBeVisible();
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${secondTableName}`,
);
await expect(
page.getByRole('link', { name: secondTableName, exact: true }),
).toBeVisible();
});
test('should not be able to create a table with a name that already exists', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const tableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'name', type: 'text' },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${tableName}`,
);
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text' },
{ name: 'author_id', type: 'uuid' },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await expect(
page.getByText(/error: a table with this name already exists/i),
).toBeVisible();
});

View File

@@ -0,0 +1,191 @@
import {
TEST_PROJECT_NAME,
TEST_PROJECT_SLUG,
TEST_WORKSPACE_SLUG,
} from '@/e2e/env';
import { openProject, prepareTable } from '@/e2e/utils';
import { faker } from '@faker-js/faker';
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
let page: Page;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto('/');
await openProject({
page,
projectName: TEST_PROJECT_NAME,
workspaceSlug: TEST_WORKSPACE_SLUG,
projectSlug: TEST_PROJECT_SLUG,
});
await page
.getByRole('navigation', { name: /main navigation/i })
.getByRole('link', { name: /database/i })
.click();
});
test.afterAll(async () => {
await page.close();
});
test('should delete a table', async () => {
const tableName = faker.random.word().toLowerCase();
await page.getByRole('button', { name: /new table/i }).click();
await prepareTable({
page,
name: tableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text' },
],
});
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${tableName}`,
);
const tableLink = page.getByRole('link', {
name: tableName,
exact: true,
});
await tableLink.hover();
await page
.getByRole('listitem')
.filter({ hasText: tableName })
.getByRole('button')
.click();
await page.getByRole('menuitem', { name: /delete table/i }).click();
await expect(
page.getByRole('heading', { name: /delete table/i }),
).toBeVisible();
await page.getByRole('button', { name: /delete/i }).click();
// navigate to next URL
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/**`,
);
await expect(
page.getByRole('link', { name: tableName, exact: true }),
).not.toBeVisible();
});
test('should not be able to delete a table if other tables have foreign keys referencing it', async () => {
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const firstTableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: firstTableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'name', type: 'text' },
],
});
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${firstTableName}`,
);
await page.getByRole('button', { name: /new table/i }).click();
await expect(page.getByText(/create a new table/i)).toBeVisible();
const secondTableName = faker.random.word().toLowerCase();
await prepareTable({
page,
name: secondTableName,
primaryKey: 'id',
columns: [
{ name: 'id', type: 'uuid', defaultValue: 'gen_random_uuid()' },
{ name: 'title', type: 'text' },
{ name: 'author_id', type: 'uuid' },
],
});
await page.getByRole('button', { name: /add foreign key/i }).click();
// select column in current table
await page
.getByRole('button', { name: /column/i })
.first()
.click();
await page.getByRole('option', { name: /author_id/i }).click();
// select reference schema
await page.getByRole('button', { name: /schema/i }).click();
await page.getByRole('option', { name: /public/i }).click();
// select reference table
await page.getByRole('button', { name: /table/i }).click();
await page.getByRole('option', { name: firstTableName, exact: true }).click();
// select reference column
await page
.getByRole('button', { name: /column/i })
.nth(1)
.click();
await page.getByRole('option', { name: /id/i }).click();
await page.getByRole('button', { name: /add/i }).click();
await expect(
page.getByText(`public.${firstTableName}.id`, { exact: true }),
).toBeVisible();
// create table
await page.getByRole('button', { name: /create/i }).click();
await page.waitForURL(
`/${TEST_WORKSPACE_SLUG}/${TEST_PROJECT_SLUG}/database/browser/default/public/${secondTableName}`,
);
await expect(
page.getByRole('link', { name: secondTableName, exact: true }),
).toBeVisible();
// try to delete the first table that is referenced by the second table
const tableLink = page.getByRole('link', {
name: firstTableName,
exact: true,
});
await tableLink.hover();
await page
.getByRole('listitem')
.filter({ hasText: firstTableName })
.getByRole('button')
.click();
await page.getByRole('menuitem', { name: /delete table/i }).click();
await expect(
page.getByRole('heading', { name: /delete table/i }),
).toBeVisible();
await page.getByRole('button', { name: /delete/i }).click();
await expect(
page.getByText(
/constraint [a-zA-Z_]+ on table [a-zA-Z_]+ depends on table [a-zA-Z_]+/i,
),
).toBeVisible();
});

42
dashboard/e2e/env.ts Normal file
View File

@@ -0,0 +1,42 @@
import slugify from 'slugify';
/**
* URL of the dashboard to test against.
*/
export const TEST_DASHBOARD_URL = process.env.NHOST_TEST_DASHBOARD_URL;
/**
* Name of the workspace to test against.
*/
export const TEST_WORKSPACE_NAME = process.env.NHOST_TEST_WORKSPACE_NAME;
/**
* Slugified name of the workspace to test against.
*/
export const TEST_WORKSPACE_SLUG = slugify(TEST_WORKSPACE_NAME, {
lower: true,
strict: true,
});
/**
* Name of the project to test against.
*/
export const TEST_PROJECT_NAME = process.env.NHOST_TEST_PROJECT_NAME;
/**
* Slugified name of the project to test against.
*/
export const TEST_PROJECT_SLUG = slugify(TEST_PROJECT_NAME, {
lower: true,
strict: true,
});
/**
* Email of the test account to use.
*/
export const TEST_USER_EMAIL = process.env.NHOST_TEST_USER_EMAIL;
/**
* Password of the test account to use.
*/
export const TEST_USER_PASSWORD = process.env.NHOST_TEST_USER_PASSWORD;

View File

@@ -0,0 +1,109 @@
import {
TEST_PROJECT_NAME,
TEST_PROJECT_SLUG,
TEST_WORKSPACE_NAME,
TEST_WORKSPACE_SLUG,
} from '@/e2e/env';
import { openProject } from '@/e2e/utils';
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
let page: Page;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
await page.goto('/');
await openProject({
page,
projectName: TEST_PROJECT_NAME,
workspaceSlug: TEST_WORKSPACE_SLUG,
projectSlug: TEST_PROJECT_SLUG,
});
});
test.afterAll(async () => {
await page.close();
});
test('should show a sidebar with menu items', async () => {
const navLocator = page.getByRole('navigation', { name: /main navigation/i });
await expect(navLocator).toBeVisible();
await expect(navLocator.getByRole('list').getByRole('listitem')).toHaveCount(
10,
);
await expect(
navLocator.getByRole('link', { name: /overview/i }),
).toBeVisible();
await expect(
navLocator.getByRole('link', { name: /database/i }),
).toBeVisible();
await expect(
navLocator.getByRole('link', { name: /graphql/i }),
).toBeVisible();
await expect(navLocator.getByRole('link', { name: /hasura/i })).toBeVisible();
await expect(navLocator.getByRole('link', { name: /auth/i })).toBeVisible();
await expect(
navLocator.getByRole('link', { name: /storage/i }),
).toBeVisible();
await expect(
navLocator.getByRole('link', { name: /deployments/i }),
).toBeVisible();
await expect(
navLocator.getByRole('link', { name: /backups/i }),
).toBeVisible();
await expect(navLocator.getByRole('link', { name: /logs/i })).toBeVisible();
await expect(
navLocator.getByRole('link', { name: /settings/i }),
).toBeVisible();
});
test('should show a header with a logo, the workspace name, and the project name', async () => {
await expect(
page.getByRole('banner').getByRole('link', { name: TEST_WORKSPACE_NAME }),
).toBeVisible();
await expect(
page.getByRole('banner').getByRole('link', { name: TEST_PROJECT_NAME }),
).toBeVisible();
});
test("should show the project's name, the Upgrade button and the Settings button", async () => {
await expect(
page.getByRole('heading', { name: TEST_PROJECT_NAME }),
).toBeVisible();
await expect(page.getByText(/free plan/i)).toBeVisible();
await expect(page.getByRole('button', { name: /upgrade/i })).toBeVisible();
await expect(
page.getByRole('main').getByRole('link', { name: /settings/i }),
).toBeVisible();
});
test("should show the project's region and subdomain", async () => {
await expect(page.locator('p:has-text("Region") + div p').nth(0)).toHaveText(
/frankfurt \(eu-central-1\)/i,
);
await expect(
page.locator('p:has-text("Subdomain") + div p').nth(0),
).toHaveText(/[a-z]{20}/i);
});
test('should not have a GitHub repository connected', async () => {
await expect(
page.getByRole('button', { name: /connect to github/i }),
).toBeVisible();
});
test('should show proper limits for the free project', async () => {
// Limit for Database
await expect(page.getByText(/of 500 MB/i)).toBeVisible();
// Limit for Storage
await expect(page.getByText(/of 1 GB/i)).toBeVisible();
// Limit for Users
await expect(page.getByText(/of 10000/i)).toBeVisible();
// Limit for Functions
await expect(page.getByText(/of 10$/i, { exact: true })).toBeVisible();
});

113
dashboard/e2e/utils.ts Normal file
View File

@@ -0,0 +1,113 @@
import type { Page } from '@playwright/test';
/**
* Open a project by navigating to the project's overview page.
*
* @param page - The Playwright page object.
* @param workspaceSlug - The slug of the workspace that contains the project.
* @param projectSlug - The slug of the project to open.
* @param projectName - The name of the project to open.
* @returns A promise that resolves when the project is opened.
*/
export async function openProject({
page,
projectName,
workspaceSlug,
projectSlug,
}: {
page: Page;
workspaceSlug: string;
projectSlug: string;
projectName: string;
}) {
await page.getByRole('link', { name: projectName }).click();
await page.waitForURL(`/${workspaceSlug}/${projectSlug}`);
}
/**
* Prepares a table by filling out the form.
*
* @param page - The Playwright page object.
* @param name - The name of the table to create.
* @param columns - The columns to create in the table.
* @returns A promise that resolves when the table is prepared.
*/
export async function prepareTable({
page,
name: tableName,
primaryKey,
columns,
}: {
page: Page;
name: string;
primaryKey: string;
columns: Array<{
name: string;
type: string;
nullable?: boolean;
unique?: boolean;
defaultValue?: string;
}>;
}) {
if (!columns.some(({ name }) => name === primaryKey)) {
throw new Error('Primary key must be one of the columns.');
}
await page.getByRole('textbox', { name: /name/i }).first().fill(tableName);
await Promise.all(
columns.map(
async (
{ name: columnName, type, nullable, unique, defaultValue },
index,
) => {
// set name
await page.getByPlaceholder(/name/i).nth(index).fill(columnName);
// set type
await page
.getByRole('combobox', { name: /type/i })
.nth(index)
.fill(type);
await page.getByRole('option', { name: type }).first().click();
// optionally set default value
if (defaultValue) {
await page
.getByRole('combobox', { name: /default value/i })
.first()
.fill(defaultValue);
await page
.getByRole('option', { name: defaultValue })
.first()
.click();
}
// optionally check unique
if (unique) {
await page
.getByRole('checkbox', { name: /unique/i })
.nth(index)
.check();
}
// optionally check nullable
if (nullable) {
await page
.getByRole('checkbox', { name: /nullable/i })
.nth(index)
.check();
}
// add new column if not last
if (index < columns.length - 1) {
await page.getByRole('button', { name: /add column/i }).click();
}
},
),
);
// select the first column as primary key
await page.getByRole('button', { name: /primary key/i }).click();
await page.getByRole('option', { name: primaryKey, exact: true }).click();
}

27
dashboard/global-setup.ts Normal file
View File

@@ -0,0 +1,27 @@
import { chromium } from '@playwright/test';
import {
TEST_DASHBOARD_URL,
TEST_USER_EMAIL,
TEST_USER_PASSWORD,
} from './e2e/env';
async function globalSetup() {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(TEST_DASHBOARD_URL);
await page.waitForURL(`${TEST_DASHBOARD_URL}/signin`);
await page.getByRole('link', { name: /continue with email/i }).click();
await page.waitForURL(`${TEST_DASHBOARD_URL}/signin/email`);
await page.getByLabel('Email').fill(TEST_USER_EMAIL);
await page.getByLabel('Password').fill(TEST_USER_PASSWORD);
await page.getByRole('button', { name: /sign in/i }).click();
await page.waitForURL(TEST_DASHBOARD_URL);
await page.context().storageState({ path: 'storageState.json' });
await browser.close();
}
export default globalSetup;

View File

@@ -1,5 +1,5 @@
schema:
- http://localhost:1337/v1/graphql:
- https://local.graphql.nhost.run/v1:
headers:
x-hasura-admin-secret: nhost-admin-secret
generates:

View File

@@ -1,6 +1,6 @@
{
"name": "@nhost/dashboard",
"version": "0.13.5",
"version": "0.13.10",
"private": true,
"scripts": {
"preinstall": "npx only-allow pnpm",
@@ -14,10 +14,11 @@
"nhost:dev": "nhost dev -d",
"format": "prettier --write \"src/**/*.{js,ts,tsx,jsx,json,md}\" --plugin-search-dir=.",
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook"
"build-storybook": "build-storybook",
"e2e": "npx playwright@1.31.2 install --with-deps && playwright test"
},
"dependencies": {
"@apollo/client": "^3.7.3",
"@apollo/client": "^3.7.10",
"@codemirror/language": "^6.3.0",
"@emotion/cache": "^11.10.5",
"@emotion/react": "^11.10.5",
@@ -29,7 +30,7 @@
"@graphiql/toolkit": "^0.8.2",
"@headlessui/react": "^1.6.5",
"@heroicons/react": "^1.0.6",
"@hookform/resolvers": "^2.9.10",
"@hookform/resolvers": "^3.0.0",
"@mui/base": "^5.0.0-alpha.106",
"@mui/material": "^5.10.14",
"@mui/system": "^5.10.14",
@@ -62,7 +63,7 @@
"prettysize": "^2.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-error-boundary": "^3.1.4",
"react-error-boundary": "^4.0.0",
"react-hook-form": "^7.42.1",
"react-hot-toast": "^2.4.0",
"react-is": "18.2.0",
@@ -70,23 +71,25 @@
"react-merge-refs": "^1.1.0",
"react-syntax-highlighter": "^15.4.5",
"react-table": "^7.8.0",
"sharp": "^0.31.2",
"sharp": "^0.32.0",
"slugify": "^1.6.5",
"stripe": "^10.17.0",
"tailwind-merge": "^1.8.0",
"utility-types": "^3.10.0",
"validator": "^13.7.0",
"yup": "^0.32.11",
"yup": "^1.0.2",
"yup-password": "^0.2.2"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@faker-js/faker": "^7.6.0",
"@graphql-codegen/cli": "^3.0.0",
"@graphql-codegen/typescript": "^3.0.0",
"@graphql-codegen/typescript-graphql-request": "^4.5.1",
"@graphql-codegen/typescript-operations": "^3.0.0",
"@graphql-codegen/typescript-react-apollo": "^3.3.1",
"@next/bundle-analyzer": "^12.3.1",
"@playwright/test": "^1.31.2",
"@storybook/addon-actions": "^6.5.14",
"@storybook/addon-essentials": "^6.5.14",
"@storybook/addon-interactions": "^6.5.14",
@@ -116,6 +119,7 @@
"babel-loader": "^8.3.0",
"babel-plugin-transform-remove-console": "^6.9.4",
"csstype": "^3.0.10",
"dotenv": "^16.0.3",
"encoding": "^0.1.13",
"eslint": "^8.28.0",
"eslint-config-airbnb": "19.0.4",
@@ -161,4 +165,4 @@
"msw": {
"workerDirectory": "public"
}
}
}

View File

@@ -0,0 +1,32 @@
import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config({ path: path.resolve(__dirname, '.env.test') });
export default defineConfig({
testDir: './e2e',
timeout: 30 * 1000,
expect: {
timeout: 5000,
},
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: 'html',
globalSetup: require.resolve('./global-setup'),
use: {
actionTimeout: 0,
trace: 'on-first-retry',
storageState: 'storageState.json',
baseURL: process.env.NHOST_TEST_DASHBOARD_URL,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
});

View File

@@ -9,28 +9,39 @@ import Link from '@/ui/v2/Link';
import Text from '@/ui/v2/Text';
import { copy } from '@/utils/copy';
import { getApplicationStatusString } from '@/utils/helpers';
import { triggerToast } from '@/utils/toast';
import getServerError from '@/utils/settings/getServerError';
import { formatDistance } from 'date-fns';
import { useRouter } from 'next/router';
import { toast } from 'react-hot-toast';
export default function ApplicationInfo() {
const { currentApplication } = useCurrentWorkspaceAndApplication();
const [deleteApplication, { client }] = useDeleteApplicationMutation({
const [deleteApplication] = useDeleteApplicationMutation({
refetchQueries: [GetOneUserDocument],
});
const router = useRouter();
async function handleClickRemove() {
await deleteApplication({
variables: {
appId: currentApplication.id,
},
});
await router.push('/');
await client.refetchQueries({
include: ['getOneUser'],
});
triggerToast(`${currentApplication.name} deleted`);
try {
await toast.promise(
deleteApplication({
variables: {
appId: currentApplication.id,
},
}),
{
loading: 'Deleting project...',
success: 'The project has been deleted successfully.',
error: getServerError(
'An error occurred while deleting the project. Please try again.',
),
},
);
await router.push('/');
} catch {
// Note: The toast will handle the error.
}
}
return (

View File

@@ -3,54 +3,81 @@ import { ChangePlanModal } from '@/components/applications/ChangePlanModal';
import { StagingMetadata } from '@/components/applications/StagingMetadata';
import { useDialog } from '@/components/common/DialogProvider';
import Container from '@/components/layout/Container';
import { useUpdateApplicationMutation } from '@/generated/graphql';
import {
GetOneUserDocument,
useGetFreeAndActiveProjectsQuery,
useUnpauseApplicationMutation,
} from '@/generated/graphql';
import { useCurrentWorkspaceAndApplication } from '@/hooks/useCurrentWorkspaceAndApplication';
import { ApplicationStatus } from '@/types/application';
import { Modal } from '@/ui';
import { Alert } from '@/ui/Alert';
import ActivityIndicator from '@/ui/v2/ActivityIndicator';
import Box from '@/ui/v2/Box';
import Button from '@/ui/v2/Button';
import Text from '@/ui/v2/Text';
import { discordAnnounce } from '@/utils/discordAnnounce';
import { triggerToast } from '@/utils/toast';
import { updateOwnCache } from '@/utils/updateOwnCache';
import { MAX_FREE_PROJECTS } from '@/utils/CONSTANTS';
import { getToastStyleProps } from '@/utils/settings/settingsConstants';
import type { ApolloError } from '@apollo/client';
import { useUserData } from '@nhost/nextjs';
import Image from 'next/image';
import { useState } from 'react';
import { toast } from 'react-hot-toast';
import { RemoveApplicationModal } from './RemoveApplicationModal';
export default function ApplicationPaused() {
const { openAlertDialog } = useDialog();
const { currentWorkspace, currentApplication } =
useCurrentWorkspaceAndApplication();
const [changingApplicationStateLoading, setChangingApplicationStateLoading] =
useState(false);
const [updateApplication, { client }] = useUpdateApplicationMutation();
const { id, email } = useUserData();
const { id } = useUserData();
const isOwner = currentWorkspace.members.some(
({ userId, type }) => userId === id && type === 'owner',
);
const isPro = currentApplication.plan.name === 'Pro';
const [showDeletingModal, setShowDeletingModal] = useState(false);
const [unpauseApplication, { loading: changingApplicationStateLoading }] =
useUnpauseApplicationMutation({
refetchQueries: [GetOneUserDocument],
});
const { data, loading } = useGetFreeAndActiveProjectsQuery({
variables: { userId: id },
fetchPolicy: 'cache-and-network',
});
const numberOfFreeAndLiveProjects = data?.freeAndActiveProjects.length || 0;
const wakeUpDisabled = numberOfFreeAndLiveProjects >= MAX_FREE_PROJECTS;
async function handleTriggerUnpausing() {
setChangingApplicationStateLoading(true);
try {
await updateApplication({
variables: {
appId: currentApplication.id,
app: {
desiredState: ApplicationStatus.Live,
await toast.promise(
unpauseApplication({ variables: { appId: currentApplication.id } }),
{
loading: 'Starting the project...',
success: `The project has been started successfully.`,
error: (arg: ApolloError) => {
// we need to get the internal error message from the GraphQL error
const { internal } = arg.graphQLErrors[0]?.extensions || {};
const { message } = (internal as Record<string, any>)?.error || {};
// we use the default Apollo error message if we can't find the
// internal error message
return (
message ||
arg.message ||
'An error occurred while waking up the project. Please try again.'
);
},
},
});
await updateOwnCache(client);
discordAnnounce(
`App ${currentApplication.name} (${email}) set to awake.`,
getToastStyleProps(),
);
triggerToast(`${currentApplication.name} set to awake.`);
} catch (e) {
triggerToast(`Error trying to awake ${currentApplication.name}`);
} catch {
// Note: The toast will handle the error.
}
}
if (loading) {
return <ActivityIndicator label="Loading user data..." delay={1000} />;
}
return (
<>
<Modal
@@ -65,7 +92,7 @@ export default function ApplicationPaused() {
/>
</Modal>
<Container className="mx-auto mt-20 grid max-w-sm grid-flow-row gap-2 text-center">
<Container className="mx-auto mt-20 grid max-w-lg grid-flow-row gap-4 text-center">
<div className="mx-auto flex w-centImage flex-col text-center">
<Image
src="/assets/PausedApp.svg"
@@ -75,16 +102,18 @@ export default function ApplicationPaused() {
/>
</div>
<Text variant="h3" component="h1" className="mt-4">
{currentApplication.name} is sleeping
</Text>
<Box className="grid grid-flow-row gap-1">
<Text variant="h3" component="h1">
{currentApplication.name} is sleeping
</Text>
<Text className="mt-1">
Projects on the free plan stop responding to API calls after 7 days of
no traffic.
</Text>
<Text>
Starter projects stop responding to API calls after 7 days of
inactivity. Upgrade to Pro to avoid autosleep.
</Text>
</Box>
{!isPro && (
<Box className="grid grid-flow-row gap-2">
<Button
className="mx-auto w-full max-w-[280px]"
onClick={() => {
@@ -101,32 +130,41 @@ export default function ApplicationPaused() {
});
}}
>
Upgrade to Pro to avoid autosleep
</Button>
)}
<div className="grid grid-flow-row gap-2">
<Button
variant="borderless"
className="mx-auto w-full max-w-[280px]"
loading={changingApplicationStateLoading}
disabled={changingApplicationStateLoading}
onClick={handleTriggerUnpausing}
>
Wake Up
Upgrade to Pro
</Button>
{isOwner && (
<div className="grid grid-flow-row gap-2">
<Button
color="error"
variant="borderless"
className="mx-auto w-full max-w-[280px]"
onClick={() => setShowDeletingModal(true)}
loading={changingApplicationStateLoading}
disabled={changingApplicationStateLoading || wakeUpDisabled}
onClick={handleTriggerUnpausing}
>
Delete Project
Wake Up
</Button>
)}
</div>
{wakeUpDisabled && (
<Alert severity="warning" className="mx-auto max-w-xs text-left">
Note: Only one free project can be active at any given time.
Please pause your active free project before unpausing{' '}
{currentApplication.name}.
</Alert>
)}
{isOwner && (
<Button
color="error"
variant="borderless"
className="mx-auto w-full max-w-[280px]"
onClick={() => setShowDeletingModal(true)}
>
Delete Project
</Button>
)}
</div>
</Box>
<StagingMetadata>
<ApplicationInfo />
</StagingMetadata>

View File

@@ -6,7 +6,10 @@ import Divider from '@/ui/v2/Divider';
import Text from '@/ui/v2/Text';
import { discordAnnounce } from '@/utils/discordAnnounce';
import { triggerToast } from '@/utils/toast';
import { useDeleteApplicationMutation } from '@/utils/__generated__/graphql';
import {
GetOneUserDocument,
useDeleteApplicationMutation,
} from '@/utils/__generated__/graphql';
import router from 'next/router';
import { useState } from 'react';
import { twMerge } from 'tailwind-merge';
@@ -42,7 +45,9 @@ export function RemoveApplicationModal({
description,
className,
}: RemoveApplicationModalProps) {
const [deleteApplication, { client }] = useDeleteApplicationMutation();
const [deleteApplication] = useDeleteApplicationMutation({
refetchQueries: [GetOneUserDocument],
});
const [loadingRemove, setLoadingRemove] = useState(false);
const { currentApplication } = useCurrentWorkspaceAndApplication();
@@ -73,9 +78,6 @@ export function RemoveApplicationModal({
}
close();
await router.push('/');
await client.refetchQueries({
include: ['getOneUser'],
});
triggerToast(`${currentApplication.name} deleted`);
}

View File

@@ -111,9 +111,8 @@ export function RenderWorkspacesWithApps({
)}
<StateBadge
status={checkStatusOfTheApplication(
app.appStates,
)}
state={checkStatusOfTheApplication(app.appStates)}
desiredState={app.desiredState}
title={getApplicationStatusString(
checkStatusOfTheApplication(app.appStates),
)}

View File

@@ -6,7 +6,7 @@ import type { PropsWithChildren } from 'react';
export function StagingMetadata({ children }: PropsWithChildren<unknown>) {
return (
isDevOrStaging() && (
<div className="mt-10">
<div className="mx-auto mt-10 max-w-sm">
<Box className="mx-auto flex flex-col rounded-md border p-5 text-center">
<Status status={StatusEnum.Deploying}>Internal info</Status>
{children}

View File

@@ -76,7 +76,8 @@ function AddPaymentMethodForm({
if (createPaymentMethodError) {
throw new Error(
createPaymentMethodError.message || 'Unknown error occurred.',
createPaymentMethodError.message ||
'An unknown error occurred. Please try again.',
);
}
@@ -90,7 +91,10 @@ function AddPaymentMethodForm({
);
if (attachPaymentMethodError) {
throw Error((attachPaymentMethodError as any).response.data);
throw new Error(
(attachPaymentMethodError as any)?.response?.data ||
'An unknown error occurred. Please try again.',
);
}
// update workspace with new country code in database
@@ -151,7 +155,7 @@ function AddPaymentMethodForm({
};
return (
<Box className="w-modal2 px-6 pt-6 pb-6 text-left rounded-lg">
<Box className="w-modal2 rounded-lg px-6 pt-6 pb-6 text-left">
<div className="flex flex-col">
<form onSubmit={handleSubmit}>
<Text className="text-center text-lg font-medium">
@@ -203,7 +207,7 @@ function AddPaymentMethodForm({
type BillingPaymentMethodFormProps = {
close: () => void;
onPaymentMethodAdded?: () => Promise<void>;
onPaymentMethodAdded?: (e?: any) => Promise<void>;
workspaceId: string;
};

View File

@@ -39,17 +39,17 @@ const ruleGroupSchema = Yup.object().shape({
const baseValidationSchema = Yup.object().shape({
filter: ruleGroupSchema.nullable().required('Please select a filter type.'),
columns: Yup.array().of(Yup.string()).nullable(true),
columns: Yup.array().of(Yup.string()).nullable(),
});
const selectValidationSchema = baseValidationSchema.shape({
limit: Yup.number()
.label('Limit')
.min(0, 'Limit must not be negative.')
.nullable(true),
allowAggregations: Yup.boolean().nullable(true),
queryRootFields: Yup.array().of(Yup.string()).nullable(true),
subscriptionRootFields: Yup.array().of(Yup.string()).nullable(true),
.nullable(),
allowAggregations: Yup.boolean().nullable(),
queryRootFields: Yup.array().of(Yup.string()).nullable(),
subscriptionRootFields: Yup.array().of(Yup.string()).nullable(),
});
const columnPresetSchema = Yup.object().shape({
@@ -88,17 +88,17 @@ const columnPresetSchema = Yup.object().shape({
});
const insertValidationSchema = baseValidationSchema.shape({
backendOnly: Yup.boolean().nullable(true),
columnPresets: Yup.array().of(columnPresetSchema).nullable(true),
backendOnly: Yup.boolean().nullable(),
columnPresets: Yup.array().of(columnPresetSchema).nullable(),
});
const updateValidationSchema = baseValidationSchema.shape({
backendOnly: Yup.boolean().nullable(true),
columnPresets: Yup.array().of(columnPresetSchema).nullable(true),
backendOnly: Yup.boolean().nullable(),
columnPresets: Yup.array().of(columnPresetSchema).nullable(),
});
const deleteValidationSchema = baseValidationSchema.shape({
columnPresets: Yup.array().of(columnPresetSchema).nullable(true),
columnPresets: Yup.array().of(columnPresetSchema).nullable(),
});
const validationSchemas: Record<DatabaseAction, Yup.ObjectSchema<any>> = {

View File

@@ -7,7 +7,7 @@ import Link from 'next/link';
export default function Sidebar() {
return (
<div className="grid grid-flow-row gap-8 mt-2 ml-10 w-full md:grid md:w-workspaceSidebar content-start">
<div className="mt-2 grid w-full grid-flow-row content-start gap-8 md:ml-10 md:grid md:w-workspaceSidebar">
<WorkspaceSection />
<Resources />

View File

@@ -2,8 +2,9 @@ import { UserDataProvider } from '@/context/workspace1-context';
import type { Project } from '@/types/application';
import { ApplicationStatus } from '@/types/application';
import type { Workspace } from '@/types/workspace';
import nhostGraphQLLink from '@/utils/msw/mocks/graphql/nhostGraphQLLink';
import { render, screen, waitForElementToBeRemoved } from '@/utils/testUtils';
import { graphql, rest } from 'msw';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { afterAll, beforeAll, vi } from 'vitest';
import OverviewDeployments from '.';
@@ -73,13 +74,11 @@ const mockWorkspace: Workspace = {
applications: [mockApplication],
};
const mockGraphqlLink = graphql.link('http://localhost:1337/v1/graphql');
const server = setupServer(
rest.get('http://localhost:1337/v1/graphql', (req, res, ctx) =>
rest.get('https://local.graphql.nhost.run/v1', (_req, res, ctx) =>
res(ctx.status(200)),
),
mockGraphqlLink.operation(async (req, res, ctx) =>
nhostGraphQLLink.operation(async (_req, res, ctx) =>
res(
ctx.data({
deployments: [],
@@ -143,7 +142,7 @@ test('should render an empty state when GitHub is connected, but there are no de
test('should render a list of deployments', async () => {
server.use(
mockGraphqlLink.operation(async (req, res, ctx) => {
nhostGraphQLLink.operation(async (req, res, ctx) => {
const requestPayload = await req.json();
if (requestPayload.operationName === 'ScheduledOrPendingDeploymentsSub') {
@@ -193,7 +192,7 @@ test('should render a list of deployments', async () => {
test('should disable redeployments if a deployment is already in progress', async () => {
server.use(
mockGraphqlLink.operation(async (req, res, ctx) => {
nhostGraphQLLink.operation(async (req, res, ctx) => {
const requestPayload = await req.json();
if (requestPayload.operationName === 'ScheduledOrPendingDeploymentsSub') {

View File

@@ -24,22 +24,30 @@ import { twMerge } from 'tailwind-merge';
import * as Yup from 'yup';
const validationSchema = Yup.object({
teamId: Yup.string().label('Team ID').when('enabled', {
is: true,
then: Yup.string().required(),
}),
keyId: Yup.string().label('Key ID').when('enabled', {
is: true,
then: Yup.string().required(),
}),
clientId: Yup.string().label('Client ID').when('enabled', {
is: true,
then: Yup.string().required(),
}),
privateKey: Yup.string().label('Private Key').when('enabled', {
is: true,
then: Yup.string().required(),
}),
teamId: Yup.string()
.label('Team ID')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
keyId: Yup.string()
.label('Key ID')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
clientId: Yup.string()
.label('Client ID')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
privateKey: Yup.string()
.label('Private Key')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
enabled: Yup.boolean(),
});

View File

@@ -3,14 +3,18 @@ import { useFormContext } from 'react-hook-form';
import * as Yup from 'yup';
export const baseProviderValidationSchema = Yup.object({
clientId: Yup.string().label('Client ID').when('enabled', {
is: true,
then: Yup.string().required(),
}),
clientSecret: Yup.string().label('Client Secret').when('enabled', {
is: true,
then: Yup.string().required(),
}),
clientId: Yup.string()
.label('Client ID')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
clientSecret: Yup.string()
.label('Client Secret')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
enabled: Yup.bool(),
});

View File

@@ -22,19 +22,23 @@ import { twMerge } from 'tailwind-merge';
import * as Yup from 'yup';
const validationSchema = Yup.object({
accountSid: Yup.string().label('Account SID').when('enabled', {
is: true,
then: Yup.string().required(),
}),
authToken: Yup.string().label('Auth Token').when('enabled', {
is: true,
then: Yup.string().required(),
}),
accountSid: Yup.string()
.label('Account SID')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
authToken: Yup.string()
.label('Auth Token')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
messagingServiceId: Yup.string()
.label('Messaging Service ID')
.when('enabled', {
is: true,
then: Yup.string().required(),
then: (schema) => schema.required(),
}),
enabled: Yup.boolean().label('Enabled'),
});

View File

@@ -23,14 +23,18 @@ import { twMerge } from 'tailwind-merge';
import * as Yup from 'yup';
const validationSchema = Yup.object({
consumerSecret: Yup.string().label('Consumer Secret').when('enabled', {
is: true,
then: Yup.string().required(),
}),
consumerKey: Yup.string().label('Consumer Key').when('enabled', {
is: true,
then: Yup.string().required(),
}),
consumerSecret: Yup.string()
.label('Consumer Secret')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
consumerKey: Yup.string()
.label('Consumer Key')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
enabled: Yup.boolean(),
});

View File

@@ -24,22 +24,30 @@ import { twMerge } from 'tailwind-merge';
import * as Yup from 'yup';
const validationSchema = Yup.object({
clientId: Yup.string().label('Client ID').when('enabled', {
is: true,
then: Yup.string().required(),
}),
clientSecret: Yup.string().label('Client Secret').when('enabled', {
is: true,
then: Yup.string().required(),
}),
organization: Yup.string().label('Organization').when('enabled', {
is: true,
then: Yup.string().required(),
}),
connection: Yup.string().label('Connection').when('enabled', {
is: true,
then: Yup.string().required(),
}),
clientId: Yup.string()
.label('Client ID')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
clientSecret: Yup.string()
.label('Client Secret')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
organization: Yup.string()
.label('Organization')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
connection: Yup.string()
.label('Connection')
.when('enabled', {
is: true,
then: (schema) => schema.required(),
}),
enabled: Yup.boolean(),
});

View File

@@ -5,7 +5,11 @@ export interface StateBadgeProps {
/**
* This is the current state of the application.
*/
status: ApplicationStatus;
state: ApplicationStatus;
/**
* This is the desired state of the application.
*/
desiredState: ApplicationStatus;
/**
* The title to show on the application state badge.
*/
@@ -24,20 +28,28 @@ function getNormalizedTitle(title: string) {
return title;
}
export default function StateBadge({ title, status }: StateBadgeProps) {
export default function StateBadge({
title,
state,
desiredState,
}: StateBadgeProps) {
if (
desiredState === ApplicationStatus.Paused &&
state === ApplicationStatus.Live
) {
return <Chip size="small" color="default" label="Pausing" />;
}
const normalizedTitle = getNormalizedTitle(title);
if (
status === ApplicationStatus.Empty ||
status === ApplicationStatus.Unpausing
state === ApplicationStatus.Empty ||
state === ApplicationStatus.Unpausing
) {
return <Chip size="small" label={normalizedTitle} color="warning" />;
}
if (
status === ApplicationStatus.Errored ||
status === ApplicationStatus.Live
) {
if (state === ApplicationStatus.Errored || state === ApplicationStatus.Live) {
return <Chip size="small" label={normalizedTitle} color="success" />;
}

View File

@@ -6,7 +6,7 @@ import SvgIcon from '@/ui/v2/icons/SvgIcon';
import { styled } from '@mui/material';
import type { RadioProps as MaterialRadioProps } from '@mui/material/Radio';
import MaterialRadio from '@mui/material/Radio';
import type { ForwardedRef, PropsWithoutRef } from 'react';
import type { ForwardedRef, PropsWithoutRef, ReactNode } from 'react';
import { forwardRef } from 'react';
export interface RadioProps extends MaterialRadioProps {
@@ -17,7 +17,7 @@ export interface RadioProps extends MaterialRadioProps {
/**
* Label to be displayed next to the radio button.
*/
label?: string;
label?: ReactNode;
/**
* Props to be passed to individual component slots.
*/

View File

@@ -1,7 +1,9 @@
import { styled } from '@mui/material';
import Box from '@mui/material/Box';
import type { TooltipProps as MaterialTooltipProps } from '@mui/material/Tooltip';
import MaterialTooltip, { tooltipClasses } from '@mui/material/Tooltip';
import MaterialTooltip, {
tooltipClasses as materialTooltipClasses,
} from '@mui/material/Tooltip';
import type { ForwardedRef } from 'react';
import { forwardRef } from 'react';
@@ -21,7 +23,7 @@ export interface TooltipProps extends MaterialTooltipProps {
}
const StyledTooltip = styled(Box)(({ theme }) => ({
[`&.${tooltipClasses.tooltip}`]: {
[`&.${materialTooltipClasses.tooltip}`]: {
fontSize: '0.9375rem',
lineHeight: '1.375rem',
backgroundColor:
@@ -36,9 +38,23 @@ const StyledTooltip = styled(Box)(({ theme }) => ({
'0px 1px 4px rgba(14, 24, 39, 0.1), 0px 8px 24px rgba(14, 24, 39, 0.1)',
maxWidth: '17.5rem',
},
[`&.${tooltipClasses.tooltipPlacementBottom}`]: {
[`& .${materialTooltipClasses.arrow}`]: {
color:
theme.palette.mode === 'dark'
? theme.palette.grey[300]
: theme.palette.grey[700],
},
[`&.${materialTooltipClasses.tooltipPlacementBottom}`]: {
marginTop: `${theme.spacing(0.75)} !important`,
},
[`&.${materialTooltipClasses.tooltipPlacementBottom} .${materialTooltipClasses.arrow}`]:
{
marginTop: `${theme.spacing(-0.5)} !important`,
color:
theme.palette.mode === 'dark'
? theme.palette.grey[300]
: theme.palette.grey[700],
},
}));
function Tooltip(
@@ -69,6 +85,8 @@ function Tooltip(
);
}
export { materialTooltipClasses as tooltipClasses };
Tooltip.displayName = 'NhostTooltip';
export default forwardRef(Tooltip);

View File

@@ -165,7 +165,7 @@ export default function CreateUserForm({
</Alert>
)}
<div className="grid grid-flow-row gap-2">
<Button type="submit" loading={isSubmitting} disabled={isSubmitting}>
<Button type="submit" disabled={isSubmitting}>
Create
</Button>

View File

@@ -242,7 +242,11 @@ export default function UsersBody({ users, onSubmit }: UsersBodyProps) {
secondaryAction={
<Dropdown.Root>
<Dropdown.Trigger asChild hideChevron>
<IconButton variant="borderless" color="secondary">
<IconButton
variant="borderless"
color="secondary"
aria-label={`More options for ${user.displayName}`}
>
<DotsHorizontalIcon />
</IconButton>
</Dropdown.Trigger>
@@ -282,6 +286,7 @@ export default function UsersBody({ users, onSubmit }: UsersBodyProps) {
<ListItem.Button
className="grid h-full w-full grid-cols-1 py-2.5 lg:grid-cols-6"
onClick={() => handleViewUser(user)}
aria-label={`View ${user.displayName}`}
>
<div className="col-span-2 grid grid-flow-col place-content-start gap-4">
<Avatar

View File

@@ -0,0 +1,5 @@
mutation PauseApplication($appId: uuid!) {
updateApp(pk_columns: { id: $appId }, _set: { desiredState: 6 }) {
id
}
}

View File

@@ -0,0 +1,5 @@
mutation UnpauseApplication($appId: uuid!) {
updateApp(pk_columns: { id: $appId }, _set: { desiredState: 5 }) {
id
}
}

View File

@@ -0,0 +1,11 @@
query GetFreeAndActiveProjects($userId: uuid!) {
freeAndActiveProjects: apps(
where: {
creatorUserId: { _eq: $userId }
plan: { isFree: { _eq: true } }
desiredState: { _eq: 5 }
}
) {
id
}
}

View File

@@ -11,7 +11,7 @@ export default function useNotFoundRedirect() {
const router = useRouter();
const {
query: { workspaceSlug, appSlug, updating },
} = useRouter();
} = router;
const notIn404Already = router.pathname !== '/404';
const noResolvedWorkspace = workspaceSlug && currentWorkspace === undefined;

View File

@@ -8,7 +8,8 @@ import { useUI } from '@/context/UIContext';
import {
GetOneUserDocument,
useDeleteApplicationMutation,
useUpdateAppMutation,
usePauseApplicationMutation,
useUpdateApplicationMutation,
} from '@/generated/graphql';
import { useCurrentWorkspaceAndApplication } from '@/hooks/useCurrentWorkspaceAndApplication';
import Input from '@/ui/v2/Input';
@@ -37,9 +38,13 @@ export type ProjectNameValidationSchema = Yup.InferType<
export default function SettingsGeneralPage() {
const { currentApplication } = useCurrentWorkspaceAndApplication();
const { openDialog, closeDialog } = useDialog();
const [updateApp] = useUpdateAppMutation();
const { openDialog, openAlertDialog, closeDialog } = useDialog();
const [updateApp] = useUpdateApplicationMutation();
const client = useApolloClient();
const [pauseApplication] = usePauseApplicationMutation({
variables: { appId: currentApplication?.id },
refetchQueries: [GetOneUserDocument],
});
const [deleteApplication] = useDeleteApplicationMutation({
variables: { appId: currentApplication?.id },
refetchQueries: [GetOneUserDocument],
@@ -61,7 +66,7 @@ export default function SettingsGeneralPage() {
const { register, formState } = form;
const handleProjectNameChange = async (data: ProjectNameValidationSchema) => {
async function handleProjectNameChange(data: ProjectNameValidationSchema) {
// In this bit of code we spread the props of the current path (e.g. /workspace/...) and add one key-value pair: `updating: true`.
// We want to indicate that the currently we're in the process of running a mutation state that will affect the routing behaviour of the website
// i.e. redirecting to 404 if there's no workspace/project with that slug.
@@ -83,7 +88,7 @@ export default function SettingsGeneralPage() {
const updateAppMutation = updateApp({
variables: {
id: currentApplication.id,
appId: currentApplication.id,
app: {
name: data.name,
slug: newProjectSlug,
@@ -108,34 +113,50 @@ export default function SettingsGeneralPage() {
}
try {
await client.refetchQueries({
include: ['getOneUser'],
});
form.reset(undefined, { keepValues: true, keepDirty: false });
await router.push(
`/${currentWorkspace.slug}/${newProjectSlug}/settings/general`,
);
await client.refetchQueries({ include: [GetOneUserDocument] });
} catch (error) {
await discordAnnounce(
error.message || 'Error while trying to update application cache',
error.message ||
'An error occurred while trying to update application cache.',
);
}
};
}
const handleDeleteApplication = async () => {
async function handleDeleteApplication() {
await toast.promise(
deleteApplication(),
{
loading: `Deleting ${currentApplication.name}...`,
success: `${currentApplication.name} deleted`,
success: `${currentApplication.name} has been deleted successfully.`,
error: getServerError(
`Error while trying to ${currentApplication.name} project name`,
`An error occurred while trying to delete the project "${currentApplication.name}". Please try again.`,
),
},
getToastStyleProps(),
);
await router.push('/');
};
}
async function handlePauseApplication() {
await toast.promise(
pauseApplication(),
{
loading: `Pausing ${currentApplication.name}...`,
success: `${currentApplication.name} will be paused, but please note that it may take some time to complete the process.`,
error: getServerError(
`An error occurred while trying to pause the project "${currentApplication.name}". Please try again.`,
),
},
getToastStyleProps(),
);
await router.push('/');
}
return (
<Container
@@ -171,6 +192,32 @@ export default function SettingsGeneralPage() {
</Form>
</FormProvider>
{currentApplication.plan.isFree && (
<SettingsContainer
title="Pause Project"
description="While your project is paused, it will not be accessible. You can wake it up anytime after."
submitButtonText="Pause"
slotProps={{
submitButton: {
type: 'button',
color: 'primary',
variant: 'contained',
disabled: maintenanceActive,
onClick: () => {
openAlertDialog({
title: 'Pause Project?',
payload:
'Are you sure you want to pause this project? It will not be accessible until you unpause it.',
props: {
onPrimaryAction: handlePauseApplication,
},
});
},
},
}}
/>
)}
<SettingsContainer
title="Delete Project"
description="The project will be permanently deleted, including its database, metadata, files, etc. This action is irreversible and can not be undone."

View File

@@ -11,23 +11,25 @@ import { Modal } from '@/ui/Modal';
import ActivityIndicator from '@/ui/v2/ActivityIndicator';
import Box from '@/ui/v2/Box';
import Button from '@/ui/v2/Button';
import Checkbox from '@/ui/v2/Checkbox';
import IconButton from '@/ui/v2/IconButton';
import CopyIcon from '@/ui/v2/icons/CopyIcon';
import Input from '@/ui/v2/Input';
import InputAdornment from '@/ui/v2/InputAdornment';
import Option from '@/ui/v2/Option';
import Radio from '@/ui/v2/Radio';
import RadioGroup from '@/ui/v2/RadioGroup';
import Select from '@/ui/v2/Select';
import type { TextProps } from '@/ui/v2/Text';
import Text from '@/ui/v2/Text';
import Tooltip from '@/ui/v2/Tooltip';
import { MAX_FREE_PROJECTS } from '@/utils/CONSTANTS';
import { copy } from '@/utils/copy';
import { discordAnnounce } from '@/utils/discordAnnounce';
import { getErrorMessage } from '@/utils/getErrorMessage';
import { getCurrentEnvironment, slugifyString } from '@/utils/helpers';
import { nhost } from '@/utils/nhost';
import { getCurrentEnvironment } from '@/utils/helpers';
import { planDescriptions } from '@/utils/planDescriptions';
import generateRandomDatabasePassword from '@/utils/settings/generateRandomDatabasePassword';
import { resetDatabasePasswordValidationSchema } from '@/utils/settings/resetDatabasePasswordValidationSchema';
import { getToastStyleProps } from '@/utils/settings/settingsConstants';
import { triggerToast } from '@/utils/toast';
import type {
PrefetchNewAppPlansFragment,
@@ -35,19 +37,25 @@ import type {
PrefetchNewAppWorkspaceFragment,
} from '@/utils/__generated__/graphql';
import {
useGetFreeAndActiveProjectsQuery,
useInsertApplicationMutation,
usePrefetchNewAppQuery,
} from '@/utils/__generated__/graphql';
import type { ApolloError } from '@apollo/client';
import { useUserData } from '@nhost/nextjs';
import Image from 'next/image';
import { useRouter } from 'next/router';
import type { ReactElement } from 'react';
import { cloneElement, isValidElement, useState } from 'react';
import { toast } from 'react-hot-toast';
import slugify from 'slugify';
import { twMerge } from 'tailwind-merge';
type NewAppPageProps = {
regions: PrefetchNewAppRegionsFragment[];
plans: PrefetchNewAppPlansFragment[];
workspaces: PrefetchNewAppWorkspaceFragment[];
numberOfFreeAndLiveProjects: number;
preSelectedWorkspace: PrefetchNewAppWorkspaceFragment;
preSelectedRegion: PrefetchNewAppRegionsFragment;
};
@@ -56,6 +64,7 @@ export function NewProjectPageContent({
regions,
plans,
workspaces,
numberOfFreeAndLiveProjects,
preSelectedWorkspace,
preSelectedRegion,
}: NewAppPageProps) {
@@ -86,15 +95,23 @@ export function NewProjectPageContent({
generateRandomDatabasePassword(),
);
const [plan, setPlan] = useState(plans[0]);
// find the first acceptable plan as default plan
const defaultSelectedPlan = plans.find((plan) => {
if (!plan.isFree) {
return true;
}
return numberOfFreeAndLiveProjects < MAX_FREE_PROJECTS;
});
const [plan, setPlan] = useState(defaultSelectedPlan);
// state
const { submitState, setSubmitState } = useSubmitState();
const [applicationError, setApplicationError] = useState<any>('');
const [showPaymentModal, setShowPaymentModal] = useState(false);
// graphql mutations
const [insertApp] = useInsertApplicationMutation();
const [insertApp] = useInsertApplicationMutation({});
const { refetchUserData } = useLazyRefetchUserData();
// options
@@ -119,8 +136,6 @@ export function NewProjectPageContent({
(availableWorkspace) => availableWorkspace.id === selectedWorkspace.id,
);
const user = nhost.auth.getUser();
const isK8SPostgresEnabledInCurrentEnvironment = features[
'k8s-postgres'
].enabled.find((e) => e === getCurrentEnvironment());
@@ -133,30 +148,24 @@ export function NewProjectPageContent({
setDatabasePassword(newRandomDatabasePassword);
};
const handleSubmit = async () => {
const handleSubmit = async (e) => {
e.preventDefault();
if (!plan.isFree && workspace.paymentMethods.length === 0) {
setShowPaymentModal(true);
return;
}
setSubmitState({
error: null,
loading: true,
});
if (name.length < 1 || name.length > 32) {
setApplicationError(
`The project name must be between 1 and 32 characters`,
);
setSubmitState({
error: null,
error: Error('The project name must be between 1 and 32 characters'),
loading: false,
});
}
const slug = slugifyString(name);
if (slug.length < 1 || slug.length > 32) {
setSubmitState({
error: Error('The project slug must be between 1 and 32 characters.'),
loading: false,
});
return;
}
@@ -173,14 +182,11 @@ export function NewProjectPageContent({
}
}
// NOTE: Maybe we'll reintroduce this way of creating the subdomain in the future
// https://www.rfc-editor.org/rfc/rfc1034#section-3.1
// subdomain max length is 63 characters
// const subdomain = `${slug}-${workspaceSlug}`.substring(0, 63);
const slug = slugify(name, { lower: true, strict: true });
try {
if (isK8SPostgresEnabledInCurrentEnvironment) {
await insertApp({
await toast.promise(
insertApp({
variables: {
app: {
name,
@@ -188,37 +194,40 @@ export function NewProjectPageContent({
planId: plan.id,
workspaceId: selectedWorkspace.id,
regionId: selectedRegion.id,
postgresPassword: databasePassword,
postgresPassword: isK8SPostgresEnabledInCurrentEnvironment
? databasePassword
: undefined,
},
},
});
} else {
await insertApp({
variables: {
app: {
name,
slug,
planId: plan.id,
workspaceId: selectedWorkspace.id,
regionId: selectedRegion.id,
},
},
});
}
}),
{
loading: 'Creating the project...',
success: 'The project has been created successfully.',
error: (arg: ApolloError) => {
// we need to get the internal error message from the GraphQL error
const { internal } = arg.graphQLErrors[0]?.extensions || {};
const { message } = (internal as Record<string, any>)?.error || {};
triggerToast(`New project ${name} created`);
} catch (error) {
discordAnnounce(
`Error creating project: ${error.message}. (${user.email})`,
// we use the default Apollo error message if we can't find the
// internal error message
return (
message ||
arg.message ||
'An error occurred while creating the project. Please try again.'
);
},
},
getToastStyleProps(),
);
await refetchUserData();
await router.push(`/${selectedWorkspace.slug}/${slug}`);
} catch (error) {
setSubmitState({
error: Error(getErrorMessage(error, 'application')),
error: null,
loading: false,
});
}
await refetchUserData();
router.push(`/${selectedWorkspace.slug}/${slug}`);
};
if (!selectedWorkspace) {
@@ -243,383 +252,376 @@ export function NewProjectPageContent({
return (
<Container>
<div className="mx-auto grid max-w-[760px] grid-flow-row gap-4 py-6 sm:py-14">
<Text variant="h2" component="h1">
New Project
</Text>
<form onSubmit={handleSubmit}>
<div className="mx-auto grid max-w-[760px] grid-flow-row gap-4 py-6 sm:py-14">
<Text variant="h2" component="h1">
New Project
</Text>
<div className="grid grid-flow-row gap-4">
<Input
id="name"
autoComplete="off"
label="Project Name"
variant="inline"
fullWidth
hideEmptyHelperText
placeholder="Project Name"
onChange={(event) => {
setSubmitState({
error: null,
loading: false,
});
setApplicationError('');
setName(event.target.value);
}}
value={name}
autoFocus
/>
<Select
id="workspace"
label="Workspace"
variant="inline"
hideEmptyHelperText
placeholder="Select Workspace"
slotProps={{
root: { className: 'grid grid-flow-col gap-1' },
}}
onChange={(_event, value) => {
const workspaceInList = workspaces.find(({ id }) => id === value);
setPlan(plans[0]);
setSelectedWorkspace({
id: workspaceInList.id,
name: workspaceInList.name,
disabled: false,
slug: workspaceInList.slug,
});
}}
value={selectedWorkspace.id}
renderValue={(option) => (
<span className="inline-grid grid-flow-col items-center gap-2">
{option?.label}
</span>
)}
>
{workspaceOptions.map((option) => (
<Option
value={option.id}
key={option.id}
className="grid grid-flow-col items-center gap-2"
>
<span className="inline-block h-6 w-6 overflow-hidden rounded-md">
<Image
src="/logos/new.svg"
alt="Nhost Logo"
width={24}
height={24}
/>
</span>
{option.name}
</Option>
))}
</Select>
{isK8SPostgresEnabledInCurrentEnvironment && (
<div className="grid grid-flow-row gap-4">
<Input
name="databasePassword"
id="databasePassword"
autoComplete="new-password"
label="Database Password"
value={databasePassword}
id="name"
autoComplete="off"
label="Project Name"
variant="inline"
type="password"
error={!!passwordError}
fullWidth
hideEmptyHelperText
endAdornment={
<InputAdornment position="end" className="mr-2">
<IconButton
color="secondary"
onClick={() => {
copy(databasePassword, 'Postgres password');
}}
variant="borderless"
aria-label="Copy password"
>
<CopyIcon className="h-4 w-4" />
</IconButton>
</InputAdornment>
}
slotProps={{
// Note: this is supposed to fix a `validateDOMNesting` error
helperText: { component: 'div' },
}}
helperText={
<div className="grid max-w-xs grid-flow-row gap-2">
{passwordError && (
<Text
variant="subtitle2"
sx={{
color: (theme) =>
`${theme.palette.error.main} !important`,
}}
>
{passwordError}
</Text>
)}
<Box className="font-medium">
The root Postgres password for your database - it must be
strong and hard to guess.{' '}
<Button
type="button"
variant="borderless"
color="secondary"
onClick={handleGenerateRandomPassword}
className="px-1 py-0.5 text-xs underline underline-offset-2 hover:underline"
tabIndex={-1}
>
Generate a password
</Button>
</Box>
</div>
}
onChange={async (e) => {
e.preventDefault();
placeholder="Project Name"
onChange={(event) => {
setSubmitState({
error: null,
loading: false,
});
if (e.target.value.length === 0) {
setDatabasePassword(e.target.value);
setPasswordError('Please enter a password');
return;
}
setDatabasePassword(e.target.value);
setPasswordError('');
try {
await resetDatabasePasswordValidationSchema.validate({
databasePassword: e.target.value,
});
setPasswordError('');
} catch (validationError) {
setPasswordError(validationError.message);
}
setName(event.target.value);
}}
fullWidth
value={name}
autoFocus
/>
)}
<Select
id="region"
label="Region"
variant="inline"
hideEmptyHelperText
placeholder="Select Region"
slotProps={{
root: { className: 'grid grid-flow-col gap-1' },
}}
onChange={(_event, value) => {
const regionInList = regions.find(({ id }) => id === value);
setPlan(plans[0]);
setSelectedRegion({
id: regionInList.id,
name: regionInList.country.name,
disabled: false,
code: regionInList.country.code,
});
}}
value={selectedRegion.id}
renderValue={(option) => {
const [flag, , country] = (option?.label as any[]) || [];
return (
<span className="inline-grid grid-flow-col grid-rows-none items-center gap-x-2">
{flag}
{isValidElement<TextProps>(country)
? cloneElement(country, {
...country.props,
variant: 'body1',
})
: null}
</span>
);
}}
>
{regionOptions.map((option) => (
<Option
value={option.id}
key={option.id}
className={twMerge(
'relative grid grid-flow-col grid-rows-2 items-center justify-start gap-x-3',
option.disabled && 'pointer-events-none opacity-50',
)}
disabled={option.disabled}
>
<span className="row-span-2 flex">
<Image
src={`/assets/flags/${option.code}.svg`}
alt={`${option.country} country flag`}
width={16}
height={12}
/>
<Select
id="workspace"
label="Workspace"
variant="inline"
hideEmptyHelperText
placeholder="Select Workspace"
slotProps={{
root: { className: 'grid grid-flow-col gap-1' },
}}
onChange={(_event, value) => {
const workspaceInList = workspaces.find(
({ id }) => id === value,
);
setPlan(plans[0]);
setSelectedWorkspace({
id: workspaceInList.id,
name: workspaceInList.name,
disabled: false,
slug: workspaceInList.slug,
});
}}
value={selectedWorkspace.id}
renderValue={(option) => (
<span className="inline-grid grid-flow-col items-center gap-2">
{option?.label}
</span>
)}
>
{workspaceOptions.map((option) => (
<Option
value={option.id}
key={option.id}
className="grid grid-flow-col items-center gap-2"
>
<span className="inline-block h-6 w-6 overflow-hidden rounded-md">
<Image
src="/logos/new.svg"
alt="Nhost Logo"
width={24}
height={24}
/>
</span>
<Text className="row-span-1 font-medium">{option.name}</Text>
{option.name}
</Option>
))}
</Select>
<Text variant="subtitle2" className="row-span-1">
{option.country}
</Text>
{isK8SPostgresEnabledInCurrentEnvironment && (
<Input
name="databasePassword"
id="databasePassword"
autoComplete="new-password"
label="Database Password"
value={databasePassword}
variant="inline"
type="password"
error={!!passwordError}
hideEmptyHelperText
endAdornment={
<InputAdornment position="end" className="mr-2">
<IconButton
color="secondary"
onClick={() => {
copy(databasePassword, 'Postgres password');
}}
variant="borderless"
aria-label="Copy password"
>
<CopyIcon className="h-4 w-4" />
</IconButton>
</InputAdornment>
}
slotProps={{
// Note: this is supposed to fix a `validateDOMNesting` error
helperText: { component: 'div' },
}}
helperText={
<div className="grid max-w-xs grid-flow-row gap-2">
{passwordError && (
<Text
variant="subtitle2"
sx={{
color: (theme) =>
`${theme.palette.error.main} !important`,
}}
>
{passwordError}
</Text>
)}
{option.disabled && (
<Text
variant="subtitle2"
className="absolute top-1/2 right-4 -translate-y-1/2"
>
Disabled
</Text>
)}
</Option>
))}
</Select>
<Box className="font-medium">
The root Postgres password for your database - it must be
strong and hard to guess.{' '}
<Button
type="button"
variant="borderless"
color="secondary"
onClick={handleGenerateRandomPassword}
className="px-1 py-0.5 text-xs underline underline-offset-2 hover:underline"
tabIndex={-1}
>
Generate a password
</Button>
</Box>
</div>
}
onChange={async (e) => {
e.preventDefault();
setSubmitState({
error: null,
loading: false,
});
setDatabasePassword(e.target.value);
<div className="grid w-full grid-cols-8 gap-x-4 gap-y-2">
<div className="col-span-8 sm:col-span-2">
<Text className="text-xs font-medium">Plan</Text>
<Text variant="subtitle2">You can change this later.</Text>
</div>
try {
await resetDatabasePasswordValidationSchema.validate({
databasePassword: e.target.value,
});
setPasswordError('');
} catch (validationError) {
setPasswordError(validationError.message);
}
}}
fullWidth
/>
)}
<div className="col-span-8 sm:col-span-6">
{plans.map((currentPlan) => {
const checked = plan.id === currentPlan.id;
<Select
id="region"
label="Region"
variant="inline"
hideEmptyHelperText
placeholder="Select Region"
slotProps={{
root: { className: 'grid grid-flow-col gap-1' },
}}
onChange={(_event, value) => {
const regionInList = regions.find(({ id }) => id === value);
setSelectedRegion({
id: regionInList.id,
name: regionInList.country.name,
disabled: false,
code: regionInList.country.code,
});
}}
value={selectedRegion.id}
renderValue={(option) => {
const [flag, , country] = (option?.label as any[]) || [];
return (
<Box
className="border-t py-4 last-of-type:border-b"
key={currentPlan.id}
>
<Checkbox
label={
<>
<span className="inline-block max-w-xs">
<span className="font-medium">
{currentPlan.name}:
</span>{' '}
{planDescriptions[currentPlan.name]}
</span>
<span className="inline-grid grid-flow-col grid-rows-none items-center gap-x-2">
{flag}
{currentPlan.isFree ? (
<Text variant="h3" component="span">
Free
</Text>
) : (
<Text
variant="h3"
component="span"
className="inline-grid grid-flow-col items-center gap-1"
>
$ {currentPlan.price}{' '}
<Text variant="subtitle2" component="span">
/ mo
</Text>
</Text>
)}
</>
}
componentsProps={{
formControlLabel: {
className: 'flex',
componentsProps: {
typography: {
className:
'font-regular text-xs grid grid-flow-col justify-between items-center w-full',
},
},
},
}}
checked={checked}
key={currentPlan.id}
onChange={(event, inputChecked) => {
if (!inputChecked) {
event.preventDefault();
return;
}
setPlan(currentPlan);
}}
/>
</Box>
{isValidElement<TextProps>(country)
? cloneElement(country, {
...country.props,
variant: 'body1',
})
: null}
</span>
);
})}
</div>
</div>
</div>
{submitState.error && (
<Alert severity="error" className="text-left">
<Text className="font-medium">Warning</Text>{' '}
<Text className="font-medium">
{submitState.error &&
getErrorMessage(submitState.error, 'application')}
</Text>
</Alert>
)}
<div className="flex justify-end">
{showPaymentModal && (
<Modal
showModal={showPaymentModal}
close={() => {
setShowPaymentModal(false);
}}
>
<BillingPaymentMethodForm
{regionOptions.map((option) => (
<Option
value={option.id}
key={option.id}
className={twMerge(
'relative grid grid-flow-col grid-rows-2 items-center justify-start gap-x-3',
option.disabled && 'pointer-events-none opacity-50',
)}
disabled={option.disabled}
>
<span className="row-span-2 flex">
<Image
src={`/assets/flags/${option.code}.svg`}
alt={`${option.country} country flag`}
width={16}
height={12}
/>
</span>
<Text className="row-span-1 font-medium">{option.name}</Text>
<Text variant="subtitle2" className="row-span-1">
{option.country}
</Text>
{option.disabled && (
<Text
variant="subtitle2"
className="absolute top-1/2 right-4 -translate-y-1/2"
>
Disabled
</Text>
)}
</Option>
))}
</Select>
<div className="grid w-full grid-cols-8 gap-x-4 gap-y-2">
<div className="col-span-8 sm:col-span-2">
<Text className="text-xs font-medium">Plan</Text>
<Text variant="subtitle2">You can change this later.</Text>
</div>
<RadioGroup
value={plan.id}
onChange={(_event, value) => {
setPlan(plans.find((p) => p.id === value));
}}
className="col-span-8 space-y-2 sm:col-span-6"
>
{plans.map((currentPlan) => {
const disabledPlan =
currentPlan.isFree &&
numberOfFreeAndLiveProjects >= MAX_FREE_PROJECTS;
return (
<Tooltip
visible={disabledPlan}
title="Only one free project can be active at any given time. Please pause your active free project before creating a new one."
key={currentPlan.id}
slotProps={{
tooltip: { className: '!max-w-xs w-full text-center' },
}}
>
<Box className="w-full rounded-md border">
<Radio
slotProps={{
formControl: {
className: 'p-3 w-full',
slotProps: {
typography: { className: 'w-full' },
},
},
}}
value={currentPlan.id}
disabled={disabledPlan}
label={
<div className="flex w-full items-center justify-between ">
<div className="inline-block max-w-xs">
<Text className="font-medium text-[inherit]">
{currentPlan.name}
</Text>
<Text className="text-xs text-[inherit]">
{planDescriptions[currentPlan.name]}
</Text>
</div>
{currentPlan.isFree ? (
<Text
variant="h3"
component="span"
className="text-[inherit]"
>
Free
</Text>
) : (
<Text variant="h3" component="span">
${currentPlan.price}/mo
</Text>
)}
</div>
}
/>
</Box>
</Tooltip>
);
})}
</RadioGroup>
</div>
</div>
{submitState.error && (
<Alert severity="error" className="text-left">
<Text className="font-medium">Error</Text>{' '}
<Text className="font-medium">
{submitState.error &&
getErrorMessage(submitState.error, 'application')}{' '}
</Text>
</Alert>
)}
<div className="flex justify-end">
{showPaymentModal && (
<Modal
showModal={showPaymentModal}
close={() => {
setShowPaymentModal(false);
}}
onPaymentMethodAdded={handleSubmit}
workspaceId={workspace.id}
/>
</Modal>
)}
>
<BillingPaymentMethodForm
close={() => {
setShowPaymentModal(false);
}}
onPaymentMethodAdded={handleSubmit}
workspaceId={workspace.id}
/>
</Modal>
)}
<Button
onClick={() => {
if (!plan.isFree && workspace.paymentMethods.length === 0) {
setShowPaymentModal(true);
return;
}
handleSubmit();
}}
type="submit"
loading={submitState.loading}
disabled={
!!applicationError ||
!!submitState.error ||
!!passwordError ||
maintenanceActive
}
id="create-app"
>
Create Project
</Button>
<Button
type="submit"
loading={submitState.loading}
disabled={!!passwordError || maintenanceActive}
id="create-app"
>
Create Project
</Button>
</div>
</div>
</div>
</form>
</Container>
);
}
export default function NewProjectPage() {
const { data, loading, error } = usePrefetchNewAppQuery();
const router = useRouter();
const user = useUserData();
if (error) {
throw error;
const { data, loading, error } = usePrefetchNewAppQuery();
const {
data: freeAndActiveProjectsData,
loading: freeAndActiveProjectsLoading,
error: freeAndActiveProjectsError,
} = useGetFreeAndActiveProjectsQuery({
variables: { userId: user?.id },
fetchPolicy: 'cache-and-network',
});
if (error || freeAndActiveProjectsError) {
throw error || freeAndActiveProjectsError;
}
if (loading) {
if (loading || freeAndActiveProjectsLoading) {
return (
<ActivityIndicator delay={500} label="Loading plans and regions..." />
);
}
const { workspace } = router.query;
const { regions, plans, workspaces } = data;
// get pre-selected workspace
@@ -628,13 +630,16 @@ export default function NewProjectPage() {
? workspaces.find((w) => w.slug === workspace)
: workspaces[0];
const preSelectedRegion = regions.filter((region) => region.active)[0];
const preSelectedRegion = regions.find((region) => region.active);
return (
<NewProjectPageContent
regions={regions}
plans={plans}
workspaces={workspaces}
numberOfFreeAndLiveProjects={
freeAndActiveProjectsData?.freeAndActiveProjects.length
}
preSelectedWorkspace={preSelectedWorkspace}
preSelectedRegion={preSelectedRegion}
/>

View File

@@ -22,3 +22,8 @@ export const READ_ONLY_SCHEMAS = ['auth', 'storage'];
* Key used to store the color preference in local storage.
*/
export const COLOR_PREFERENCE_STORAGE_KEY = 'nhost-color-preference';
/**
* Maximum number of free projects a user is allowed to have.
*/
export const MAX_FREE_PROJECTS = 1;

View File

@@ -865,6 +865,38 @@ export type ConfigBooleanComparisonExp = {
_nin?: InputMaybe<Array<Scalars['Boolean']>>;
};
export type ConfigClaimMap = {
__typename?: 'ConfigClaimMap';
claim: Scalars['String'];
default?: Maybe<Scalars['String']>;
path?: Maybe<Scalars['String']>;
value?: Maybe<Scalars['String']>;
};
export type ConfigClaimMapComparisonExp = {
_and?: InputMaybe<Array<ConfigClaimMapComparisonExp>>;
_not?: InputMaybe<ConfigClaimMapComparisonExp>;
_or?: InputMaybe<Array<ConfigClaimMapComparisonExp>>;
claim?: InputMaybe<ConfigStringComparisonExp>;
default?: InputMaybe<ConfigStringComparisonExp>;
path?: InputMaybe<ConfigStringComparisonExp>;
value?: InputMaybe<ConfigStringComparisonExp>;
};
export type ConfigClaimMapInsertInput = {
claim: Scalars['String'];
default?: InputMaybe<Scalars['String']>;
path?: InputMaybe<Scalars['String']>;
value?: InputMaybe<Scalars['String']>;
};
export type ConfigClaimMapUpdateInput = {
claim?: InputMaybe<Scalars['String']>;
default?: InputMaybe<Scalars['String']>;
path?: InputMaybe<Scalars['String']>;
value?: InputMaybe<Scalars['String']>;
};
export type ConfigConfig = {
__typename?: 'ConfigConfig';
auth?: Maybe<ConfigAuth>;
@@ -1079,6 +1111,7 @@ export type ConfigJwtSecret = {
allowed_skew?: Maybe<Scalars['ConfigUint32']>;
audience?: Maybe<Scalars['String']>;
claims_format?: Maybe<Scalars['String']>;
claims_map?: Maybe<Array<ConfigClaimMap>>;
claims_namespace?: Maybe<Scalars['String']>;
claims_namespace_path?: Maybe<Scalars['String']>;
header?: Maybe<Scalars['String']>;
@@ -1095,6 +1128,7 @@ export type ConfigJwtSecretComparisonExp = {
allowed_skew?: InputMaybe<ConfigUint32ComparisonExp>;
audience?: InputMaybe<ConfigStringComparisonExp>;
claims_format?: InputMaybe<ConfigStringComparisonExp>;
claims_map?: InputMaybe<ConfigClaimMapComparisonExp>;
claims_namespace?: InputMaybe<ConfigStringComparisonExp>;
claims_namespace_path?: InputMaybe<ConfigStringComparisonExp>;
header?: InputMaybe<ConfigStringComparisonExp>;
@@ -1108,6 +1142,7 @@ export type ConfigJwtSecretInsertInput = {
allowed_skew?: InputMaybe<Scalars['ConfigUint32']>;
audience?: InputMaybe<Scalars['String']>;
claims_format?: InputMaybe<Scalars['String']>;
claims_map?: InputMaybe<Array<ConfigClaimMapInsertInput>>;
claims_namespace?: InputMaybe<Scalars['String']>;
claims_namespace_path?: InputMaybe<Scalars['String']>;
header?: InputMaybe<Scalars['String']>;
@@ -1121,6 +1156,7 @@ export type ConfigJwtSecretUpdateInput = {
allowed_skew?: InputMaybe<Scalars['ConfigUint32']>;
audience?: InputMaybe<Scalars['String']>;
claims_format?: InputMaybe<Scalars['String']>;
claims_map?: InputMaybe<Array<ConfigClaimMapUpdateInput>>;
claims_namespace?: InputMaybe<Scalars['String']>;
claims_namespace_path?: InputMaybe<Scalars['String']>;
header?: InputMaybe<Scalars['String']>;
@@ -16361,6 +16397,13 @@ export type InsertApplicationMutationVariables = Exact<{
export type InsertApplicationMutation = { __typename?: 'mutation_root', insertApp?: { __typename?: 'apps', id: any, name: string, slug: string, workspace: { __typename?: 'workspaces', id: any, name: string, slug: string } } | null };
export type PauseApplicationMutationVariables = Exact<{
appId: Scalars['uuid'];
}>;
export type PauseApplicationMutation = { __typename?: 'mutation_root', updateApp?: { __typename?: 'apps', id: any } | null };
export type PrefetchNewAppRegionsFragment = { __typename?: 'regions', id: any, city: string, active: boolean, country: { __typename?: 'countries', code: any, name: string } };
export type PrefetchNewAppPlansFragment = { __typename?: 'plans', id: any, name: string, isDefault: boolean, isFree: boolean, price: number, featureBackupEnabled: boolean, featureCustomDomainsEnabled: boolean, featureMaxDbSize: number };
@@ -16454,6 +16497,13 @@ export type UpdateConfigMutationVariables = Exact<{
export type UpdateConfigMutation = { __typename?: 'mutation_root', updateConfig: { __typename?: 'ConfigConfig', id: 'ConfigConfig' } };
export type UnpauseApplicationMutationVariables = Exact<{
appId: Scalars['uuid'];
}>;
export type UnpauseApplicationMutation = { __typename?: 'mutation_root', updateApp?: { __typename?: 'apps', id: any } | null };
export type UpdateAppMutationVariables = Exact<{
id: Scalars['uuid'];
app: Apps_Set_Input;
@@ -16784,6 +16834,13 @@ export type GetAvatarQueryVariables = Exact<{
export type GetAvatarQuery = { __typename?: 'query_root', user?: { __typename?: 'users', id: any, avatarUrl: string } | null };
export type GetFreeAndActiveProjectsQueryVariables = Exact<{
userId: Scalars['uuid'];
}>;
export type GetFreeAndActiveProjectsQuery = { __typename?: 'query_root', freeAndActiveProjects: Array<{ __typename?: 'apps', id: any }> };
export type ProjectFragment = { __typename?: 'apps', id: any, slug: string, name: string, repositoryProductionBranch: string, subdomain: string, isProvisioned: boolean, createdAt: any, desiredState: number, nhostBaseFolder: string, providersUpdated?: boolean | null, config?: { __typename?: 'ConfigConfig', hasura: { __typename?: 'ConfigHasura', adminSecret: string } } | null, featureFlags: Array<{ __typename?: 'featureFlags', description: string, id: any, name: string, value: string }>, appStates: Array<{ __typename?: 'appStateHistory', id: any, appId: any, message?: string | null, stateId: number, createdAt: any }>, region: { __typename?: 'regions', id: any, countryCode: string, awsName: string, city: string }, plan: { __typename?: 'plans', id: any, name: string, isFree: boolean }, githubRepository?: { __typename?: 'githubRepositories', fullName: string } | null, deployments: Array<{ __typename?: 'deployments', id: any, commitSHA: string, commitMessage?: string | null, commitUserName?: string | null, deploymentStartedAt?: any | null, deploymentEndedAt?: any | null, commitUserAvatarUrl?: string | null, deploymentStatus?: string | null }> };
export type GetOneUserQueryVariables = Exact<{
@@ -17729,6 +17786,39 @@ export function useInsertApplicationMutation(baseOptions?: Apollo.MutationHookOp
export type InsertApplicationMutationHookResult = ReturnType<typeof useInsertApplicationMutation>;
export type InsertApplicationMutationResult = Apollo.MutationResult<InsertApplicationMutation>;
export type InsertApplicationMutationOptions = Apollo.BaseMutationOptions<InsertApplicationMutation, InsertApplicationMutationVariables>;
export const PauseApplicationDocument = gql`
mutation PauseApplication($appId: uuid!) {
updateApp(pk_columns: {id: $appId}, _set: {desiredState: 6}) {
id
}
}
`;
export type PauseApplicationMutationFn = Apollo.MutationFunction<PauseApplicationMutation, PauseApplicationMutationVariables>;
/**
* __usePauseApplicationMutation__
*
* To run a mutation, you first call `usePauseApplicationMutation` within a React component and pass it any options that fit your needs.
* When your component renders, `usePauseApplicationMutation` returns a tuple that includes:
* - A mutate function that you can call at any time to execute the mutation
* - An object with fields that represent the current status of the mutation's execution
*
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
*
* @example
* const [pauseApplicationMutation, { data, loading, error }] = usePauseApplicationMutation({
* variables: {
* appId: // value for 'appId'
* },
* });
*/
export function usePauseApplicationMutation(baseOptions?: Apollo.MutationHookOptions<PauseApplicationMutation, PauseApplicationMutationVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useMutation<PauseApplicationMutation, PauseApplicationMutationVariables>(PauseApplicationDocument, options);
}
export type PauseApplicationMutationHookResult = ReturnType<typeof usePauseApplicationMutation>;
export type PauseApplicationMutationResult = Apollo.MutationResult<PauseApplicationMutation>;
export type PauseApplicationMutationOptions = Apollo.BaseMutationOptions<PauseApplicationMutation, PauseApplicationMutationVariables>;
export const PrefetchNewAppDocument = gql`
query PrefetchNewApp {
regions(order_by: {city: asc}) {
@@ -18314,6 +18404,39 @@ export function useUpdateConfigMutation(baseOptions?: Apollo.MutationHookOptions
export type UpdateConfigMutationHookResult = ReturnType<typeof useUpdateConfigMutation>;
export type UpdateConfigMutationResult = Apollo.MutationResult<UpdateConfigMutation>;
export type UpdateConfigMutationOptions = Apollo.BaseMutationOptions<UpdateConfigMutation, UpdateConfigMutationVariables>;
export const UnpauseApplicationDocument = gql`
mutation UnpauseApplication($appId: uuid!) {
updateApp(pk_columns: {id: $appId}, _set: {desiredState: 5}) {
id
}
}
`;
export type UnpauseApplicationMutationFn = Apollo.MutationFunction<UnpauseApplicationMutation, UnpauseApplicationMutationVariables>;
/**
* __useUnpauseApplicationMutation__
*
* To run a mutation, you first call `useUnpauseApplicationMutation` within a React component and pass it any options that fit your needs.
* When your component renders, `useUnpauseApplicationMutation` returns a tuple that includes:
* - A mutate function that you can call at any time to execute the mutation
* - An object with fields that represent the current status of the mutation's execution
*
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
*
* @example
* const [unpauseApplicationMutation, { data, loading, error }] = useUnpauseApplicationMutation({
* variables: {
* appId: // value for 'appId'
* },
* });
*/
export function useUnpauseApplicationMutation(baseOptions?: Apollo.MutationHookOptions<UnpauseApplicationMutation, UnpauseApplicationMutationVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useMutation<UnpauseApplicationMutation, UnpauseApplicationMutationVariables>(UnpauseApplicationDocument, options);
}
export type UnpauseApplicationMutationHookResult = ReturnType<typeof useUnpauseApplicationMutation>;
export type UnpauseApplicationMutationResult = Apollo.MutationResult<UnpauseApplicationMutation>;
export type UnpauseApplicationMutationOptions = Apollo.BaseMutationOptions<UnpauseApplicationMutation, UnpauseApplicationMutationVariables>;
export const UpdateAppDocument = gql`
mutation updateApp($id: uuid!, $app: apps_set_input!) {
updateApp(pk_columns: {id: $id}, _set: $app) {
@@ -20072,6 +20195,46 @@ export type GetAvatarQueryResult = Apollo.QueryResult<GetAvatarQuery, GetAvatarQ
export function refetchGetAvatarQuery(variables: GetAvatarQueryVariables) {
return { query: GetAvatarDocument, variables: variables }
}
export const GetFreeAndActiveProjectsDocument = gql`
query GetFreeAndActiveProjects($userId: uuid!) {
freeAndActiveProjects: apps(
where: {creatorUserId: {_eq: $userId}, plan: {isFree: {_eq: true}}, desiredState: {_eq: 5}}
) {
id
}
}
`;
/**
* __useGetFreeAndActiveProjectsQuery__
*
* To run a query within a React component, call `useGetFreeAndActiveProjectsQuery` and pass it any options that fit your needs.
* When your component renders, `useGetFreeAndActiveProjectsQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useGetFreeAndActiveProjectsQuery({
* variables: {
* userId: // value for 'userId'
* },
* });
*/
export function useGetFreeAndActiveProjectsQuery(baseOptions: Apollo.QueryHookOptions<GetFreeAndActiveProjectsQuery, GetFreeAndActiveProjectsQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useQuery<GetFreeAndActiveProjectsQuery, GetFreeAndActiveProjectsQueryVariables>(GetFreeAndActiveProjectsDocument, options);
}
export function useGetFreeAndActiveProjectsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<GetFreeAndActiveProjectsQuery, GetFreeAndActiveProjectsQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useLazyQuery<GetFreeAndActiveProjectsQuery, GetFreeAndActiveProjectsQueryVariables>(GetFreeAndActiveProjectsDocument, options);
}
export type GetFreeAndActiveProjectsQueryHookResult = ReturnType<typeof useGetFreeAndActiveProjectsQuery>;
export type GetFreeAndActiveProjectsLazyQueryHookResult = ReturnType<typeof useGetFreeAndActiveProjectsLazyQuery>;
export type GetFreeAndActiveProjectsQueryResult = Apollo.QueryResult<GetFreeAndActiveProjectsQuery, GetFreeAndActiveProjectsQueryVariables>;
export function refetchGetFreeAndActiveProjectsQuery(variables: GetFreeAndActiveProjectsQueryVariables) {
return { query: GetFreeAndActiveProjectsDocument, variables: variables }
}
export const GetOneUserDocument = gql`
query getOneUser($userId: uuid!) {
user(id: $userId) {

View File

@@ -7,7 +7,7 @@ export interface ColumnDetails {
hasDefaultValue: boolean;
}
function createGenericValidationSchema<T extends yup.BaseSchema>(
function createGenericValidationSchema<T extends yup.Schema>(
genericSchema: T,
{ isNullable, hasDefaultValue, isIdentity }: ColumnDetails,
): T {
@@ -136,7 +136,10 @@ export function createDynamicValidationSchema(
};
}
if (column.type === 'text' && column.specificType === 'jsonb') {
if (
column.type === 'text' &&
(column.specificType === 'jsonb' || column.specificType === 'json')
) {
return {
...schema,
[column.id]: createJSONValidationSchema(details),

View File

@@ -4,6 +4,10 @@ import { isDevOrStaging } from './helpers';
* @param content {string} This string to log on the particular channel.
*/
export const discordAnnounce = async (content: string) => {
if (!process.env.NEXT_PUBLIC_DISCORD_LOGGING) {
return;
}
const username = isDevOrStaging() ? 'console-next(dev)' : 'console-next';
const params = {

View File

@@ -1,5 +1,5 @@
import { graphql } from 'msw';
const nhostGraphQLLink = graphql.link('http://localhost:1337/v1/graphql');
const nhostGraphQLLink = graphql.link('https://local.graphql.nhost.run/v1');
export default nhostGraphQLLink;

View File

@@ -79,7 +79,7 @@ function Providers({ children }: PropsWithChildren<{}>) {
<NhostApolloProvider
nhost={nhost}
link={createHttpLink({
uri: 'http://localhost:1337/v1/graphql',
uri: 'https://local.graphql.nhost.run/v1',
})}
>
<WorkspaceProvider>

View File

@@ -19,6 +19,7 @@
"baseUrl": "./src",
"useUnknownInCatchVariables": false,
"paths": {
"@/e2e/*": ["../e2e/*"],
"@/components/*": ["components/*"],
"@/hooks/*": ["hooks/*"],
"@/utils/*": ["utils/*"],

View File

@@ -4,6 +4,7 @@
"jsx": "react-jsx",
"types": ["vitest/globals"],
"paths": {
"@/e2e/*": ["../e2e/*"],
"@/components/*": ["components/*"],
"@/hooks/*": ["hooks/*"],
"@/utils/*": ["utils/*"],

View File

@@ -10,5 +10,6 @@ export default defineConfig({
environment: 'jsdom',
globals: true,
setupFiles: 'src/setupTests.ts',
include: ['src/**/*.(spec|test).{js,jsx,ts,tsx}'],
},
});

View File

@@ -56,6 +56,7 @@ Follow this guide to sign in users with Google.
- Click on **Credentials** under **APIs & Services** in the left menu.
- Click **+ CREATE CREDENTIALS** and then **OAuth client ID** in the top menu.
- On the **Create OAuth client ID** page for **Application Type** select **Web application**.
- Under **Authorized JavaScript origins**, add your project's redirect URL for the Google sign-in method in the following format: `https://<subdomain>.auth.<region>.nhost.run`
- Under **Authorized redirect URIs** add your **OAuth Callback URL** from Nhost.
- Click **CREATE**.

View File

@@ -31,11 +31,11 @@ export default (req: Request, res: Response) => {
To get the `Request`, and `Response` types you can install the `@types/express` package.
```bash
npm install -d @types/express
npm install -D @types/express
# or yarn
yarn add -d @types/express
yarn add -D @types/express
# or pnpm
pnpm add -d @types/express
pnpm add -D @types/express
```
:::

View File

@@ -16,9 +16,9 @@
},
"dependencies": {
"@algolia/client-search": "^4.9.1",
"@docusaurus/core": "2.3.1",
"@docusaurus/plugin-sitemap": "2.3.1",
"@docusaurus/preset-classic": "2.3.1",
"@docusaurus/core": "2.4.0",
"@docusaurus/plugin-sitemap": "2.4.0",
"@docusaurus/preset-classic": "2.4.0",
"@mdx-js/react": "^1.6.22",
"clsx": "^1.2.1",
"docusaurus-plugin-image-zoom": "^0.1.1",
@@ -30,7 +30,7 @@
"unist-util-visit": "^2.0.0"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "2.3.1",
"@docusaurus/module-type-aliases": "2.4.0",
"@tsconfig/docusaurus": "^1.0.6",
"typescript": "^4.8.4"
},

View File

@@ -1,9 +1,5 @@
module.exports = {
extends: [
'../../config/.eslintrc.js',
'plugin:react/jsx-runtime',
'plugin:@next/next/recommended'
],
extends: ['../../config/.eslintrc.js', 'plugin:@next/next/recommended'],
rules: {
'react/react-in-jsx-scope': 'off'
}

View File

@@ -25,5 +25,7 @@ yarn-error.log*
.nhost
functions/node_modules
cypress/videos
cypress/screenshots
/test-results/
/playwright-report/
/playwright/.cache/
storageState.json

View File

@@ -1,5 +1,11 @@
# @nhost-examples/react-apollo
## 0.1.10
### Patch Changes
- caba147b: chore(examples): improve tests of the React Apollo example
## 0.1.9
### Patch Changes

View File

@@ -1,16 +0,0 @@
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
chromeWebSecurity: false,
// * for some reason, the mailhog API is not systematically available
// * when using `localhost` instead of `127.0.0.1`
mailHogUrl: 'http://127.0.0.1:8025',
env: {
backendUrl: 'http://localhost:1337'
},
defaultCommandTimeout: 20000,
requestTimeout: 20000
}
} as Cypress.ConfigOptions)

View File

@@ -1,8 +0,0 @@
context('Authentication guards', () => {
it('should redirect to /sign-in when not authenticated', () => {
cy.visit('/')
cy.location('pathname').should('equal', '/sign-in')
cy.visit('/apollo')
cy.location('pathname').should('equal', '/sign-in')
})
})

View File

@@ -1,21 +0,0 @@
import { faker } from '@faker-js/faker'
context('Forgot password', () => {
it('should reset password', () => {
const email = faker.internet.email()
const password = faker.internet.password(8)
cy.signUpEmailPassword(email, password)
cy.contains('Verification email sent').should('be.visible')
cy.visit('/sign-in')
cy.findByRole('button', { name: /Continue with email \+ password/i }).click()
cy.findByRole('button', { name: /Forgot Password?/i }).click()
cy.findByPlaceholderText('Email Address').type(email)
cy.findByRole('button', { name: /Reset your password/i }).click()
cy.confirmEmail(email)
cy.contains('Profile page')
})
})

View File

@@ -1,51 +0,0 @@
import { faker } from '@faker-js/faker'
context('Anonymous users', () => {
beforeEach(() => {
cy.signInAnonymous()
})
it('should sign-up anonymously', () => {
cy.contains('You signed in anonymously')
})
it('should deanonymise with email+password', () => {
cy.fetchUserData()
.its('id')
.then((id) => {
const email = faker.internet.email()
const password = faker.internet.password()
cy.signUpEmailPassword(email, password)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
cy.contains('You signed in anonymously').should('not.exist')
cy.fetchUserData().then((user) => {
cy.wrap(user).its('id').should('equal', id)
cy.wrap(user).its('email').should('equal', email)
})
})
})
it('should deanonymise with a magic link', () => {
cy.fetchUserData()
.its('id')
.then((id) => {
const email = faker.internet.email()
cy.signUpEmailPasswordless(email)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
cy.goToHomePage()
cy.contains('You signed in anonymously').should('not.exist')
cy.fetchUserData().then((user) => {
cy.wrap(user).its('id').should('equal', id)
cy.wrap(user).its('email').should('equal', email)
})
})
})
// TODO implement deanonymisation with Oauth?
// TODO forbid email/password change, MFA activation, and password reset when the following PR is released
// * https://github.com/nhost/hasura-auth/pull/190
})

View File

@@ -1,28 +0,0 @@
import { faker } from '@faker-js/faker'
context('Sign up with email+password', () => {
it('should sign-up with email and password', () => {
const email = faker.internet.email()
const password = faker.internet.password()
cy.signUpEmailPassword(email, password)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
cy.contains('You are authenticated')
})
it('shoud raise an error when trying to sign up with an existing email', () => {
const email = faker.internet.email()
const password = faker.internet.password(10)
cy.signUpEmailPassword(email, password)
cy.contains('Verification email sent').should('be.visible')
cy.signUpEmailPassword(email, password)
cy.contains('Email already in use').should('be.visible')
})
// TODO implement in the UI
it.skip('should fail when network is not available', () => {
cy.disconnectBackend()
cy.signUpEmailPassword(faker.internet.email(), faker.internet.password())
cy.contains('Error').should('be.visible')
})
})

View File

@@ -1,17 +0,0 @@
import { faker } from '@faker-js/faker'
context('Sign up with a magic link', () => {
it('should sign-up with a magic link', () => {
const email = faker.internet.email()
cy.signUpEmailPasswordless(email)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
cy.contains('Profile page')
})
it('should fail when network is not available', () => {
cy.disconnectBackend()
cy.signUpEmailPasswordless(faker.internet.email())
cy.contains('Error').should('be.visible')
})
})

View File

@@ -1,74 +0,0 @@
import totp from 'totp-generator'
import { faker } from '@faker-js/faker'
import { Decoder } from '@nuintun/qrcode'
context('Sign in with email+password', () => {
it('should sign-in with email and password', () => {
const email = faker.internet.email()
const password = faker.internet.password()
cy.signUpEmailPassword(email, password)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
cy.signOut()
cy.contains('Sign in to the Application').should('be.visible')
cy.signInEmailPassword(email, password)
cy.contains('You are authenticated')
})
// TODO implement in the UI
it.skip('should fail when network is not available', () => {
const email = faker.internet.email()
const password = faker.internet.password()
cy.disconnectBackend()
cy.signInEmailPassword(email, password)
cy.contains('Error').should('be.visible')
})
it('should activate and sign-in with MFA', () => {
// * Sign-up with email+password
const email = faker.internet.email()
const password = faker.internet.email()
cy.signUpEmailPassword(email, password)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
cy.getNavBar()
.findByRole('button', { name: /Profile/i })
.click()
cy.findByText(/Activate 2-step verification/i)
.parent()
.findByRole('button')
.click()
cy.findAllByAltText(/qrcode/i).then(async (img) => {
// * Activate MFA
const result = await new Decoder().scan(img.prop('src'))
const [, params] = result.data.split('?')
const { secret, algorithm, digits, period } = Object.fromEntries(new URLSearchParams(params))
const code = totp(secret, {
algorithm: algorithm.replace('SHA1', 'SHA-1'),
digits: parseInt(digits),
period: parseInt(period)
})
cy.findByPlaceholderText('Enter activation code').type(code)
cy.findByRole('button', { name: /Activate/i }).click()
cy.contains('MFA has been activated!!!')
cy.signOut()
// * Sign-in with MFA
cy.visit('/sign-in')
cy.findByRole('button', { name: /Continue with email \+ password/i }).click()
cy.findByPlaceholderText('Email Address').type(email)
cy.findByPlaceholderText('Password').type(password)
cy.findByRole('button', { name: /Sign in/i }).click()
cy.contains('Send 2-step verification code')
const newCode = totp(secret, { timestamp: Date.now() })
cy.findByPlaceholderText('One-time password').type(newCode)
cy.findByRole('button', { name: /Send 2-step verification code/i }).click()
cy.contains('You are authenticated')
})
})
})

View File

@@ -1,22 +0,0 @@
context('Automatic sign-in with a refresh token', () => {
it('should sign in automatically with a refresh token', () => {
cy.signUpAndConfirmEmail()
cy.contains('Profile page')
cy.clearLocalStorage()
cy.reload()
cy.contains('Sign in to the Application')
cy.visitPathWithRefreshToken('/profile')
cy.contains('Profile page')
})
it('should fail automatic sign-in when network is not available', () => {
cy.signUpAndConfirmEmail()
cy.contains('Profile page')
cy.disconnectBackend()
cy.clearLocalStorage()
cy.reload()
cy.contains('Sign in to the Application')
cy.visitPathWithRefreshToken('/profile')
cy.contains('Could not sign in automatically. Retrying to get user information..')
})
})

View File

@@ -1,45 +0,0 @@
import { faker } from '@faker-js/faker'
context('Apollo', () => {
const addItemTest = (sentence: string) => {
cy.getNavBar()
.findByRole('button', { name: /Apollo/i })
.click()
cy.contains('Todo list')
cy.focused().type(sentence)
cy.findByRole('button', { name: /Add/i }).click()
}
it('should add an item to the todo list when normally authenticated', () => {
cy.signUpAndConfirmEmail()
const sentence = faker.lorem.sentence()
addItemTest(sentence)
cy.get('li').contains(sentence)
})
it('should add an item to the todo list when anonymous', () => {
cy.signInAnonymous()
const sentence = faker.lorem.sentence()
addItemTest(sentence)
cy.get('li').contains(sentence)
})
it('should add an item to the todo list after a token refresh', () => {
// * This test has a limitation: Hasura's clock is not changing, so the previous JWT will still be valid.
cy.signUpAndConfirmEmail()
const now = Date.now()
cy.clock(now)
cy.tick(4 * 7 * 24 * 60 * 60 * 1000)
const sentence = faker.lorem.sentence()
addItemTest(sentence)
cy.get('li').contains(sentence)
})
it('should not add an item when backend is disconnected', () => {
cy.signUpAndConfirmEmail()
cy.disconnectBackend()
addItemTest(faker.lorem.sentence())
cy.contains('Network error')
cy.get('ul').should('be.empty')
})
})

View File

@@ -1,30 +0,0 @@
import { faker } from '@faker-js/faker'
context('Change email', () => {
it('should change email', () => {
const newEmail = faker.internet.email()
cy.signUpAndConfirmEmail()
cy.findByPlaceholderText('New email').type(newEmail)
cy.findByText(/Change Email/i)
.parent()
.findByRole('button')
.click()
cy.contains('Please check your inbox and follow the link to confirm the email change').should(
'be.visible'
)
cy.signOut()
cy.confirmEmail(newEmail)
cy.contains('Profile page')
})
it('should not accept an invalid email', () => {
const newEmail = faker.random.alphaNumeric()
cy.signUpAndConfirmEmail()
cy.findByPlaceholderText('New email').type(newEmail)
cy.findByText(/Change Email/i)
.parent()
.findByRole('button')
.click()
cy.contains('Email is incorrectly formatted').should('be.visible')
})
})

View File

@@ -1,29 +0,0 @@
import { faker } from '@faker-js/faker'
context('Change password', () => {
it('should change password', () => {
const email = faker.internet.email()
const newPassword = faker.internet.password()
cy.signUpAndConfirmEmail(email)
cy.findByPlaceholderText('New password').type(newPassword)
cy.findByText(/Change Password/i)
.parent()
.findByRole('button')
.click()
cy.contains('Password changed successfully').should('be.visible')
cy.signOut()
cy.signInEmailPassword(email, newPassword)
cy.contains('You are authenticated')
})
it('should not accept an invalid password', () => {
const newPassword = faker.random.alphaNumeric(2)
cy.signUpAndConfirmEmail()
cy.findByPlaceholderText('New password').type(newPassword)
cy.findByText(/Change Password/i)
.parent()
.findByRole('button')
.click()
cy.contains('Password is incorrectly formatted').should('be.visible')
})
})

View File

@@ -1,75 +0,0 @@
context('File uploads', () => {
it('should upload a single file', () => {
cy.signUpAndConfirmEmail()
cy.findByRole('button', { name: /Storage/i }).click()
cy.findByRole('button', { name: /Drag a file here or click to select/i })
.children('input[type=file]')
.selectFile(
{
contents: Cypress.Buffer.from('file contents'),
fileName: 'file.txt',
mimeType: 'text/plain',
lastModified: Date.now()
},
{ force: true }
)
.parent()
.contains('Successfully uploaded')
})
it('should upload two files using the same single file uploader', () => {
cy.signUpAndConfirmEmail()
cy.findByRole('button', { name: /Storage/i }).click()
cy.findByRole('button', { name: /Drag a file here or click to select/i })
.children('input[type=file]')
.selectFile(
{
contents: Cypress.Buffer.from('file contents'),
fileName: 'file.txt',
mimeType: 'text/plain',
lastModified: Date.now()
},
{ force: true }
)
.selectFile(
{
contents: Cypress.Buffer.from('file contents'),
fileName: 'file.txt',
mimeType: 'text/plain',
lastModified: Date.now()
},
{ force: true }
)
.parent()
.contains('Successfully uploaded')
})
it('should upload multiple files', () => {
const files: Required<Cypress.FileReferenceObject>[] = [
{
contents: Cypress.Buffer.from('file contents'),
fileName: 'file1.txt',
mimeType: 'text/plain',
lastModified: Date.now()
},
{
contents: Cypress.Buffer.from('file contents'),
fileName: 'file2.txt',
mimeType: 'text/plain',
lastModified: Date.now()
}
]
cy.signUpAndConfirmEmail()
cy.findByRole('button', { name: /Storage/i }).click()
cy.findByRole('button', { name: /Drag files here or click to select/i })
.children('input[type=file]')
.selectFile(files, { force: true })
cy.findByRole('button', { name: /Upload/i }).click()
cy.findByRole('button', { name: /Successfully uploaded/i }).should('be.visible')
cy.findByRole('table').within(() => {
files.forEach((file) => {
cy.contains(file.fileName).parent().findByTitle('success').should('exist')
})
})
})
})

View File

@@ -1,13 +0,0 @@
context('Sign out', () => {
beforeEach(() => {
cy.signUpAndConfirmEmail()
})
it('should sign out', () => {
cy.visitPathWithRefreshToken()
cy.goToProfilePage()
cy.contains('Profile page')
cy.signOut()
cy.contains('Sign in to the Application')
})
})

View File

@@ -1,30 +0,0 @@
import { faker } from '@faker-js/faker'
context('Token refresh', () => {
it('should refresh token one minute before it expires', () => {
const email = faker.internet.email()
cy.signUpEmailPasswordless(email)
cy.contains('Verification email sent').should('be.visible')
const now = Date.now()
cy.clock(now)
cy.confirmEmail(email)
cy.intercept(Cypress.env('backendUrl') + '/v1/auth/token').as('tokenRequest')
cy.tick(14 * 60 * 1000)
cy.wait('@tokenRequest').its('response.statusCode').should('eq', 200)
})
it('should refresh session from localStorage after 4 weeks of inactivity', () => {
const email = faker.internet.email()
cy.signUpEmailPasswordless(email)
cy.contains('Verification email sent').should('be.visible')
const now = Date.now()
cy.clock(now)
cy.confirmEmail(email)
cy.contains('Profile page')
cy.tick(4 * 7 * 24 * 60 * 60 * 1000)
cy.reload()
cy.contains('Profile page')
})
})

View File

@@ -1,139 +0,0 @@
import { faker } from '@faker-js/faker'
import { User } from '@nhost/react'
import '@testing-library/cypress/add-commands'
import 'cypress-mailhog'
declare module 'mocha' {
export interface Context {
refreshToken?: string
}
}
declare global {
namespace Cypress {
interface Chainable {
signUpEmailPassword(email: string, password: string): Chainable<Element>
signUpEmailPasswordless(email: string): Chainable<Element>
signInEmailPassword(email: string, password: string): Chainable<Element>
signInAnonymous(): Chainable<Element>
/** Sign in from the refresh token stored in the global state */
visitPathWithRefreshToken(path?: string): Chainable<Element>
/** Click on the 'Sign Out' item of the left side menu to sign out the current user */
signOut(): Chainable<Element>
/** Run a sign-up + authentication sequence with passwordless to use an authenticated user in other tests */
signUpAndConfirmEmail(email?: string): Chainable<Element>
/** Gets a confirmation email and click on the link */
confirmEmail(email: string): Chainable<Element>
/** Save the refresh token in the global state so it can be reused with `this.refreshToken` */
saveRefreshToken(): Chainable<Element>
/** Make the Nhost backend unavailable */
disconnectBackend(): Chainable<Element>
/** Get the left side navigation bar */
getNavBar(): Chainable<Element>
/** Go to the profile page */
goToProfilePage(): Chainable<Element>
/** Go to the home page */
goToHomePage(): Chainable<Element>
/** Go getch the user ID in the profile page*/
fetchUserData(): Chainable<User>
}
}
}
Cypress.Commands.add('signUpEmailPassword', (email, password) => {
cy.visit('/sign-up')
cy.findByRole('button', { name: /Continue with email \+ password/i }).click()
cy.findByPlaceholderText('First name').type(faker.name.firstName())
cy.findByPlaceholderText('Last name').type(faker.name.lastName())
cy.findByPlaceholderText('Email Address').type(email)
cy.findByPlaceholderText('Password').type(password)
cy.findByPlaceholderText('Confirm Password').type(password)
cy.findByRole('button', { name: /Continue with email \+ password/i }).click()
})
Cypress.Commands.add('signUpEmailPasswordless', (email) => {
cy.visit('/sign-up')
cy.findByRole('button', { name: /Continue with a magic link/i }).click()
cy.findByPlaceholderText('Email Address').type(email)
cy.findByRole('button', { name: /Continue with email/i }).click()
})
Cypress.Commands.add('signInEmailPassword', (email, password) => {
cy.visit('/sign-in')
cy.findByRole('button', { name: /Continue with email \+ password/i }).click()
cy.findByPlaceholderText('Email Address').type(email)
cy.findByPlaceholderText('Password').type(password)
cy.findByRole('button', { name: /Sign in/i }).click()
cy.saveRefreshToken()
})
Cypress.Commands.add('signInAnonymous', () => {
cy.visit('/sign-in')
cy.findByRole('link', { name: /sign in anonymously/i }).click()
cy.saveRefreshToken()
})
Cypress.Commands.add('visitPathWithRefreshToken', function (path = '/') {
cy.visit(path + '#refreshToken=' + this.refreshToken)
})
Cypress.Commands.add('signOut', () => {
cy.getNavBar()
.findByRole('button', { name: /Sign Out/i })
.click()
})
Cypress.Commands.add('confirmEmail', (email) => {
cy.mhGetMailsByRecipient(email, 1)
.should('have.length', 1)
.then(([message]) => {
cy.visit(message.Content.Headers['X-Link'][0])
cy.saveRefreshToken()
})
})
Cypress.Commands.add('signUpAndConfirmEmail', (givenEmail) => {
const email = givenEmail || faker.internet.email()
cy.signUpEmailPasswordless(email)
cy.contains('Verification email sent').should('be.visible')
cy.confirmEmail(email)
})
Cypress.Commands.add('saveRefreshToken', () => {
cy.getNavBar()
.findByRole('button', { name: /Sign Out/i })
.then(() => localStorage.getItem('nhostRefreshToken'))
.as('refreshToken')
})
Cypress.Commands.add('disconnectBackend', () => {
cy.intercept(Cypress.env('backendUrl') + '/**', {
forceNetworkError: true
})
})
Cypress.Commands.add('getNavBar', () => {
cy.findByRole(`navigation`, { name: /main navigation/i })
})
Cypress.Commands.add('goToProfilePage', () => {
cy.getNavBar()
.findByRole('button', { name: /Profile/i })
.click()
})
Cypress.Commands.add('goToHomePage', () => {
cy.getNavBar().findByRole('button', { name: /Home/i }).click()
})
Cypress.Commands.add('fetchUserData', () => {
cy.goToProfilePage()
cy.findByText('User information')
.parent()
.within(() => {
cy.get('pre')
.invoke('text')
.then((text) => JSON.parse(text))
.as('user')
})
return cy.get<User>('@user')
})

View File

@@ -0,0 +1,52 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { signInAnonymously, signUpWithEmailAndPassword, verifyEmail } from '../utils'
test('should add an item to the todo list when authenticated with email and password', async ({
page
}) => {
const email = faker.internet.email()
const password = faker.internet.password()
const sentence = faker.lorem.sentence()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /apollo/i }).click()
await expect(newPage.getByText(/todo list/i)).toBeVisible()
await newPage.getByRole('textbox').fill(sentence)
await newPage.getByRole('button', { name: /add/i }).click()
await expect(newPage.getByRole('listitem').first()).toHaveText(sentence)
})
test('should add an item to the todo list when authenticated anonymously', async ({ page }) => {
const sentence = faker.lorem.sentence()
await page.goto('/')
await signInAnonymously({ page })
await page.getByRole('button', { name: /apollo/i }).click()
await expect(page.getByText(/todo list/i)).toBeVisible()
await page.getByRole('textbox').fill(sentence)
await page.getByRole('button', { name: /add/i }).click()
await expect(page.getByRole('listitem').first()).toHaveText(sentence)
})
test('should fail when network is not available', async ({ page }) => {
const sentence = faker.lorem.sentence()
await page.goto('/')
await signInAnonymously({ page })
await page.getByRole('button', { name: /apollo/i }).click()
await expect(page.getByText(/todo list/i)).toBeVisible()
await page.route('**', (route) => route.abort('internetdisconnected'))
await page.getByRole('textbox').fill(sentence)
await page.getByRole('button', { name: /add/i }).click()
await expect(page.getByText(/network error/i)).toBeVisible()
})

View File

@@ -0,0 +1,56 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { signUpWithEmailAndPassword, verifyEmail } from '../utils'
test('should be able to change email', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /profile/i }).click()
const newEmail = faker.internet.email()
await newPage.getByPlaceholder(/new email/i).fill(newEmail)
await newPage.locator('h1:has-text("Change email") + div button:has-text("Change")').click()
await expect(
newPage.getByText(/please check your inbox and follow the link to confirm the email change/i)
).toBeVisible()
await newPage.getByRole('button', { name: /sign out/i }).click()
const updatedEmailPage = await verifyEmail({
page: newPage,
email: newEmail,
context: page.context(),
linkText: /change email/i
})
await expect(updatedEmailPage.getByText(/profile page/i)).toBeVisible()
})
test('should not accept an invalid email', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /profile/i }).click()
const newEmail = faker.random.alphaNumeric()
await newPage.getByPlaceholder(/new email/i).fill(newEmail)
await newPage.locator('h1:has-text("Change email") + div button:has-text("Change")').click()
await expect(newPage.getByText(/email is incorrectly formatted/i)).toBeVisible()
})

View File

@@ -0,0 +1,47 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { signInWithEmailAndPassword, signUpWithEmailAndPassword, verifyEmail } from '../utils'
test('should be able to change password', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /profile/i }).click()
const newPassword = faker.internet.password()
await newPage.getByPlaceholder(/new password/i).fill(newPassword)
await newPage.locator('h1:has-text("Change password") + div button:has-text("Change")').click()
await expect(newPage.getByText(/password changed successfully/i)).toBeVisible()
await newPage.getByRole('button', { name: /sign out/i }).click()
await signInWithEmailAndPassword({ page: newPage, email, password: newPassword })
await expect(newPage.getByText(/you are authenticated/i)).toBeVisible()
})
test('should not accept an invalid email', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /profile/i }).click()
const newPassword = faker.internet.password(2)
await newPage.getByPlaceholder(/new password/i).fill(newPassword)
await newPage.locator('h1:has-text("Change password") + div button:has-text("Change")').click()
await expect(newPage.getByText(/password is incorrectly formatted/i)).toBeVisible()
})

View File

@@ -0,0 +1,97 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { signUpWithEmailAndPassword, verifyEmail } from '../utils'
test('should upload a single file', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /storage/i }).click()
await newPage
.getByRole('button', { name: /drag a file here or click to select/i })
.locator('input[type=file]')
.setInputFiles({
buffer: Buffer.from('file contents', 'utf-8'),
name: 'file.txt',
mimeType: 'text/plain'
})
await expect(newPage.getByText(/successfully uploaded/i)).toBeVisible()
})
test('should upload two files using the same single file uploader', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /storage/i }).click()
await newPage
.getByRole('button', { name: /drag a file here or click to select/i })
.locator('input[type=file]')
.setInputFiles({
buffer: Buffer.from('file contents 1', 'utf-8'),
name: 'file1.txt',
mimeType: 'text/plain'
})
await expect(newPage.getByText(/successfully uploaded/i)).toBeVisible()
await newPage
.getByRole('button', { name: /successfully uploaded/i })
.locator('input[type=file]')
.setInputFiles({
buffer: Buffer.from('file contents 2', 'utf-8'),
name: 'file2.txt',
mimeType: 'text/plain'
})
await expect(newPage.getByText(/successfully uploaded/i)).toBeVisible()
})
test('should upload multiple files at once', async ({ page }) => {
const email = faker.internet.email()
const password = faker.internet.password()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await newPage.getByRole('button', { name: /storage/i }).click()
await newPage
.getByRole('button', { name: /drag files here or click to select/i })
.locator('input[type=file]')
.setInputFiles([
{
buffer: Buffer.from('file contents 1', 'utf-8'),
name: 'file1.txt',
mimeType: 'text/plain'
},
{
buffer: Buffer.from('file contents 2', 'utf-8'),
name: 'file2.txt',
mimeType: 'text/plain'
}
])
await expect(newPage.getByRole('row').nth(0)).toHaveText('file1.txt')
await expect(newPage.getByRole('row').nth(1)).toHaveText('file2.txt')
await newPage.getByRole('button', { name: /upload/i }).click()
await expect(newPage.getByText(/successfully uploaded/i)).toBeVisible()
})

View File

@@ -0,0 +1,3 @@
export const baseURL = 'http://localhost:3000'
export const mailhogURL = 'http://localhost:8025'
export const authBackendURL = 'https://local.auth.nhost.run'

View File

@@ -0,0 +1,72 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { authBackendURL, baseURL } from '../config'
import {
clearStorage,
getValueFromLocalStorage,
signUpWithEmailAndPassword,
verifyEmail
} from '../utils'
test('should sign in automatically with a refresh token', async ({ page }) => {
await page.goto('/')
const email = faker.internet.email()
const password = faker.internet.password()
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await expect(newPage.getByText(/you are authenticated/i)).toBeVisible()
const refreshToken = await getValueFromLocalStorage({
page: newPage,
origin: baseURL,
key: 'nhostRefreshToken'
})
// Clear storage and reload the page
await clearStorage({ page: newPage })
await newPage.reload()
await expect(newPage.getByText(/sign in to the application/i)).toBeVisible()
// User should be signed in automatically
await newPage.goto(`${baseURL}/profile#refreshToken=${refreshToken}`)
await expect(newPage.getByText(/profile page/i)).toBeVisible()
})
test('should fail automatic sign-in when network is not available', async ({ page }) => {
await page.goto('/')
const email = faker.internet.email()
const password = faker.internet.password()
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await expect(newPage.getByText(/you are authenticated/i)).toBeVisible()
const refreshToken = await getValueFromLocalStorage({
page: newPage,
origin: baseURL,
key: 'nhostRefreshToken'
})
// Clear storage and reload the page
await clearStorage({ page: newPage })
await newPage.reload()
await expect(newPage.getByText(/sign in to the application/i)).toBeVisible()
await newPage.route(`${authBackendURL}/**`, (route) => route.abort('internetdisconnected'))
// User should be signed in automatically
await newPage.goto(`${baseURL}/profile#refreshToken=${refreshToken}`)
await expect(
newPage.getByText(/could not sign in automatically. retrying to get user information/i)
).toBeVisible()
})

View File

@@ -0,0 +1,84 @@
import { faker } from '@faker-js/faker'
import type { Page } from '@playwright/test'
import { expect, test } from '@playwright/test'
import totp from 'totp-generator'
import { baseURL } from '../config'
import {
decodeQRCode,
signInWithEmailAndPassword,
signUpWithEmailAndPassword,
verifyEmail
} from '../utils'
const email = faker.internet.email()
const password = faker.internet.password()
let page: Page
test.describe.configure({ mode: 'serial' })
test.beforeAll(async ({ browser }) => {
page = await browser.newPage()
await page.goto('/')
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const newPage = await verifyEmail({ page, email, context: page.context() })
await expect(newPage.getByText(/you are authenticated/i)).toBeVisible()
await newPage.getByRole('button', { name: /sign out/i }).click()
page = newPage
})
test.afterEach(async () => {
await page.getByRole('button', { name: /sign out/i }).click()
})
test.afterAll(() => {
page.close()
})
test('should sign in with email and password', async () => {
await page.goto('/')
await signInWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/you are authenticated/i)).toBeVisible()
})
// TODO: Create email verification test
test('should activate and sign in with MFA', async () => {
await page.goto('/')
await signInWithEmailAndPassword({ page, email, password })
await page.waitForURL(baseURL)
await page.getByRole('button', { name: /profile/i }).click()
await page.getByRole('button', { name: /generate/i }).click()
const image = page.getByAltText(/qrcode/i)
const src = await image.getAttribute('src')
const { secret, algorithm, digits, period } = decodeQRCode(src)
const code = totp(secret, {
algorithm: algorithm.replace('SHA1', 'SHA-1'),
digits: parseInt(digits),
period: parseInt(period)
})
await page.getByPlaceholder(/enter activation code/i).fill(code)
await page.getByRole('button', { name: /activate/i }).click()
await expect(page.getByText(/mfa has been activated/i)).toBeVisible()
await page.getByRole('button', { name: /sign out/i }).click()
await page.getByRole('button', { name: /continue with email \+ password/i }).click()
await signInWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/send 2-step verification code/i)).toBeVisible()
const newCode = totp(secret, { timestamp: Date.now() })
await page.getByPlaceholder(/one-time password/i).fill(newCode)
await page.getByRole('button', { name: /send 2-step verification code/i }).click()
await expect(page.getByText(/you are authenticated/i)).toBeVisible()
})

View File

@@ -0,0 +1,53 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import {
getUserData,
signInAnonymously,
signUpWithEmailAndPassword,
signUpWithEmailPasswordless,
verifyEmail,
verifyMagicLink
} from '../utils'
test('should deanonymize with email and password', async ({ page, context }) => {
const email = faker.internet.email()
const password = faker.internet.password(8)
await page.goto('/')
await signInAnonymously({ page })
await page.getByRole('button', { name: /profile/i }).click()
const userData = await getUserData(page)
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const authenticatedPage = await verifyEmail({ page, context, email })
await authenticatedPage.getByRole('button', { name: /profile/i }).click()
const updatedUserData = await getUserData(authenticatedPage)
expect(updatedUserData.id).toBe(userData.id)
expect(updatedUserData.email).toBe(email)
})
test('should deanonymize with a magic link', async ({ page, context }) => {
const email = faker.internet.email()
await page.goto('/')
await signInAnonymously({ page })
await page.getByRole('button', { name: /profile/i }).click()
const userData = await getUserData(page)
await signUpWithEmailPasswordless({ page, email })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const authenticatedPage = await verifyMagicLink({ page, context, email })
await authenticatedPage.getByRole('button', { name: /profile/i }).click()
const updatedUserData = await getUserData(authenticatedPage)
expect(updatedUserData.id).toBe(userData.id)
expect(updatedUserData.email).toBe(email)
})

View File

@@ -0,0 +1,44 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { signUpWithEmailAndPassword, verifyEmail } from '../utils'
test('should sign up with email and password', async ({ page, context }) => {
page.goto('/')
const email = faker.internet.email()
const password = faker.internet.password()
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const authenticatedPage = await verifyEmail({ page, context, email })
await expect(authenticatedPage.getByText(/you are authenticated/i)).toBeVisible()
})
test('should raise an error when trying to sign up with an existing email', async ({ page }) => {
page.goto('/')
const email = faker.internet.email()
const password = faker.internet.password()
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
// close modal
await page.getByRole('dialog').getByRole('button').click()
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/email already in use/i)).toBeVisible()
})
test('should fail when network is not available', async ({ page }) => {
await page.goto('/')
const email = faker.internet.email()
const password = faker.internet.password()
await page.route('**', (route) => route.abort('internetdisconnected'))
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/network error/i)).toBeVisible()
})

View File

@@ -0,0 +1,27 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { signUpWithEmailPasswordless, verifyMagicLink } from '../utils'
test('should sign up with a magic link', async ({ page, context }) => {
page.goto('/')
const email = faker.internet.email()
await signUpWithEmailPasswordless({ page, email })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
const authenticatedPage = await verifyMagicLink({ page, context, email })
await authenticatedPage.getByRole('button', { name: /home/i }).click()
await expect(authenticatedPage.getByText(/you are authenticated/i)).toBeVisible()
})
test('should fail when network is not available', async ({ page }) => {
await page.goto('/')
const email = faker.internet.email()
await page.route('**', (route) => route.abort('internetdisconnected'))
await signUpWithEmailPasswordless({ page, email })
await expect(page.getByText(/network error/i)).toBeVisible()
})

View File

@@ -0,0 +1,14 @@
import { expect, test } from '@playwright/test'
import { baseURL } from '../config'
test('should redirect to /sign-in when not authenticated', async ({ page }) => {
await page.goto(`${baseURL}`)
await page.waitForURL(`${baseURL}/sign-in`)
await expect(page.getByText(/sign in to the application/i)).toBeVisible()
await page.goto(`${baseURL}/apollo`)
await page.waitForURL(`${baseURL}/sign-in`)
await expect(page.getByText(/sign in to the application/i)).toBeVisible()
})

View File

@@ -0,0 +1,28 @@
import { faker } from '@faker-js/faker'
import { expect, test } from '@playwright/test'
import { baseURL } from '../config'
import { resetPassword, signUpWithEmailAndPassword } from '../utils'
test('should reset password', async ({ page, context }) => {
await page.goto('/')
const email = faker.internet.email()
const password = faker.internet.password(8)
await signUpWithEmailAndPassword({ page, email, password })
await expect(page.getByText(/verification email sent/i)).toBeVisible()
await page.goto(`${baseURL}/sign-in`)
await page.getByRole('button', { name: /continue with email \+ password/i }).click()
await page.getByRole('button', { name: /forgot password?/i }).click()
await page.getByPlaceholder('Email Address').type(email)
await page.getByRole('button', { name: /reset your password/i }).click()
const authenticatedPage = await resetPassword({ page, context, email })
await authenticatedPage.waitForLoadState()
await authenticatedPage.getByRole('button', { name: /profile/i }).click()
await expect(authenticatedPage.getByText(/profile page/i)).toBeVisible()
})

View File

@@ -0,0 +1,265 @@
import { faker } from '@faker-js/faker'
import type { User } from '@nhost/react'
import type { BrowserContext, Page } from '@playwright/test'
import jsQR from 'jsqr'
import { PNG } from 'pngjs'
import { baseURL, mailhogURL } from './config'
/**
* Returns the user data from the profile page.
*
* @param page - The page to get the user data from.
* @returns The user data.
*/
export async function getUserData(page: Page) {
const textContent = await page.locator('h1:has-text("User information") + div pre').textContent()
const userData = textContent ? JSON.parse(textContent) : {}
return userData as User
}
/**
* Returns a promise that resolves when the sign up flow is completed.
*
* @param page - The page to sign up with.
* @param email - The email address to sign up with.
* @param password - The password to sign up with.
*/
export async function signUpWithEmailAndPassword({
page,
email,
password
}: {
page: Page
email: string
password: string
}) {
await page.getByRole('button', { name: /home/i }).click()
await page.getByRole('link', { name: /sign up/i }).click()
await page.getByRole('button', { name: /continue with email \+ password/i }).click()
await page.getByPlaceholder(/first name/i).type(faker.name.firstName())
await page.getByPlaceholder(/last name/i).type(faker.name.lastName())
await page.getByPlaceholder(/email address/i).type(email)
await page.getByPlaceholder(/^password$/i).type(password)
await page.getByPlaceholder(/confirm password/i).type(password)
await page.getByRole('button', { name: /continue with email \+ password/i }).click()
}
/**
* Returns a promise that resolves when the sign in flow is completed.
*
* @param page - The page to sign in with.
* @param email - The email address to sign in with.
* @param password - The password to sign in with.
*/
export async function signInWithEmailAndPassword({
page,
email,
password
}: {
page: Page
email: string
password: string
}) {
await page.getByRole('button', { name: /home/i }).click()
await page.getByRole('button', { name: /continue with email \+ password/i }).click()
await page.getByPlaceholder(/email address/i).type(email)
await page.getByPlaceholder(/password/i).type(password)
await page.getByRole('button', { name: /sign in/i }).click()
}
/**
* Returns a promise that resolves when the sign in flow is completed.
*
* @param page - The page to sign in with.
*/
export async function signInAnonymously({ page }: { page: Page }) {
await page.getByRole('button', { name: /home/i }).click()
await page.getByRole('link', { name: /sign in anonymously/i }).click()
await page.waitForURL(baseURL)
}
/**
* Returns a promise that resolves when the sign up flow is completed.
*
* @param page - The page to sign up with.
* @param email - The email address to sign up with.
*/
export async function signUpWithEmailPasswordless({ page, email }: { page: Page; email: string }) {
await page.getByRole('button', { name: /home/i }).click()
await page.getByRole('link', { name: /sign up/i }).click()
await page.getByRole('button', { name: /continue with a magic link/i }).click()
await page.getByPlaceholder(/email address/i).fill(email)
await page.getByRole('button', { name: /continue with email/i }).click()
}
/**
* Returns a promise that resolves to a new page that is opened after clicking
* the magic link in the email.
*
* @param email - The email address to reset the password for.
* @param page - The page to click the magic link in.
* @param context - The browser context.
* @returns A promise that resolves to a new page.
*/
export async function verifyMagicLink({
email,
page,
context
}: {
email: string
page: Page
context: BrowserContext
}) {
await page.goto(mailhogURL)
await page.locator('.messages > .msglist-message', { hasText: email }).nth(0).click()
// Based on: https://playwright.dev/docs/pages#handling-new-pages
const authenticatedPagePromise = context.waitForEvent('page')
await page
.frameLocator('#preview-html')
.getByRole('link', { name: /sign in/i })
.click()
const authenticatedPage = await authenticatedPagePromise
await authenticatedPage.waitForLoadState()
return authenticatedPage
}
/**
* Returns a promise that resolves to a new page that is opened after clicking
* the reset password link in the email.
*
* @param email - The email address to reset the password for.
* @param page - The page to click the reset password link in.
* @param context - The browser context.
* @returns A promise that resolves to a new page.
*/
export async function resetPassword({
email,
page,
context
}: {
email: string
page: Page
context: BrowserContext
}) {
await page.goto(mailhogURL)
await page.locator('.messages > .msglist-message', { hasText: email }).nth(0).click()
// Based on: https://playwright.dev/docs/pages#handling-new-pages
const authenticatedPagePromise = context.waitForEvent('page')
await page
.frameLocator('#preview-html')
.getByRole('link', { name: /reset password/i })
.click()
const authenticatedPage = await authenticatedPagePromise
await authenticatedPage.waitForLoadState()
return authenticatedPage
}
/**
* Returns a promise that resolves to a new page that is opened after clicking
* the verify email link in the email.
*
* @param email - The email address to verify.
* @param page - The page to click the verify email link in.
* @param context - The browser context.
* @param linkText - The text of the link to click.
* @returns A promise that resolves to a new page.
*/
export async function verifyEmail({
email,
page,
context,
linkText = /verify email/i
}: {
email: string
page: Page
context: BrowserContext
linkText?: string | RegExp
}) {
await page.goto(mailhogURL)
await page.locator('.messages > .msglist-message', { hasText: email }).nth(0).click()
// Based on: https://playwright.dev/docs/pages#handling-new-pages
const authenticatedPagePromise = context.waitForEvent('page')
await page.frameLocator('#preview-html').getByRole('link', { name: linkText }).click()
const authenticatedPage = await authenticatedPagePromise
await authenticatedPage.waitForLoadState()
return authenticatedPage
}
/**
* Returns decoded data from a QR code.
*
* @param base64String - The base64 encoded string of the QR code.
* @returns The decoded data.
*/
export function decodeQRCode(base64String?: string | null) {
if (!base64String) {
return {
secret: '',
algorithm: '',
digits: '',
period: ''
}
}
const buffer = Buffer.from(base64String.replace('data:image/png;base64,', ''), 'base64')
const pngData = PNG.sync.read(buffer)
const decoded = jsQR(Uint8ClampedArray.from(pngData.data), pngData.width, pngData.height)
const params = decoded?.data?.split('?').at(-1)
// note: we are decoding MFA here
const { secret, algorithm, digits, period } = Object.fromEntries(new URLSearchParams(params))
return { secret, algorithm, digits, period }
}
/**
* Clears the local and session storage for a page.
*
* @param page - The page to clear the storage for.
*/
export async function clearStorage({ page }: { page: Page }) {
await page.evaluate(() => {
localStorage.clear()
sessionStorage.clear()
})
}
/**
* Returns a promise that resolves to the value of a key in local storage.
*
* @param page - The page to get the value from.
* @param origin - The origin of the local storage.
* @param key - The key to get the value for.
* @returns The value of the key in local storage.
*/
export async function getValueFromLocalStorage({
page,
origin: externalOrigin,
key
}: {
page: Page
origin: string
key: string
}) {
const storageState = await page.context().storageState()
const localStorage = storageState.origins.find(
({ origin }) => origin === externalOrigin
)?.localStorage
const value = localStorage?.find(({ name }) => name === key)?.value
return value || null
}

View File

@@ -1,6 +1,6 @@
{
"name": "@nhost-examples/react-apollo",
"version": "0.1.9",
"version": "0.1.10",
"private": true,
"dependencies": {
"@apollo/client": "^3.6.9",
@@ -22,11 +22,10 @@
"scripts": {
"dev": "vite --host localhost --port 3000",
"generate": "graphql-codegen --config graphql.config.yaml",
"cypress": "cypress open",
"e2e": "start-test e2e:backend http-get://localhost:9695 e2e:frontend 3000 e2e:test",
"e2e:test": "cypress run",
"e2e:backend": "nhost dev --no-browser",
"e2e:frontend": "run-s build preview",
"e2e": "start-test e2e:start-backend http-get://localhost:9695 e2e:test",
"e2e:test": "npx playwright@1.31.2 install --with-deps && playwright test",
"e2e:start-backend": "nhost dev --no-browser",
"e2e:start-ui": "run-s build preview",
"build": "vite build",
"preview": "vite preview --host localhost --port 3000",
"prettier": "prettier --check .",
@@ -52,14 +51,16 @@
"@faker-js/faker": "^7.6.0",
"@graphql-codegen/cli": "^2.12.0",
"@nuintun/qrcode": "^3.3.0",
"@testing-library/cypress": "^8.0.3",
"@playwright/test": "^1.31.2",
"@types/pngjs": "^6.0.1",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"@types/totp-generator": "^0.0.4",
"@vitejs/plugin-react": "^3.0.0",
"@xstate/inspect": "^0.6.2",
"cypress": "^10.7.0",
"cypress-mailhog": "^1.6.0",
"dotenv": "^16.0.3",
"jsqr": "^1.4.0",
"pngjs": "^7.0.0",
"start-server-and-test": "^1.15.2",
"totp-generator": "^0.0.13",
"typescript": "^4.8.2",

View File

@@ -0,0 +1,32 @@
import { defineConfig, devices } from '@playwright/test'
import dotenv from 'dotenv'
import path from 'path'
dotenv.config({ path: path.resolve(__dirname, '.env.test') })
export default defineConfig({
testDir: './e2e',
timeout: 30 * 1000,
expect: {
timeout: 5000
},
webServer: {
command: 'pnpm e2e:start-ui',
port: 3000
},
use: {
baseURL: 'http://localhost:3000'
},
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: 'html',
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] }
}
]
})

View File

@@ -14,7 +14,7 @@ if (devTools) {
}
const nhost = new NhostClient({
subdomain: import.meta.env.VITE_NHOST_SUBDOMAIN || 'localhost',
subdomain: import.meta.env.VITE_NHOST_SUBDOMAIN || 'local',
region: import.meta.env.VITE_NHOST_REGION,
devTools
})

View File

@@ -15,8 +15,8 @@
"resolveJsonModule": true,
"isolatedModules": false,
"noEmit": true,
"types": ["node", "cypress", "@testing-library/cypress"],
"types": ["node"],
"jsx": "react-jsx"
},
"include": ["src", "cypress", "cypress.config.ts"]
"include": ["src"]
}

View File

@@ -1,5 +1,17 @@
# @nhost/apollo
## 5.1.3
### Patch Changes
- @nhost/nhost-js@2.1.2
## 5.1.2
### Patch Changes
- 912ed76c: fix(apollo): retry subscriptions on error
## 5.1.1
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@nhost/apollo",
"version": "5.1.1",
"version": "5.1.3",
"description": "Nhost Apollo Client library",
"license": "MIT",
"keywords": [
@@ -59,15 +59,15 @@
"verify:fix": "run-p prettier:fix lint:fix"
},
"peerDependencies": {
"@nhost/nhost-js": "workspace:*",
"@apollo/client": "^3.6.2"
"@apollo/client": "^3.7.10",
"@nhost/nhost-js": "workspace:*"
},
"dependencies": {
"graphql": "16.6.0",
"graphql-ws": "^5.10.1"
},
"devDependencies": {
"@nhost/nhost-js": "workspace:*",
"@apollo/client": "^3.7.3"
"@apollo/client": "^3.7.10",
"@nhost/nhost-js": "workspace:*"
}
}

View File

@@ -1,6 +1,5 @@
import {
ApolloClient,
ApolloClientOptions,
createHttpLink,
from,
InMemoryCache,
@@ -38,16 +37,19 @@ export const createApolloClient = ({
connectToDevTools = isBrowser && process.env.NODE_ENV === 'development',
onError,
link: customLink
}: NhostApolloClientOptions): ApolloClient<any> => {
let backendUrl = graphqlUrl || nhost?.graphql.getUrl()
}: NhostApolloClientOptions) => {
const backendUrl = graphqlUrl || nhost?.graphql.httpUrl
if (!backendUrl) {
throw Error("Can't initialize the Apollo Client: no backend Url has been provided")
}
const uri = backendUrl
const interpreter = nhost?.auth.client.interpreter
let token: string | null = null
const getAuthHeaders = () => {
function getAuthHeaders() {
// add headers
const resHeaders = {
...headers,
@@ -66,33 +68,28 @@ export const createApolloClient = ({
return resHeaders
}
const uri = backendUrl
const wsClient =
isBrowser &&
createRestartableClient({
url: uri.startsWith('https') ? uri.replace(/^https/, 'wss') : uri.replace(/^http/, 'ws'),
connectionParams: () => ({
headers: {
...headers,
...getAuthHeaders()
}
const wsClient = isBrowser
? createRestartableClient({
url: uri.startsWith('https') ? uri.replace(/^https/, 'wss') : uri.replace(/^http/, 'ws'),
shouldRetry: () => true,
retryAttempts: 10,
connectionParams: () => ({
headers: {
...headers,
...getAuthHeaders()
}
})
})
})
const wsLink = wsClient && new GraphQLWsLink(wsClient)
: null
const httpLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
...getAuthHeaders()
}
const wsLink = wsClient ? new GraphQLWsLink(wsClient) : null
const httpLink = setContext((_, { headers }) => ({
headers: {
...headers,
...getAuthHeaders()
}
}).concat(
createHttpLink({
uri
})
)
})).concat(createHttpLink({ uri }))
const link = wsLink
? split(
@@ -112,7 +109,7 @@ export const createApolloClient = ({
)
: httpLink
const apolloClientOptions: ApolloClientOptions<any> = {
const client = new ApolloClient({
cache: cache || new InMemoryCache(),
ssrMode: !isBrowser,
defaultOptions: {
@@ -120,34 +117,35 @@ export const createApolloClient = ({
fetchPolicy
}
},
connectToDevTools
}
// add link
if (customLink) {
apolloClientOptions.link = from([customLink])
} else {
apolloClientOptions.link = typeof onError === 'function' ? from([onError, link]) : from([link])
}
const client = new ApolloClient(apolloClientOptions)
connectToDevTools,
link: customLink
? from([customLink])
: from(typeof onError === 'function' ? [onError, link] : [link])
})
interpreter?.onTransition(async (state, event) => {
if (['SIGNOUT', 'SIGNED_IN', 'TOKEN_CHANGED'].includes(event.type)) {
const newToken = state.context.accessToken.value
token = newToken
if (event.type === 'SIGNOUT') {
token = null
try {
await client.resetStore()
} catch (error) {
console.error('Error resetting Apollo client cache')
console.error(error)
}
} else {
if (isBrowser && wsClient && wsClient.started()) {
wsClient.restart()
}
return
}
// update token
token = state.context.accessToken.value
if (!isBrowser) {
return
}
wsClient?.restart()
}
})

View File

@@ -3,7 +3,6 @@ import { Client, ClientOptions, createClient } from 'graphql-ws'
export interface RestartableClient extends Client {
restart(): void
started(): boolean
}
export function createRestartableClient(options: ClientOptions): RestartableClient {
@@ -11,18 +10,41 @@ export function createRestartableClient(options: ClientOptions): RestartableClie
let restart = () => {
restartRequested = true
}
let _started = false
const started = () => _started
let socket: WebSocket
let timedOut: NodeJS.Timeout
const client = createClient({
...options,
on: {
...options.on,
connected: () => {
_started = true
error: (error) => {
console.error(error)
options.on?.error?.(error)
restart()
},
ping: (received) => {
if (!received /* sent */) {
timedOut = setTimeout(() => {
// a close event `4499: Terminated` is issued to the current WebSocket and an
// artificial `{ code: 4499, reason: 'Terminated', wasClean: false }` close-event-like
// object is immediately emitted without waiting for the one coming from `WebSocket.onclose`
//
// calling terminate is not considered fatal and a connection retry will occur as expected
//
// see: https://github.com/enisdenjo/graphql-ws/discussions/290
client.terminate()
restart()
}, 5_000)
}
},
pong: (received) => {
if (received) {
clearTimeout(timedOut)
}
},
opened: (originalSocket) => {
const socket = originalSocket as WebSocket
socket = originalSocket as WebSocket
options.on?.opened?.(socket)
restart = () => {
@@ -47,7 +69,6 @@ export function createRestartableClient(options: ClientOptions): RestartableClie
return {
...client,
restart: () => restart(),
started
restart: () => restart()
}
}

View File

@@ -1,5 +1,20 @@
# @nhost/react-apollo
## 5.0.14
### Patch Changes
- @nhost/apollo@5.1.3
- @nhost/react@2.0.12
## 5.0.13
### Patch Changes
- 912ed76c: fix(apollo): retry subscriptions on error
- Updated dependencies [912ed76c]
- @nhost/apollo@5.1.2
## 5.0.12
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@nhost/react-apollo",
"version": "5.0.12",
"version": "5.0.14",
"description": "Nhost React Apollo client",
"license": "MIT",
"keywords": [
@@ -63,14 +63,14 @@
"@nhost/apollo": "workspace:*"
},
"peerDependencies": {
"@apollo/client": "^3.6.2",
"@apollo/client": "^3.7.10",
"@nhost/react": "workspace:*",
"graphql": "^16.0.0",
"react": "^17.0.0 || ^18.0.0",
"react-dom": "^17.0.0 || ^18.0.0"
},
"devDependencies": {
"@apollo/client": "^3.7.1",
"@apollo/client": "^3.7.10",
"@nhost/react": "workspace:*",
"@types/react": "^18.0.25",
"graphql": "16.6.0",

View File

@@ -9,7 +9,10 @@ import {
} from '@apollo/client'
import { useAuthenticated } from '@nhost/react'
export function useAuthQuery<TData = any, TVariables = OperationVariables>(
export function useAuthQuery<
TData = any,
TVariables extends OperationVariables = OperationVariables
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: QueryHookOptions<TData, TVariables>
) {
@@ -18,7 +21,10 @@ export function useAuthQuery<TData = any, TVariables = OperationVariables>(
return useQuery(query, newOptions)
}
export function useAuthSubscription<TData = any, TVariables = OperationVariables>(
export function useAuthSubscription<
TData = any,
TVariables extends OperationVariables = OperationVariables
>(
subscription: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: SubscriptionHookOptions<TData, TVariables>
) {

View File

@@ -22,9 +22,7 @@ export const NhostApolloProvider: React.FC<PropsWithChildren<NhostApolloClientOp
if (!client) {
setClient(createApolloClient(options))
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}, [client, options])
return <ApolloProvider client={client || mockApolloClient}>{children}</ApolloProvider>
}

View File

@@ -1,5 +1,11 @@
# @nhost/react-urql
## 2.0.12
### Patch Changes
- @nhost/react@2.0.12
## 2.0.11
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@nhost/react-urql",
"version": "2.0.11",
"version": "2.0.12",
"description": "Nhost React URQL client",
"license": "MIT",
"keywords": [

Some files were not shown because too many files have changed in this diff Show More