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