adjusted code styling to match ours
This commit is contained in:
8 files changed
+107
-104
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { VoiceRecorder } from '$plugins/voice-recorder-kit';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { Box, Text, color, config } from 'folds';
|
||||
import { useRef } from 'react';
|
||||
import { VoiceRecorder } from 'src/voice-recorder-kit';
|
||||
|
||||
type AudioMessageRecorderProps = {
|
||||
onRecordingComplete: (audioBlob: Blob) => void;
|
||||
|
||||
File renamed without changes.
+20
-29
@@ -1,10 +1,10 @@
|
||||
import type { FC, CSSProperties } from 'react';
|
||||
import type { CSSProperties } from 'react';
|
||||
import { useMemo, useRef, useEffect, useState } from 'react';
|
||||
import { useVoiceRecorder } from './useVoiceRecorder';
|
||||
import type { VoiceRecorderProps } from './types';
|
||||
import { PlayIcon, PauseIcon, StopIcon, RepeatIcon, DeleteIcon, ResumeIcon } from './icons';
|
||||
|
||||
const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
function VoiceRecorder(props: VoiceRecorderProps) {
|
||||
const {
|
||||
width,
|
||||
height,
|
||||
@@ -49,9 +49,7 @@ const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
state,
|
||||
isRecording,
|
||||
isStopped,
|
||||
isTemporaryStopped,
|
||||
isPlaying,
|
||||
isPaused,
|
||||
seconds,
|
||||
levels,
|
||||
error,
|
||||
@@ -63,7 +61,6 @@ const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
handlePlay,
|
||||
handleRestart,
|
||||
handleDelete,
|
||||
handleRecordAgain,
|
||||
} = useVoiceRecorder(recorderOptions);
|
||||
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
@@ -104,7 +101,7 @@ const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
}
|
||||
const calculatedBars = Math.floor(visualizerWidth / (barWidth + barGap));
|
||||
return Math.max(calculatedBars, 1);
|
||||
}, [visualizerWidth, levels.length]);
|
||||
}, [visualizerWidth, levels.length, barWidth, barGap]);
|
||||
|
||||
const displayedLevels = useMemo(() => {
|
||||
if (maxBars <= 0 || levels.length === 0) {
|
||||
@@ -148,7 +145,7 @@ const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
border: `1px solid ${borderColor}`,
|
||||
padding: typeof padding === 'number' ? `${padding}px` : padding,
|
||||
width: width ?? '100%',
|
||||
height: height,
|
||||
height,
|
||||
boxSizing: 'border-box',
|
||||
...style,
|
||||
}),
|
||||
@@ -189,27 +186,21 @@ const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
justifyContent: 'flex-start',
|
||||
}}
|
||||
>
|
||||
{displayedLevels.map((level, index) => {
|
||||
const clamped = Math.max(0.1, Math.min(level, 1));
|
||||
const barHeight = 1 + clamped * (visualizerBarHeight - 1);
|
||||
const barColor =
|
||||
typeof visualizerBarColor === 'function'
|
||||
? visualizerBarColor(level, index)
|
||||
: visualizerBarColor;
|
||||
|
||||
return (
|
||||
<span
|
||||
key={index}
|
||||
style={{
|
||||
width: barWidth,
|
||||
borderRadius: 999,
|
||||
backgroundColor: barColor,
|
||||
height: barHeight,
|
||||
flexShrink: 0,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
{displayedLevels.map((level) => (
|
||||
<span
|
||||
key={Math.random()}
|
||||
style={{
|
||||
width: barWidth,
|
||||
borderRadius: 999,
|
||||
backgroundColor:
|
||||
typeof visualizerBarColor === 'function'
|
||||
? visualizerBarColor(level, displayedLevels.indexOf(level))
|
||||
: visualizerBarColor,
|
||||
height: 1 + Math.max(0.1, Math.min(level, 1)) * (visualizerBarHeight - 1),
|
||||
flexShrink: 0,
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{state === 'recording' && (
|
||||
@@ -624,6 +615,6 @@ const VoiceRecorder: FC<VoiceRecorderProps> = (props) => {
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export default VoiceRecorder;
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { FC } from 'react';
|
||||
|
||||
export const PlayIcon: FC<{ size?: number }> = function ({ size = 18 }) {
|
||||
return (
|
||||
<svg
|
||||
width={size}
|
||||
height={size}
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.8" />
|
||||
<path d="M8.5 7L17 12L8.5 17Z" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
export const ResumeIcon: FC<{ size?: number }> = function ({ size = 18 }) {
|
||||
return (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<polygon points="8,5 19,12 8,19" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
export const PauseIcon: FC<{ size?: number }> = function ({ size = 18 }) {
|
||||
return (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<rect x="6" y="5" width="4" height="14" fill="currentColor" />
|
||||
<rect x="14" y="5" width="4" height="14" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
export const StopIcon: FC<{ size?: number }> = function ({ size = 18 }) {
|
||||
return (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<rect x="6" y="6" width="12" height="12" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
export const DeleteIcon: FC<{ size?: number }> = function ({ size = 18 }) {
|
||||
return (
|
||||
<svg
|
||||
width={size}
|
||||
height={size}
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path d="M4 7H20" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
<path d="M9 4H15" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
<path
|
||||
d="M6 7L7 20C7.06457 20.7423 7.3378 21.25 8.25 21.25H15.75C16.6622 21.25 16.9354 20.7423 17 20L18 7"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
<path d="M10 11V17" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
<path d="M14 11V17" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
export const RepeatIcon: FC<{ size?: number }> = function ({ size = 18 }) {
|
||||
return (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
File renamed without changes.
File renamed without changes.
+10
-10
@@ -19,11 +19,11 @@ function downsampleWaveform(samples: number[], targetCount: number): number[] {
|
||||
}
|
||||
const result: number[] = [];
|
||||
const blockSize = samples.length / targetCount;
|
||||
for (let i = 0; i < targetCount; i++) {
|
||||
for (let i = 0; i < targetCount; i += 1) {
|
||||
const start = Math.floor(i * blockSize);
|
||||
const end = Math.floor((i + 1) * blockSize);
|
||||
let sum = 0;
|
||||
for (let j = start; j < end; j++) {
|
||||
for (let j = start; j < end; j += 1) {
|
||||
sum += samples[j];
|
||||
}
|
||||
result.push(sum / (end - start));
|
||||
@@ -199,7 +199,7 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
analyserRef.current = analyser;
|
||||
dataArrayRef.current = dataArray;
|
||||
source.connect(analyser);
|
||||
void audioContext.resume();
|
||||
audioContext.resume().catch(() => {});
|
||||
animateLevels();
|
||||
},
|
||||
[animateLevels]
|
||||
@@ -219,7 +219,7 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
dataArrayRef.current = dataArray;
|
||||
source.connect(analyser);
|
||||
analyser.connect(audioContext.destination);
|
||||
void audioContext.resume();
|
||||
audioContext.resume().catch(() => {});
|
||||
animateLevels();
|
||||
},
|
||||
[animateLevels]
|
||||
@@ -326,7 +326,7 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
]);
|
||||
|
||||
const start = useCallback(() => {
|
||||
void internalStartRecording();
|
||||
internalStartRecording();
|
||||
}, [internalStartRecording]);
|
||||
|
||||
const handlePause = useCallback(() => {
|
||||
@@ -346,7 +346,7 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
}
|
||||
|
||||
if (audioContextRef.current && audioContextRef.current.state !== 'closed') {
|
||||
void audioContextRef.current.suspend();
|
||||
audioContextRef.current.suspend().catch(() => {});
|
||||
}
|
||||
|
||||
setLevels(Array.from({ length: BAR_COUNT }, () => 0.15));
|
||||
@@ -497,7 +497,7 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
audio.currentTime = 0;
|
||||
setSeconds(0);
|
||||
}
|
||||
void audio.play();
|
||||
audio.play().catch(() => {});
|
||||
}
|
||||
}, [
|
||||
audioUrl,
|
||||
@@ -546,7 +546,7 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
audio.currentTime = 0;
|
||||
setSeconds(0);
|
||||
}
|
||||
void audio.play();
|
||||
audio.play().catch(() => {});
|
||||
}
|
||||
}, [audioUrl, cleanupAudioContext, isPlaying, setupPlaybackGraph, startPlaybackTimer, stopTimer]);
|
||||
|
||||
@@ -730,12 +730,12 @@ export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): UseVoic
|
||||
|
||||
setAudioUrl(null);
|
||||
setAudioFile(null);
|
||||
void internalStartRecording();
|
||||
internalStartRecording();
|
||||
}, [cleanupAudioContext, cleanupStream, internalStartRecording, stopTimer]);
|
||||
|
||||
useEffect(() => {
|
||||
if (autoStart) {
|
||||
void internalStartRecording();
|
||||
internalStartRecording();
|
||||
}
|
||||
return () => {
|
||||
const mediaRecorder = mediaRecorderRef.current;
|
||||
@@ -1,64 +0,0 @@
|
||||
import type { FC } from 'react';
|
||||
|
||||
export const PlayIcon: FC<{ size?: number }> = ({ size = 18 }) => (
|
||||
<svg
|
||||
width={size}
|
||||
height={size}
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.8" />
|
||||
<path d="M8.5 7L17 12L8.5 17Z" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const ResumeIcon: FC<{ size?: number }> = ({ size = 18 }) => (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<polygon points="8,5 19,12 8,19" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const PauseIcon: FC<{ size?: number }> = ({ size = 18 }) => (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<rect x="6" y="5" width="4" height="14" fill="currentColor" />
|
||||
<rect x="14" y="5" width="4" height="14" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const StopIcon: FC<{ size?: number }> = ({ size = 18 }) => (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<rect x="6" y="6" width="12" height="12" fill="currentColor" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const DeleteIcon: FC<{ size?: number }> = ({ size = 18 }) => (
|
||||
<svg
|
||||
width={size}
|
||||
height={size}
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path d="M4 7H20" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
<path d="M9 4H15" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
<path
|
||||
d="M6 7L7 20C7.06457 20.7423 7.3378 21.25 8.25 21.25H15.75C16.6622 21.25 16.9354 20.7423 17 20L18 7"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
<path d="M10 11V17" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
<path d="M14 11V17" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const RepeatIcon: FC<{ size?: number }> = ({ size = 18 }) => (
|
||||
<svg width={size} height={size} viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
Reference in new issue
Block a user