BlockViewConfiguration.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { BarChart2, Settings2, Table } from 'lucide-react'
  2. import {
  3. Checkbox,
  4. Label,
  5. Popover,
  6. PopoverContent,
  7. PopoverTrigger,
  8. Select,
  9. SelectContent,
  10. SelectGroup,
  11. SelectItem,
  12. SelectTrigger,
  13. ToggleGroup,
  14. ToggleGroupItem,
  15. } from 'ui'
  16. import { ButtonTooltip } from '../ButtonTooltip'
  17. import { ChartConfig } from '@/components/interfaces/SQLEditor/UtilityPanel/ChartConfig'
  18. interface BlockViewConfigurationProps {
  19. columns: string[]
  20. view: 'chart' | 'table'
  21. isChart: boolean
  22. lockColumns?: boolean
  23. chartConfig?: ChartConfig
  24. changeView: (value: 'chart' | 'table') => void
  25. updateChartConfig: (config: ChartConfig) => void
  26. }
  27. export const BlockViewConfiguration = ({
  28. columns,
  29. view,
  30. isChart,
  31. lockColumns = false,
  32. chartConfig,
  33. changeView,
  34. updateChartConfig,
  35. }: BlockViewConfigurationProps) => {
  36. return (
  37. <Popover modal={false}>
  38. <PopoverTrigger asChild>
  39. <ButtonTooltip
  40. id="help-popover-button"
  41. type="text"
  42. className="px-1"
  43. icon={<Settings2 size={14} strokeWidth={1.5} />}
  44. tooltip={{ content: { side: 'bottom', text: 'View data' } }}
  45. />
  46. </PopoverTrigger>
  47. <PopoverContent side="bottom" align="center" className="w-[240px] p-3">
  48. <form className="grid gap-2">
  49. <ToggleGroup
  50. type="single"
  51. value={view}
  52. className="w-full"
  53. onValueChange={(view: 'chart' | 'table') => {
  54. if (view) changeView(view)
  55. }}
  56. >
  57. <ToggleGroupItem className="w-full" value="table" aria-label="Show as table">
  58. <Table className="h-4 w-4" />
  59. <p className="text-xs ml-2">As table</p>
  60. </ToggleGroupItem>
  61. <ToggleGroupItem className="w-full" value="chart" aria-label="Show as chart">
  62. <BarChart2 className="h-4 w-4" />
  63. <p className="text-xs ml-2">As chart</p>
  64. </ToggleGroupItem>
  65. </ToggleGroup>
  66. {isChart && chartConfig && (
  67. <>
  68. <Select
  69. disabled={lockColumns}
  70. value={chartConfig?.xKey}
  71. onValueChange={(value) => updateChartConfig({ ...chartConfig, xKey: value })}
  72. >
  73. <SelectTrigger className="text-left">
  74. X Axis {chartConfig?.xKey && `- ${chartConfig.xKey}`}
  75. </SelectTrigger>
  76. <SelectContent>
  77. <SelectGroup>
  78. {columns.map((key) => (
  79. <SelectItem value={key} key={key}>
  80. {key}
  81. </SelectItem>
  82. ))}
  83. </SelectGroup>
  84. </SelectContent>
  85. </Select>
  86. <Select
  87. disabled={lockColumns}
  88. value={chartConfig?.yKey}
  89. onValueChange={(value) => updateChartConfig({ ...chartConfig, yKey: value })}
  90. >
  91. <SelectTrigger className="text-left">
  92. Y Axis {chartConfig?.yKey && `- ${chartConfig.yKey}`}
  93. </SelectTrigger>
  94. <SelectContent>
  95. <SelectGroup>
  96. {columns.map((key) => (
  97. <SelectItem value={key} key={key}>
  98. {key}
  99. </SelectItem>
  100. ))}
  101. </SelectGroup>
  102. </SelectContent>
  103. </Select>
  104. <div className="*:flex *:gap-2 *:items-center grid gap-2 *:text-foreground-light *:p-1.5 *:pl-0">
  105. <Label htmlFor="cumulative">
  106. <Checkbox
  107. id="cumulative"
  108. checked={chartConfig?.cumulative}
  109. onClick={() =>
  110. updateChartConfig({
  111. ...chartConfig,
  112. cumulative: !chartConfig?.cumulative,
  113. })
  114. }
  115. />
  116. Cumulative
  117. </Label>
  118. <Label htmlFor="logScale">
  119. <Checkbox
  120. id="logScale"
  121. checked={chartConfig?.logScale}
  122. onClick={() =>
  123. updateChartConfig({
  124. ...chartConfig,
  125. logScale: !chartConfig?.logScale,
  126. })
  127. }
  128. />
  129. Log scale
  130. </Label>
  131. </div>
  132. </>
  133. )}
  134. </form>
  135. </PopoverContent>
  136. </Popover>
  137. )
  138. }