addons/isl/src/atoms/submitOptionAtoms.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {atom} from 'jotai';
b69ab319import {configBackedAtom} from '../jotaiUtils';
b69ab3110
b69ab3111/**
b69ab3112 * Atom for storing whether diffs should be submitted as drafts.
b69ab3113 * When true, the `--draft` flag is passed to submit commands.
b69ab3114 * Backed by user config 'isl.submitAsDraft'.
b69ab3115 *
b69ab3116 * Note: When this is set to false, publishWhenReady is also set to false
b69ab3117 * (since publish-when-ready requires draft mode).
b69ab3118 */
b69ab3119const submitAsDraftRaw = configBackedAtom<boolean>('isl.submitAsDraft', false);
b69ab3120
b69ab3121export const submitAsDraft = atom(
b69ab3122 get => get(submitAsDraftRaw),
b69ab3123 (_get, set, update: boolean | ((prev: boolean) => boolean)) => {
b69ab3124 const newValue = typeof update === 'function' ? update(_get(submitAsDraftRaw)) : update;
b69ab3125 set(submitAsDraftRaw, newValue);
b69ab3126 // Auto-disable publishWhenReady when draft is disabled
b69ab3127 if (!newValue) {
b69ab3128 set(publishWhenReadyRaw, false);
b69ab3129 }
b69ab3130 },
b69ab3131);
b69ab3132
b69ab3133/**
b69ab3134 * Raw atom for storing whether diffs should be published when ready.
b69ab3135 * This is the underlying storage atom; use publishWhenReady for the derived version
b69ab3136 * that enforces the constraint that draft mode must be enabled.
b69ab3137 */
b69ab3138const publishWhenReadyRaw = configBackedAtom<boolean>('isl.publishWhenReady', false);
b69ab3139
b69ab3140/**
b69ab3141 * Atom for storing whether diffs should be published when ready.
b69ab3142 * When true, the `--publish-when-ready` flag is passed to submit commands,
b69ab3143 * which triggers CI validation immediately on draft diffs and auto-publishes
b69ab3144 * when all signals pass.
b69ab3145 *
b69ab3146 * This atom enforces the constraint that publishWhenReady requires submitAsDraft:
b69ab3147 * - Reading: Returns false if submitAsDraft is false (even if raw value is true)
b69ab3148 * - Writing: When set to true, also enables submitAsDraft
b69ab3149 */
b69ab3150export const publishWhenReady = atom(
b69ab3151 get => get(submitAsDraftRaw) && get(publishWhenReadyRaw),
b69ab3152 (_get, set, update: boolean | ((prev: boolean) => boolean)) => {
b69ab3153 const currentValue = _get(submitAsDraftRaw) && _get(publishWhenReadyRaw);
b69ab3154 const newValue = typeof update === 'function' ? update(currentValue) : update;
b69ab3155 set(publishWhenReadyRaw, newValue);
b69ab3156 // Auto-enable draft when publishWhenReady is enabled
b69ab3157 if (newValue) {
b69ab3158 set(submitAsDraftRaw, true);
b69ab3159 }
b69ab3160 },
b69ab3161);