Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 15, 2023
1 parent d3c8c7a commit 53c56b7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 42 deletions.
7 changes: 3 additions & 4 deletions components/episode-ai-thingy/episode-ai-thingy-generator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ export function EpisodeAIThingyGenerator({ credits, id }: Props) {

const generationButtonText = useMemo(() => {
switch (state.status) {
case 'countdown':
return state.timeRemaining === 0
? `THERE YOU GO!`
: `Presenting the AI thingy in ${state.timeRemaining} seconds...`;
case 'error':
return 'Error';

case 'idle':
return 'Do the AI thingy!';

case 'loading':
return 'Doing the AI thingy...';

case 'success':
return 'Done!';
}
Expand Down
6 changes: 3 additions & 3 deletions components/episode-ai-thingy/episode-ai-thingy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ export async function EpisodeAIThingy(props: Props) {
src={episodeContent.user.avatar_url ?? ''}
/>

<Text color="gray" size="2">
<Text color="gray" size="1">
Generated by{' '}
<Text highContrast>{episodeContent.user.display_name}</Text>
</Text>
</Flex>
) : (
<Text color="gray" size="2">
Generated by a generous beecast user
<Text color="gray" size="1">
Generated by a generous beecast user.
</Text>
)}
</Flex>
Expand Down
37 changes: 2 additions & 35 deletions components/episode-ai-thingy/use-episode-content-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
import type { Tables } from '@/types/supabase/database';

import { generateEpisodeContent } from '@/lib/services/ai/generate-episode-content';
import { useEffect, useState } from 'react';
import { useState } from 'react';

type Params = {
id: Tables<'episode'>['id'];
};

type State =
| {
data: string;
status: 'countdown';
timeRemaining: number;
}
| {
data: string;
status: 'success';
Expand All @@ -41,40 +36,12 @@ export function useEpisodeContentGenerator({ id }: Params) {

setState({
data: episodeContent.text_summary ?? '',
status: 'countdown',
timeRemaining: 3,
status: 'success',
});
} catch (error) {
setState({ error, status: 'error' });
}
};

useEffect(() => {
switch (state.status) {
case 'countdown': {
const interval = setInterval(() => {
setState((_state) => {
if (_state.status !== 'countdown') {
return _state;
}

if (_state.timeRemaining <= 0) {
return { ..._state, status: 'success' };
}

return { ..._state, timeRemaining: _state.timeRemaining - 1 };
});
}, 1000);

return () => {
clearInterval(interval);
};
}

default:
break;
}
}, [state]);

return [state, run] as const;
}

0 comments on commit 53c56b7

Please sign in to comment.