|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { useAppDispatch, useAppSelector } from '../redux/hooks'; |
| 3 | +import TextField from '@webapp/ui/Form/TextField'; |
| 4 | +import { |
| 5 | + Dialog, |
| 6 | + DialogBody, |
| 7 | + DialogFooter, |
| 8 | + DialogHeader, |
| 9 | +} from '@webapp/ui/Dialog'; |
| 10 | +import Button from '@webapp/ui/Button'; |
| 11 | +import { |
| 12 | + checkTenancyIsRequired, |
| 13 | + selectTenancy, |
| 14 | + actions, |
| 15 | + selectTenantID, |
| 16 | +} from '@phlare/redux/reducers/tenant'; |
| 17 | + |
| 18 | +export function TenantWall({ children }: { children: React.ReactNode }) { |
| 19 | + const dispatch = useAppDispatch(); |
| 20 | + const tenancy = useAppSelector(selectTenancy); |
| 21 | + const currentTenant = useAppSelector(selectTenantID); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + void dispatch(checkTenancyIsRequired()); |
| 25 | + }, [dispatch]); |
| 26 | + |
| 27 | + // Don't rerender all the children when this component changes |
| 28 | + // For example, when user wants to change the tenant ID |
| 29 | + const memoedChildren = React.useMemo(() => children, []); |
| 30 | + |
| 31 | + switch (tenancy) { |
| 32 | + case 'unknown': |
| 33 | + case 'loading': { |
| 34 | + return <></>; |
| 35 | + } |
| 36 | + case 'wants_to_change': { |
| 37 | + return ( |
| 38 | + <> |
| 39 | + <SelectTenantIDDialog |
| 40 | + currentTenantID={currentTenant} |
| 41 | + onSaved={(tenantID) => { |
| 42 | + void dispatch(actions.setTenantID(tenantID)); |
| 43 | + }} |
| 44 | + /> |
| 45 | + {memoedChildren} |
| 46 | + </> |
| 47 | + ); |
| 48 | + } |
| 49 | + case 'needs_tenant_id': { |
| 50 | + return ( |
| 51 | + <SelectTenantIDDialog |
| 52 | + currentTenantID={currentTenant} |
| 53 | + onSaved={(tenantID) => { |
| 54 | + void dispatch(actions.setTenantID(tenantID)); |
| 55 | + }} |
| 56 | + /> |
| 57 | + ); |
| 58 | + } |
| 59 | + case 'multi_tenant': |
| 60 | + case 'single_tenant': { |
| 61 | + return <>{memoedChildren}</>; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function SelectTenantIDDialog({ |
| 67 | + currentTenantID, |
| 68 | + onSaved, |
| 69 | +}: { |
| 70 | + currentTenantID?: string; |
| 71 | + onSaved: (tenantID: string) => void; |
| 72 | +}) { |
| 73 | + const [isDialogOpen] = useState(true); |
| 74 | + const handleFormSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
| 75 | + e.preventDefault(); |
| 76 | + |
| 77 | + const target = e.target as typeof e.target & { |
| 78 | + tenantID: { value: string }; |
| 79 | + }; |
| 80 | + |
| 81 | + onSaved(target.tenantID.value); |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <Dialog open={isDialogOpen} aria-labelledby="dialog-header"> |
| 87 | + <> |
| 88 | + <DialogHeader> |
| 89 | + <h3 id="dialog-header">Enter a Tenant ID</h3> |
| 90 | + </DialogHeader> |
| 91 | + <form |
| 92 | + onSubmit={(e) => { |
| 93 | + void handleFormSubmit(e); |
| 94 | + }} |
| 95 | + > |
| 96 | + <DialogBody> |
| 97 | + <> |
| 98 | + <p> |
| 99 | + Your instance has been detected as a multitenant one. Please |
| 100 | + enter a Tenant ID (You can change it at any time via the |
| 101 | + sidebar). |
| 102 | + </p> |
| 103 | + <p> |
| 104 | + Notice that if you migrated from a non-multitenant version, |
| 105 | + data can be found under Tenant ID "anonymous". |
| 106 | + </p> |
| 107 | + |
| 108 | + <TextField |
| 109 | + defaultValue={currentTenantID} |
| 110 | + label="Tenant ID" |
| 111 | + required |
| 112 | + id="tenantID" |
| 113 | + type="text" |
| 114 | + autoFocus |
| 115 | + /> |
| 116 | + </> |
| 117 | + </DialogBody> |
| 118 | + <DialogFooter> |
| 119 | + <Button type="submit" kind="secondary"> |
| 120 | + Submit |
| 121 | + </Button> |
| 122 | + </DialogFooter> |
| 123 | + </form> |
| 124 | + </> |
| 125 | + </Dialog> |
| 126 | + </> |
| 127 | + ); |
| 128 | +} |
0 commit comments