|
| 1 | +import { activeSubscriptions, db } from '@/library/powersync/ConnectionManager'; |
| 2 | +import Button from '@mui/material/Button'; |
| 3 | +import Dialog from '@mui/material/Dialog'; |
| 4 | +import DialogActions from '@mui/material/DialogActions'; |
| 5 | +import DialogContent from '@mui/material/DialogContent'; |
| 6 | +import DialogContentText from '@mui/material/DialogContentText'; |
| 7 | +import DialogTitle from '@mui/material/DialogTitle'; |
| 8 | +import FormControl from '@mui/material/FormControl'; |
| 9 | +import InputLabel from '@mui/material/InputLabel'; |
| 10 | +import MenuItem from '@mui/material/MenuItem'; |
| 11 | +import Select from '@mui/material/Select'; |
| 12 | +import Stack from '@mui/material/Stack'; |
| 13 | +import TextField from '@mui/material/TextField'; |
| 14 | +import { Formik, FormikErrors } from 'formik'; |
| 15 | + |
| 16 | +interface NewStreamSubscriptionValues { |
| 17 | + stream: string; |
| 18 | + parameters: string; |
| 19 | + override_priority: 0 | 1 | 2 | 3 | null; |
| 20 | +} |
| 21 | + |
| 22 | +export function NewStreamSubscription(props: { open: boolean; close: () => void }) { |
| 23 | + const { open, close } = props; |
| 24 | + |
| 25 | + const validate = (values: NewStreamSubscriptionValues) => { |
| 26 | + const errors: FormikErrors<NewStreamSubscriptionValues> = {}; |
| 27 | + |
| 28 | + if (values.stream.length == 0) { |
| 29 | + errors.stream = 'Stream is required'; |
| 30 | + } |
| 31 | + |
| 32 | + if (values.parameters.length) { |
| 33 | + try { |
| 34 | + JSON.parse(values.parameters); |
| 35 | + } catch (e) { |
| 36 | + errors.parameters = 'Must be empty or a JSON object'; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return errors; |
| 41 | + }; |
| 42 | + |
| 43 | + const addSubscription = async (values: NewStreamSubscriptionValues) => { |
| 44 | + const parameters = values.parameters == '' ? null : JSON.parse(values.parameters); |
| 45 | + |
| 46 | + const subscription = await db |
| 47 | + .syncStream(values.stream, parameters) |
| 48 | + .subscribe({ priority: values.override_priority ?? undefined }); |
| 49 | + |
| 50 | + // We need to store subscriptions globally, because they have a finalizer set on them that would eventually clear |
| 51 | + // them otherwise. |
| 52 | + activeSubscriptions.push(subscription); |
| 53 | + close(); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <Formik<NewStreamSubscriptionValues> |
| 58 | + initialValues={{ stream: '', parameters: '', override_priority: null }} |
| 59 | + validateOnChange={true} |
| 60 | + onSubmit={addSubscription} |
| 61 | + validate={validate}> |
| 62 | + {({ values, errors, handleChange, handleBlur, isSubmitting, handleSubmit }) => ( |
| 63 | + <Dialog onClose={close} open={open}> |
| 64 | + <DialogTitle>Subscribe to sync stream</DialogTitle> |
| 65 | + <DialogContent> |
| 66 | + <form id="subscription-form" onSubmit={handleSubmit}> |
| 67 | + <DialogContentText> |
| 68 | + Enter stream name and parameters (as a JSON object or an empty string for null) to subscribe to a |
| 69 | + stream. |
| 70 | + </DialogContentText> |
| 71 | + <Stack direction="row" useFlexGap spacing={1} sx={{ mt: 2, mb: 1 }}> |
| 72 | + <FormControl sx={{ flex: 2, minWidth: 120 }}> |
| 73 | + <TextField |
| 74 | + autoFocus |
| 75 | + required |
| 76 | + label="Stream name" |
| 77 | + name="stream" |
| 78 | + value={values.stream} |
| 79 | + onChange={handleChange} |
| 80 | + onBlur={handleBlur} |
| 81 | + error={!!errors.stream} |
| 82 | + helperText={errors.stream} |
| 83 | + /> |
| 84 | + </FormControl> |
| 85 | + <FormControl sx={{ flex: 1, minWidth: 120 }}> |
| 86 | + <InputLabel id="new-stream-priority">Override priority</InputLabel> |
| 87 | + <Select |
| 88 | + labelId="new-stream-priority" |
| 89 | + value={`${values.override_priority}`} |
| 90 | + label="Override priority" |
| 91 | + name="override_priority" |
| 92 | + onChange={handleChange}> |
| 93 | + <MenuItem value="null">Use default</MenuItem> |
| 94 | + <MenuItem value="0">0</MenuItem> |
| 95 | + <MenuItem value="1">1</MenuItem> |
| 96 | + <MenuItem value="2">2</MenuItem> |
| 97 | + <MenuItem value="3">2</MenuItem> |
| 98 | + </Select> |
| 99 | + </FormControl> |
| 100 | + </Stack> |
| 101 | + <Stack direction={'column'} sx={{ mt: 1, mb: 1 }}> |
| 102 | + <TextField |
| 103 | + label="Parameters" |
| 104 | + name="parameters" |
| 105 | + value={values.parameters} |
| 106 | + onChange={handleChange} |
| 107 | + onBlur={handleBlur} |
| 108 | + error={!!errors.parameters} |
| 109 | + helperText={errors.parameters} |
| 110 | + /> |
| 111 | + </Stack> |
| 112 | + </form> |
| 113 | + </DialogContent> |
| 114 | + <DialogActions> |
| 115 | + <Button onClick={close}>Cancel</Button> |
| 116 | + <Button type="submit" form="subscription-form" disabled={isSubmitting}> |
| 117 | + Subscribe |
| 118 | + </Button> |
| 119 | + </DialogActions> |
| 120 | + </Dialog> |
| 121 | + )} |
| 122 | + </Formik> |
| 123 | + ); |
| 124 | +} |
0 commit comments