| 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 type {RepoInfo} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {atom} from 'jotai'; |
| b69ab31 | | | 11 | import serverAPI from './ClientToServerAPI'; |
| b69ab31 | | | 12 | import {writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 13 | import {repositoryInfo} from './serverAPIState'; |
| b69ab31 | | | 14 | import {registerDisposable} from './utils'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | /** |
| b69ab31 | | | 17 | * Hidden master branch config fetched from sitevar. |
| b69ab31 | | | 18 | * Maps OD type to list of repo paths where master should be hidden. |
| b69ab31 | | | 19 | * Example: {'instagram_www': ['fbsource/fbcode/instagram-server', 'fbsource/www']} |
| b69ab31 | | | 20 | */ |
| b69ab31 | | | 21 | export const hiddenMasterBranchConfigAtom = atom<Record<string, Array<string>> | null>(null); |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | /** |
| b69ab31 | | | 24 | * OD type fetched once on startup |
| b69ab31 | | | 25 | */ |
| b69ab31 | | | 26 | const odTypeAtom = atom<string | null>(null); |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | /** |
| b69ab31 | | | 29 | * Current working directory from server |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | const cwdAtom = atom<string>(''); |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | /** |
| b69ab31 | | | 34 | * Computed atom that determines if master branch should be hidden |
| b69ab31 | | | 35 | * based on the fetched config, OD type, and current repo path. |
| b69ab31 | | | 36 | */ |
| b69ab31 | | | 37 | export const shouldHideMasterAtom = atom(get => { |
| b69ab31 | | | 38 | const config = get(hiddenMasterBranchConfigAtom); |
| b69ab31 | | | 39 | const odType = get(odTypeAtom); |
| b69ab31 | | | 40 | const cwd = get(cwdAtom); |
| b69ab31 | | | 41 | const repoInfo = get(repositoryInfo); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | if (!repoInfo) { |
| b69ab31 | | | 44 | return false; |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | return checkShouldHideMaster(config, odType, cwd, repoInfo); |
| b69ab31 | | | 48 | }); |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | /** |
| b69ab31 | | | 51 | * Computed atom that indicates if the hidden master feature is available. |
| b69ab31 | | | 52 | * This is true when the sitevar config has been fetched and the current OD type |
| b69ab31 | | | 53 | * is enabled in the config. |
| b69ab31 | | | 54 | */ |
| b69ab31 | | | 55 | export const hiddenMasterFeatureAvailableAtom = atom(get => { |
| b69ab31 | | | 56 | const config = get(hiddenMasterBranchConfigAtom); |
| b69ab31 | | | 57 | const odType = get(odTypeAtom); |
| b69ab31 | | | 58 | // Feature is available if config exists and current OD type is in the config |
| b69ab31 | | | 59 | return config != null && odType != null && odType in config; |
| b69ab31 | | | 60 | }); |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | /** |
| b69ab31 | | | 63 | * Check if master branch should be hidden based on sitevar config and repo path. |
| b69ab31 | | | 64 | */ |
| b69ab31 | | | 65 | function checkShouldHideMaster( |
| b69ab31 | | | 66 | hiddenMasterBranchConfig: Record<string, Array<string>> | null, |
| b69ab31 | | | 67 | odType: string | null, |
| b69ab31 | | | 68 | cwd: string, |
| b69ab31 | | | 69 | repoInfo: RepoInfo, |
| b69ab31 | | | 70 | ): boolean { |
| b69ab31 | | | 71 | if (!hiddenMasterBranchConfig || !odType) { |
| b69ab31 | | | 72 | return false; |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | if (repoInfo.type !== 'success') { |
| b69ab31 | | | 76 | return false; |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | const repoPaths = hiddenMasterBranchConfig[odType]; |
| b69ab31 | | | 80 | if (!repoPaths) { |
| b69ab31 | | | 81 | return false; |
| b69ab31 | | | 82 | } |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | const repoRoot = repoInfo.repoRoot; |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | // Check if current working directory matches any of the configured paths |
| b69ab31 | | | 87 | const shouldHide = repoPaths.some(configPath => { |
| b69ab31 | | | 88 | // Strip 'fbsource/' prefix if present to get the relative path |
| b69ab31 | | | 89 | const relativePath = configPath.startsWith('fbsource/') |
| b69ab31 | | | 90 | ? configPath.substring('fbsource/'.length) |
| b69ab31 | | | 91 | : configPath; |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | // Construct the full expected path |
| b69ab31 | | | 94 | const fullPath = `${repoRoot}/${relativePath}`; |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | // Check if current working directory matches the configured path |
| b69ab31 | | | 97 | return cwd === fullPath || cwd.startsWith(`${fullPath}/`); |
| b69ab31 | | | 98 | }); |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | return shouldHide; |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | // Listen for config from server and store it |
| b69ab31 | | | 104 | registerDisposable( |
| b69ab31 | | | 105 | serverAPI, |
| b69ab31 | | | 106 | serverAPI.onMessageOfType('fetchedHiddenMasterBranchConfig', data => { |
| b69ab31 | | | 107 | // Store the config, OD type, and cwd in atoms for quick access |
| b69ab31 | | | 108 | writeAtom(hiddenMasterBranchConfigAtom, data.config || {}); |
| b69ab31 | | | 109 | writeAtom(odTypeAtom, data.odType || ''); |
| b69ab31 | | | 110 | writeAtom(cwdAtom, data.cwd); |
| b69ab31 | | | 111 | }), |
| b69ab31 | | | 112 | import.meta.hot, |
| b69ab31 | | | 113 | ); |