-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show completion provider status if down
- Loading branch information
Showing
14 changed files
with
441 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/components/CompletionProviderStatus/CompletionProviderStatus.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.memori--completion-provider-status--icon { | ||
width: 1em; | ||
height: 1em; | ||
fill: var(--memori-error-color, red); | ||
} | ||
|
||
.memori--completion-provider-status--tooltip.memori-tooltip.memori-tooltip--align-topLeft:not(.memori-tooltip--disabled).memori-tooltip--visible .memori-tooltip--content, | ||
.memori--completion-provider-status--tooltip.memori-tooltip.memori-tooltip--align-topLeft:not(.memori-tooltip--disabled):not(.memori-tooltip--visible):hover .memori-tooltip--content { | ||
transform: translateY(calc(-100% - 2em)) translateX(3em); | ||
} | ||
|
||
.memori--completion-provider-status--tooltip.memori-tooltip .memori-tooltip--content p { | ||
margin: 0.5em auto; | ||
} | ||
|
||
.memori--completion-provider-status--tooltip.memori-tooltip .memori-tooltip--content p+p { | ||
margin-top: 1em; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/components/CompletionProviderStatus/CompletionProviderStatus.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import { Meta, Story } from '@storybook/react'; | ||
import CompletionProviderStatus, { Props } from './CompletionProviderStatus'; | ||
|
||
import './CompletionProviderStatus.css'; | ||
|
||
const meta: Meta = { | ||
title: 'Completion Provider Status', | ||
component: CompletionProviderStatus, | ||
argTypes: {}, | ||
parameters: { | ||
layout: 'centered', | ||
controls: { expanded: true }, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
const Template: Story<Props> = args => <CompletionProviderStatus {...args} />; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = {}; | ||
|
||
export const Errored = Template.bind({}); | ||
Errored.args = { | ||
forceStatus: 'major', | ||
}; | ||
|
||
export const WithSpecifiedProvider = Template.bind({}); | ||
WithSpecifiedProvider.args = { | ||
provider: 'OpenAI', | ||
}; | ||
|
||
export const ErroredWithSpecifiedProvider = Template.bind({}); | ||
ErroredWithSpecifiedProvider.args = { | ||
forceStatus: 'major', | ||
provider: 'OpenAI', | ||
}; |
29 changes: 29 additions & 0 deletions
29
src/components/CompletionProviderStatus/CompletionProviderStatus.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import CompletionProviderStatus from './CompletionProviderStatus'; | ||
|
||
it('renders CompletionProviderStatus unchanged', () => { | ||
const { container } = render(<CompletionProviderStatus forceStatus="none" />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders CompletionProviderStatus errored unchanged', () => { | ||
const { container } = render( | ||
<CompletionProviderStatus forceStatus="major" /> | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders CompletionProviderStatus with provider specified unchanged', () => { | ||
const { container } = render( | ||
<CompletionProviderStatus provider="OpenAI" forceStatus="none" /> | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders CompletionProviderStatus errored with provider specified unchanged', () => { | ||
const { container } = render( | ||
<CompletionProviderStatus provider="OpenAI" forceStatus="major" /> | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); |
75 changes: 75 additions & 0 deletions
75
src/components/CompletionProviderStatus/CompletionProviderStatus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useEffect, useState } from 'react'; | ||
import Tooltip from '../ui/Tooltip'; | ||
import Warning from '../icons/Warning'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
export interface Props { | ||
forceStatus?: string; | ||
provider?: 'OpenAI' | string | null; | ||
} | ||
|
||
const initProviderStatus = (provider?: Props['provider']) => { | ||
switch (provider) { | ||
case 'DEFAULT': | ||
case 'OpenAI': | ||
return { | ||
getStatus: async () => { | ||
const res = await fetch( | ||
'https://status.openai.com/api/v2/summary.json' | ||
); | ||
const data = await res.json(); | ||
return data.status.indicator ?? 'none'; | ||
}, | ||
statusPage: 'https://status.openai.com/', | ||
}; | ||
default: | ||
return { | ||
getStatus: async () => 'none', | ||
statusPage: '', | ||
}; | ||
} | ||
}; | ||
|
||
const CompletionProviderStatus = ({ forceStatus, provider }: Props) => { | ||
const { t } = useTranslation(); | ||
const [status, setStatus] = useState(forceStatus ?? 'none'); | ||
|
||
const providerStatus = initProviderStatus(provider); | ||
|
||
useEffect(() => { | ||
if (forceStatus) return; | ||
|
||
providerStatus.getStatus().then(status => setStatus(status)); | ||
}, [forceStatus, provider]); | ||
|
||
return status !== 'none' ? ( | ||
<Tooltip | ||
className="memori--completion-provider-status--tooltip" | ||
align="topLeft" | ||
content={ | ||
<div> | ||
<p> | ||
{t('completionProviderDown', { | ||
provider: provider ?? t('completionProviderFallbackName'), | ||
})} | ||
</p> | ||
{!!providerStatus.statusPage?.length && ( | ||
<p> | ||
<a | ||
href={providerStatus.statusPage} | ||
rel="noopener noreferrer" | ||
target="_blank" | ||
> | ||
{t('completionProviderCheckStatusPage')} | ||
</a> | ||
</p> | ||
)} | ||
</div> | ||
} | ||
> | ||
<Warning className="memori--completion-provider-status--icon" /> | ||
</Tooltip> | ||
) : null; | ||
}; | ||
|
||
export default CompletionProviderStatus; |
82 changes: 82 additions & 0 deletions
82
src/components/CompletionProviderStatus/__snapshots__/CompletionProviderStatus.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders CompletionProviderStatus errored unchanged 1`] = ` | ||
<div> | ||
<div | ||
class="memori-tooltip memori-tooltip--align-topLeft memori--completion-provider-status--tooltip" | ||
> | ||
<div | ||
class="memori-tooltip--content" | ||
> | ||
<div> | ||
<p> | ||
completionProviderDown | ||
</p> | ||
</div> | ||
</div> | ||
<div | ||
class="memori-tooltip--trigger" | ||
> | ||
<svg | ||
aria-hidden="true" | ||
class="memori--completion-provider-status--icon" | ||
focusable="false" | ||
role="img" | ||
viewBox="0 0 1024 1024" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M464 720a48 48 0 1096 0 48 48 0 10-96 0zm16-304v184c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V416c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8zm475.7 440l-416-720c-6.2-10.7-16.9-16-27.7-16s-21.6 5.3-27.7 16l-416 720C56 877.4 71.4 904 96 904h832c24.6 0 40-26.6 27.7-48zm-783.5-27.9L512 239.9l339.8 588.2H172.2z" | ||
/> | ||
</svg> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`renders CompletionProviderStatus errored with provider specified unchanged 1`] = ` | ||
<div> | ||
<div | ||
class="memori-tooltip memori-tooltip--align-topLeft memori--completion-provider-status--tooltip" | ||
> | ||
<div | ||
class="memori-tooltip--content" | ||
> | ||
<div> | ||
<p> | ||
completionProviderDown | ||
</p> | ||
<p> | ||
<a | ||
href="https://status.openai.com/" | ||
rel="noopener noreferrer" | ||
target="_blank" | ||
> | ||
completionProviderCheckStatusPage | ||
</a> | ||
</p> | ||
</div> | ||
</div> | ||
<div | ||
class="memori-tooltip--trigger" | ||
> | ||
<svg | ||
aria-hidden="true" | ||
class="memori--completion-provider-status--icon" | ||
focusable="false" | ||
role="img" | ||
viewBox="0 0 1024 1024" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M464 720a48 48 0 1096 0 48 48 0 10-96 0zm16-304v184c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V416c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8zm475.7 440l-416-720c-6.2-10.7-16.9-16-27.7-16s-21.6 5.3-27.7 16l-416 720C56 877.4 71.4 904 96 904h832c24.6 0 40-26.6 27.7-48zm-783.5-27.9L512 239.9l339.8 588.2H172.2z" | ||
/> | ||
</svg> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`renders CompletionProviderStatus unchanged 1`] = `<div />`; | ||
|
||
exports[`renders CompletionProviderStatus with provider specified unchanged 1`] = `<div />`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.