Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/_locales/de/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Thema",
"API Mode": "API-Modus",
"Get": "Erhalten",
"Balance": "Kontostand",
"Preferred Language": "Bevorzugte Sprache",
"Insert ChatGPT at the top of search results": "ChatGPT am Anfang der Suchergebnisse einfügen",
"Lock scrollbar while answering": "Bildlaufleiste beim Beantworten sperren",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Theme",
"API Mode": "API Mode",
"Get": "Get",
"Balance": "Balance",
"Preferred Language": "Preferred Language",
"Insert ChatGPT at the top of search results": "Insert ChatGPT at the top of search results",
"Lock scrollbar while answering": "Lock scrollbar while answering",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/es/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Tema",
"API Mode": "Modo API",
"Get": "Obtener",
"Balance": "Balance",
"Preferred Language": "Idioma preferido",
"Insert ChatGPT at the top of search results": "Insertar ChatGPT en la parte superior de los resultados de búsqueda",
"Lock scrollbar while answering": "Bloquear barra de desplazamiento mientras se responde",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/fr/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Thème",
"API Mode": "Mode API",
"Get": "Obtenir",
"Balance": "Solde",
"Preferred Language": "Langue préférée",
"Insert ChatGPT at the top of search results": "Insérer ChatGPT en haut des résultats de recherche",
"Lock scrollbar while answering": "Verrouiller la barre de défilement pendant la réponse",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/in/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Tema",
"API Mode": "Mode API",
"Get": "Dapatkan",
"Balance": "Saldo",
"Preferred Language": "Bahasa yang Dipilih",
"Insert ChatGPT at the top of search results": "Masukkan ChatGPT di bagian atas hasil pencarian",
"Lock scrollbar while answering": "Kunci scrollbar saat menjawab",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/it/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Tema",
"API Mode": "Modalità API",
"Get": "Ottenere",
"Balance": "Saldo",
"Preferred Language": "Lingua preferita",
"Insert ChatGPT at the top of search results": "Inserisci ChatGPT in cima ai risultati di ricerca",
"Lock scrollbar while answering": "Blocca la barra di scorrimento durante la risposta",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/ja/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "テーマ",
"API Mode": "APIモード",
"Get": "取得",
"Balance": "残高",
"Preferred Language": "言語設定",
"Insert ChatGPT at the top of search results": "検索結果のトップにチャットGPTを挿入",
"Lock scrollbar while answering": "回答中にスクロールバーをロック",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/ko/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "테마",
"API Mode": "API 모드",
"Get": "받다",
"Balance": "잔액",
"Preferred Language": "선호하는 언어",
"Insert ChatGPT at the top of search results": "검색 결과 상단에 ChatGPT 삽입",
"Lock scrollbar while answering": "답변 중 스크롤바 잠금",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/pt/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Tema",
"API Mode": "Modo API",
"Get": "Obter",
"Balance": "Saldo",
"Preferred Language": "Idioma Preferido",
"Insert ChatGPT at the top of search results": "Inserir ChatGPT no topo dos resultados de pesquisa",
"Lock scrollbar while answering": "Bloquear barra de rolagem ao responder",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/ru/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Тема",
"API Mode": "Режим API",
"Get": "Получить",
"Balance": "Баланс",
"Preferred Language": "Предпочитаемый язык",
"Insert ChatGPT at the top of search results": "Вставить ChatGPT в верхней части результатов поиска",
"Lock scrollbar while answering": "Начало блокировки прокрутки во время ответа",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/tr/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "Tema",
"API Mode": "API Modu",
"Get": "Al",
"Balance": "Bakiye",
"Preferred Language": "Tercih Edilen Dil",
"Insert ChatGPT at the top of search results": "ChatGPT'yi arama sonuçlarının en üstüne ekle",
"Lock scrollbar while answering": "Cevap verirken kaydırma çubuğunu kilitle",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/zh-hans/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "主题",
"API Mode": "API模式",
"Get": "获取",
"Balance": "余额",
"Preferred Language": "语言偏好",
"Insert ChatGPT at the top of search results": "将对话卡片插入到搜索结果顶部",
"Lock scrollbar while answering": "回答时锁定滚动条",
Expand Down
1 change: 0 additions & 1 deletion src/_locales/zh-hant/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"Theme": "主題",
"API Mode": "API 模式",
"Get": "取得",
"Balance": "餘額",
"Preferred Language": "偏好語言",
"Insert ChatGPT at the top of search results": "在搜尋結果頂端插入 ChatGPT 對話卡片",
"Lock scrollbar while answering": "回答時鎖定捲軸",
Expand Down
80 changes: 1 addition & 79 deletions src/popup/sections/GeneralPart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useTranslation } from 'react-i18next'
import { useLayoutEffect, useState } from 'react'
import FileSaver from 'file-saver'
import {
openUrl,
modelNameToDesc,
isApiModeSelected,
getApiModesFromConfig,
Expand Down Expand Up @@ -38,66 +37,12 @@ GeneralPart.propTypes = {
setTabIndex: PropTypes.func.isRequired,
}

function formatDate(date) {
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')

return `${year}-${month}-${day}`
}

async function checkBilling(apiKey, apiUrl) {
const now = new Date()
let startDate = new Date(now - 90 * 24 * 60 * 60 * 1000)
const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1000)
const subDate = new Date(now)
subDate.setDate(1)

const urlSubscription = `${apiUrl}/v1/dashboard/billing/subscription`
let urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${formatDate(
startDate,
)}&end_date=${formatDate(endDate)}`
const headers = {
Authorization: 'Bearer ' + apiKey,
'Content-Type': 'application/json',
}

try {
let response = await fetch(urlSubscription, { headers })
if (!response.ok) {
console.log('Your account has been suspended. Please log in to OpenAI to check.')
return [null, null, null]
}
const subscriptionData = await response.json()
const totalAmount = subscriptionData.hard_limit_usd

if (totalAmount > 20) {
startDate = subDate
}

urlUsage = `${apiUrl}/v1/dashboard/billing/usage?start_date=${formatDate(
startDate,
)}&end_date=${formatDate(endDate)}`

response = await fetch(urlUsage, { headers })
const usageData = await response.json()
const totalUsage = usageData.total_usage / 100
const remaining = totalAmount - totalUsage

return [totalAmount, totalUsage, remaining]
} catch (error) {
console.error(error)
return [null, null, null]
}
}

function isUsingSpecialCustomModel(configOrSession) {
return isUsingCustomModel(configOrSession) && !configOrSession.apiMode
}

export function GeneralPart({ config, updateConfig, setTabIndex }) {
const { t, i18n } = useTranslation()
const [balance, setBalance] = useState(null)
const [apiModes, setApiModes] = useState([])

useLayoutEffect(() => {
Expand All @@ -109,21 +54,6 @@ export function GeneralPart({ config, updateConfig, setTabIndex }) {
config.ollamaModelName,
])

const getBalance = async () => {
const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.apiKey}`,
},
})
if (response.ok) setBalance((await response.json()).total_available.toFixed(2))
else {
const billing = await checkBilling(config.apiKey, config.customOpenAiApiUrl)
if (billing && billing.length > 2 && billing[2]) setBalance(`${billing[2].toFixed(2)}`)
else openUrl('https://platform.openai.com/account/usage')
}
}

return (
<>
<label>
Expand Down Expand Up @@ -251,15 +181,7 @@ export function GeneralPart({ config, updateConfig, setTabIndex }) {
{t('Get')}
</button>
</a>
) : balance ? (
<button type="button" onClick={getBalance}>
{balance}
</button>
) : (
<button type="button" onClick={getBalance}>
{t('Balance')}
</button>
)}
) : null}
</span>
)}
{isUsingSpecialCustomModel(config) && (
Expand Down
Loading