addons/isl/src/HiddenMasterData.tsxblame
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 type {RepoInfo} from './types';
b69ab319
b69ab3110import {atom} from 'jotai';
b69ab3111import serverAPI from './ClientToServerAPI';
b69ab3112import {writeAtom} from './jotaiUtils';
b69ab3113import {repositoryInfo} from './serverAPIState';
b69ab3114import {registerDisposable} from './utils';
b69ab3115
b69ab3116/**
b69ab3117 * Hidden master branch config fetched from sitevar.
b69ab3118 * Maps OD type to list of repo paths where master should be hidden.
b69ab3119 * Example: {'instagram_www': ['fbsource/fbcode/instagram-server', 'fbsource/www']}
b69ab3120 */
b69ab3121export const hiddenMasterBranchConfigAtom = atom<Record<string, Array<string>> | null>(null);
b69ab3122
b69ab3123/**
b69ab3124 * OD type fetched once on startup
b69ab3125 */
b69ab3126const odTypeAtom = atom<string | null>(null);
b69ab3127
b69ab3128/**
b69ab3129 * Current working directory from server
b69ab3130 */
b69ab3131const cwdAtom = atom<string>('');
b69ab3132
b69ab3133/**
b69ab3134 * Computed atom that determines if master branch should be hidden
b69ab3135 * based on the fetched config, OD type, and current repo path.
b69ab3136 */
b69ab3137export const shouldHideMasterAtom = atom(get => {
b69ab3138 const config = get(hiddenMasterBranchConfigAtom);
b69ab3139 const odType = get(odTypeAtom);
b69ab3140 const cwd = get(cwdAtom);
b69ab3141 const repoInfo = get(repositoryInfo);
b69ab3142
b69ab3143 if (!repoInfo) {
b69ab3144 return false;
b69ab3145 }
b69ab3146
b69ab3147 return checkShouldHideMaster(config, odType, cwd, repoInfo);
b69ab3148});
b69ab3149
b69ab3150/**
b69ab3151 * Computed atom that indicates if the hidden master feature is available.
b69ab3152 * This is true when the sitevar config has been fetched and the current OD type
b69ab3153 * is enabled in the config.
b69ab3154 */
b69ab3155export const hiddenMasterFeatureAvailableAtom = atom(get => {
b69ab3156 const config = get(hiddenMasterBranchConfigAtom);
b69ab3157 const odType = get(odTypeAtom);
b69ab3158 // Feature is available if config exists and current OD type is in the config
b69ab3159 return config != null && odType != null && odType in config;
b69ab3160});
b69ab3161
b69ab3162/**
b69ab3163 * Check if master branch should be hidden based on sitevar config and repo path.
b69ab3164 */
b69ab3165function checkShouldHideMaster(
b69ab3166 hiddenMasterBranchConfig: Record<string, Array<string>> | null,
b69ab3167 odType: string | null,
b69ab3168 cwd: string,
b69ab3169 repoInfo: RepoInfo,
b69ab3170): boolean {
b69ab3171 if (!hiddenMasterBranchConfig || !odType) {
b69ab3172 return false;
b69ab3173 }
b69ab3174
b69ab3175 if (repoInfo.type !== 'success') {
b69ab3176 return false;
b69ab3177 }
b69ab3178
b69ab3179 const repoPaths = hiddenMasterBranchConfig[odType];
b69ab3180 if (!repoPaths) {
b69ab3181 return false;
b69ab3182 }
b69ab3183
b69ab3184 const repoRoot = repoInfo.repoRoot;
b69ab3185
b69ab3186 // Check if current working directory matches any of the configured paths
b69ab3187 const shouldHide = repoPaths.some(configPath => {
b69ab3188 // Strip 'fbsource/' prefix if present to get the relative path
b69ab3189 const relativePath = configPath.startsWith('fbsource/')
b69ab3190 ? configPath.substring('fbsource/'.length)
b69ab3191 : configPath;
b69ab3192
b69ab3193 // Construct the full expected path
b69ab3194 const fullPath = `${repoRoot}/${relativePath}`;
b69ab3195
b69ab3196 // Check if current working directory matches the configured path
b69ab3197 return cwd === fullPath || cwd.startsWith(`${fullPath}/`);
b69ab3198 });
b69ab3199
b69ab31100 return shouldHide;
b69ab31101}
b69ab31102
b69ab31103// Listen for config from server and store it
b69ab31104registerDisposable(
b69ab31105 serverAPI,
b69ab31106 serverAPI.onMessageOfType('fetchedHiddenMasterBranchConfig', data => {
b69ab31107 // Store the config, OD type, and cwd in atoms for quick access
b69ab31108 writeAtom(hiddenMasterBranchConfigAtom, data.config || {});
b69ab31109 writeAtom(odTypeAtom, data.odType || '');
b69ab31110 writeAtom(cwdAtom, data.cwd);
b69ab31111 }),
b69ab31112 import.meta.hot,
b69ab31113);