Skip to content

Commit

Permalink
Merge pull request #66 from alpaca-tc/render-module-dependencies
Browse files Browse the repository at this point in the history
Render module dependencies of source
  • Loading branch information
alpaca-tc authored Jun 26, 2024
2 parents 839f3aa + 4bf0298 commit 032bddb
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 13 deletions.
1 change: 1 addition & 0 deletions frontend/models/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type RelatedDefinition = {

type ReverseDependency = {
sourceName: string
modules: Module[]
methodIds: MethodId[]
}

Expand Down
58 changes: 58 additions & 0 deletions frontend/pages/Sources/Show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { KEY } from '@/hooks/useBitIdHash'
import { useSource } from '@/repositories/sourceRepository'
import { encode, idsToBitId } from '@/utils/bitId'
import { stringify } from '@/utils/queryString'
import { Module } from '@/models/module'
import { isEqual } from '@/utils/isEqual'

export const Show: React.FC = () => {
const sourceName = useParams().sourceName ?? ''
Expand All @@ -24,6 +26,16 @@ export const Show: React.FC = () => {
}
}, [specificSource])

const reverseDependencyModules: Module[][] = useMemo(() => {
if (specificSource) {
return specificSource.reverseDependencies
.map(({ modules }) => modules)
.filter((mods) => !isEqual(mods, specificSource.modules))
} else {
return []
}
}, [specificSource])

return (
<StyledSection>
<Stack>
Expand Down Expand Up @@ -82,6 +94,40 @@ export const Show: React.FC = () => {
</Stack>
</Section>

<Section>
<Stack gap={0.5}>
<Heading type="sectionTitle">Reverse dependency Modules</Heading>
<div style={{ overflow: 'clip' }}>
<Table fixedHead>
<thead>
<tr>
<Th>Module Name</Th>
</tr>
</thead>
{specificSource.modules.length === 0 ? (
<EmptyTableBody>
<Text>no modules</Text>
</EmptyTableBody>
) : (
<tbody>
{reverseDependencyModules.map((modules, index) => (
<tr key={modules.join('-')}>
<Td>
{modules.map((module, index) => (
<Text key={index} as="div" whiteSpace="nowrap">
<Link to={path.modules.show(modules.slice(0, index + 1))}>{module}</Link>
</Text>
))}
</Td>
</tr>
))}
</tbody>
)}
</Table>
</div>
</Stack>
</Section>

<Section>
<Stack gap={0.5}>
<Cluster>
Expand Down Expand Up @@ -126,6 +172,7 @@ export const Show: React.FC = () => {
<Table fixedHead>
<thead>
<tr>
<Th>Module Name</Th>
<Th>Source Name</Th>
<Th>Method Id</Th>
<Th>Path</Th>
Expand All @@ -140,6 +187,17 @@ export const Show: React.FC = () => {
{specificSource.reverseDependencies.map((reverseDependency) =>
reverseDependency.methodIds.map((methodId, index) => (
<tr key={`${reverseDependency.sourceName}-${methodId.context}-${methodId.name}`}>
<Td>
{index === 0
? reverseDependency.modules.map((module, index) => (
<Text key={index} as="div" whiteSpace="nowrap">
<Link to={path.modules.show(reverseDependency.modules.slice(0, index + 1))}>
{module}
</Link>
</Text>
))
: null}
</Td>
<Td>
{index === 0 ? (
<Link to={`${path.sources.show(reverseDependency.sourceName)}`}>
Expand Down
2 changes: 2 additions & 0 deletions frontend/repositories/sourceRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type SpecificSourceResponse = {
}>
reverse_dependencies: Array<{
source_name: string
modules: string[]
method_ids: Array<{
name: string
context: 'instance' | 'class'
Expand All @@ -63,6 +64,7 @@ export const useSource = (sourceName: string) => {
relatedDefinitions: response.related_definitions.map((definition) => ({ id: definition.id, title: definition.title })),
reverseDependencies: response.reverse_dependencies.map((dependency) => ({
sourceName: dependency.source_name,
modules: dependency.modules,
methodIds: dependency.method_ids.map((methodId) => ({
name: methodId.name,
context: methodId.context,
Expand Down
34 changes: 34 additions & 0 deletions frontend/utils/isEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export const isEqual = (obj1: any, obj2: any): boolean => {
if (obj1 === obj2) return true

if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 == null || obj2 == null) {
return false
}

const keysA = Object.keys(obj1)
const keysB = Object.keys(obj2)

if (keysA.length !== keysB.length) {
return false
}

let result = true

keysA.forEach((key) => {
if (!keysB.includes(key)) {
result = false
}

if (typeof obj1[key] === 'function' || typeof obj2[key] === 'function') {
if (obj1[key].toString() !== obj2[key].toString()) {
result = false
}
}

if (!isEqual(obj1[key], obj2[key])) {
result = false
}
})

return result
}
19 changes: 6 additions & 13 deletions lib/diver_down/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,9 @@ def update_source_alias(alias_name, old_alias_name, source_names)
def sources
source_names = Set.new

# rubocop:disable Style/HashEachMethods
@store.each do |_, definition|
definition.sources.each do |source|
source_names.add(source.source_name)
end
@store.combined_definition.sources.each do |source|
source_names.add(source.source_name)
end
# rubocop:enable Style/HashEachMethods

classified_sources_count = source_names.count { @metadata.source(_1).modules? }

Expand All @@ -91,14 +87,10 @@ def modules
# Hash{ DiverDown::Definition::Modulee => Set<Integer> }
module_set = Set.new

# rubocop:disable Style/HashEachMethods
@store.each do |_, definition|
definition.sources.each do |source|
modules = @metadata.source(source.source_name).modules
module_set.add(modules) unless modules.empty?
end
@store.combined_definition.sources.each do |source|
modules = @metadata.source(source.source_name).modules
module_set.add(modules) unless modules.empty?
end
# rubocop:enable Style/HashEachMethods

json(
modules: module_set.sort
Expand Down Expand Up @@ -313,6 +305,7 @@ def source(source_name)
reverse_dependencies: reverse_dependencies.values.map { |reverse_dependency|
{
source_name: reverse_dependency.source_name,
modules: @metadata.source(reverse_dependency.source_name).modules,
method_ids: reverse_dependency.method_ids.sort.map do |method_id|
{
context: method_id.context,
Expand Down

0 comments on commit 032bddb

Please sign in to comment.