|
| 1 | +import { useGuideContext, useStore } from "@knocklabs/react-core"; |
| 2 | +import { Button } from "@telegraph/button"; |
| 3 | +import { Box, Stack } from "@telegraph/layout"; |
| 4 | +import { Tooltip } from "@telegraph/tooltip"; |
| 5 | +import { Text } from "@telegraph/typography"; |
| 6 | +import { ChevronLeft, ChevronRight, X } from "lucide-react"; |
| 7 | + |
| 8 | +import { DisplayOption } from "./helpers"; |
| 9 | +// import React from "react"; |
| 10 | + |
| 11 | +import { InspectionResultOk } from "./useInspectGuideClientStore"; |
| 12 | + |
| 13 | +// const findNextPrevKey = ( |
| 14 | +// currKey: string, |
| 15 | +// guides: InspectionResultOk["guides"] |
| 16 | +// ) => { |
| 17 | +// const selectableGuides = guides.filter(g => !!g.annotation.selectable); |
| 18 | +// const currIndex = selectableGuides.findIndex(g => g.key === currKey); |
| 19 | +// |
| 20 | +// const prevGuide = currIndex === 0 |
| 21 | +// ? undefined |
| 22 | +// : selectableGuides[currIndex - 1]; |
| 23 | +// |
| 24 | +// const nextGuide = currIndex + 1 > selectableGuides.length - 1 |
| 25 | +// ? undefined |
| 26 | +// : selectableGuides[currIndex + 1]; |
| 27 | +// |
| 28 | +// return { prev: prevGuide, next: nextGuide }; |
| 29 | +// } |
| 30 | +// const findNextGuideKey = ( |
| 31 | +// currKey: string, |
| 32 | +// guides: InspectionResultOk["guides"] |
| 33 | +// ) => { |
| 34 | +// const selectableGuides = guides.filter(g => !!g.annotation.selectable); |
| 35 | +// const currIndex = selectableGuides.findIndex(g => g.key === currKey); |
| 36 | +// |
| 37 | +// const prevGuide = currIndex === 0 |
| 38 | +// ? undefined |
| 39 | +// : selectableGuides[currIndex - 1]; |
| 40 | +// |
| 41 | +// const nextGuide = currIndex + 1 > selectableGuides.length - 1 |
| 42 | +// ? undefined |
| 43 | +// : selectableGuides[currIndex + 1]; |
| 44 | +// |
| 45 | +// return { prev: prevGuide, next: nextGuide }; |
| 46 | +// } |
| 47 | + |
| 48 | +type Props = { |
| 49 | + guides: InspectionResultOk["guides"]; |
| 50 | + displayOption: DisplayOption; |
| 51 | +}; |
| 52 | + |
| 53 | +export const FocusChin = ({ guides, displayOption }: Props) => { |
| 54 | + const { client } = useGuideContext(); |
| 55 | + const { debugSettings } = useStore(client.store, (state) => ({ |
| 56 | + debugSettings: state.debug || {}, |
| 57 | + })); |
| 58 | + |
| 59 | + const focusedKeys = Object.keys(debugSettings.focusedGuideKeys || {}); |
| 60 | + |
| 61 | + // const { prev, next } = React.useMemo(() => { |
| 62 | + // if (focusedKeys.length === 0) { |
| 63 | + // return {} |
| 64 | + // } |
| 65 | + // const currentKey = focusedKeys[0]; |
| 66 | + // |
| 67 | + // return findNextAndPrevGuideKey(currentKey, guides) |
| 68 | + // }, [displayOption, focusedKeys.length]); |
| 69 | + |
| 70 | + const isFocused = focusedKeys.length > 0; |
| 71 | + if (!isFocused) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + const currentKey = focusedKeys[0]; |
| 76 | + |
| 77 | + // const currentIndex = currentKey ? guideKeys.indexOf(currentKey) : -1; |
| 78 | + |
| 79 | + // const setFocus = (key: string) => { |
| 80 | + // client.setDebug({ |
| 81 | + // ...debugSettings, |
| 82 | + // focusedGuideKeys: { [key]: true }, |
| 83 | + // }); |
| 84 | + // }; |
| 85 | + // |
| 86 | + // const hasPrev = currentIndex > 0; |
| 87 | + // const hasNext = currentIndex >= 0 && currentIndex < guideKeys.length - 1; |
| 88 | + |
| 89 | + return ( |
| 90 | + <Box |
| 91 | + borderTop="px" |
| 92 | + px="3" |
| 93 | + py="1" |
| 94 | + overflow="hidden" |
| 95 | + backgroundColor="blue-2" |
| 96 | + > |
| 97 | + <Stack align="center" justify="space-between"> |
| 98 | + <Text as="span" size="1" weight="medium" color="blue"> |
| 99 | + Focus lock: {currentKey} |
| 100 | + </Text> |
| 101 | + <Stack align="center" gap="1"> |
| 102 | + <Tooltip label="Focus previous guide"> |
| 103 | + <Button |
| 104 | + size="0" |
| 105 | + variant="ghost" |
| 106 | + color="blue" |
| 107 | + leadingIcon={{ icon: ChevronLeft, alt: "Previous guide" }} |
| 108 | + // disabled={!hasPrev} |
| 109 | + onClick={() => { |
| 110 | + const selectableGuides = guides.filter( |
| 111 | + (g) => !!g.annotation.selectable.status, |
| 112 | + ); |
| 113 | + const currIndex = selectableGuides.findIndex( |
| 114 | + (g) => g.key === currentKey, |
| 115 | + ); |
| 116 | + |
| 117 | + const prevGuide = |
| 118 | + currIndex === 0 ? undefined : selectableGuides[currIndex - 1]; |
| 119 | + |
| 120 | + if (!prevGuide) return; |
| 121 | + |
| 122 | + client.setDebug({ |
| 123 | + ...debugSettings, |
| 124 | + focusedGuideKeys: { [prevGuide.key]: true }, |
| 125 | + }); |
| 126 | + }} |
| 127 | + /> |
| 128 | + </Tooltip> |
| 129 | + <Tooltip label="Focus next guide"> |
| 130 | + <Button |
| 131 | + size="0" |
| 132 | + variant="ghost" |
| 133 | + color="blue" |
| 134 | + leadingIcon={{ icon: ChevronRight, alt: "Next guide" }} |
| 135 | + // disabled={!hasNext} |
| 136 | + onClick={() => { |
| 137 | + const selectableGuides = guides.filter( |
| 138 | + (g) => !!g.annotation.selectable.status, |
| 139 | + ); |
| 140 | + console.log(1, selectableGuides); |
| 141 | + const currIndex = selectableGuides.findIndex( |
| 142 | + (g) => g.key === currentKey, |
| 143 | + ); |
| 144 | + |
| 145 | + const nextGuide = |
| 146 | + currIndex + 1 > selectableGuides.length - 1 |
| 147 | + ? undefined |
| 148 | + : selectableGuides[currIndex + 1]; |
| 149 | + |
| 150 | + if (!nextGuide) return; |
| 151 | + |
| 152 | + client.setDebug({ |
| 153 | + ...debugSettings, |
| 154 | + focusedGuideKeys: { [nextGuide.key]: true }, |
| 155 | + }); |
| 156 | + }} |
| 157 | + /> |
| 158 | + </Tooltip> |
| 159 | + <Tooltip label="Exit focus lock"> |
| 160 | + <Button |
| 161 | + size="0" |
| 162 | + variant="ghost" |
| 163 | + color="blue" |
| 164 | + leadingIcon={{ icon: X, alt: "Clear focus" }} |
| 165 | + onClick={() => { |
| 166 | + client.setDebug({ ...debugSettings, focusedGuideKeys: {} }); |
| 167 | + }} |
| 168 | + /> |
| 169 | + </Tooltip> |
| 170 | + </Stack> |
| 171 | + </Stack> |
| 172 | + </Box> |
| 173 | + ); |
| 174 | +}; |
0 commit comments