| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {atom} from 'jotai'; |
| 9 | import {configBackedAtom} from '../jotaiUtils'; |
| 10 | |
| 11 | /** |
| 12 | * Atom for storing whether diffs should be submitted as drafts. |
| 13 | * When true, the `--draft` flag is passed to submit commands. |
| 14 | * Backed by user config 'isl.submitAsDraft'. |
| 15 | * |
| 16 | * Note: When this is set to false, publishWhenReady is also set to false |
| 17 | * (since publish-when-ready requires draft mode). |
| 18 | */ |
| 19 | const submitAsDraftRaw = configBackedAtom<boolean>('isl.submitAsDraft', false); |
| 20 | |
| 21 | export const submitAsDraft = atom( |
| 22 | get => get(submitAsDraftRaw), |
| 23 | (_get, set, update: boolean | ((prev: boolean) => boolean)) => { |
| 24 | const newValue = typeof update === 'function' ? update(_get(submitAsDraftRaw)) : update; |
| 25 | set(submitAsDraftRaw, newValue); |
| 26 | // Auto-disable publishWhenReady when draft is disabled |
| 27 | if (!newValue) { |
| 28 | set(publishWhenReadyRaw, false); |
| 29 | } |
| 30 | }, |
| 31 | ); |
| 32 | |
| 33 | /** |
| 34 | * Raw atom for storing whether diffs should be published when ready. |
| 35 | * This is the underlying storage atom; use publishWhenReady for the derived version |
| 36 | * that enforces the constraint that draft mode must be enabled. |
| 37 | */ |
| 38 | const publishWhenReadyRaw = configBackedAtom<boolean>('isl.publishWhenReady', false); |
| 39 | |
| 40 | /** |
| 41 | * Atom for storing whether diffs should be published when ready. |
| 42 | * When true, the `--publish-when-ready` flag is passed to submit commands, |
| 43 | * which triggers CI validation immediately on draft diffs and auto-publishes |
| 44 | * when all signals pass. |
| 45 | * |
| 46 | * This atom enforces the constraint that publishWhenReady requires submitAsDraft: |
| 47 | * - Reading: Returns false if submitAsDraft is false (even if raw value is true) |
| 48 | * - Writing: When set to true, also enables submitAsDraft |
| 49 | */ |
| 50 | export const publishWhenReady = atom( |
| 51 | get => get(submitAsDraftRaw) && get(publishWhenReadyRaw), |
| 52 | (_get, set, update: boolean | ((prev: boolean) => boolean)) => { |
| 53 | const currentValue = _get(submitAsDraftRaw) && _get(publishWhenReadyRaw); |
| 54 | const newValue = typeof update === 'function' ? update(currentValue) : update; |
| 55 | set(publishWhenReadyRaw, newValue); |
| 56 | // Auto-enable draft when publishWhenReady is enabled |
| 57 | if (newValue) { |
| 58 | set(submitAsDraftRaw, true); |
| 59 | } |
| 60 | }, |
| 61 | ); |
| 62 | |