From 96ea3bb63a816ed63dfa47e2e446a4e6e03dc3b6 Mon Sep 17 00:00:00 2001 From: fregante Date: Sun, 10 Nov 2024 15:56:32 +0700 Subject: [PATCH] Refactor conversation state detections (#202) --- index.ts | 40 +++++++++++----------------------------- package.json | 1 + 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/index.ts b/index.ts index 4090e57..57eede6 100644 --- a/index.ts +++ b/index.ts @@ -286,38 +286,20 @@ TEST: addTests('isQuickPR', [ 'https://github.com/sindresorhus/refined-github/compare/test-branch?quick_pull=1', ]); -const stateSelector = [ - '.State', - '[data-testid="header-state"]', -].join(','); - -export const isDraftPR = (): boolean => $(stateSelector)?.textContent!.trim() === 'Draft'; -export const isOpenPR = (): boolean => { - if (!isPR()) { - return false; - } - - const status = $(stateSelector)!.textContent!.trim(); +const getStateLabel = (): string | undefined => $([ + '.State', // Old view + '[class^="StateLabel"]', // React version +].join(','))?.textContent?.trim(); + +export const isMergedPR = (): boolean => getStateLabel() === 'Merged'; +export const isDraftPR = (): boolean => getStateLabel() === 'Draft'; +export const isOpenConversation = (): boolean => { + const status = getStateLabel(); return status === 'Open' || status === 'Draft'; }; -export const isMergedPR = (): boolean => $(stateSelector)?.textContent!.trim() === 'Merged'; - -export const isClosedPR = (): boolean => { - if (!isPR()) { - return false; - } - - const status = $(stateSelector)!.textContent!.trim(); - return status === 'Closed' || status === 'Merged'; -}; - -export const isClosedIssue = (): boolean => { - if (!isIssue()) { - return false; - } - - const status = $(stateSelector)!.textContent!.trim(); +export const isClosedConversation = (): boolean => { + const status = getStateLabel(); return status === 'Closed' || status === 'Closed as not planned'; }; diff --git a/package.json b/package.json index c4d4f59..30a1cae 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "build:esbuild": "esbuild index.ts --bundle --external:github-reserved-names --outdir=distribution --format=esm --drop-labels=TEST", "build:typescript": "tsc --declaration --emitDeclarationOnly", "build:demo": "vite build demo", + "fix": "xo --fix", "prepack": "npm run build", "test": "run-p build test:* xo", "test:unit": "vitest",