|
| 1 | +import { useState } from "react"; |
| 2 | +import { Button, Flex, Heading, Text } from "@chakra-ui/react"; |
| 3 | + |
| 4 | +import { useHasPermission } from "@/hooks/useHasPermissions"; |
| 5 | +import AuthStatus from "@/components/transcript/AuthStatus"; |
| 6 | +import { |
| 7 | + useTranscriptionQueue, |
| 8 | + useTranscriptionBacklog, |
| 9 | +} from "@/services/api/admin"; |
| 10 | +import { SelectSourceMenu } from "@/components/tables/components"; |
| 11 | + |
| 12 | +import BaseTable from "@/components/tables/BaseTable"; |
| 13 | +import { convertStringToArray } from "@/utils"; |
| 14 | +import TitleWithTags from "@/components/tables/TitleWithTags"; |
| 15 | +import { TableStructure } from "@/components/tables/types"; |
| 16 | +import { TranscriptMetadata, TranscriptionQueueItem } from "../../../types"; |
| 17 | + |
| 18 | +const Sources = () => { |
| 19 | + const [selectedSource, setSelectedSource] = useState<string>("all"); |
| 20 | + const [removeFromQueueSelection, setRemoveFromQueueSelection] = useState< |
| 21 | + string[] |
| 22 | + >([]); |
| 23 | + const [addToQueueSelection, setAddToQueueSelection] = useState<string[]>([]); |
| 24 | + const canAccessTranscription = useHasPermission("accessTranscription"); |
| 25 | + |
| 26 | + const { transcriptionBacklog, sources } = |
| 27 | + useTranscriptionBacklog(selectedSource); |
| 28 | + const { |
| 29 | + transcriptionQueue, |
| 30 | + remainingBacklog, |
| 31 | + addToQueue, |
| 32 | + removeFromQueue, |
| 33 | + startTranscription, |
| 34 | + isTranscribing, |
| 35 | + refetch, |
| 36 | + } = useTranscriptionQueue(transcriptionBacklog.data); |
| 37 | + |
| 38 | + const handleAddToQueue = async () => { |
| 39 | + await addToQueue.mutateAsync(addToQueueSelection); |
| 40 | + setAddToQueueSelection([]); |
| 41 | + }; |
| 42 | + |
| 43 | + const handleRemoveFromQueue = async () => { |
| 44 | + await removeFromQueue.mutateAsync(removeFromQueueSelection); |
| 45 | + setRemoveFromQueueSelection([]); |
| 46 | + }; |
| 47 | + |
| 48 | + const tableStructure = [ |
| 49 | + { |
| 50 | + name: "Title", |
| 51 | + type: "default", |
| 52 | + modifier: () => null, |
| 53 | + component: (data) => { |
| 54 | + const allTags = convertStringToArray(data.tags); |
| 55 | + return ( |
| 56 | + <TitleWithTags |
| 57 | + title={data.title} |
| 58 | + allTags={allTags} |
| 59 | + categories={[]} |
| 60 | + loc={data.loc} |
| 61 | + url={data.media} |
| 62 | + id={0} |
| 63 | + length={allTags.length} |
| 64 | + shouldSlice={false} |
| 65 | + /> |
| 66 | + ); |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "speakers", |
| 71 | + type: "text-long", |
| 72 | + modifier: (data) => data.speakers.join(", "), |
| 73 | + }, |
| 74 | + { name: "Publish Date", type: "text-short", modifier: (data) => data.date }, |
| 75 | + ] satisfies TableStructure<TranscriptMetadata>[]; |
| 76 | + |
| 77 | + const transcriptionQueueTableStructure = [ |
| 78 | + ...tableStructure, |
| 79 | + { |
| 80 | + name: "status", |
| 81 | + type: "text-short", |
| 82 | + modifier: (data) => data.status, |
| 83 | + }, |
| 84 | + ] satisfies TableStructure<TranscriptionQueueItem>[]; |
| 85 | + |
| 86 | + if (!canAccessTranscription) { |
| 87 | + return ( |
| 88 | + <AuthStatus |
| 89 | + title="Unauthorized" |
| 90 | + message="You are not authorized to access this page" |
| 91 | + /> |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <> |
| 97 | + <Flex flexDir="column"> |
| 98 | + <Heading size={"md"} mb={10}> |
| 99 | + {`Transcription Management`} |
| 100 | + </Heading> |
| 101 | + <BaseTable |
| 102 | + data={transcriptionQueue.data} |
| 103 | + emptyView={ |
| 104 | + <Flex w="full" justifyContent="center" alignItems="center" gap={2}> |
| 105 | + <Text>Transcription queue is empty</Text> |
| 106 | + </Flex> |
| 107 | + } |
| 108 | + isLoading={transcriptionQueue.isLoading} |
| 109 | + isError={transcriptionQueue.isError} |
| 110 | + tableStructure={transcriptionQueueTableStructure} |
| 111 | + tableHeaderComponent={ |
| 112 | + <Heading size="sm" mb={1}> |
| 113 | + Transcription Queue |
| 114 | + </Heading> |
| 115 | + } |
| 116 | + enableCheckboxes={!isTranscribing} |
| 117 | + selectedRowIds={removeFromQueueSelection} |
| 118 | + onSelectedRowIdsChange={setRemoveFromQueueSelection} |
| 119 | + getRowId={(row) => row.media} |
| 120 | + refetch={refetch} |
| 121 | + actionItems={ |
| 122 | + <> |
| 123 | + {!isTranscribing && ( |
| 124 | + <Button |
| 125 | + isDisabled={removeFromQueueSelection.length == 0} |
| 126 | + isLoading={removeFromQueue.isLoading} |
| 127 | + onClick={handleRemoveFromQueue} |
| 128 | + > |
| 129 | + Remove from Queue |
| 130 | + </Button> |
| 131 | + )} |
| 132 | + <Button |
| 133 | + isDisabled={ |
| 134 | + transcriptionBacklog.isLoading || |
| 135 | + transcriptionQueue.data?.length == 0 || |
| 136 | + isTranscribing |
| 137 | + } |
| 138 | + onClick={() => startTranscription.mutate()} |
| 139 | + > |
| 140 | + {`${ |
| 141 | + isTranscribing |
| 142 | + ? "Transcription in Progress..." |
| 143 | + : "Start Transcription" |
| 144 | + }`} |
| 145 | + </Button> |
| 146 | + </> |
| 147 | + } |
| 148 | + /> |
| 149 | + <BaseTable |
| 150 | + data={remainingBacklog} |
| 151 | + emptyView={ |
| 152 | + <Flex w="full" justifyContent="center" alignItems="center" gap={2}> |
| 153 | + <Text> |
| 154 | + Transcription backlog is empty for the selected source |
| 155 | + </Text> |
| 156 | + </Flex> |
| 157 | + } |
| 158 | + isLoading={transcriptionBacklog.isLoading} |
| 159 | + isError={transcriptionBacklog.isError} |
| 160 | + tableStructure={tableStructure} |
| 161 | + tableHeaderComponent={ |
| 162 | + <Heading size="sm" mb={1}> |
| 163 | + {`Transcription Backlog (${selectedSource})`} |
| 164 | + </Heading> |
| 165 | + } |
| 166 | + enableCheckboxes |
| 167 | + selectedRowIds={addToQueueSelection} |
| 168 | + onSelectedRowIdsChange={setAddToQueueSelection} |
| 169 | + getRowId={(row) => row.media} |
| 170 | + actionItems={ |
| 171 | + <> |
| 172 | + <Button |
| 173 | + isLoading={addToQueue.isLoading} |
| 174 | + isDisabled={addToQueueSelection.length == 0} |
| 175 | + onClick={handleAddToQueue} |
| 176 | + > |
| 177 | + Add to Queue |
| 178 | + </Button> |
| 179 | + <SelectSourceMenu |
| 180 | + sources={sources.data} |
| 181 | + isLoading={sources.isLoading} |
| 182 | + onSelection={(source: string) => setSelectedSource(source)} |
| 183 | + /> |
| 184 | + </> |
| 185 | + } |
| 186 | + /> |
| 187 | + </Flex> |
| 188 | + </> |
| 189 | + ); |
| 190 | +}; |
| 191 | + |
| 192 | +export default Sources; |
0 commit comments